Exemplo n.º 1
0
        public async Task <OneOf <Success, Exception> > WriteAsync(StorageFile file, CancellationToken cancellationToken = default)
        {
            var path = GetPath(file.Name);

            var data = MessagePackSerializer.Serialize(new FileContainer
            {
                Data      = await file.Stream.ToArrayAsync(cancellationToken),
                MediaType = file.MediaType
            }, _serializerOptions);

            try
            {
                using (var temp = new TemporaryFile())
                {
                    // write to temporary file first
                    await using (var stream = temp.OpenAsync())
                    {
                        await stream.WriteAsync(data, cancellationToken);

                        await stream.FlushAsync(cancellationToken);
                    }

                    // move temp file to storage
                    await MoveFileAsync(temp.Path, path, cancellationToken);
                }

                _logger.LogInformation($"Wrote file '{file.Name}'.");

                return(new Success());
            }
            catch (Exception e)
            {
                _logger.LogWarning(e, $"Failed to write file '{file.Name}'.");
                return(e);
            }
        }