45 lines
1.6 KiB
C#
45 lines
1.6 KiB
C#
using System.Security.Cryptography;
|
|
|
|
namespace VideoDownloader.Server;
|
|
|
|
/// <summary>저장 폴더 미디어 라이브러리: 파일 나열/조회(ID = SHA-256 경로 해시 12자).</summary>
|
|
public static class MediaLibrary
|
|
{
|
|
private static readonly string[] VideoExts = { ".mp4", ".mkv", ".webm", ".ts", ".mov", ".m4v", ".avi", ".mp3", ".m4a" };
|
|
|
|
public static List<LibraryItemDto> List(string folder)
|
|
{
|
|
var result = new List<LibraryItemDto>();
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder)) return result;
|
|
foreach (var f in Directory.EnumerateFiles(folder).OrderByDescending(f => File.GetLastWriteTimeUtc(f)))
|
|
{
|
|
var ext = Path.GetExtension(f).ToLowerInvariant();
|
|
if (!VideoExts.Contains(ext)) continue;
|
|
var fi = new FileInfo(f);
|
|
result.Add(new LibraryItemDto(IdOf(f), fi.Name, fi.Length, fi.LastWriteTimeUtc));
|
|
}
|
|
}
|
|
catch { }
|
|
return result;
|
|
}
|
|
|
|
public static (string FullPath, string FileName)? Find(string folder, string id)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder)) return null;
|
|
foreach (var f in Directory.EnumerateFiles(folder))
|
|
if (IdOf(f) == id) return (f, Path.GetFileName(f));
|
|
}
|
|
catch { }
|
|
return null;
|
|
}
|
|
|
|
private static string IdOf(string path)
|
|
{
|
|
var hash = SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(Path.GetFullPath(path)));
|
|
return Convert.ToHexString(hash)[..12].ToLowerInvariant();
|
|
}
|
|
}
|