add migration to file extensions for mare storage

This commit is contained in:
rootdarkarchon
2023-05-06 02:41:17 +02:00
parent 176fc9560b
commit f512552fe7
3 changed files with 43 additions and 8 deletions

View File

@@ -105,9 +105,9 @@ public sealed class FileCacheManager : IDisposable
public List<FileCacheEntity> GetAllFileCaches() => _fileCaches.Values.SelectMany(v => v).ToList();
public string GetCacheFilePath(string hash, bool isTemporaryFile)
public string GetCacheFilePath(string hash, string extension, bool isTemporaryFile)
{
return Path.Combine(_configService.Current.CacheFolder, hash + (isTemporaryFile ? ".tmp" : string.Empty));
return Path.Combine(_configService.Current.CacheFolder, hash + "." + extension + (isTemporaryFile ? ".tmp" : string.Empty));
}
public FileCacheEntity? GetFileCacheByHash(string hash)
@@ -181,12 +181,13 @@ public sealed class FileCacheManager : IDisposable
{
sb.AppendLine(entry.CsvEntry);
}
if (File.Exists(_csvPath))
{
File.Copy(_csvPath, CsvBakPath, overwrite: true);
}
lock (_fileWriteLock)
{
if (File.Exists(_csvPath))
{
File.Copy(_csvPath, CsvBakPath, overwrite: true);
}
try
{
File.WriteAllText(_csvPath, sb.ToString());
@@ -199,6 +200,31 @@ public sealed class FileCacheManager : IDisposable
}
}
internal FileCacheEntity MigrateFileHashToExtension(FileCacheEntity fileCache, string ext)
{
try
{
RemoveHashedFile(fileCache);
FileInfo oldCache = new(fileCache.ResolvedFilepath);
var extensionPath = fileCache.ResolvedFilepath.ToUpper() + "." + ext;
File.Move(fileCache.ResolvedFilepath, extensionPath, true);
var newHashedEntity = new FileCacheEntity(fileCache.Hash, fileCache.PrefixedFilePath + "." + ext, DateTime.UtcNow.Ticks.ToString());
newHashedEntity.SetResolvedFilePath(extensionPath);
FileInfo newCache = new FileInfo(extensionPath);
newCache.LastAccessTime = oldCache.LastAccessTime;
newCache.LastWriteTime = oldCache.LastWriteTime;
AddHashedFile(newHashedEntity);
_logger.LogDebug("Migrated from {oldPath} to {newPath}", fileCache.ResolvedFilepath, newHashedEntity.ResolvedFilepath);
return newHashedEntity;
}
catch (Exception ex)
{
AddHashedFile(fileCache);
_logger.LogWarning(ex, "Failed to migrate entity {entity}", fileCache.PrefixedFilePath);
return fileCache;
}
}
private void AddHashedFile(FileCacheEntity fileCache)
{
if (!_fileCaches.TryGetValue(fileCache.Hash, out var entries))

View File

@@ -665,6 +665,7 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase
List<FileReplacementData> missingFiles = new();
moddedDictionary = new Dictionary<string, string>(StringComparer.Ordinal);
ConcurrentDictionary<string, string> outputDict = new(StringComparer.Ordinal);
bool hasMigrationChanges = false;
try
{
var replacementList = charaData.FileReplacements.SelectMany(k => k.Value.Where(v => string.IsNullOrEmpty(v.FileSwapPath))).ToList();
@@ -679,6 +680,12 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase
var fileCache = _fileDbManager.GetFileCacheByHash(item.Hash);
if (fileCache != null)
{
if (string.IsNullOrEmpty(new FileInfo(fileCache.ResolvedFilepath).Extension))
{
hasMigrationChanges = true;
fileCache = _fileDbManager.MigrateFileHashToExtension(fileCache, item.GamePaths[0].Split(".").Last());
}
foreach (var gamePath in item.GamePaths)
{
outputDict[gamePath] = fileCache.ResolvedFilepath;
@@ -706,6 +713,7 @@ public sealed class CachedPlayer : DisposableMediatorSubscriberBase
{
PluginLog.Error(ex, "Something went wrong during calculation replacements");
}
if (hasMigrationChanges) _fileDbManager.WriteOutFullCsv();
st.Stop();
Logger.LogDebug("ModdedPaths calculated in {time}ms, missing files: {count}, total files: {total}", st.ElapsedMilliseconds, missingFiles.Count, moddedDictionary.Keys.Count);
return missingFiles;

View File

@@ -191,7 +191,8 @@ public partial class FileDownloadManager : DisposableMediatorSubscriberBase
foreach (var file in fileGroup)
{
var tempPath = _fileDbManager.GetCacheFilePath(file.Hash, isTemporaryFile: true);
var ext = fileReplacement.First(f => string.Equals(f.Hash, file.Hash, StringComparison.OrdinalIgnoreCase)).GamePaths.First().Split(".").Last();
var tempPath = _fileDbManager.GetCacheFilePath(file.Hash, ext, isTemporaryFile: true);
Progress<long> progress = new((bytesDownloaded) =>
{
try
@@ -235,7 +236,7 @@ public partial class FileDownloadManager : DisposableMediatorSubscriberBase
var tempFileData = await File.ReadAllBytesAsync(tempPath, token).ConfigureAwait(false);
var extractedFile = LZ4Codec.Unwrap(tempFileData);
File.Delete(tempPath);
var filePath = _fileDbManager.GetCacheFilePath(file.Hash, isTemporaryFile: false);
var filePath = _fileDbManager.GetCacheFilePath(file.Hash, ext, isTemporaryFile: false);
await File.WriteAllBytesAsync(filePath, extractedFile, token).ConfigureAwait(false);
var fi = new FileInfo(filePath);
Func<DateTime> RandomDayInThePast()