Skip to content

Commit

Permalink
添加缓存管理
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaoyww committed Nov 2, 2024
1 parent bc34e88 commit 7cfb778
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 197 deletions.
23 changes: 16 additions & 7 deletions src/NonsPlayer.Core/Resources/LocalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace NonsPlayer.Core.Resources
{
public class LocalSettings
{
[JsonIgnore] public string DataPath { get; set; }
[JsonIgnore] public string MainPath { get; set; }

[JsonIgnore] public string ConfigFilePath;

Expand Down Expand Up @@ -40,20 +40,28 @@ public class LocalSettings

[JsonPropertyName("log")] public string Log { get; set; }

[JsonPropertyName("log_path")] public string LogPath { get; set; }

[JsonPropertyName("cache_path")] public string CachePath { get; set; }

[JsonPropertyName("smtc")] public bool SMTCEnable { get; set; }

[JsonPropertyName("today_play_duration")]
public Tuple<DateTime, TimeSpan> TodayPlayDuration { get; set; }

[JsonPropertyName("local_artist_sep")] public List<string> LocalArtistSep { get; set; }

public LocalSettings()
{
DataPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
MainPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"NonsPlayer");
ConfigFilePath = Path.Join(DataPath, "config.json");
AdapterPath = Path.Join(DataPath, "adapters");
PluginPath = Path.Join(DataPath, "plugins");
Data = Path.Join(DataPath, "Data");
Log = Path.Combine(DataPath, "logs", $"NonsPlayer_{DateTime.Now:yyMMdd_HHmmss}.log");
ConfigFilePath = Path.Join(MainPath, "config.json");
AdapterPath = Path.Join(MainPath, "adapters");
PluginPath = Path.Join(MainPath, "plugins");
Data = Path.Join(MainPath, "data");
CachePath = Path.Join(MainPath, "cache");
LogPath = Path.Join(MainPath, "logs");
Log = Path.Combine(LogPath, $"NonsPlayer_{DateTime.Now:yyMMdd_HHmmss}.log");
Theme = "Light";
Volume = 50;
TotalPlayCount = 0;
Expand All @@ -63,6 +71,7 @@ public LocalSettings()
DisabledPlugins = string.Empty;
TodayPlayDuration = new Tuple<DateTime, TimeSpan>(DateTime.Now, TimeSpan.Zero);
LocalArtistSep = [";", ","];
SMTCEnable = true;
}
}
}
6 changes: 3 additions & 3 deletions src/NonsPlayer.Core/Services/ExpectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ public class ExceptionService

public void Throw(string content)
{
Debug.WriteLine($"抛出了一个异常: {content}");
Debug.WriteLine($"Threw an Exception: {content}");
ExceptionThrew?.Invoke(content);
}

public void Throw(Exception exception)
{
Debug.WriteLine($"抛出了一个异常: {exception}");
Debug.WriteLine($"Threw an Exception:: {exception}");
ExceptionThrew?.Invoke(exception.Message);
}

public void Throw(Exception exception, string content)
{
Debug.WriteLine($"抛出了一个异常: {exception}");
Debug.WriteLine($"Threw an Exception:: {exception}");
ExceptionThrew?.Invoke(content);
}
}
95 changes: 91 additions & 4 deletions src/NonsPlayer.Core/Services/FileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public string ReadData(string filename)
File.Create(path).Dispose();
return string.Empty;
}

using var reader = new StreamReader(path);
var json = reader.ReadToEnd();
return json;
Expand All @@ -48,12 +49,42 @@ public void Save<T>(string folderPath, string fileName, T content)
File.WriteAllText(Path.Combine(folderPath, fileName), fileContent, Encoding.UTF8);
}

public void Delete(string folderPath, string fileName)
public void Delete(string filePath)
{
if (fileName != null && File.Exists(Path.Combine(folderPath, fileName)))
File.Delete(Path.Combine(folderPath, fileName));
if (filePath != null && File.Exists(filePath))
{
File.Delete(filePath);
}
}

public void DeleteFolder(string folderPath)
{
if (!Directory.Exists(folderPath))
{
return;
}

try
{
string[] files = Directory.GetFiles(folderPath, "*", SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
Delete(file);
}
}
catch (UnauthorizedAccessException ex)
{
ExceptionService.Instance.Throw(ex);
}
catch (IOException ex)
{
// ignore
}
catch (Exception ex)
{
ExceptionService.Instance.Throw(ex);
}
}
public void Move<T>(string targetFile, string folderPath)
{
}
Expand All @@ -66,4 +97,60 @@ public void WriteData(string filename, string content)
writer.Write(content);
}
}
}

public static bool Exist(string path)
{
if (File.Exists(path))
{
return true;

}
else if (Directory.Exists(path))
{
return true;
}

return false;
}
public static long GetFileSize(string filePath)
{
if (File.Exists(filePath))
{
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}

return 0;

}
public static long GetDirectorySize(string folderPath)
{
if (!Directory.Exists(folderPath))
{
return 0;
}

long size = 0;

foreach (string file in Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories))
{
size += new FileInfo(file).Length;
}

return size;
}
public static string FormatSize(long bytes)
{
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
int order = 0;
double size = bytes;

while (size >= 1024 && order < sizes.Length - 1)
{
order++;
size /= 1024;
}

return $"{size:0.##} {sizes[order]}";
}
}
2 changes: 1 addition & 1 deletion src/NonsPlayer/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public AppConfig()

public string? IgnoreVersion { get; set; }
public string? AppVersion { get; set; }
public string ConfigPath = Path.Combine(ConfigManager.Instance.Settings.DataPath, "app_config.json");
public string ConfigPath = Path.Combine(ConfigManager.Instance.Settings.MainPath, "app_config.json");

private void Initialize()
{
Expand Down
17 changes: 17 additions & 0 deletions src/NonsPlayer/Helpers/DialogHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.UI.Xaml.Controls;
using static NonsPlayer.Core.Services.ExceptionService;

namespace NonsPlayer.Helpers;

public class DialogHelper
{
public static DialogHelper Instance { get; } = new();
public delegate void DialogShowHandle(string content);
public event DialogShowHandle DialogShowing;


public void Show(string content)
{
DialogShowing?.Invoke(content);
}
}
16 changes: 0 additions & 16 deletions src/NonsPlayer/Helpers/RequestHelper.cs

This file was deleted.

20 changes: 0 additions & 20 deletions src/NonsPlayer/Helpers/RuntimeHelper.cs

This file was deleted.

101 changes: 0 additions & 101 deletions src/NonsPlayer/Helpers/SettingsStorageExtensions.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/NonsPlayer/Helpers/StartUpHelper.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/NonsPlayer/Services/SMTCService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NAudio.Wave;
using NonsPlayer.Core.Contracts.Models.Music;
using NonsPlayer.Core.Nons.Player;
using NonsPlayer.Core.Services;
using NonsPlayer.Helpers;
using NonsPlayer.ViewModels;
using Timer = System.Timers.Timer;
Expand All @@ -22,7 +23,7 @@ public class SMTCService

public SMTCService()
{
if (AppConfig.Instance.AppSettings.SMTCEnable)
if (ConfigManager.Instance.Settings.SMTCEnable)
{
Player.Instance.MusicChanged += MusicChangedHandle;

Expand Down
Loading

0 comments on commit 7cfb778

Please sign in to comment.