public async Task CleanupDB() { Console.Out.WriteLine("--== Starting Cleanup ==--"); var files = context.MediaFiles.ToList(); foreach (var file in files) { var exists = File.Exists(file.FilePath + "/" + file.FileName); if (!exists) { context.RemoveRange(file); } } var filesWError = context.FilesWithErrors.ToList(); foreach (var file in filesWError) { var exists = File.Exists(file.FilePath); if (!exists) { context.RemoveRange(file); } } await context.SaveChangesAsync(); Console.Out.WriteLine("--== Finished Cleanup ==--"); }
public async Task ProcessFile(string filename, string content_type, string?processed_format) { var fileInfo = new FileInfo(filename); var filenameRegexString = config[$"MP:FilenameRegex:{content_type}"]; List <String> ignoreRegex = config.GetSection("MP:Ignore").Get <List <String> >(); foreach (String regString in ignoreRegex) { Regex reg = new Regex(regString); if (reg.IsMatch(filename)) { return; } } try { var oldAnalysis = context.MediaFiles .Where(s => s.FileName == fileInfo.Name) .Where(s => s.FilePath == fileInfo.DirectoryName) .ToList(); long oldSize = 0; if (oldAnalysis.FirstOrDefault() != null) { oldSize = oldAnalysis.FirstOrDefault().Size; } if (oldSize != fileInfo.Length) { context.RemoveRange(oldAnalysis); await context.SaveChangesAsync(); MediaFile mediaFile = await AnalyzeFile(content_type, fileInfo); mediaFile.ProcessedFormat = processed_format; if (mediaFile != null) { context.MediaFiles.Add(mediaFile); await context.SaveChangesAsync(); Console.Out.WriteLine($"[Added] {filename}"); } } } catch (System.NullReferenceException e) { await LogFileWithError(e, filename); } catch (System.InvalidOperationException e) { await LogFileWithError(e, filename); } catch (System.DivideByZeroException e) { await LogFileWithError(e, filename); } catch (Microsoft.EntityFrameworkCore.DbUpdateException e) { await LogFileWithError(e, filename); } }
public async Task ProcessSingleDB() { string targetFormat = config["MP:Conversion:ProfileName"]; MediaFile currentFile = context.MediaFiles .Where(m => m.ProcessedFormat != targetFormat) .Where(m => m.LastProcessingUpdate <= DateTime.UtcNow.AddHours(-1)) .OrderByDescending(m => m.BytesPerSecondPerPixel) .First(); Console.Out.WriteLine($"[Processing] \"{currentFile.FileName}\""); try { await ProcessFile(currentFile); Console.Out.WriteLine($"[Finished] \"{currentFile.FileName}\""); } catch (System.IO.FileNotFoundException e) { Console.Out.WriteLine($"[Error] File not found, removing: \"{currentFile.FileName}\""); context.RemoveRange(currentFile); await context.SaveChangesAsync(); } }
public async Task ProcessSingleDB() { string targetFormat = config["MP:Conversion:ProfileName"]; var mfQuery = context.MediaFiles .Where(m => m.ProcessedFormat != targetFormat) .Where(m => m.LastProcessingUpdate <= DateTime.UtcNow.AddHours(-1)); string sortOrder = config["MP:Conversion:SortOrder"]; sortOrder ??= "pixel"; switch (sortOrder) { case "pixel": mfQuery = mfQuery.OrderByDescending(m => m.BytesPerSecondPerPixel); break; case "frame": mfQuery = mfQuery.OrderByDescending(m => m.BytesPerSecond); break; default: throw new System.Exception($"Unsupported MP:Conversion:SortOrder \"{sortOrder}\""); } MediaFile currentFile = mfQuery.First(); Console.Out.WriteLine($"[Processing] \"{currentFile.FileName}\""); try { await ProcessFile(currentFile); Console.Out.WriteLine($"[Finished] \"{currentFile.FileName}\""); } catch (System.IO.FileNotFoundException e) { Console.Out.WriteLine($"[Error] File not found, removing: \"{currentFile.FileName}\""); context.RemoveRange(currentFile); await context.SaveChangesAsync(); } }