Skip to content

Commit

Permalink
A bunch of refactoring for File Operations
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Jan 12, 2024
1 parent 3667002 commit a5822b5
Show file tree
Hide file tree
Showing 19 changed files with 985 additions and 1,190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Shoko.Server.Filters.Legacy;
using Shoko.Server.Models;
using Shoko.Server.Repositories;
using Shoko.Server.Services;
using Shoko.Server.Tasks;
using Shoko.Server.Utilities;

Expand Down Expand Up @@ -974,7 +975,9 @@ public string DeleteVideoLocalPlaceAndFile(int videoplaceid)
return "Database entry does not exist";
}

return place.RemoveAndDeleteFile().Item2;
var service = HttpContext.RequestServices.GetRequiredService<VideoLocal_PlaceService>();
service.RemoveRecordAndDeletePhysicalFile(place);
return string.Empty;
}
catch (Exception ex)
{
Expand All @@ -999,7 +1002,9 @@ public string DeleteVideoLocalPlaceAndFileSkipFolder(int videoplaceid)
return "Database entry does not exist";
}

return place.RemoveAndDeleteFile(false).Item2;
var service = HttpContext.RequestServices.GetRequiredService<VideoLocal_PlaceService>();
service.RemoveRecordAndDeletePhysicalFile(place);
return string.Empty;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -2609,6 +2614,7 @@ public string DeleteAnimeSeries(int animeSeriesID, bool deleteFiles, bool delete
}

var animeGroupID = ser.AnimeGroupID;
var service = HttpContext.RequestServices.GetRequiredService<VideoLocal_PlaceService>();

