Esempio n. 1
0
        private void OrganizeMovie(string sourcePath,
                                   Movie movie,
                                   MovieFileOrganizationOptions options,
                                   FileOrganizationResult result,
                                   CancellationToken cancellationToken)
        {
            _logger.Info("Sorting file {0} into movie {1}", sourcePath, movie.Path);

            bool isNew = string.IsNullOrWhiteSpace(result.Id);

            if (isNew)
            {
                _organizationService.SaveResult(result, cancellationToken);
            }

            if (!_organizationService.AddToInProgressList(result, isNew))
            {
                throw new OrganizationException("File is currently processed otherwise. Please try again later.");
            }

            try
            {
                // Proceed to sort the file
                var newPath = movie.Path;
                _logger.Info("Sorting file {0} to new path {1}", sourcePath, newPath);
                result.TargetPath = newPath;

                var fileExists = _fileSystem.FileExists(result.TargetPath);

                if (!options.OverwriteExistingFiles)
                {
                    if (options.CopyOriginalFile && fileExists && IsSameMovie(sourcePath, newPath))
                    {
                        var msg = string.Format("File '{0}' already copied to new path '{1}', stopping organization", sourcePath, newPath);
                        _logger.Info(msg);
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        return;
                    }

                    if (fileExists)
                    {
                        var msg = string.Format("File '{0}' already exists as '{1}', stopping organization", sourcePath, newPath);
                        _logger.Info(msg);
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        result.TargetPath    = newPath;
                        return;
                    }
                }

                PerformFileSorting(options, result);
            }
            catch (OrganizationException ex)
            {
                result.Status        = FileSortingStatus.Failure;
                result.StatusMessage = ex.Message;
            }
            catch (Exception ex)
            {
                result.Status        = FileSortingStatus.Failure;
                result.StatusMessage = ex.Message;
                _logger.Warn(ex.Message);
            }
            finally
            {
                _organizationService.RemoveFromInprogressList(result);
            }
        }
