Exemple #1
0
        public async Task DownloadFile(string filename, Stream s)
        {
            using (DropboxClient client = new DropboxClient(_accessKey))
            {
                IDownloadResponse <FileMetadata> resp =
                    await client.Files.DownloadAsync(filename);

                Stream ds = await resp.GetContentAsStreamAsync();

                await ds.CopyToAsync(s);

                ds.Dispose();
            }
        }
Exemple #2
0
        public async Task <StorageFile> DownloadFileAsync(string remoteFullPath, CancellationToken cancellationToken)
        {
            if (!await IsAuthenticatedAsync().ConfigureAwait(false))
            {
                return(null);
            }

            try
            {
                StorageFile result = await CoreHelper.RetryAsync(async() =>
                {
                    using (await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false))
                        using (IDownloadResponse <FileMetadata> downloadResponse = await _dropboxClient.Files.DownloadAsync("/" + remoteFullPath).ConfigureAwait(false))
                            using (Stream remoteFileContentStream = await downloadResponse.GetContentAsStreamAsync().ConfigureAwait(false))
                            {
                                string fileName         = Path.GetFileName(remoteFullPath);
                                var localUserDataFolder = await CoreHelper.GetOrCreateUserDataStorageFolderAsync().ConfigureAwait(false);
                                StorageFile localFile   = await localUserDataFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                                using (IRandomAccessStream localFileContent = await localFile.OpenAsync(FileAccessMode.ReadWrite))
                                    using (Stream localFileContentStream = localFileContent.AsStreamForWrite())
                                    {
                                        await remoteFileContentStream.CopyToAsync(localFileContentStream, bufferSize: TransferBufferSize, cancellationToken).ConfigureAwait(false);
                                        await localFileContentStream.FlushAsync(cancellationToken).ConfigureAwait(false);
                                        await localFileContent.FlushAsync();
                                    }

                                return(localFile);
                            }
                }).ConfigureAwait(false);

                _logger.LogEvent(DownloadFileEvent, $"File downloaded.");

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogFault(DownloadFileFaultEvent, "Unable to download a file.", ex);
                return(null);
            }
        }
Exemple #3
0
            protected override async Task PerformIO()
            {
                if (!FileExists(_client, ODFileUtils.CombinePaths(Folder, FileName, '/')))
                {
                    throw new Exception("File not found.");
                }
                IDownloadResponse <FileMetadata> response = await _client.Files.DownloadAsync(ODFileUtils.CombinePaths(Folder, FileName, '/'));

                DownloadFileSize = response.Response.Size;
                ulong numChunks = DownloadFileSize / MAX_FILE_SIZE_BYTES + 1;
                int   chunkSize = (int)DownloadFileSize / (int)numChunks;

                byte[] buffer      = new byte[chunkSize];
                byte[] finalBuffer = new byte[DownloadFileSize];
                int    index       = 0;

                using (Stream stream = await response.GetContentAsStreamAsync()) {
                    int length = 0;
                    do
                    {
                        if (DoCancel)
                        {
                            throw new Exception("Operation cancelled by user");
                        }
                        length = stream.Read(buffer, 0, chunkSize);
                        //Convert each chunk to a MemoryStream. This plays nicely with garbage collection.
                        using (MemoryStream memstream = new MemoryStream()) {
                            memstream.Write(buffer, 0, length);
                            Array.Copy(memstream.ToArray(), 0, finalBuffer, index, length);
                            index += length;
                            OnProgress((double)index / (double)1024 / (double)1024, "?currentVal MB of ?maxVal MB downloaded", (double)DownloadFileSize / (double)1024 / (double)1024, "");
                        }
                    } while(length > 0);
                }
                FileContent = finalBuffer;
            }
Exemple #4
0
        /// <summary>
        /// Reads content of the underlying file as a byte array.
        /// </summary>
        /// <returns>Content of the underlying file as a byte array.</returns>
        /// <exception cref="AccessDeniedException"></exception>
        /// <exception cref="DirectoryNotFoundException"></exception>
        /// <exception cref="FileNotFoundException"></exception>
        /// <exception cref="PathTooLongException"></exception>
        /// <exception cref="FileStorageException"></exception>
        public async Task <Stream> ReadStreamAsync()
        {
            try
            {
                using (DropboxClient dropboxClient = new DropboxClient(this.accessToken))
                    using (IDownloadResponse <FileMetadata> response = await dropboxClient.Files.DownloadAsync(this.filepath))
                        return(await response.GetContentAsStreamAsync());
            }

            catch (ApiException <DownloadError> e)
            {
                if (e.ErrorResponse.IsPath)
                {
                    throw new DirectoryNotFoundException($"Directory not found: \"{this.filepath}\". See inner exception for details.", e);
                }

                throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
            }

            catch (Exception e)
            {
                throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
            }
        }