Example #1
0
        public async Task <OptionCollection> Options(Uri url, TusRequestOption option = default, CancellationToken ct = default)
        {
            var httpReqMsg = new HttpRequestMessage(HttpMethod.Options, url);

            ConfigHttpRequestMsg(option, httpReqMsg);

            if (_httpClient.DefaultRequestHeaders.Contains("Tus-Resumable"))
            {
                _httpClient.DefaultRequestHeaders.Remove("Tus-Resumable");
            }

            var response = await _httpClient.SendAsync(httpReqMsg, ct);

            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.NoContent)
            {
                throw new TusException($"Options response statusCode is {response.StatusCode}", httpReqMsg, response);
            }

            OptionCollection result = new OptionCollection();

            result["Tus-Version"]   = response.GetValueOfHeader("Tus-Version");
            result["Tus-Resumable"] = response.GetValueOfHeader("Tus-Resumable");

            if (response.Headers.Contains("Tus-Extension"))
            {
                result["Tus-Extension"] = response.GetValueOfHeader("Tus-Extension");
            }

            if (response.Headers.Contains("Tus-Max-Size"))
            {
                result["Tus-Max-Size"] = response.GetValueOfHeader("Tus-Max-Size");
            }

            return(result);
        }
Example #2
0
        public async Task <Uri> CreatePartialAsync(Uri host, long uploadLength, TusRequestOption option = default, CancellationToken ct = default)
        {
            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers["Upload-Concat"] = "partial";
            var fileUrl = await CreateFileAsync(host, uploadLength, string.Empty, headers, option, ct);

            return(fileUrl);
        }
Example #3
0
        public async Task <Uri> ConcatenateAsync(Uri host, string[] partialFiles, TusRequestOption option = default, CancellationToken ct = default)
        {
            Dictionary <string, string> headers = new Dictionary <string, string>();
            var filesStr = string.Join(" ", partialFiles);

            headers["Upload-Concat"] = $"final;{filesStr}";
            var finalFileUrl = await CreateFileAsync(host, -1, string.Empty, headers, option, ct);

            return(finalFileUrl);
        }
Example #4
0
 private static void ConfigHttpRequestMsg(TusRequestOption option, HttpRequestMessage httpReqMsg)
 {
     if (option is not null)
     {
         option.Validate();
         foreach (var kv in option.HttpHeader)
         {
             httpReqMsg.Headers.Add(kv.Key, kv.Value);
         }
     }
 }
Example #5
0
        static async Task Main(string[] args)
        {
            var location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            // file to be uploaded
            FileInfo fileInfo = new FileInfo(Path.Combine(location, @"TestFile/test.txt"));

            // remote tus service
            var hostUri = new Uri(@"http://localhost:6000/files");

            // build a standalone tus client instance
            var tusClient = TusBuild.DefaultTusClientBuild(hostUri)
                            .Configure((options, httpClientBuilder) =>
            {
                //customize http client
                httpClientBuilder.ConfigureHttpClient(httpClient =>
                {
                    httpClient.DefaultRequestHeaders.Authorization =
                        new AuthenticationHeaderValue("Bearer", "ACCESS_TOKEN");
                });

                /* httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
                 * {
                 *   UseCookies = false,
                 * });*/
            })
                            .Build();

            //hook up events
            tusClient.UploadProgress += printUploadProcess;
            tusClient.UploadFinish   += uploadFinish;

            //define additional file metadata
            MetadataCollection metadata = new MetadataCollection();

            metadata["filename"] = fileInfo.FullName;

            TusRequestOption requestOption = new TusRequestOption();

            requestOption.HttpHeader["hello"] = "hello";

            //create upload url
            var fileUrl = await tusClient.Create(fileInfo, null, requestOption);

            var uploadOpt = new TusRequestOption()
            {
                UploadWithStreaming = true //enable streaming Upload
            };

            //upload file
            var uploadResult = await tusClient.Upload(fileUrl, fileInfo, null, uploadOpt);
        }
Example #6
0
        public async Task <bool> Delete(Uri url, TusRequestOption option = default, CancellationToken ct = default)
        {
            var httpReqMsg = new HttpRequestMessage(HttpMethod.Delete, url);

            ConfigHttpRequestMsg(option, httpReqMsg);

            var response = await _httpClient.SendAsync(httpReqMsg, ct);

            if (response.StatusCode != HttpStatusCode.NoContent)
            {
                throw new TusException($"delete response statusCode is {response.StatusCode}", httpReqMsg, response);
            }
            return(true);
        }
