Fixed: Update path before importing to ensure it hasn't changed

Closes #684
This commit is contained in:
Mark McDowall 2020-12-30 14:01:01 -08:00 committed by ta264
parent b3c217d713
commit 16fcba02ba
3 changed files with 41 additions and 16 deletions

View file

@ -38,7 +38,6 @@ public void Setup()
_trackedDownload = Builder<TrackedDownload>.CreateNew()
.With(c => c.State = TrackedDownloadState.Downloading)
.With(c => c.ImportItem = completed)
.With(c => c.DownloadItem = completed)
.With(c => c.RemoteBook = remoteBook)
.Build();
@ -65,6 +64,10 @@ public void Setup()
Mocker.GetMock<IHistoryService>()
.Setup(s => s.FindByDownloadId(It.IsAny<string>()))
.Returns(new List<EntityHistory>());
Mocker.GetMock<IProvideImportItemService>()
.Setup(s => s.ProvideImportItem(It.IsAny<DownloadClientItem>(), It.IsAny<DownloadClientItem>()))
.Returns<DownloadClientItem, DownloadClientItem>((i, p) => i);
}
private Book CreateBook(int id)

View file

@ -27,21 +27,21 @@ public class CompletedDownloadService : ICompletedDownloadService
{
private readonly IEventAggregator _eventAggregator;
private readonly IHistoryService _historyService;
private readonly IProvideImportItemService _provideImportItemService;
private readonly IDownloadedBooksImportService _downloadedTracksImportService;
private readonly IProvideImportItemService _importItemService;
private readonly ITrackedDownloadAlreadyImported _trackedDownloadAlreadyImported;
private readonly Logger _logger;
public CompletedDownloadService(IEventAggregator eventAggregator,
IHistoryService historyService,
IProvideImportItemService importItemService,
IProvideImportItemService provideImportItemService,
IDownloadedBooksImportService downloadedTracksImportService,
ITrackedDownloadAlreadyImported trackedDownloadAlreadyImported,
Logger logger)
{
_eventAggregator = eventAggregator;
_historyService = historyService;
_importItemService = importItemService;
_provideImportItemService = provideImportItemService;
_downloadedTracksImportService = downloadedTracksImportService;
_trackedDownloadAlreadyImported = trackedDownloadAlreadyImported;
_logger = logger;
@ -54,7 +54,7 @@ public void Check(TrackedDownload trackedDownload)
return;
}
trackedDownload.ImportItem = _importItemService.ProvideImportItem(trackedDownload.DownloadItem, trackedDownload.ImportItem);
SetImportItem(trackedDownload);
// Only process tracked downloads that are still downloading
if (trackedDownload.State != TrackedDownloadState.Downloading)
@ -70,18 +70,8 @@ public void Check(TrackedDownload trackedDownload)
return;
}
var downloadItemOutputPath = trackedDownload.ImportItem.OutputPath;
if (downloadItemOutputPath.IsEmpty)
if (!ValidatePath(trackedDownload))
{
trackedDownload.Warn("Download doesn't contain intermediate path, Skipping.");
return;
}
if ((OsInfo.IsWindows && !downloadItemOutputPath.IsWindowsPath) ||
(OsInfo.IsNotWindows && !downloadItemOutputPath.IsUnixPath))
{
trackedDownload.Warn("[{0}] is not a valid local path. You may need a Remote Path Mapping.", downloadItemOutputPath);
return;
}
@ -90,6 +80,13 @@ public void Check(TrackedDownload trackedDownload)
public void Import(TrackedDownload trackedDownload)
{
SetImportItem(trackedDownload);
if (!ValidatePath(trackedDownload))
{
return;
}
trackedDownload.State = TrackedDownloadState.Importing;
var outputPath = trackedDownload.ImportItem.OutputPath.FullPath;
@ -190,5 +187,30 @@ public bool VerifyImport(TrackedDownload trackedDownload, List<ImportResult> imp
_logger.Debug("Not all books have been imported for {0}", trackedDownload.DownloadItem.Title);
return false;
}
private void SetImportItem(TrackedDownload trackedDownload)
{
trackedDownload.ImportItem = _provideImportItemService.ProvideImportItem(trackedDownload.DownloadItem, trackedDownload.ImportItem);
}
private bool ValidatePath(TrackedDownload trackedDownload)
{
var downloadItemOutputPath = trackedDownload.ImportItem.OutputPath;
if (downloadItemOutputPath.IsEmpty)
{
trackedDownload.Warn("Download doesn't contain intermediate path, Skipping.");
return false;
}
if ((OsInfo.IsWindows && !downloadItemOutputPath.IsWindowsPath) ||
(OsInfo.IsNotWindows && !downloadItemOutputPath.IsUnixPath))
{
trackedDownload.Warn("[{0}] is not a valid local path. You may need a Remote Path Mapping.", downloadItemOutputPath);
return false;
}
return true;
}
}
}