public static async Task <ObjectModel> UploadAsync(
            IChunkedObjectUploader uploader, string dataPath, CancellationToken ct)
        {
            string uid      = Guid.NewGuid().ToString();
            string filename = Path.GetFileName(dataPath);

            using (FileStream stream = new FileStream(dataPath, FileMode.Open))
            {
                long chunkLength = 1048576;
                if (stream.Length < chunkLength)
                {
                    chunkLength = stream.Length;
                }
                long totalChunks = stream.Length / chunkLength;

                byte[] chunk       = new byte[chunkLength];
                int    chunkNumber = 1;
                int    maximumNumberOfBytesToRead = chunk.Length;
                while (stream.Read(chunk, 0, maximumNumberOfBytesToRead) > 0)
                {
                    using (MemoryStream chunkStream = new MemoryStream(chunk))
                    {
                        await uploader.UploadAsync(Convert.ToInt32(totalChunks), chunkNumber, uid, chunkStream, ct);
                    }

                    long numberOfBytesToReadLeft = stream.Length - stream.Position;
                    if (numberOfBytesToReadLeft < maximumNumberOfBytesToRead)
                    {
                        maximumNumberOfBytesToRead = (int)numberOfBytesToReadLeft;
                        chunk = new byte[maximumNumberOfBytesToRead];
                    }
                    chunkNumber++;
                }
            }
            return(await uploader.CommitAsync(filename, filename, uid, ct));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectsController"/> class.
 /// </summary>
 /// <param name="uploader">The resumable uploader.</param>
 public ObjectsController(IChunkedObjectUploader uploader)
 {
     this.uploader = uploader ?? throw new ArgumentNullException(nameof(uploader));
 }
示例#3
0
 public ChunkedObjectUploaderTests()
 {
     uploader = GetService <IChunkedObjectUploader>();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ObjectsController"/> class.
 /// </summary>
 /// <param name="uploader">The resumable uploader.</param>
 public ObjectsController(IChunkedObjectUploader uploader)
 {
     this.uploader = uploader;
 }