Example #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <param name="uploadLength">if uploadLength less than 0, http does not include header of Upload-Length</param>
        /// <param name="uploadMetadata"></param>
        /// <param name="headers"></param>
        /// <param name="requestCancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="TusException"></exception>
        /// <exception cref="Exception"></exception>
        private async Task <Uri> CreateFileAsync(Uri url, long uploadLength, string uploadMetadata, IDictionary <string, string> headers,
                                                 TusRequestOption option = default, CancellationToken ct = default)
        {
            var httpReqMsg = new HttpRequestMessage(HttpMethod.Post, url);

            if (uploadLength >= 0)
            {
                httpReqMsg.Headers.Add("Upload-Length", uploadLength.ToString());
            }

            if (!string.IsNullOrEmpty(uploadMetadata))
            {
                httpReqMsg.Headers.Add("Upload-Metadata", uploadMetadata);
            }

            if (headers is not null && headers.Any())
            {
                foreach (var key in headers.Keys)
                {
                    httpReqMsg.Headers.Add(key, headers[key]);
                }
            }
            ConfigHttpRequestMsg(option, httpReqMsg);

            var response = await _httpClient.SendAsync(httpReqMsg, ct);

            if (response.StatusCode != HttpStatusCode.Created)
            {
                throw new TusException($"creation response statusCode is {response.StatusCode}", httpReqMsg, response);
            }

            string fileUrlStr = response.GetValueOfHeader("Location");
            Uri    fileUrl    = null;

            if (Uri.TryCreate(fileUrlStr, UriKind.RelativeOrAbsolute, out fileUrl))
            {
                if (fileUrlStr.StartsWith("https://") || fileUrlStr.StartsWith("http://"))
                {
                    return(fileUrl);
                }
                fileUrl = new Uri(url, fileUrl);
            }
            else
            {
                throw new Exception("Invalid location header");
            }

            return(fileUrl);
        }
        public async Task UploadFileWithStreamingAsync()
        {
            var tusClient          = this.BuildClient();
            var fileInfo           = new FileInfo(@"TestFile/bigFile");
            MetadataCollection dir = new MetadataCollection();

            var fileUrl = await tusClient.Create(fileInfo, dir);

            var uploadOption = new TusRequestOption()
            {
                UploadWithStreaming = true
            };
            var uploadResult = await tusClient.Upload(fileUrl, fileInfo, null, uploadOption);

            Assert.True(uploadResult);
        }
Example #9
0
        /// <summary>
        /// </summary>
        /// <param name="url"></param>
        /// <param name="requestCancellationToken"></param>
        /// <returns></returns>
        public async Task <Dictionary <string, string> > Head(Uri url, TusRequestOption option = default, CancellationToken ct = default)
        {
            var httpReqMsg = new HttpRequestMessage(HttpMethod.Head, url);

            ConfigHttpRequestMsg(option, httpReqMsg);
            var response = await _httpClient.SendAsync(httpReqMsg, ct);

            if (response.StatusCode == HttpStatusCode.NotFound ||
                response.StatusCode == HttpStatusCode.Gone ||
                response.StatusCode == HttpStatusCode.Forbidden)
            {
                throw new TusException($"response's statusCode is{response.StatusCode.ToString()} ");
            }

            Dictionary <string, string> result = new Dictionary <string, string>();

            result["Upload-Offset"] = response.GetValueOfHeader("Upload-Offset");
            result["Tus-Resumable"] = response.GetValueOfHeader("Tus-Resumable");

            return(result);
        }
Example #10
0
        public async Task <Dictionary <string, string> > Patch(Uri url, byte[] uploadData, long offset,
                                                               TusRequestOption option = default, CancellationToken ct = default)
        {
            var httpReqMsg = new HttpRequestMessage(new HttpMethod("PATCH"), url);

            httpReqMsg.Headers.Add("Upload-Offset", offset.ToString());
            ConfigHttpRequestMsg(option, httpReqMsg);

            httpReqMsg.Content = new ByteArrayContent(uploadData);
            httpReqMsg.Content.Headers.Add("Content-Type", "application/offset+octet-stream");

            var response = await _httpClient.SendAsync(httpReqMsg, ct);

            if (response.StatusCode != HttpStatusCode.NoContent)
            {
                throw new TusException($"patch response statusCode is {response.StatusCode.ToString()}");
            }
            Dictionary <string, string> result = new Dictionary <string, string>();

            result["Upload-Offset"] = response.GetValueOfHeader("Upload-Offset");
            result["Tus-Resumable"] = response.GetValueOfHeader("Tus-Resumable");

            return(result);
        }
Example #11
0
 public Task <Dictionary <string, string> > CreationWithUploadAsync(Uri url, long uploadLength, string uploadMetadata, byte[] uploadData,
                                                                    TusRequestOption option = default, CancellationToken ct = default)
 {
     throw new NotImplementedException();
 }
Example #12
0
 public async Task <Uri> Creation(Uri url, long uploadLength, string uploadMetadata, TusRequestOption option = default, CancellationToken ct = default)
 {
     return(await CreateFileAsync(url, uploadLength, uploadMetadata, null, option, ct));
 }