2024-08-04 17:53:19 -05:00
|
|
|
|
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;
|
2024-05-28 00:24:29 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2025-08-18 17:48:37 -05:00
|
|
|
|
using LANCommander.SDK;
|
2024-05-28 00:24:29 -05:00
|
|
|
|
|
2024-08-04 17:53:19 -05:00
|
|
|
|
namespace LANCommander.Launcher.Services
|
2024-05-28 00:24:29 -05:00
|
|
|
|
{
|
|
|
|
|
|
public class MediaService : BaseDatabaseService<Media>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly Settings Settings;
|
|
|
|
|
|
|
2024-09-11 00:24:34 -05:00
|
|
|
|
public MediaService(DatabaseContext dbContext, SDK.Client client, ILogger<CollectionService> logger) : base(dbContext, client, logger)
|
|
|
|
|
|
{
|
2024-05-28 00:24:29 -05:00
|
|
|
|
Settings = SettingService.GetSettings();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-19 00:03:21 -06:00
|
|
|
|
public override Task DeleteAsync(Media entity)
|
2024-05-28 00:24:29 -05:00
|
|
|
|
{
|
|
|
|
|
|
DeleteLocalMediaFile(entity);
|
|
|
|
|
|
|
2025-01-19 00:03:21 -06:00
|
|
|
|
return base.DeleteAsync(entity);
|
2024-05-28 00:24:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-01-19 00:03:21 -06:00
|
|
|
|
var entity = await GetAsync(id);
|
2024-05-28 00:24:29 -05:00
|
|
|
|
|
|
|
|
|
|
return GetImagePath(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetStoragePath()
|
|
|
|
|
|
{
|
|
|
|
|
|
var settings = SettingService.GetSettings();
|
|
|
|
|
|
|
2025-08-18 17:48:37 -05:00
|
|
|
|
return Path.Combine(AppPaths.GetConfigDirectory(), settings.Media.StoragePath);
|
2024-05-28 00:24:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static 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}");
|
2024-05-28 00:24:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteLocalMediaFile(Media entity)
|
|
|
|
|
|
{
|
2024-09-16 21:00:24 -05:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = GetImagePath(entity);
|
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger?.LogError(ex, "An unknown error occurred while trying to delete a local file");
|
|
|
|
|
|
}
|
2024-05-28 00:24:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|