foreach (var ep in ser.GetAnimeEpisodes())
{
Expand All @@ -2620,25 +2626,18 @@ public string DeleteAnimeSeries(int animeSeriesID, bool deleteFiles, bool delete
var place = places[index];
if (deleteFiles)
{
bool success;
string result;
if (index < places.Count - 1)
try
{
(success, result) = place.RemoveAndDeleteFile(false);
service.RemoveRecordAndDeletePhysicalFile(place, index >= places.Count - 1);
}
else
{
(success, result) = place.RemoveAndDeleteFile();
}

if (!success)
catch (Exception e)
{
return result;
return e.Message;
}
}
else
{
place.RemoveRecord();
service.RemoveRecord(place);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Shoko.Server.Providers.AniDB.Titles;
using Shoko.Server.Repositories;
using Shoko.Server.Server;
using Shoko.Server.Services;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;

Expand Down Expand Up @@ -184,9 +185,21 @@ public bool DeleteMultipleFilesWithPreferences(int userID)
}

var result = true;
var service = HttpContext.RequestServices.GetRequiredService<VideoLocal_PlaceService>();
foreach (var toDelete in videosToDelete)
{
result &= toDelete.Places.All(a => a.RemoveAndDeleteFile().Item1);
result &= toDelete.Places.All(a =>
{
try
{
service.RemoveRecordAndDeletePhysicalFile(a);
return true;
}
catch
{
return false;
}
});
}
return result;
}
Expand Down Expand Up @@ -471,24 +484,25 @@ public CL_VideoLocal_Renamed RenameAndMoveFile(int videoLocalID, string scriptNa
var errorCount = 0;
var errorString = string.Empty;
var name = Path.GetFileName(vid.GetBestVideoLocalPlace().FilePath);
var service = HttpContext.RequestServices.GetRequiredService<VideoLocal_PlaceService>();

foreach (var place in vid.Places)
{
if (move)
{
var resultString = place.MoveWithResultString(scriptName);
if (!string.IsNullOrEmpty(resultString.Item2))
var result = service.MoveFile(place, scriptName: scriptName);
if (!result.IsSuccess)
{
errorCount++;
errorString = resultString.Item2;
errorString = result.ErrorMessage;
continue;
}
ret.NewDestination = resultString.Item1;
ret.NewDestination = result.NewFolder;
}

var output = place.RenameFile(false, scriptName);
var error = output.Item3;
if (string.IsNullOrEmpty(error)) name = output.Item2;
var output = service.RenameFile(place, scriptName: scriptName);
var error = output.ErrorMessage;
if (string.IsNullOrEmpty(error)) name = output.NewFilename;
else
{
errorCount++;
Expand All @@ -502,8 +516,8 @@ public CL_VideoLocal_Renamed RenameAndMoveFile(int videoLocalID, string scriptNa
ret.NewFileName = errorString;
return ret;
}
if (ret.VideoLocal == null)
ret.VideoLocal = new CL_VideoLocal {VideoLocalID = videoLocalID };

ret.VideoLocal ??= new CL_VideoLocal { VideoLocalID = videoLocalID };
}
catch (Exception ex)
{
Expand Down Expand Up @@ -1198,7 +1212,9 @@ public string DeleteDuplicateFile(int videoLocalPlaceID)
try
{
var place = RepoFactory.VideoLocalPlace.GetByID(videoLocalPlaceID);
return place.RemoveAndDeleteFile().Item2;
var service = HttpContext.RequestServices.GetRequiredService<VideoLocal_PlaceService>();
service.RemoveRecordAndDeletePhysicalFile(place, false);
return string.Empty;
}
catch (Exception ex)
{
Expand Down
14 changes: 8 additions & 6 deletions Shoko.Server/API/v3/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Shoko.Server.Providers.TraktTV;
using Shoko.Server.Commands;
using Shoko.Server.Repositories;
using Shoko.Server.Services;
using Shoko.Server.Settings;

using CommandRequestPriority = Shoko.Server.Server.CommandRequestPriority;
Expand All @@ -44,13 +45,14 @@ public class FileController : BaseController
internal const string FileNotFoundWithFileID = "No File entry for the given fileID";

private readonly TraktTVHelper _traktHelper;

private readonly ICommandRequestFactory _commandFactory;
private readonly VideoLocal_PlaceService _vlPlaceService;

public FileController(TraktTVHelper traktHelper, ICommandRequestFactory commandFactory, ISettingsProvider settingsProvider) : base(settingsProvider)
public FileController(TraktTVHelper traktHelper, ICommandRequestFactory commandFactory, ISettingsProvider settingsProvider, VideoLocal_PlaceService vlPlaceService) : base(settingsProvider)
{
_traktHelper = traktHelper;
_commandFactory = commandFactory;
_vlPlaceService = vlPlaceService;
}

internal const string FileForbiddenForUser = "Accessing File is not allowed for the current user";
Expand Down Expand Up @@ -145,9 +147,9 @@ public ActionResult DeleteFiles([FromBody] File.Input.BatchDeleteBody body = nul
foreach (var place in file.Places)
{
if (body.removeFiles)
place.RemoveRecordAndDeletePhysicalFile(body.removeFolders);
_vlPlaceService.RemoveRecordAndDeletePhysicalFile(place, body.removeFolders);
else
place.RemoveRecord();
_vlPlaceService.RemoveRecord(place);
}
}

Expand Down Expand Up @@ -193,9 +195,9 @@ public ActionResult DeleteFile([FromRoute] int fileID, [FromQuery] bool removeFi

foreach (var place in file.Places)
if (removeFiles)
place.RemoveRecordAndDeletePhysicalFile(removeFolder);
_vlPlaceService.RemoveRecordAndDeletePhysicalFile(place, removeFolder);
else
place.RemoveRecord();
_vlPlaceService.RemoveRecord(place);
return Ok();
}

Expand Down
13 changes: 7 additions & 6 deletions Shoko.Server/Commands/Import/CommandRequest_HashFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Shoko.Server.Repositories;
using Shoko.Server.Repositories.Cached;
using Shoko.Server.Server;
using Shoko.Server.Services;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;

Expand All @@ -26,6 +27,7 @@ public class CommandRequest_HashFile : CommandRequestImplementation
{
private readonly ICommandRequestFactory _commandFactory;
private readonly ISettingsProvider _settingsProvider;
private readonly VideoLocal_PlaceService _vlPlaceService;
public virtual string FileName { get; set; }
public virtual bool ForceHash { get; set; }
public virtual bool SkipMyList { get; set; }
Expand Down Expand Up @@ -121,7 +123,7 @@ protected override void Process()

if ((vlocal.Media?.GeneralStream?.Duration ?? 0) == 0 || vlocal.MediaVersion < SVR_VideoLocal.MEDIA_VERSION)
{
if (vlocalplace.RefreshMediaInfo())
if (_vlPlaceService.RefreshMediaInfo(vlocalplace))
{
RepoFactory.VideoLocal.Save(vlocalplace.VideoLocal, true);
}
Expand Down Expand Up @@ -534,7 +536,7 @@ private bool ProcessDuplicates(SVR_VideoLocal vlocal, SVR_VideoLocal_Place vloca
Logger.LogWarning("---------------------------------------------");

var settings = _settingsProvider.GetSettings();
if (settings.Import.AutomaticallyDeleteDuplicatesOnImport) vlocalplace.RemoveRecordAndDeletePhysicalFile();
if (settings.Import.AutomaticallyDeleteDuplicatesOnImport) _vlPlaceService.RemoveRecordAndDeletePhysicalFile(vlocalplace);
return true;
}

Expand Down Expand Up @@ -584,14 +586,13 @@ protected override bool Load()
return FileName.Trim().Length > 0;
}

public CommandRequest_HashFile(ILoggerFactory loggerFactory, ICommandRequestFactory commandFactory, ISettingsProvider settingsProvider) :
public CommandRequest_HashFile(ILoggerFactory loggerFactory, ICommandRequestFactory commandFactory, ISettingsProvider settingsProvider, VideoLocal_PlaceService vlPlaceService) :
base(loggerFactory)
{
_commandFactory = commandFactory;
_settingsProvider = settingsProvider;
_vlPlaceService = vlPlaceService;
}

protected CommandRequest_HashFile()
{
}
protected CommandRequest_HashFile() { }
}
13 changes: 7 additions & 6 deletions Shoko.Server/Commands/Import/CommandRequest_LinkFileManually.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Shoko.Server.Models;
using Shoko.Server.Repositories;
using Shoko.Server.Server;
using Shoko.Server.Services;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;

Expand All @@ -23,6 +24,7 @@ public class CommandRequest_LinkFileManually : CommandRequestImplementation
{
private readonly ICommandRequestFactory _commandFactory;
private readonly IServerSettings _settings;
private readonly VideoLocal_PlaceService _vlPlaceService;
public virtual int VideoLocalID { get; set; }
public virtual int EpisodeID { get; set; }
public virtual int Percentage { get; set; }
Expand Down Expand Up @@ -73,7 +75,7 @@ protected override void Process()

ProcessFileQualityFilter();

_vlocal.Places.ForEach(a => { a.RenameAndMoveAsRequired(); });
_vlocal.Places.ForEach(a => { _vlPlaceService.RenameAndMoveAsRequired(a); });

// Set the import date.
_vlocal.DateTimeImported = DateTime.Now;
Expand Down Expand Up @@ -115,7 +117,7 @@ private void ProcessFileQualityFilter()

videoLocals = videoLocals.Where(FileQualityFilter.CheckFileKeep).ToList();

foreach (var toDelete in videoLocals) toDelete.Places.ForEach(a => a.RemoveRecordAndDeletePhysicalFile());
foreach (var toDelete in videoLocals) toDelete.Places.ForEach(a => _vlPlaceService.RemoveRecordAndDeletePhysicalFile(a));
}

/// <summary>
Expand Down Expand Up @@ -156,14 +158,13 @@ protected override bool Load()
return true;
}

public CommandRequest_LinkFileManually(ILoggerFactory loggerFactory, ICommandRequestFactory commandFactory, ISettingsProvider settingsProvider) :
public CommandRequest_LinkFileManually(ILoggerFactory loggerFactory, ICommandRequestFactory commandFactory, ISettingsProvider settingsProvider, VideoLocal_PlaceService vlPlaceService) :
base(loggerFactory)
{
_commandFactory = commandFactory;
_vlPlaceService = vlPlaceService;
_settings = settingsProvider.GetSettings();
}

protected CommandRequest_LinkFileManually()
{
}
protected CommandRequest_LinkFileManually() { }
}
15 changes: 7 additions & 8 deletions Shoko.Server/Commands/Import/CommandRequest_ProcessFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Shoko.Server.Providers.AniDB.Interfaces;
using Shoko.Server.Repositories;
using Shoko.Server.Server;
using Shoko.Server.Services;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;

Expand All @@ -25,10 +26,9 @@ namespace Shoko.Server.Commands;
public class CommandRequest_ProcessFile : CommandRequestImplementation
{
private readonly ICommandRequestFactory _commandFactory;

private readonly IServerSettings _settings;

private readonly IUDPConnectionHandler _udpConnectionHandler;
private readonly VideoLocal_PlaceService _vlPlaceService;

public virtual int VideoLocalID { get; set; }

Expand Down Expand Up @@ -84,7 +84,7 @@ protected override void Process()
var aniFile = ProcessFile_AniDB(vlocal);

// Rename and/or move the physical file(s) if needed.
vlocal.Places.ForEach(a => { a.RenameAndMoveAsRequired(); });
vlocal.Places.ForEach(a => { _vlPlaceService.RenameAndMoveAsRequired(a); });

// Check if an AniDB file is now available and if the cross-references changed.
var newXRefs = vlocal.EpisodeCrossRefs
Expand Down Expand Up @@ -157,7 +157,7 @@ private SVR_AniDB_File ProcessFile_AniDB(SVR_VideoLocal vidLocal)

videoLocals = videoLocals.Where(a => !FileQualityFilter.CheckFileKeep(a)).ToList();

videoLocals.ForEach(a => a.Places.ForEach(b => b.RemoveRecordAndDeletePhysicalFile()));
videoLocals.ForEach(a => a.Places.ForEach(b => _vlPlaceService.RemoveRecordAndDeletePhysicalFile(b)));
}

// we have an AniDB File, so check the release group info
Expand Down Expand Up @@ -453,15 +453,14 @@ protected override bool Load()
return true;
}

public CommandRequest_ProcessFile(ILoggerFactory loggerFactory, ICommandRequestFactory commandFactory, ISettingsProvider settingsProvider, IUDPConnectionHandler udpConnectionHandler) :
public CommandRequest_ProcessFile(ILoggerFactory loggerFactory, ICommandRequestFactory commandFactory, ISettingsProvider settingsProvider, IUDPConnectionHandler udpConnectionHandler, VideoLocal_PlaceService vlPlaceService) :
base(loggerFactory)
{
_commandFactory = commandFactory;
_settings = settingsProvider.GetSettings();
_udpConnectionHandler = udpConnectionHandler;
_vlPlaceService = vlPlaceService;
}

protected CommandRequest_ProcessFile()
{
}
protected CommandRequest_ProcessFile() { }
}
Loading

0 comments on commit a5822b5

Please sign in to comment.