Repository navigation
feat(media): extract archive downloads on import and handle missing root folder #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
465e9d1
dc2c402
b06201f
cf85836
22f879d
733ced7
fd8014a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using NLog; | ||
| using NzbDrone.Common; | ||
| using NzbDrone.Common.Disk; | ||
| using NzbDrone.Common.Extensions; | ||
| using NzbDrone.Common.EnvironmentInfo; | ||
|
|
@@ -54,6 +55,7 @@ public partial class DownloadedBooksImportService : IDownloadedBooksImportServic | |
| private readonly IEventAggregator _eventAggregator; | ||
| private readonly IRuntimeInfo _runtimeInfo; | ||
| private readonly IMediaInfoExtractor _mediaInfoExtractor; | ||
| private readonly IArchiveService _archiveService; | ||
| private readonly Logger _logger; | ||
| private readonly NzbDrone.Core.Queue.IQueueService _queueService; | ||
| private readonly IConversionJobService _conversionJobService; | ||
|
|
@@ -77,7 +79,8 @@ public DownloadedBooksImportService( | |
| IMediaInfoExtractor mediaInfoExtractor, | ||
| Logger logger, | ||
| NzbDrone.Core.Queue.IQueueService queueService, | ||
| IConversionJobService conversionJobService) | ||
| IConversionJobService conversionJobService, | ||
| IArchiveService archiveService) | ||
| { | ||
| _diskProvider = diskProvider; | ||
| _diskScanService = diskScanService; | ||
|
|
@@ -98,6 +101,7 @@ public DownloadedBooksImportService( | |
| _logger = logger; | ||
| _queueService = queueService ?? throw new ArgumentNullException(nameof(queueService)); | ||
| _conversionJobService = conversionJobService ?? throw new ArgumentNullException(nameof(conversionJobService)); | ||
| _archiveService = archiveService; | ||
| } | ||
|
|
||
| public List<ImportResult> ProcessRootFolder(IDirectoryInfo directoryInfo) | ||
|
|
@@ -194,6 +198,23 @@ private List<ImportResult> ProcessFolder(IDirectoryInfo directoryInfo, ImportMod | |
| (!targetMedia.HasValue || QualityMediaTypeHelper.GetMediaTypeFromPath(f.FullName) == targetMedia)) | ||
| .ToList(); | ||
|
|
||
| if (!mediaFiles.Any()) | ||
| { | ||
| var archives = fileListScoped | ||
| ? visibleFiles.Where(f => IsSupportedArchive(f.FullName)).Select(f => f.FullName).ToList() | ||
| : _diskProvider.GetFiles(directoryInfo.FullName, true).Where(IsSupportedArchive).ToList(); | ||
|
|
||
| if (archives.Any() && ExtractArchives(directoryInfo, archives)) | ||
| { | ||
| visibleFiles = _diskProvider.GetFileInfos(directoryInfo.FullName, true); | ||
| mediaFiles = visibleFiles | ||
| .Where(f => MediaFileExtensions.AllExtensions.Contains(f.Extension) && | ||
| !DiskScanService.IsExcludedFile(f.FullName) && | ||
| (!targetMedia.HasValue || QualityMediaTypeHelper.GetMediaTypeFromPath(f.FullName) == targetMedia)) | ||
| .ToList(); | ||
| } | ||
| } | ||
|
|
||
| if (!mediaFiles.Any()) | ||
| { | ||
| _logger.Debug("[DOWNLOAD-IMPORT] No media files found in: {0}", directoryInfo.FullName); | ||
|
|
@@ -1579,5 +1600,32 @@ public List<ImportResult> ProcessFile(string path, ImportMode importMode = Impor | |
| var fileInfo = _diskProvider.GetFileInfo(path); | ||
| return ProcessFile(fileInfo, importMode, author, downloadClientItem, remoteBook, requireDefaultRootFolderForMissingAuthors: requireDefaultRootFolderForMissingAuthors); | ||
| } | ||
|
|
||
| private bool ExtractArchives(IDirectoryInfo directoryInfo, IEnumerable<string> archives) | ||
| { | ||
| var extractedAny = false; | ||
|
|
||
| foreach (var archive in archives) | ||
| { | ||
| try | ||
| { | ||
| _archiveService.Extract(archive, directoryInfo.FullName); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scoped extraction can truncate an unrelated file in the shared download folder. For a scoped download, the archive is extracted into Extraction for a scoped item must not replace or truncate files that do not belong to that item. Choose the mechanism that enforces this. Options include extracting into an isolated staging folder and promoting only non-colliding files, or rejecting colliding destinations before any write. Restricting the later rescan does not prevent the overwrite.
|
||
| extractedAny = true; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| _logger.Warn(e, "Failed to extract archive during import: {0}", archive); | ||
| } | ||
|
Comment on lines
+1615
to
+1618
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove failed extraction outputs before retrying import.
This catch leaves Extract each archive into a staging directory. Publish its files only after extraction succeeds, and remove the staging directory on failure.
|
||
| } | ||
|
|
||
| return extractedAny; | ||
| } | ||
|
|
||
| private static bool IsSupportedArchive(string path) | ||
| { | ||
| return path.EndsWith(".zip", StringComparison.OrdinalIgnoreCase) || | ||
| path.EndsWith(".tgz", StringComparison.OrdinalIgnoreCase) || | ||
| path.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve download-item scope after extraction.
If
FilePathscontains onlyA.zipin a shared download folder, this scan also admits an unrelatedB.m4b. Line 234 sendsB.m4bto matching. Line 392 also receives unrelated extra files. Target-book restrictions cannot distinguish unrelated copies of the same book.Extract into an item-specific directory. Build
visibleFilesfrom the original scoped files and that directory's successful extraction outputs, not the entire download folder.