Esempio n. 2
0
        private async Task OrganizeEpisode(string sourcePath,
                                           Series series,
                                           int?seasonNumber,
                                           int?episodeNumber,
                                           int?endingEpiosdeNumber,
                                           DateTime?premiereDate,
                                           AutoOrganizeOptions options,
                                           bool overwriteExisting,
                                           bool rememberCorrection,
                                           FileOrganizationResult result,
                                           CancellationToken cancellationToken)
        {
            _logger.Info("Sorting file {0} into series {1}", sourcePath, series.Path);

            var originalExtractedSeriesString = result.ExtractedName;

            bool isNew = string.IsNullOrWhiteSpace(result.Id);

            if (isNew)
            {
                await _organizationService.SaveResult(result, cancellationToken);
            }

            if (!_organizationService.AddToInProgressList(result, isNew))
            {
                throw new Exception("File is currently processed otherwise. Please try again later.");
            }

            try
            {
                // Proceed to sort the file
                var newPath = await GetNewPath(sourcePath, series, seasonNumber, episodeNumber, endingEpiosdeNumber, premiereDate, options.TvOptions, cancellationToken).ConfigureAwait(false);

                if (string.IsNullOrEmpty(newPath))
                {
                    var msg = string.Format("Unable to sort {0} because target path could not be determined.", sourcePath);
                    throw new Exception(msg);
                }

                _logger.Info("Sorting file {0} to new path {1}", sourcePath, newPath);
                result.TargetPath = newPath;

                var fileExists          = _fileSystem.FileExists(result.TargetPath);
                var otherDuplicatePaths = GetOtherDuplicatePaths(result.TargetPath, series, seasonNumber, episodeNumber, endingEpiosdeNumber);

                if (!overwriteExisting)
                {
                    if (options.TvOptions.CopyOriginalFile && fileExists && IsSameEpisode(sourcePath, newPath))
                    {
                        var msg = string.Format("File '{0}' already copied to new path '{1}', stopping organization", sourcePath, newPath);
                        _logger.Info(msg);
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        return;
                    }

                    if (fileExists)
                    {
                        var msg = string.Format("File '{0}' already exists as '{1}', stopping organization", sourcePath, newPath);
                        _logger.Info(msg);
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        result.TargetPath    = newPath;
                        return;
                    }

                    if (otherDuplicatePaths.Count > 0)
                    {
                        var msg = string.Format("File '{0}' already exists as these:'{1}'. Stopping organization", sourcePath, string.Join("', '", otherDuplicatePaths));
                        _logger.Info(msg);
                        result.Status         = FileSortingStatus.SkippedExisting;
                        result.StatusMessage  = msg;
                        result.DuplicatePaths = otherDuplicatePaths;
                        return;
                    }
                }

                PerformFileSorting(options.TvOptions, result);

                if (overwriteExisting)
                {
                    var hasRenamedFiles = false;

                    foreach (var path in otherDuplicatePaths)
                    {
                        _logger.Debug("Removing duplicate episode {0}", path);

                        _libraryMonitor.ReportFileSystemChangeBeginning(path);

                        var renameRelatedFiles = !hasRenamedFiles &&
                                                 string.Equals(_fileSystem.GetDirectoryName(path), _fileSystem.GetDirectoryName(result.TargetPath), StringComparison.OrdinalIgnoreCase);

                        if (renameRelatedFiles)
                        {
                            hasRenamedFiles = true;
                        }

                        try
                        {
                            DeleteLibraryFile(path, renameRelatedFiles, result.TargetPath);
                        }
                        catch (IOException ex)
                        {
                            _logger.ErrorException("Error removing duplicate episode", ex, path);
                        }
                        finally
                        {
                            _libraryMonitor.ReportFileSystemChangeComplete(path, true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status        = FileSortingStatus.Failure;
                result.StatusMessage = ex.Message;
                _logger.Warn(ex.Message);
                return;
            }
            finally
            {
                _organizationService.RemoveFromInprogressList(result);
            }

            if (rememberCorrection)
            {
                SaveSmartMatchString(originalExtractedSeriesString, series, options);
            }
        }
        private Task OrganizeMovie(
            string sourcePath,
            Movie movie,
            MovieFileOrganizationOptions options,
            bool overwriteExisting,
            FileOrganizationResult result,
            CancellationToken cancellationToken)
        {
            _logger.LogInformation("Sorting file {0} into movie {1}", sourcePath, movie.Path);

            bool isNew = string.IsNullOrWhiteSpace(result.Id);

            if (isNew)
            {
                _organizationService.SaveResult(result, cancellationToken);
            }

            if (!_organizationService.AddToInProgressList(result, isNew))
            {
                throw new OrganizationException("File is currently processed otherwise. Please try again later.");
            }

            try
            {
                // Proceed to sort the file
                var newPath = movie.Path;
                _logger.LogInformation("Sorting file {0} to new path {1}", sourcePath, newPath);
                result.TargetPath = newPath;

                var fileExists = File.Exists(result.TargetPath);

                if (!overwriteExisting)
                {
                    if (options.CopyOriginalFile && fileExists && IsSameMovie(sourcePath, newPath))
                    {
                        var msg = $"File '{sourcePath}' already copied to new path '{newPath}', stopping organization";
                        _logger.LogInformation("File {SourcePath} already copied to new path {NewPath}, stopping organization", sourcePath, newPath);
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        return(Task.CompletedTask);
                    }

                    if (fileExists)
                    {
                        var msg = $"File '{sourcePath}' already exists as '{newPath}', stopping organization";
                        _logger.LogInformation("File {SourcePath} already exists as {NewPath}, stopping organization", sourcePath, newPath);
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        result.TargetPath    = newPath;
                        return(Task.CompletedTask);
                    }
                }

                PerformFileSorting(options, result);
            }
            catch (OrganizationException ex)
            {
                result.Status        = FileSortingStatus.Failure;
                result.StatusMessage = ex.Message;
            }
            catch (Exception ex)
            {
                result.Status        = FileSortingStatus.Failure;
                result.StatusMessage = ex.Message;
                _logger.LogError(ex, "Caught a generic exception while organizing {0}", sourcePath);
            }
            finally
            {
                _organizationService.RemoveFromInprogressList(result);
            }

            return(Task.CompletedTask);
        }
Esempio n. 4
0
        private Task OrganizeEpisode(
            string sourcePath,
            Series series,
            Episode episode,
            TvFileOrganizationOptions options,
            bool rememberCorrection,
            FileOrganizationResult result,
            CancellationToken cancellationToken)
        {
            _logger.LogInformation("Sorting file {0} into series {1}", sourcePath, series.Path);

            var originalExtractedSeriesString = result.ExtractedName;

            bool isNew = string.IsNullOrWhiteSpace(result.Id);

            if (isNew)
            {
                _organizationService.SaveResult(result, cancellationToken);
            }

            if (!_organizationService.AddToInProgressList(result, isNew))
            {
                throw new OrganizationException("File is currently processed otherwise. Please try again later.");
            }

            try
            {
                // Proceed to sort the file
                var newPath = episode.Path;

                if (string.IsNullOrEmpty(newPath))
                {
                    var msg = $"Unable to sort {sourcePath} because target path could not be determined.";
                    throw new OrganizationException(msg);
                }

                _logger.LogInformation("Sorting file {0} to new path {1}", sourcePath, newPath);
                result.TargetPath = newPath;

                var fileExists          = File.Exists(result.TargetPath);
                var otherDuplicatePaths = GetOtherDuplicatePaths(result.TargetPath, series, episode);

                if (!options.OverwriteExistingEpisodes)
                {
                    if (options.CopyOriginalFile && fileExists && IsSameEpisode(sourcePath, newPath))
                    {
                        var msg = $"File '{sourcePath}' already copied to new path '{newPath}', stopping organization";
                        _logger.LogInformation(msg);
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        return(Task.CompletedTask);
                    }

                    if (fileExists)
                    {
                        var msg = $"File '{sourcePath}' already exists as '{newPath}', stopping organization";
                        _logger.LogInformation(msg);
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        result.TargetPath    = newPath;
                        return(Task.CompletedTask);
                    }

                    if (otherDuplicatePaths.Count > 0)
                    {
                        var msg = $"File '{sourcePath}' already exists as these:'{string.Join("', '", otherDuplicatePaths)}'. Stopping organization";
                        _logger.LogInformation(msg);
                        result.Status         = FileSortingStatus.SkippedExisting;
                        result.StatusMessage  = msg;
                        result.DuplicatePaths = otherDuplicatePaths;
                        return(Task.CompletedTask);
                    }
                }

                PerformFileSorting(options, result);

                if (options.OverwriteExistingEpisodes)
                {
                    var hasRenamedFiles = false;

                    foreach (var path in otherDuplicatePaths)
                    {
                        _logger.LogDebug("Removing duplicate episode {0}", path);

                        _libraryMonitor.ReportFileSystemChangeBeginning(path);

                        var renameRelatedFiles = !hasRenamedFiles &&
                                                 string.Equals(Path.GetDirectoryName(path), Path.GetDirectoryName(result.TargetPath), StringComparison.OrdinalIgnoreCase);

                        if (renameRelatedFiles)
                        {
                            hasRenamedFiles = true;
                        }

                        try
                        {
                            DeleteLibraryFile(path, renameRelatedFiles, result.TargetPath);
                        }
                        catch (IOException ex)
                        {
                            _logger.LogError(ex, "Error removing duplicate episode: {0}", path);
                        }
                        finally
                        {
                            _libraryMonitor.ReportFileSystemChangeComplete(path, true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status        = FileSortingStatus.Failure;
                result.StatusMessage = ex.Message;
                _logger.LogError(ex, "Caught a generic exception while organizing an episode");
                return(Task.CompletedTask);
            }
            finally
            {
                _organizationService.RemoveFromInprogressList(result);
            }

            if (rememberCorrection)
            {
                SaveSmartMatchString(originalExtractedSeriesString, series, cancellationToken);
            }

            return(Task.CompletedTask);
        }
Esempio n. 5
0
        private void OrganizeEpisode(string sourcePath,
                                     Series series,
                                     Episode episode,
                                     TvFileOrganizationOptions options,
                                     bool rememberCorrection,
                                     FileOrganizationResult result,
                                     CancellationToken cancellationToken)
        {
            _logger.Info("Sorting file {0} into series {1}", sourcePath, series.Path);

            var originalExtractedSeriesString = result.ExtractedName;

            bool isNew = string.IsNullOrWhiteSpace(result.Id);

            if (isNew)
            {
                _organizationService.SaveResult(result, cancellationToken);
            }

            if (!_organizationService.AddToInProgressList(result, isNew))
            {
                var msg = string.Format("File {0} is currently processed otherwise. Please try again later.", sourcePath);
                _logger.Warn(msg + " Stopping organization");
                result.Status        = FileSortingStatus.Failure;
                result.StatusMessage = msg;
                return;
            }

            try
            {
                // Proceed to sort the file
                var newPath = episode.Path;

                if (string.IsNullOrEmpty(newPath))
                {
                    var msg = string.Format("Unable to sort {0} because target path could not be determined.", sourcePath);
                    _logger.Info(msg + " Stopping organization");
                    result.Status        = FileSortingStatus.Failure;
                    result.StatusMessage = msg;
                    return;
                }

                _logger.Info("Sorting file {0} to new path {1}", sourcePath, newPath);
                result.TargetPath = newPath;

                var fileExists = _fileSystem.FileExists(result.TargetPath);
                if (options.SingleEpisodeVersion) //add value here to ensure returned to user regardless of result below
                {
                    result.DuplicatePaths = GetOtherDuplicatePaths(result.TargetPath, series, episode);
                    _logger.Info(string.Format("otherDuplicatePaths: '{0}'", string.Join("', '", result.DuplicatePaths)));
                }

                if (!options.OverwriteExistingEpisodes)
                {
                    if (options.CopyOriginalFile && fileExists && IsSameEpisode(sourcePath, newPath) && result.DuplicatePaths.Count == 1)
                    {
                        var msg = string.Format("File '{0}' already copied to new path '{1}.'", sourcePath, newPath);
                        _logger.Info(msg + " Stopping organization");
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        return;
                    }

                    if (result.DuplicatePaths.Count > 0)
                    {
                        var msg = string.Format("File '{0}' already exists as: '{1}'.", sourcePath, string.Join("', '", result.DuplicatePaths), (result.DuplicatePaths.Count > 1 ? "these" : ""));
                        _logger.Info(msg + " Stopping organization");
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        return;
                    }

                    if (fileExists)
                    {
                        var msg = string.Format("File '{0}' already exists as '{1}'.", sourcePath, newPath);
                        _logger.Info(msg + " Stopping organization");
                        result.Status        = FileSortingStatus.SkippedExisting;
                        result.StatusMessage = msg;
                        result.TargetPath    = newPath;
                        return;
                    }
                }

                PerformFileSorting(options, result);

                if (options.SingleEpisodeVersion)
                {
                    var hasRenamedFiles = false;

                    foreach (var path in result.DuplicatePaths)
                    {
                        if (!string.Equals(path, newPath, StringComparison.OrdinalIgnoreCase))//dont remove file matching destination path
                        {
                            _logger.Info("Removing duplicate episode {0}", path);

                            _libraryMonitor.ReportFileSystemChangeBeginning(path);

                            var renameRelatedFiles = !hasRenamedFiles &&
                                                     string.Equals(_fileSystem.GetDirectoryName(path), _fileSystem.GetDirectoryName(result.TargetPath), StringComparison.OrdinalIgnoreCase);

                            if (renameRelatedFiles)
                            {
                                hasRenamedFiles = true;
                            }

                            try
                            {
                                DeleteLibraryFile(path, renameRelatedFiles, result.TargetPath);
                            }
                            catch (IOException ex)
                            {
                                _logger.ErrorException("Error removing duplicate episode {0}", ex, path);
                            }
                            finally
                            {
                                _libraryMonitor.ReportFileSystemChangeComplete(path, true);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status        = FileSortingStatus.Failure;
                result.StatusMessage = string.Format("Error sorting episode: '{0}'.", ex.Message);
                _logger.ErrorException("Error sorting episode: {0}", ex, episode.Path);
                return;
            }
            finally
            {
                _organizationService.RemoveFromInprogressList(result);
            }

            if (rememberCorrection)
            {
                SaveSmartMatchString(originalExtractedSeriesString, series.Name, cancellationToken);
            }
        }