Пример #1
0
        public async Task WriteAsync(FilePartyWriteRequest request, CancellationToken cancellationToken = default)
        {
            if (await ExistsAsync(request.StoragePointer, cancellationToken) && request.WriteMode == WriteMode.Create)
            {
                throw Errors.FileAlreadyExistsException;
            }

            var transferRequest = new TransferUtilityUploadRequest
            {
                BucketName  = GetBucketInfo().Name,
                InputStream = request.Stream,
                Key         = request.StoragePointer
            };

            if (WriteProgressEvent != null)
            {
                transferRequest.UploadProgressEvent += (_, args) =>
                {
                    WriteProgressEvent.Invoke(this, new WriteProgressEventArgs(request.Id, request.StoragePointer, args.TransferredBytes, args.TotalBytes));
                };
            }

            var creds = GetAmazonCredentials();

            using (var s3Client = new AmazonS3Client(creds, GetBucketInfo().GetRegionEndpoint()))
                using (var transferUtility = new TransferUtility(s3Client))
                {
                    await transferUtility.UploadAsync(transferRequest, cancellationToken);
                }
        }
Пример #2
0
 public override void Write(FilePartyWriteRequest request)
 {
     for (var i = 1; i <= 10; i++)
     {
         WriteProgressEvent?.Invoke(this, new WriteProgressEventArgs(request.Id, request.StoragePointer, 10 * i, 100));
         Thread.Sleep(i * 10);
     }
 }
Пример #3
0
        public override Task WriteAsync(FilePartyWriteRequest request, CancellationToken cancellationToken)
        {
            for (var i = 1; i <= 10; i++)
            {
                WriteProgressEvent?.Invoke(this, new WriteProgressEventArgs(request.Id, request.StoragePointer, 10 * i, 100));
                Thread.Sleep(i * 10);
            }

            return(Task.CompletedTask);
        }
        public async Task WriteAsync(FilePartyWriteRequest request, CancellationToken cancellationToken = default)
        {
            var client          = GetClient(request.StoragePointer);
            var progressHandler = new Progress <long>();
            var totalFileBytes  = request.Stream.Length;

            if (request.WriteMode != WriteMode.CreateOrReplace)
            {
                var exists = await client.ExistsAsync(cancellationToken);

                if (request.WriteMode == WriteMode.Create && exists)
                {
                    throw Core.Exceptions.Errors.FileAlreadyExistsException;
                }

                if (request.WriteMode == WriteMode.Replace && !exists)
                {
                    throw Core.Exceptions.Errors.FileNotFoundException;
                }
            }


            progressHandler.ProgressChanged += (_, totalBytesTransferred) =>
            {
                WriteProgressEvent?.Invoke(
                    this,
                    new WriteProgressEventArgs(
                        request.Id,
                        request.StoragePointer,
                        totalBytesTransferred,
                        totalFileBytes));
            };

            await client.UploadAsync(
                request.Stream,
                progressHandler : progressHandler,
                cancellationToken : cancellationToken);
        }
Пример #5
0
        public async Task WriteAsync(FilePartyWriteRequest request, CancellationToken cancellationToken = default)
        {
            if (await ExistsAsync(request.StoragePointer, cancellationToken) && request.WriteMode == WriteMode.Create)
            {
                throw Errors.FileAlreadyExistsException;
            }

            using (var client = GetClient())
            {
                if (client == null)
                {
                    throw Errors.UnknownException;
                }
                await client.ConnectAsync(cancellationToken);

                var reply = await client.UploadAsync(
                    request.Stream,
                    GetFullPath(request.StoragePointer),
                    FtpRemoteExists.Overwrite,
                    true,
                    new Progress <FtpProgress>(a =>
                                               WriteProgressEvent?.Invoke(
                                                   this,
                                                   new WriteProgressEventArgs(
                                                       request.Id,
                                                       request.StoragePointer,
                                                       a.TransferredBytes,
                                                       request.Stream.Length)))
                    , cancellationToken);

                if (reply != FtpStatus.Success)
                {
                    throw new StorageException("FTP-002", "FTP Server did not indicate success");
                }
            }
        }
Пример #6
0
        public async Task WriteAsync(FilePartyWriteRequest request, CancellationToken cancellationToken = default)
        {
            var storagePointer = request.StoragePointer;
            var writeMode      = request.WriteMode;

            ApplyBaseDirectoryToStoragePointer(ref storagePointer);

            if (request.Stream is null)
            {
                throw new ArgumentNullException(nameof(request), "Request Stream may not be null");
            }

            var exists = await ExistsAsync(storagePointer, cancellationToken);

            switch (exists)
            {
            case true when writeMode == WriteMode.Create:
                throw Errors.FileAlreadyExistsException;

            case false when writeMode == WriteMode.Replace:
                throw Errors.FileNotFoundException;
            }

            var directoryPath = Path.GetDirectoryName(storagePointer);

            if (!string.IsNullOrWhiteSpace(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            using (var writeStream = File.Create(storagePointer))
            {
                if (request.Stream.CanSeek)
                {
                    request.Stream.Seek(0, SeekOrigin.Begin);
                }

                if (writeStream.CanSeek)
                {
                    writeStream.Seek(0, SeekOrigin.End);
                }

                var  buffer            = new byte[BufferSize];
                long totalBytesWritten = 0;
                var  streamSize        = request.Stream.Length;
                int  bytesRead;

                do
                {
                    bytesRead = await request.Stream.ReadAsync(buffer, 0, BufferSize, cancellationToken);

                    await writeStream.WriteAsync(buffer, 0, bytesRead, cancellationToken);

                    await writeStream.FlushAsync(cancellationToken);

                    totalBytesWritten += bytesRead;

                    WriteProgressEvent?.Invoke(this, new WriteProgressEventArgs(request.Id, storagePointer, totalBytesWritten, streamSize));
                } while (bytesRead > 0 && totalBytesWritten < streamSize);
            }
        }