public void HandleAsync(AuthorDeletedEvent message)
        {
            if (message.DeleteFiles)
            {
                var author     = message.Author;
                var allArtists = _authorService.GetAllAuthors();

                foreach (var s in allArtists)
                {
                    if (s.Id == author.Id)
                    {
                        continue;
                    }

                    if (author.Path.IsParentPath(s.Path))
                    {
                        _logger.Error("Author path: '{0}' is a parent of another author, not deleting files.", author.Path);
                        return;
                    }

                    if (author.Path.PathEquals(s.Path))
                    {
                        _logger.Error("Author path: '{0}' is the same as another author, not deleting files.", author.Path);
                        return;
                    }
                }

                if (_diskProvider.FolderExists(message.Author.Path))
                {
                    _recycleBinProvider.DeleteFolder(message.Author.Path);
                }
            }
        }
Esempio n. 2
0
        public void HandleAsync(SeriesDeletedEvent message)
        {
            if (message.DeleteFiles)
            {
                var series    = message.Series;
                var allSeries = _seriesService.GetAllSeries();

                foreach (var s in allSeries)
                {
                    if (s.Id == series.Id)
                    {
                        continue;
                    }

                    if (series.Path.IsParentPath(s.Path))
                    {
                        _logger.Error("Series path: '{0}' is a parent of another series, not deleting files.", series.Path);
                        return;
                    }

                    if (series.Path.PathEquals(s.Path))
                    {
                        _logger.Error("Series path: '{0}' is the same as another series, not deleting files.", series.Path);
                        return;
                    }
                }

                if (_diskProvider.FolderExists(message.Series.Path))
                {
                    _recycleBinProvider.DeleteFolder(message.Series.Path);
                }
                _eventAggregator.PublishEvent(new DeleteCompletedEvent());
            }
        }
        public void HandleAsync(MoviesDeletedEvent message)
        {
            if (message.DeleteFiles)
            {
                var allMovies = _movieService.GetAllMovies();

                foreach (var movie in message.Movies)
                {
                    foreach (var s in allMovies)
                    {
                        if (s.Id == movie.Id)
                        {
                            continue;
                        }

                        if (movie.Path.IsParentPath(s.Path))
                        {
                            _logger.Error("Movie path: '{0}' is a parent of another movie, not deleting files.", movie.Path);
                            return;
                        }

                        if (movie.Path.PathEquals(s.Path))
                        {
                            _logger.Error("Movie path: '{0}' is the same as another movie, not deleting files.", movie.Path);
                            return;
                        }
                    }

                    if (_diskProvider.FolderExists(movie.Path))
                    {
                        _recycleBinProvider.DeleteFolder(movie.Path);
                    }
                }
            }
        }
Esempio n. 4
0
        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);
        }
 public void HandleAsync(SeriesDeletedEvent message)
 {
     if (message.DeleteFiles)
     {
         if (_diskProvider.FolderExists(message.Series.Path))
         {
             _recycleBinProvider.DeleteFolder(message.Series.Path);
         }
     }
 }
Esempio n. 6
0
        public void HandleAsync(AuthorDeletedEvent message)
        {
            if (message.DeleteFiles)
            {
                var author = message.Author;

                var rootFolder = _rootFolderService.GetBestRootFolder(message.Author.Path);
                var isCalibre  = rootFolder.IsCalibreLibrary && rootFolder.CalibreSettings != null;

                if (!isCalibre)
                {
                    var allAuthors = _authorService.AllAuthorPaths();

                    foreach (var s in allAuthors)
                    {
                        if (s.Key == author.Id)
                        {
                            continue;
                        }

                        if (author.Path.IsParentPath(s.Value))
                        {
                            _logger.Error("Author path: '{0}' is a parent of another author, not deleting files.", author.Path);
                            return;
                        }

                        if (author.Path.PathEquals(s.Value))
                        {
                            _logger.Error("Author path: '{0}' is the same as another author, not deleting files.", author.Path);
                            return;
                        }
                    }

                    if (_diskProvider.FolderExists(message.Author.Path))
                    {
                        _recycleBinProvider.DeleteFolder(message.Author.Path);
                    }

                    _eventAggregator.PublishEvent(new DeleteCompletedEvent());
                }
            }
        }
Esempio n. 7
0
        public void RenameMoviePath(Movie movie, bool shouldRenameFiles = true)
        {
            var newFolder = _filenameBuilder.BuildMoviePath(movie);

            if (newFolder != movie.Path && movie.PathState == MoviePathState.Dynamic)
            {
                if (!_configService.AutoRenameFolders)
                {
                    _logger.Info("{0}'s movie should be {1} according to your naming config.", movie, newFolder);
                    return;
                }

                _logger.Info("{0}'s movie folder changed to: {1}", movie, newFolder);
                var oldFolder = movie.Path;
                movie.Path = newFolder;

                _diskProvider.MoveFolder(oldFolder, movie.Path);

                if (false)
                {
                    var movieFiles = _mediaFileService.GetFilesByMovie(movie.Id);
                    _logger.ProgressInfo("Renaming movie files for {0}", movie.Title);
                    RenameFiles(movieFiles, movie, oldFolder);
                    _logger.ProgressInfo("All movie files renamed for {0}", movie.Title);
                }

                _movieService.UpdateMovie(movie);

                if (_diskProvider.GetFiles(oldFolder, SearchOption.AllDirectories).Count() == 0)
                {
                    _recycleBinProvider.DeleteFolder(oldFolder);
                }
            }

            if (movie.PathState == MoviePathState.StaticOnce)
            {
                movie.PathState = MoviePathState.Dynamic;
                _movieService.UpdateMovie(movie);
            }
        }
        public void HandleAsync(ArtistsDeletedEvent message)
        {
            if (message.DeleteFiles)
            {
                var artists    = message.Artists;
                var allArtists = _artistService.AllArtistPaths();

                foreach (var artist in artists)
                {
                    foreach (var s in allArtists)
                    {
                        if (s.Key == artist.Id)
                        {
                            continue;
                        }

                        if (artist.Path.IsParentPath(s.Value))
                        {
                            _logger.Error("Artist path: '{0}' is a parent of another artist, not deleting files.", artist.Path);
                            return;
                        }

                        if (artist.Path.PathEquals(s.Value))
                        {
                            _logger.Error("Artist path: '{0}' is the same as another artist, not deleting files.", artist.Path);
                            return;
                        }
                    }

                    if (_diskProvider.FolderExists(artist.Path))
                    {
                        _recycleBinProvider.DeleteFolder(artist.Path);
                    }
                }
            }
        }
Esempio n. 9
0
        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);
        }