Exemplo n.º 1
0
        public async Task <T> UploadLargeAsync <T>(BasicRawUploadParams parameters, int bufferSize = 20971520) where T : UploadResult, new()
        {
            var apiUrlImgUpV = Api.ApiUrlImgUpV;

            apiUrlImgUpV.ResourceType(Enum.GetName(typeof(ResourceType), parameters.ResourceType).ToLower());
            var url = apiUrlImgUpV.BuildUrl();

            ResetInternalFileDescription(parameters.File, bufferSize);
            var extraHeaders = new Dictionary <string, string>();

            extraHeaders["X-Unique-Upload-Id"] = RandomPublicId();
            parameters.File.BufferLength       = bufferSize;
            var fileLength = parameters.File.GetFileLength();
            var obj        = default(T);

            while (!parameters.File.EOF)
            {
                var num = Math.Min(bufferSize, fileLength - parameters.File.BytesSent);
                var paramsDictionary = parameters.ToParamsDictionary();
                var str = string.Format("bytes {0}-{1}/{2}", parameters.File.BytesSent, parameters.File.BytesSent + num - 1L, fileLength);
                extraHeaders["Content-Range"] = str;
                using (var response = await Api.CallAsync(HttpMethod.Post, url, paramsDictionary, parameters.File, extraHeaders))
                {
                    obj = await BaseResult.Parse <T>(response);

                    if (obj.StatusCode != HttpStatusCode.OK)
                    {
                        throw new CloudinaryException($"An error has occured while uploading file (status code: {obj.StatusCode}). {obj.Error?.Message ?? "Unkown error"}");
                    }
                }
            }
            return(obj);
        }