Esempio n. 1
0
        public async Task UpdateContentAsync(Stream contents, string contentType = null, IProgress progress = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            var fullPath = StorageRoot.GetFullPath(LocalPath);

            try
            {
                if (null != progress)
                {
                    progress.Total = contents.Length;
                }
                using (var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.None, StorageRoot.CopyBufferSize))
                {
                    await contents.CopyToAsync(fileStream, StorageRoot.CopyBufferSize, progress, cancellationToken).ConfigureAwait(false);

                    await fileStream.FlushAsync(cancellationToken).ConfigureAwait(false);
                }
                Logger.LogDebug("Successfully updated file \"{0}\".", fullPath);
            }
            catch (Exception exn)
            {
                Logger.LogError(exn, "Failed to update file \"{0}\".", fullPath);
                throw;
            }
            MediaType = contentType ?? await StorageRoot.GetMediaTypeAsync(LocalPath, CancellationToken.None).ConfigureAwait(false);
        }