Пример #1
0
    protected async Task ProcessDownloadedFilesAsync()
    {
        // Back up existing file
        foreach (var item in InputSources)
        {
            ThrowIfCancellationRequested();

            // Get the absolute output path
            FileSystemPath file = OutputDirectory + Path.GetFileName(item.AbsolutePath);

            // Check if it's a file conflict
            if (!file.FileExists)
            {
                continue;
            }

            // Move the file to temp
            await Task.Run(() => FileManager.MoveFile(file, LocalTempDir.TempPath + file.Name, false));
        }

        // Move downloaded files to output
        foreach (var item in InputSources)
        {
            ThrowIfCancellationRequested();

            // Get the absolute path
            var file = ServerTempDir.TempPath + Path.GetFileName(item.AbsolutePath);

            var outputPath = OutputDirectory + Path.GetFileName(item.AbsolutePath);

            // Move the downloaded file from temp to the output
            FileManager.MoveFile(file, outputPath, false);

            // Flag the file as processed
            ProcessedPaths.Add(outputPath);
        }
    }
Пример #2
0
    public async Task StartAsync()
    {
        if (OperationRunning)
        {
            throw new InvalidOperationException("The downloader can not start while running");
        }

        CurrentDownloadState = DownloadState.Running;

        // Flag the operation as running
        OperationRunning = true;

        // Reset the progress counter
        CurrentStep = 0;

        using (LocalTempDir = new TempDirectory(true))
        {
            using (ServerTempDir = new TempDirectory(true))
            {
                try
                {
                    // Download files to temp
                    await DownloadFromServerToTempAsync();

                    // Create directory if it doesn't exist
                    if (!OutputDirectory.DirectoryExists)
                    {
                        Directory.CreateDirectory(OutputDirectory);
                        ProcessedPaths.Add(OutputDirectory);
                    }

                    // Process the download
                    if (IsCompressed)
                    {
                        await ProcessCompressedDownloadAsync();
                    }
                    else
                    {
                        await ProcessDownloadedFilesAsync();
                    }
                }
                catch (Exception ex)
                {
                    if (CancellationRequested)
                    {
                        Logger.Debug(ex, "Downloading files");
                        await Services.MessageUI.DisplayMessageAsync(Resources.Download_Canceled, Resources.Download_CanceledHeader, MessageType.Information);
                    }
                    else
                    {
                        Logger.Error(ex, "Downloading files");
                        await Services.MessageUI.DisplayMessageAsync(Resources.Download_Failed, Resources.Download_FailedHeader, MessageType.Error);
                    }

                    // Restore the stopped download
                    await RestoreStoppedDownloadAsync();

                    // Flag that the operation is no longer running
                    OperationRunning     = false;
                    CurrentDownloadState = CancellationRequested ? DownloadState.Canceled : DownloadState.Failed;
                    OnDownloadComplete();
                    return;
                }
            }
        }

        // Max out the progress
        ItemCurrentProgress  = ItemMaxProgress;
        TotalCurrentProgress = TotalMaxProgress;
        OnStatusUpdated(new Progress(TotalCurrentProgress, TotalMaxProgress));

        CurrentDownloadState = DownloadState.Succeeded;

        Logger.Info("The download operation has completed");

        await Services.MessageUI.DisplayMessageAsync(Resources.Download_Success, Resources.Download_SuccessHeader, MessageType.Success);

        // Flag the operation as complete
        OperationRunning = false;

        OnDownloadComplete();
    }
Пример #3
0
    protected async Task ProcessCompressedDownloadAsync()
    {
        await Task.Run(async() =>
        {
            // Handle each input source
            foreach (var inputSource in InputSources)
            {
                // Reset file progress
                ItemCurrentProgress = 0;

                // Get the absolute output path
                FileSystemPath file = ServerTempDir.TempPath + Path.GetFileName(inputSource.AbsolutePath);

                DisplayInputSource = file;

                // Open the zip file
                using (var zip = ZipFile.OpenRead(file))
                {
                    // Set file progress to its entry count
                    ItemMaxProgress = zip.Entries.Count;

                    // Extract each entry
                    foreach (var entry in zip.Entries)
                    {
                        ThrowIfCancellationRequested();

                        // Get the full entry name
                        var entryName = entry.FullName.Replace('/', '\\');

                        // Get the absolute output path
                        var outputPath = OutputDirectory + entryName;

                        DisplayOutputSource = outputPath;

                        // Check if the entry is a directory
                        if (entryName.EndsWith("\\") && entry.Name == String.Empty)
                        {
                            // Create directory if it doesn't exist
                            if (!outputPath.DirectoryExists)
                            {
                                Directory.CreateDirectory(outputPath);

                                ProcessedPaths.Add(outputPath);
                            }

                            continue;
                        }

                        // Backup conflict file
                        if (outputPath.FileExists)
                        {
                            await Task.Run(() => FileManager.MoveFile(outputPath, LocalTempDir.TempPath + entryName, false));
                        }

                        // Create directory if it doesn't exist
                        if (!outputPath.Parent.DirectoryExists)
                        {
                            Directory.CreateDirectory(outputPath.Parent);
                        }

                        // Extract the compressed file
                        entry.ExtractToFile(outputPath);

                        // Flag the file as processed
                        ProcessedPaths.Add(outputPath);

                        // Increase file progress
                        ItemCurrentProgress++;

                        // Set total progress
                        TotalCurrentProgress = (int)Math.Floor(100 * InputSources.Count + ((ItemCurrentProgress / ItemMaxProgress) * 10));
                        OnStatusUpdated(new Progress(TotalCurrentProgress, TotalMaxProgress));
                    }
                }

                // Delete the zip file
                FileManager.DeleteFile(file);
            }

            DisplayInputSource  = null;
            DisplayOutputSource = null;
        });
    }