private MovieFile TransferFile(MovieFile movieFile, Movie movie, string destinationFilePath, TransferMode mode) { Ensure.That(movieFile, () => movieFile).IsNotNull(); Ensure.That(movie, () => movie).IsNotNull(); Ensure.That(destinationFilePath, () => destinationFilePath).IsValidPath(); var movieFilePath = movieFile.Path ?? Path.Combine(movie.Path, movieFile.RelativePath); if (!_diskProvider.FileExists(movieFilePath)) { throw new FileNotFoundException("Movie file path does not exist", movieFilePath); } if (movieFilePath == destinationFilePath) { throw new SameFilenameException("File not moved, source and destination are the same", movieFilePath); } _diskTransferService.TransferFile(movieFilePath, destinationFilePath, mode); var oldMoviePath = movie.Path; var newMoviePath = new OsPath(destinationFilePath).Directory.FullPath.TrimEnd(Path.DirectorySeparatorChar); movie.Path = newMoviePath; if (oldMoviePath != newMoviePath) { _movieService.UpdateMovie(movie); } movieFile.RelativePath = movie.Path.GetRelativePath(destinationFilePath); _updateMovieFileService.ChangeFileDateForFile(movieFile, movie); try { _mediaFileAttributeService.SetFolderLastWriteTime(movie.Path, movieFile.DateAdded); } catch (Exception ex) { _logger.Warn(ex, "Unable to set last write time"); } _mediaFileAttributeService.SetFilePermissions(destinationFilePath); if (oldMoviePath != newMoviePath) { if (_diskProvider.GetFiles(oldMoviePath, SearchOption.AllDirectories).Count() == 0) { _recycleBinProvider.DeleteFolder(oldMoviePath); } } return(movieFile); }
private MovieFile TransferFile(MovieFile movieFile, Movie movie, string destinationFilePath, TransferMode mode) { Ensure.That(movieFile, () => movieFile).IsNotNull(); Ensure.That(movie, () => movie).IsNotNull(); Ensure.That(destinationFilePath, () => destinationFilePath).IsValidPath(); var movieFilePath = movieFile.Path ?? Path.Combine(movie.Path, movieFile.RelativePath); if (!_diskProvider.FileExists(movieFilePath)) { throw new FileNotFoundException("Movie file path does not exist", movieFilePath); } if (movieFilePath == destinationFilePath) { throw new SameFilenameException("File not moved, source and destination are the same", movieFilePath); } _diskTransferService.TransferFile(movieFilePath, destinationFilePath, mode); movieFile.RelativePath = movie.Path.GetRelativePath(destinationFilePath); _updateMovieFileService.ChangeFileDateForFile(movieFile, movie); try { _mediaFileAttributeService.SetFolderLastWriteTime(movie.Path, movieFile.DateAdded); } catch (Exception ex) { _logger.Warn(ex, "Unable to set last write time"); } _mediaFileAttributeService.SetFilePermissions(destinationFilePath); return(movieFile); }
private MovieFile TransferFile(MovieFile movieFile, Movie movie, string destinationFilePath, TransferMode mode) { Ensure.That(movieFile, () => movieFile).IsNotNull(); Ensure.That(movie, () => movie).IsNotNull(); Ensure.That(destinationFilePath, () => destinationFilePath).IsValidPath(); var movieFilePath = movieFile.Path ?? Path.Combine(movie.Path, movieFile.RelativePath); if (!_diskProvider.FileExists(movieFilePath)) { throw new FileNotFoundException("Movie file path does not exist", movieFilePath); } if (movieFilePath == destinationFilePath) { throw new SameFilenameException("File not moved, source and destination are the same", movieFilePath); } _diskTransferService.TransferFile(movieFilePath, destinationFilePath, mode); var oldMoviePath = movie.Path; var newMoviePath = new OsPath(destinationFilePath).Directory.FullPath.TrimEnd(Path.DirectorySeparatorChar); movie.Path = newMoviePath; //We update it when everything went well! movieFile.RelativePath = movie.Path.GetRelativePath(destinationFilePath); _updateMovieFileService.ChangeFileDateForFile(movieFile, movie); try { _mediaFileAttributeService.SetFolderLastWriteTime(movie.Path, movieFile.DateAdded); } catch (Exception ex) { _logger.Warn(ex, "Unable to set last write time"); } _mediaFileAttributeService.SetFilePermissions(destinationFilePath); if (oldMoviePath != newMoviePath && _diskProvider.FolderExists(oldMoviePath)) { //Let's move the old files before deleting the old folder. We could just do move folder, but the main file (movie file) is already moved, so eh. var files = _diskProvider.GetFiles(oldMoviePath, SearchOption.AllDirectories); foreach (var file in files) { try { var destFile = Path.Combine(newMoviePath, oldMoviePath.GetRelativePath(file)); _diskProvider.EnsureFolder(Path.GetDirectoryName(destFile)); _diskProvider.MoveFile(file, destFile); } catch (Exception e) { _logger.Warn(e, "Error while trying to move extra file {0} to new folder. Maybe it already exists? (Manual cleanup necessary!).", oldMoviePath.GetRelativePath(file)); } } if (_diskProvider.GetFiles(oldMoviePath, SearchOption.AllDirectories).Count() == 0) { _recycleBinProvider.DeleteFolder(oldMoviePath); } } //Only update the movie path if we were successfull! if (oldMoviePath != newMoviePath) { _movieService.UpdateMovie(movie); } return(movieFile); }