|
|
|
|
@@ -12,6 +12,7 @@ using SixLabors.ImageSharp;
|
|
|
|
|
|
|
|
|
|
using System.Collections.Frozen;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace AZKiServer.Services;
|
|
|
|
|
|
|
|
|
|
@@ -61,8 +62,7 @@ public class FileScannerService(MediaService mediaService, IConfiguration config
|
|
|
|
|
var upToDateFiles = existingFiles.Where(e => e.Value == MediaEntry.CUR_VERSION)
|
|
|
|
|
.Select(e => e.Key)
|
|
|
|
|
.ToFrozenSet();
|
|
|
|
|
var entriesToDelete = existingFiles.Keys.Except(filesRelative).ToArray();
|
|
|
|
|
await mediaService.DeleteEntriesBulkAsync(files, cancellationToken);
|
|
|
|
|
//await DeleteEntriesWithoutFilesAsync(existingFiles, filesRelative, cancellationToken);
|
|
|
|
|
var filesToProcess = filesRelative.Where(f => !upToDateFiles.Contains(f)).ToArray();
|
|
|
|
|
var total = 0;
|
|
|
|
|
var prog = 0;
|
|
|
|
|
@@ -70,7 +70,7 @@ public class FileScannerService(MediaService mediaService, IConfiguration config
|
|
|
|
|
{
|
|
|
|
|
prog += chunk.Length;
|
|
|
|
|
total += await ScanFileChunkAsync(path, chunk, existingFiles, cancellationToken);
|
|
|
|
|
logger.LogInformation("Added {updated} of {count} [{percentage}%]", total, filesToProcess.Length, Math.Round(((float)prog/ filesToProcess.Length) * 100));
|
|
|
|
|
logger.LogInformation("Added {updated} of {count} [{percentage}%]", total, filesToProcess.Length, Math.Round(((float)prog / filesToProcess.Length) * 100));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
@@ -79,18 +79,28 @@ public class FileScannerService(MediaService mediaService, IConfiguration config
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task DeleteEntriesWithoutFilesAsync(FrozenDictionary<string, int> existingFiles, string[] filesRelative, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
var entriesToDelete = existingFiles.Keys.Except(filesRelative).ToArray();
|
|
|
|
|
if (entriesToDelete.Length == 0)
|
|
|
|
|
return;
|
|
|
|
|
logger.LogInformation("Deleting {count} entires that no longer exist on file system.", entriesToDelete.Length);
|
|
|
|
|
await mediaService.DeleteEntriesBulkAsync(entriesToDelete, cancellationToken);
|
|
|
|
|
logger.LogInformation("{count} entries deleted.", entriesToDelete.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<int> ScanFileChunkAsync(string path, IEnumerable<string> files, FrozenDictionary<string, int> existingFiles, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
var entries = new List<MediaEntry>();
|
|
|
|
|
var upgradeEntries = new List<MediaEntry>();
|
|
|
|
|
foreach (var filePath in files)
|
|
|
|
|
foreach (var relativePath in files)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
break;
|
|
|
|
|
var relativePath = Path.GetRelativePath(path, filePath);
|
|
|
|
|
if (relativePath[0] == '.') //Ignore hidden folders
|
|
|
|
|
continue;
|
|
|
|
|
var absolutePath = Path.Combine(path, relativePath);
|
|
|
|
|
var isUpgrade = false;
|
|
|
|
|
if (existingFiles.TryGetValue(relativePath, out var version))
|
|
|
|
|
{
|
|
|
|
|
@@ -98,10 +108,10 @@ public class FileScannerService(MediaService mediaService, IConfiguration config
|
|
|
|
|
continue;
|
|
|
|
|
isUpgrade = true;
|
|
|
|
|
}
|
|
|
|
|
var metadata = ReadMetadata(filePath);
|
|
|
|
|
var metadata = ReadMetadata(absolutePath);
|
|
|
|
|
if (metadata.HasError)
|
|
|
|
|
{
|
|
|
|
|
logger.LogError(metadata.Error.GetException(), $"Failed to get metadata for file: {filePath}");
|
|
|
|
|
logger.LogError(metadata.Error.GetException(), "Failed to get metadata for file: {filePath}", relativePath);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
var entry = MediaEntry.Parse(relativePath, metadata);
|
|
|
|
|
@@ -137,7 +147,7 @@ public class FileScannerService(MediaService mediaService, IConfiguration config
|
|
|
|
|
{
|
|
|
|
|
".jpg" or ".png" or ".jpeg" => ReadImageMetadata(filePath),
|
|
|
|
|
".mp4" => ReadVideoMetadata(filePath),
|
|
|
|
|
_ => throw new NotSupportedException($"Files of type {ext} are not supported")
|
|
|
|
|
_ => new Error($"Files of type {ext} are not supported")
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -164,7 +174,7 @@ public class FileScannerService(MediaService mediaService, IConfiguration config
|
|
|
|
|
return new Error($"Could not find a primirary video stream in file.");
|
|
|
|
|
return new MediaMetadata((int)info.Duration.TotalSeconds, info.PrimaryVideoStream.Height, info.PrimaryVideoStream.Width);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return ex;
|
|
|
|
|
}
|
|
|
|
|
|