Esempio n. 1
0
        private async Task DownloadPartial(string id, TransmissionHelper transmissionHelper, string remoteFilePath, long startBytes, long endBytes, long step)
        {
            var headers = new Dictionary <string, string> {
                { "translate", "f" }, { "Range", "bytes=" + startBytes + "-" + endBytes }
            };

            if (CustomHeaders != null)
            {
                foreach (var keyValuePair in CustomHeaders)
                {
                    headers.Add(keyValuePair.Key, keyValuePair.Value);
                }
            }

            using (FileStream file = new FileStream($"{id}.dat", FileMode.Create))
                using (var stream = await DownloadFile(remoteFilePath, headers))
                {
                    var length   = endBytes - startBytes + 1;
                    var consumed = 0;
                    var buffer   = new byte[step];
                    while (consumed < length)
                    {
                        var read = await stream.ReadAsync(buffer, 0, buffer.Length);

                        await file.WriteAsync(buffer, 0, read);

                        consumed += read;
                        transmissionHelper.IncreaseRealTimeLength(read);
                    }
                }
        }
Esempio n. 2
0
        private async Task UploadPartial(TransmissionHelper transmissionHelper, Stream content, string remoteFileName, long startBytes, long endBytes, long totalLength)
        {
            Console.WriteLine($"startBytes:{startBytes}  endBytes:{endBytes}");

            // Should not have a trailing slash.
            var uploadUri = await GetServerUrl(remoteFileName, false).ConfigureAwait(false);

            HttpResponseMessage response = null;

            try
            {
                Console.WriteLine("current stream length:" + content.Length);
                response = await HttpUploadRequest(uploadUri.Uri, HttpMethod.Put, content, null, startBytes, endBytes, totalLength).ConfigureAwait(false);

                if (response.StatusCode != HttpStatusCode.OK &&
                    response.StatusCode != HttpStatusCode.NoContent &&
                    response.StatusCode != HttpStatusCode.Created)
                {
                    throw new WebDAVException((int)response.StatusCode, "Failed uploading file.");
                }

                if (response.IsSuccessStatusCode)
                {
                    transmissionHelper.IncreaseRealTimeLength(endBytes - startBytes + 1);
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }

                content.Close();
            }
        }