Exemplo n.º 1
0
        /// <summary>
        /// upload blob; will continue from where it left off if a previous upload was already in progress
        /// </summary>
        /// <param name="uploadUrl">blob upload url</param>
        /// <param name="blobStream">blob stream to be uploaded; must allow Length, ReadAsync operations. Seek operation must be available for resumed uploads.</param>
        /// <param name="state"></param>
        /// <param name="cancellationToken">cancellation token to stop the asynchronous action</param>
        /// <returns>Returns true if upload is complete; false otherwise</returns>
        public async Task <bool> Upload(Uri uploadUrl, Stream blobStream, object state, CancellationToken cancellationToken = default)
        {
            var headResult = await _tusCore.Head(uploadUrl, cancellationToken);

            long offset = long.Parse(headResult["Upload-Offset"]);
            long length = blobStream.Length;

            var tusUploadFileContext = new TusUploadContext(length, offset, uploadUrl, state);

            while (!cancellationToken.IsCancellationRequested)
            {
                if (offset == length)
                {
                    UploadFinish?.Invoke(this, tusUploadFileContext);
                    return(true);
                }

                if (blobStream.Position != offset)
                {
                    blobStream.Seek(offset, SeekOrigin.Begin);
                }

                int chunkSize = _tusClientOptions.GetChunkUploadSize(this, tusUploadFileContext);
                chunkSize = (int)Math.Min(chunkSize, length - offset);
                byte[] buffer    = new byte[chunkSize];
                var    readCount = await blobStream.ReadAsync(buffer, 0, chunkSize);

                var uploadResult = await _tusCore.Patch(uploadUrl, buffer, offset, cancellationToken);

                offset = long.Parse(uploadResult["Upload-Offset"]);
                tusUploadFileContext.UploadedSize = offset;
                UploadProgress?.Invoke(this, tusUploadFileContext);
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// upload file
        /// </summary>
        /// <param name="url"></param>
        /// <param name="uploadFileInfo"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public async Task <bool> Upload(Uri url, FileInfo uploadFileInfo, CancellationToken ct = default(CancellationToken))
        {
            var headResult = await _tusCore.Head(url, ct);

            long offset = long.Parse(headResult["Upload-Offset"]);

            var tusUploadFileContext = new TusUploadContext(totalSize: uploadFileInfo.Length,
                                                            uploadedSize: offset, uploadFileInfo: uploadFileInfo, uploadFileUrl: url);

            using (var fileStream = new FileStream(uploadFileInfo.FullName, FileMode.Open, FileAccess.Read))
            {
                while (!ct.IsCancellationRequested)
                {
                    if (offset == uploadFileInfo.Length)
                    {
                        UploadFinish?.Invoke(tusUploadFileContext);
                        break;
                    }

                    //get buffer of file
                    fileStream.Seek(offset, SeekOrigin.Begin);

                    int uploadSize = GetUploadSize(tusUploadFileContext);

                    byte[] buffer    = new byte[uploadSize];
                    var    readCount = await fileStream.ReadAsync(buffer, 0, uploadSize);

                    if (readCount < uploadSize)
                    {
                        Array.Resize(ref buffer, readCount);
                    }

                    var uploadResult = await _tusCore.Patch(url, buffer, offset, ct);

                    offset = long.Parse(uploadResult["Upload-Offset"]);
                    tusUploadFileContext.UploadedSize = offset;
                    Uploading?.Invoke(tusUploadFileContext);
                }
            }

            return(true);
        }