Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Jan 12, 2024
1 parent a5822b5 commit d7365d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
54 changes: 23 additions & 31 deletions Shoko.Server/Services/VideoLocal_PlaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public RenameFileResult RenameFile(SVR_VideoLocal_Place place, bool preview = fa

// actually rename the file
var path = Path.GetDirectoryName(fullFileName);
var newFullName = Path.Combine(path, renamed);
var newFullName = Path.Combine(path!, renamed);

try
{
Expand Down Expand Up @@ -200,20 +200,22 @@ public RenameFileResult RenameFile(SVR_VideoLocal_Place place, bool preview = fa
var filenameHash = RepoFactory.FileNameHash.GetByHash(place.VideoLocal.Hash);
if (!filenameHash.Any(a => a.FileName.Equals(renamed)))
{
var fnhash = new FileNameHash
var fnHash = new FileNameHash
{
DateTimeUpdated = DateTime.Now,
FileName = renamed,
FileSize = place.VideoLocal.FileSize,
Hash = place.VideoLocal.Hash
};
RepoFactory.FileNameHash.Save(fnhash);
RepoFactory.FileNameHash.Save(fnHash);
}

place.FilePath = filePath;
RepoFactory.VideoLocalPlace.Save(place);
// just in case
#pragma warning disable CS0618 // Type or member is obsolete
place.VideoLocal.FileName = renamed;
#pragma warning restore CS0618 // Type or member is obsolete
RepoFactory.VideoLocal.Save(place.VideoLocal, false);

ShokoEventHandler.Instance.OnFileRenamed(place.ImportFolder, Path.GetFileName(fullFileName), renamed, place);
Expand Down Expand Up @@ -241,7 +243,7 @@ private void RenameExternalSubtitles(string fullFileName, string renamed)
continue;
}

var oldSubPath = Path.Combine(path, sub.Filename);
var oldSubPath = Path.Combine(path!, sub.Filename);

if (!File.Exists(oldSubPath))
{
Expand Down Expand Up @@ -437,11 +439,11 @@ private void MoveExternalSubtitles(string newFullServerPath, string originalFile
}
catch (Exception ex)
{
_logger.LogError(ex, ex.ToString());
_logger.LogError(ex, "Unable to move subtitles for \"{Place}\": {Ex}", originalFileName, ex.ToString());
}
}

private void RecursiveDeleteEmptyDirectories(string dir, bool importfolder)
private void RecursiveDeleteEmptyDirectories(string dir, bool importFolder)
{
try
{
Expand All @@ -451,7 +453,7 @@ private void RecursiveDeleteEmptyDirectories(string dir, bool importfolder)

if (IsDirectoryEmpty(dir))
{
if (importfolder) return;
if (importFolder) return;

try
{
Expand Down Expand Up @@ -480,7 +482,7 @@ private void RecursiveDeleteEmptyDirectories(string dir, bool importfolder)
}
}

public static bool IsDirectoryEmpty(string path)
private static bool IsDirectoryEmpty(string path)
{
try
{
Expand Down Expand Up @@ -565,13 +567,7 @@ public void RemoveRecordAndDeletePhysicalFile(SVR_VideoLocal_Place place, bool d
}
catch (FileNotFoundException)
{
if (deleteFolder)
{
RecursiveDeleteEmptyDirectories(place.ImportFolder?.ImportFolderLocation, true);
}

RemoveRecord(place);
return;
// ignore
}
catch (Exception ex)
{
Expand All @@ -581,23 +577,22 @@ public void RemoveRecordAndDeletePhysicalFile(SVR_VideoLocal_Place place, bool d

if (deleteFolder)
{
RecursiveDeleteEmptyDirectories(place.ImportFolder?.ImportFolderLocation, true);
RecursiveDeleteEmptyDirectories(Path.GetDirectoryName(place.FullServerPath), true);
}

RemoveRecord(place);
}

public void RemoveAndDeleteFileWithOpenTransaction(ISession session, SVR_VideoLocal_Place place, HashSet<SVR_AnimeSeries> seriesToUpdate)
public void RemoveAndDeleteFileWithOpenTransaction(ISession session, SVR_VideoLocal_Place place, HashSet<SVR_AnimeSeries> seriesToUpdate, bool updateMyList = true, bool deleteFolders = true)
{
// TODO Make this take an argument to disable removing empty dirs. It's slow, and should only be done if needed
try
{
_logger.LogInformation("Deleting video local place record and file: {Place}", place.FullServerPath ?? place.VideoLocal_Place_ID.ToString());

if (!File.Exists(place.FullServerPath))
{
_logger.LogInformation("Unable to find file. Removing Record: {FullServerPath}", place.FullServerPath);
RemoveRecordWithOpenTransaction(session, place, seriesToUpdate);
RemoveRecordWithOpenTransaction(session, place, seriesToUpdate, updateMyList);
return;
}

Expand All @@ -606,26 +601,24 @@ public void RemoveAndDeleteFileWithOpenTransaction(ISession session, SVR_VideoLo
File.Delete(place.FullServerPath);
DeleteExternalSubtitles(place.FullServerPath);
}
catch (FileNotFoundException)
{
// ignore
}
catch (Exception ex)
{
if (ex is FileNotFoundException)
{
RecursiveDeleteEmptyDirectories(place.ImportFolder?.ImportFolderLocation, true);
RemoveRecordWithOpenTransaction(session, place, seriesToUpdate);
return;
}

_logger.LogError(ex, "Unable to delete file \'{Place}\': {Ex}", place.FullServerPath, ex);
return;
}

RecursiveDeleteEmptyDirectories(place.ImportFolder?.ImportFolderLocation, true);
RemoveRecordWithOpenTransaction(session, place, seriesToUpdate);
if (deleteFolders) RecursiveDeleteEmptyDirectories(Path.GetDirectoryName(place.FullServerPath), true);
RemoveRecordWithOpenTransaction(session, place, seriesToUpdate, updateMyList);
// For deletion of files from Trakt, we will rely on the Daily sync
}
catch (Exception ex)
{
_logger.LogError(ex, ex.ToString());
_logger.LogError(ex, "Could not delete file and remove record for \"{Place}\": {Ex}", place.FullServerPath ?? place.VideoLocal_Place_ID.ToString(),
ex);
}
}

Expand Down Expand Up @@ -749,7 +742,6 @@ public void RemoveRecord(SVR_VideoLocal_Place place, bool updateMyListStatus = t
}
}


public void RemoveRecordWithOpenTransaction(ISession session, SVR_VideoLocal_Place place, ICollection<SVR_AnimeSeries> seriesToUpdate,
bool updateMyListStatus = true)
{
Expand Down Expand Up @@ -791,7 +783,7 @@ public void RemoveRecordWithOpenTransaction(ISession session, SVR_VideoLocal_Pla
}
}

var eps = v?.GetAnimeEpisodes()?.Where(a => a != null).ToList();
var eps = v.GetAnimeEpisodes()?.Where(a => a != null).ToList();
eps?.DistinctBy(a => a.AnimeSeriesID).Select(a => a.GetAnimeSeries()).ToList().ForEach(seriesToUpdate.Add);

try
Expand Down
1 change: 0 additions & 1 deletion Shoko.Server/Utilities/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public void DeleteAllErroredFiles()

var files = ActiveErrorFiles.ToList();
ActiveErrorFiles.Clear();
var episodesToUpdate = new HashSet<SVR_AnimeEpisode>();
var seriesToUpdate = new HashSet<SVR_AnimeSeries>();
var service = Utils.ServiceContainer.GetRequiredService<VideoLocal_PlaceService>();
using (var session = DatabaseFactory.SessionFactory.OpenSession())
Expand Down

0 comments on commit d7365d8

Please sign in to comment.