2024-08-01 18:08:55 -05:00
|
|
|
|
using Force.Crc32;
|
2024-02-20 17:58:49 -06:00
|
|
|
|
using LANCommander.SDK.Models;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2024-10-04 23:37:49 -05:00
|
|
|
|
namespace LANCommander.SDK.Services
|
2024-02-20 17:58:49 -06:00
|
|
|
|
{
|
|
|
|
|
|
public class MediaService
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2024-02-20 17:58:49 -06:00
|
|
|
|
|
2025-08-18 03:02:49 -05:00
|
|
|
|
private readonly Client _client;
|
2024-02-20 17:58:49 -06:00
|
|
|
|
|
|
|
|
|
|
public MediaService(Client client)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_client = client;
|
2024-02-20 17:58:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public MediaService(Client client, ILogger logger)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
_client = client;
|
|
|
|
|
|
_logger = logger;
|
2024-02-20 17:58:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-06 17:11:35 -05:00
|
|
|
|
public async Task<Media> Get(Guid mediaId)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return await _client.GetRequestAsync<Media>($"/api/Media/{mediaId}");
|
2024-10-06 17:11:35 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-04 20:47:33 -05:00
|
|
|
|
public async Task<string> DownloadAsync(Media media, string destination)
|
2024-02-20 17:58:49 -06:00
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return await _client.DownloadRequestAsync(GetDownloadPath(media), destination);
|
2024-02-20 17:58:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetAbsoluteUrl(Media media)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return new Uri(_client.BaseUrl, GetDownloadPath(media)).ToString();
|
2024-02-20 17:58:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetDownloadPath(Media media)
|
|
|
|
|
|
{
|
|
|
|
|
|
return $"/api/Media/{media.Id}/Download?fileId={media.FileId}";
|
|
|
|
|
|
}
|
2024-08-01 18:08:55 -05:00
|
|
|
|
|
2024-11-11 19:11:03 -06:00
|
|
|
|
public string GetAbsoluteThumbnailUrl(Media media)
|
|
|
|
|
|
{
|
2025-08-18 03:02:49 -05:00
|
|
|
|
return new Uri(_client.BaseUrl, GetThumbnailPath(media)).ToString();
|
2024-11-11 19:11:03 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-07 20:41:39 -06:00
|
|
|
|
public string GetThumbnailPath(Media media)
|
|
|
|
|
|
{
|
|
|
|
|
|
return $"/api/Media/{media.Id}/Thumbnail";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-01 18:08:55 -05:00
|
|
|
|
public static string CalculateChecksum(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint crc = 0;
|
|
|
|
|
|
|
|
|
|
|
|
using (FileStream fs = File.Open(path, FileMode.Open))
|
|
|
|
|
|
{
|
|
|
|
|
|
var buffer = new byte[4096];
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
var count = fs.Read(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
crc = Crc32Algorithm.Append(crc, buffer, 0, count);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return crc.ToString("X");
|
|
|
|
|
|
}
|
2024-02-20 17:58:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|