mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-06 07:36:01 +02:00
feat: add audio playback and file download features with corresponding UI updates
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
namespace EchoHub.Server.Services;
|
||||
|
||||
public sealed class FileCleanupService : BackgroundService
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<FileCleanupService> _logger;
|
||||
|
||||
public FileCleanupService(IConfiguration configuration, ILogger<FileCleanupService> logger)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var intervalHours = _configuration.GetValue("Storage:CleanupIntervalHours", 1);
|
||||
var retentionDays = _configuration.GetValue("Storage:RetentionDays", 30);
|
||||
var storagePath = _configuration["Storage:Path"]
|
||||
?? Path.Combine(AppContext.BaseDirectory, "uploads");
|
||||
|
||||
_logger.LogInformation(
|
||||
"File cleanup service started — interval: {Hours}h, retention: {Days}d, path: {Path}",
|
||||
intervalHours, retentionDays, storagePath);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromHours(intervalHours), stoppingToken);
|
||||
|
||||
try
|
||||
{
|
||||
CleanupOldFiles(storagePath, retentionDays);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during file cleanup");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CleanupOldFiles(string storagePath, int retentionDays)
|
||||
{
|
||||
if (!Directory.Exists(storagePath))
|
||||
return;
|
||||
|
||||
var cutoff = DateTime.UtcNow.AddDays(-retentionDays);
|
||||
var files = Directory.GetFiles(storagePath);
|
||||
var deleted = 0;
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
var createdAt = File.GetCreationTimeUtc(file);
|
||||
if (createdAt < cutoff)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(file);
|
||||
deleted++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to delete old file: {File}", file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (deleted > 0)
|
||||
_logger.LogInformation("File cleanup: deleted {Count} files older than {Days} days", deleted, retentionDays);
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,20 @@ public static class FileValidationHelper
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly HashSet<string> AudioExtensions = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
".mp3", ".wav", ".ogg", ".flac", ".aac", ".m4a", ".wma"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the file name has a recognized audio extension.
|
||||
/// </summary>
|
||||
public static bool IsAudioFile(string fileName)
|
||||
{
|
||||
var ext = Path.GetExtension(fileName);
|
||||
return !string.IsNullOrEmpty(ext) && AudioExtensions.Contains(ext);
|
||||
}
|
||||
|
||||
private static bool StartsWith(byte[] buffer, int length, byte[] magic)
|
||||
{
|
||||
if (length < magic.Length)
|
||||
|
||||
Reference in New Issue
Block a user