Пример #1
0
 private DocumentFile CreateFile(DownloadDetails details, DownloadDetails.RootDirectoryDetails root, bool useExistingFile)
 {
     lock (LockFile)
     {
         return(useExistingFile ? details.FindOrCreateFile(root) : details.CreateNewFile(root));
     }
 }
Пример #2
0
        public void OpenFile(DownloadDetails details)
        {
            CheckWhetherDownloadManagerIsConfigured();

            var task = new Task <DocumentFile>(() =>
            {
                if (StorageUtils.IsInAnyStorageDevices(_configuration.Context, details.DownloadDirectory, out string rootDirectory) == false)
                {
                    throw new System.Exception($"Could not locate the root directory for the download location, '{details.DownloadDirectory}'.");
                }

                var rootDirectoryDocument = _configuration.StoragePermissionsHandler.RequestStoragePermission(rootDirectory);
                var rootDetails           = new DownloadDetails.RootDirectoryDetails(rootDirectory, rootDirectoryDocument);

                return(details.FindFile(rootDetails));
            });

            task.ContinueWith(result =>
            {
                try
                {
                    var foundFile = result.Result;
                    if (foundFile == null)
                    {
                        throw new FileNotFoundException($"The file '{details.FileName}' could not be located. Perhaps it was moved or deleted.");
                    }

                    TriggerOpenFileIntent(_configuration.Context, foundFile.Uri, details.MimeType);
                }
                catch (ActivityNotFoundException ex) { OnOpenRequestError?.Invoke(this, ex); }
                catch (FileNotFoundException ex) { OnOpenRequestError?.Invoke(this, ex); }
                catch { /* IGNORED */ }
            });

            task.Start();
        }
Пример #3
0
        private void OnDownloadTaskStarted(Object objDetails)
        {
            if (!(objDetails is object[] objects) || objects.Length < 3)
            {
                DownloadManager.Instance.TriggerDownloadErrorOccurredEvent(null, new DownloadManager.Exception(DownloadManager.Exception.Type.InvalidConfiguration, "Invalid details provided."));
                return;
            }

            var configuration   = objects[0] as DownloadManagerConfiguration;
            var downloadDetails = objects[1] as DownloadDetails;
            var useExistingFile = (bool)objects[2];

            if (configuration == null || downloadDetails == null)
            {
                DownloadManager.Instance.TriggerDownloadErrorOccurredEvent(null, new DownloadManager.Exception(DownloadManager.Exception.Type.InvalidConfiguration, "Invalid details provided."));
                return;
            }

            try
            {
                if (StorageUtils.IsInAnyStorageDevices(configuration.Context, downloadDetails.DownloadDirectory, out string rootDirectory) == false)
                {
                    throw new DownloadManager.Exception(DownloadManager.Exception.Type.FileNotFound, $"Could not locate the root directory for the download location, '{downloadDetails.DownloadDirectory}'.");
                }

                var rootDirectoryDocument = RequestStoragePermission(configuration, rootDirectory);
                var rootDetails           = new DownloadDetails.RootDirectoryDetails(rootDirectory, rootDirectoryDocument);
                var downloadingFile       = CreateFile(downloadDetails, rootDetails, useExistingFile);
                var downloadedSize        = downloadingFile.Length();

                var stream = HttpUtilities.GetDownloadStream(downloadDetails.Url, downloadedSize, out var totalFileSize);

                downloadDetails.ContentUri = downloadingFile.Uri;
                downloadDetails.CurrentProgress.Update(downloadedSize, totalFileSize);

                DownloadManager.Instance.TriggerDownloadStartedEvent(downloadDetails, totalFileSize);

                if (StorageUtils.CheckSpaceAvailable(configuration.Context, rootDetails, totalFileSize - downloadedSize) == false)
                {
                    throw new DownloadManager.Exception(DownloadManager.Exception.Type.InsufficientSpace);
                }

                if (downloadedSize == totalFileSize)
                {
                    DownloadManager.Instance.TriggerDownloadCompletedEvent(downloadDetails);
                    return;
                }

                var fileStream         = DocumentFileStream.Create(configuration, downloadingFile);
                var reportingProgress  = 0;
                var reportingThreshold = 512 * 1024;

                while (stream.CanRead)
                {
                    byte[] data       = new byte[8192];
                    var    readLength = stream.Read(data, 0, data.Length);

                    if (readLength <= 0)
                    {
                        break;
                    }

                    fileStream.Write(data, readLength);
                    downloadedSize    += readLength;
                    reportingProgress += readLength;

                    downloadDetails.CurrentProgress.Update(downloadedSize, totalFileSize);

                    if (reportingProgress >= reportingThreshold)
                    {
                        reportingProgress = 0;
                        DownloadManager.Instance.TriggerDownloadProgressChangedEvent(downloadDetails, downloadedSize, totalFileSize);
                    }
                }

                try
                {
                    fileStream.Close();
                    stream.Close();
                }
                catch { /* IGNORED */ }

                downloadDetails.CurrentProgress.Update(downloadedSize, totalFileSize);
                DownloadManager.Instance.TriggerDownloadProgressChangedEvent(downloadDetails, downloadedSize, totalFileSize);

                if (downloadedSize == totalFileSize)
                {
                    DownloadManager.Instance.TriggerDownloadCompletedEvent(downloadDetails);
                }
                else
                {
                    DownloadManager.Instance.TriggerDownloadErrorOccurredEvent(downloadDetails, new DownloadManager.Exception(DownloadManager.Exception.Type.StreamEndedBeforeCompletion, "The data stream ended before the entire file was downloaded"));
                }
            }
            catch (ThreadAbortException)
            {
                DownloadManager.Instance.TriggerDownloadErrorOccurredEvent(downloadDetails, new DownloadManager.Exception(DownloadManager.Exception.Type.UserCancelled));
            }
            catch (ThreadInterruptedException)
            {
                DownloadManager.Instance.TriggerDownloadErrorOccurredEvent(downloadDetails, new DownloadManager.Exception(DownloadManager.Exception.Type.UserCancelled));
            }
            catch (Exception e)
            {
                DownloadManager.Instance.TriggerDownloadErrorOccurredEvent(downloadDetails, new DownloadManager.Exception(DownloadManager.Exception.Type.Unknown, e));
            }
        }