LANCommander/LANCommander.Launcher.Services/MediaService.cs

94 lines
2.6 KiB
C#
Raw Permalink Normal View History

using LANCommander.Launcher.Data;
using LANCommander.Launcher.Data.Models;
using LANCommander.Launcher.Models;
2024-09-11 00:24:34 -05:00
using Microsoft.Extensions.Logging;
using LANCommander.SDK;
2025-10-08 19:51:44 -05:00
using LANCommander.SDK.Extensions;
2025-12-01 01:21:21 -06:00
using LANCommander.SDK.Services;
namespace LANCommander.Launcher.Services
{
public class MediaService(
ILogger<MediaService> logger,
DatabaseContext dbContext,
2025-12-01 01:21:21 -06:00
MediaClient mediaClient,
SettingsProvider<Settings.Settings> settingsProvider) : BaseDatabaseService<Media>(dbContext, logger)
{
public override Task DeleteAsync(Media entity)
{
DeleteLocalMediaFile(entity);
return base.DeleteAsync(entity);
}
public bool FileExists(Media entity)
{
var path = GetImagePath(entity);
return File.Exists(path);
}
public async Task<bool> FileExists(Guid id)
{
var path = await GetImagePath(id);
return File.Exists(path);
}
public async Task<string> GetImagePath(Guid id)
{
var entity = await GetAsync(id);
return GetImagePath(entity);
}
public string GetStoragePath()
=> AppPaths.GetConfigPath(settingsProvider.CurrentValue.Media.StoragePath);
public string GetImagePath(Media entity)
{
2024-06-24 19:31:56 -05:00
if (entity == null)
return "";
2024-06-01 22:47:56 -05:00
return Path.Combine(GetStoragePath(), $"{entity.FileId}-{entity.Crc32}");
}
public void DeleteLocalMediaFile(Media entity)
{
2025-10-08 19:51:44 -05:00
using (var op = Logger.BeginOperation("Deleting local media file"))
{
2025-10-08 19:51:44 -05:00
op.Enrich("Id", entity.Id);
try
{
var path = GetImagePath(entity);
2025-10-08 19:51:44 -05:00
op.Enrich("Path", path);
if (File.Exists(path))
File.Delete(path);
}
catch (Exception ex)
{
Logger?.LogError(ex, "An unknown error occurred while trying to delete a local file");
}
}
}
2025-12-01 01:21:21 -06:00
public async Task<FileInfo> DownloadAsync(Media entity)
{
var path = GetImagePath(entity);
return await mediaClient.DownloadAsync(new SDK.Models.Media
{
Id = entity.Id,
FileId = entity.FileId,
Crc32 = entity.Crc32,
Name = entity.Name,
MimeType = entity.MimeType,
Type = entity.Type,
}, path);
}
}
}