roadie/Roadie.Api.Library/Processors/FileProcessor.cs

190 lines
7.3 KiB
C#
Raw Normal View History

2018-11-11 21:13:19 +00:00
using Microsoft.Extensions.Logging;
2019-07-03 16:21:29 +00:00
using MimeMapping;
2018-11-11 21:13:19 +00:00
using Roadie.Library.Caching;
2018-11-04 20:33:37 +00:00
using Roadie.Library.Configuration;
using Roadie.Library.Data;
2019-11-17 14:10:17 +00:00
using Roadie.Library.Data.Context;
2018-11-04 20:33:37 +00:00
using Roadie.Library.Encoding;
2018-12-15 22:35:20 +00:00
using Roadie.Library.Engines;
2018-11-03 21:21:36 +00:00
using Roadie.Library.Extensions;
using Roadie.Library.FilePlugins;
2018-12-15 22:57:51 +00:00
using Roadie.Library.MetaData.Audio;
2018-11-03 21:21:36 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Roadie.Library.Processors
{
2019-07-07 03:16:33 +00:00
public sealed class FileProcessor : IFileProcessor
2018-11-03 21:21:36 +00:00
{
2019-07-07 03:16:33 +00:00
private bool DoDeleteUnknowns => Configuration.Processing.DoDeleteUnknowns;
private bool DoMoveUnknowns => Configuration.Processing.DoMoveUnknowns;
public IHttpEncoder HttpEncoder { get; }
public int? SubmissionId { get; set; }
private string UnknownFolder => Configuration.Processing.UnknownFolder;
private IArtistLookupEngine ArtistLookupEngine { get; }
private IAudioMetaDataHelper AudioMetaDataHelper { get; }
private ICacheManager CacheManager { get; }
private IRoadieSettings Configuration { get; }
private IRoadieDbContext DbContext { get; }
private ILogger Logger { get; }
private IReleaseLookupEngine ReleaseLookupEngine { get; }
2019-07-03 16:21:29 +00:00
private IEnumerable<IFilePlugin> _plugins;
2018-11-03 21:21:36 +00:00
public IEnumerable<IFilePlugin> Plugins
{
get
{
2019-07-03 16:21:29 +00:00
if (_plugins == null)
2018-11-03 21:21:36 +00:00
{
var plugins = new List<IFilePlugin>();
try
{
var type = typeof(IFilePlugin);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));
2019-07-03 16:21:29 +00:00
foreach (var t in types)
2018-11-03 21:21:36 +00:00
if (t.GetInterface("IFilePlugin") != null && !t.IsAbstract && !t.IsInterface)
{
2019-07-07 03:16:33 +00:00
var plugin = Activator.CreateInstance(t, Configuration, HttpEncoder, CacheManager, Logger, ArtistLookupEngine,
2019-07-03 16:21:29 +00:00
ReleaseLookupEngine, AudioMetaDataHelper) as IFilePlugin;
2018-11-03 21:21:36 +00:00
plugins.Add(plugin);
}
}
2018-12-14 23:38:48 +00:00
catch (Exception ex)
2018-11-04 20:33:37 +00:00
{
2019-07-03 16:21:29 +00:00
Logger.LogError(ex);
2018-11-03 21:21:36 +00:00
}
2019-07-03 16:21:29 +00:00
_plugins = plugins.ToArray();
2018-11-03 21:21:36 +00:00
}
2019-07-03 16:21:29 +00:00
return _plugins;
2018-11-03 21:21:36 +00:00
}
}
2019-07-07 03:16:33 +00:00
public FileProcessor(IRoadieSettings configuration, IHttpEncoder httpEncoder,
IRoadieDbContext context, ICacheManager cacheManager, ILogger<FileProcessor> logger,
IArtistLookupEngine artistLookupEngine, IReleaseLookupEngine releaseLookupEngine,
IAudioMetaDataHelper audioMetaDataHelper)
2018-11-03 21:21:36 +00:00
{
2019-07-07 03:16:33 +00:00
Configuration = configuration;
HttpEncoder = httpEncoder;
DbContext = context;
CacheManager = cacheManager;
Logger = logger;
ArtistLookupEngine = artistLookupEngine;
ReleaseLookupEngine = releaseLookupEngine;
AudioMetaDataHelper = audioMetaDataHelper;
2018-11-03 21:21:36 +00:00
}
2019-07-03 16:21:29 +00:00
public static string DetermineFileType(FileInfo fileinfo)
2018-11-04 20:33:37 +00:00
{
2019-07-03 16:21:29 +00:00
var r = MimeUtility.GetMimeMapping(fileinfo.FullName);
2018-11-04 20:33:37 +00:00
if (r.Equals("application/octet-stream"))
{
2019-07-03 16:21:29 +00:00
if (fileinfo.Extension.Equals(".cue")) r = "audio/r-cue";
if (fileinfo.Extension.Equals(".mp4") || fileinfo.Extension.Equals(".m4a")) r = "audio/mp4";
2018-11-04 20:33:37 +00:00
}
2019-07-03 16:21:29 +00:00
2018-11-04 20:33:37 +00:00
Trace.WriteLine(string.Format("FileType [{0}] For File [{1}]", r, fileinfo.FullName));
return r;
}
2018-11-03 21:21:36 +00:00
public async Task<OperationResult<bool>> Process(string filename, bool doJustInfo = false)
{
2019-07-03 16:21:29 +00:00
return await Process(new FileInfo(filename), doJustInfo);
2018-11-03 21:21:36 +00:00
}
public async Task<OperationResult<bool>> Process(FileInfo fileInfo, bool doJustInfo = false)
{
var result = new OperationResult<bool>();
try
{
2018-11-04 20:33:37 +00:00
// Determine what type of file this is
2019-07-03 16:21:29 +00:00
var fileType = DetermineFileType(fileInfo);
2018-11-03 21:21:36 +00:00
OperationResult<bool> pluginResult = null;
2019-07-03 16:21:29 +00:00
foreach (var p in Plugins)
2018-11-03 21:21:36 +00:00
// See if there is a plugin
if (p.HandlesTypes.Contains(fileType))
{
2019-07-07 03:16:33 +00:00
pluginResult = await p.Process(fileInfo, doJustInfo, SubmissionId);
2018-11-03 21:21:36 +00:00
break;
}
if (!doJustInfo)
// If no plugin, or if plugin not successfull and toggle then move unknown file
2019-07-03 16:21:29 +00:00
if ((pluginResult == null || !pluginResult.IsSuccess) && DoMoveUnknowns)
2018-11-03 21:21:36 +00:00
{
2019-07-03 16:21:29 +00:00
var uf = UnknownFolder;
2018-11-03 21:21:36 +00:00
if (!string.IsNullOrEmpty(uf))
{
2019-07-03 16:21:29 +00:00
if (!Directory.Exists(uf)) Directory.CreateDirectory(uf);
if (!fileInfo.DirectoryName.Equals(UnknownFolder))
2018-11-03 21:21:36 +00:00
if (File.Exists(fileInfo.FullName))
{
2019-07-03 16:21:29 +00:00
var df = Path.Combine(UnknownFolder,
string.Format("{0}~{1}~{2}", Guid.NewGuid(), fileInfo.Directory.Name,
fileInfo.Name));
Logger.LogDebug("Moving Unknown/Invalid File [{0}] -> [{1}] to UnknownFolder",
fileInfo.FullName, df);
2018-11-03 21:21:36 +00:00
fileInfo.MoveTo(df);
}
}
}
2019-07-03 16:21:29 +00:00
2018-11-03 21:21:36 +00:00
result = pluginResult;
}
2019-07-03 16:21:29 +00:00
catch (PathTooLongException ex)
2018-11-03 21:21:36 +00:00
{
2019-07-03 16:21:29 +00:00
Logger.LogError(ex, "Error Processing File. File Name Too Long. Deleting.");
if (!doJustInfo) fileInfo.Delete();
2018-11-03 21:21:36 +00:00
}
catch (Exception ex)
{
2019-07-03 16:21:29 +00:00
var willMove = !fileInfo.DirectoryName.Equals(UnknownFolder);
Logger.LogError(ex,
string.Format("Error Processing File [{0}], WillMove [{1}]\n{2}", fileInfo.FullName, willMove,
ex.Serialize()));
2018-11-03 21:21:36 +00:00
string newPath = null;
try
{
2019-07-03 16:21:29 +00:00
newPath = Path.Combine(UnknownFolder, fileInfo.Directory.Parent.Name, fileInfo.Directory.Name,
fileInfo.Name);
2018-11-03 21:21:36 +00:00
if (willMove && !doJustInfo)
{
var directoryPath = Path.GetDirectoryName(newPath);
2019-07-03 16:21:29 +00:00
if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);
2018-11-03 21:21:36 +00:00
fileInfo.MoveTo(newPath);
}
}
catch (Exception ex1)
{
2019-07-03 16:21:29 +00:00
Logger.LogError(ex1,
string.Format("Unable to move file [{0}] to [{1}]", fileInfo.FullName, newPath));
2018-11-03 21:21:36 +00:00
}
}
2019-07-03 16:21:29 +00:00
2018-11-03 21:21:36 +00:00
return result;
}
}
2018-11-04 20:33:37 +00:00
}