Ejemplo n.º 1
0
        public static HttpClient GetAuthorizedClient()
        {
            var handler = new HttpClientHandler {
                MaxConnectionsPerServer = MAX_CONNECTIONS
            };
            var client = new HttpClient(handler);

            client.DefaultRequestHeaders.Add("X-API-KEY", AuthorAPI.GetAPIKey());
            return(client);
        }
Ejemplo n.º 2
0
        public static Task <string> UploadFile(WorkQueue queue, string filename)
        {
            var tcs = new TaskCompletionSource <string>();

            queue.QueueTask(async() =>
            {
                using (var stream =
                           new StatusFileStream(File.OpenRead(filename), $"Uploading {Path.GetFileName(filename)}", queue))
                {
                    var client = new HttpClient();
                    client.DefaultRequestHeaders.Add("X-API-KEY", AuthorAPI.GetAPIKey());
                    var response = await client.PutAsync(UploadURL + $"/{Path.GetFileName(filename)}/start", new StringContent(""));
                    if (!response.IsSuccessStatusCode)
                    {
                        tcs.SetResult("FAILED");
                        return;
                    }

                    var key = await response.Content.ReadAsStringAsync();

                    var data = new byte[BLOCK_SIZE];
                    while (stream.Position < stream.Length)
                    {
                        var old_offset = stream.Position;

                        var new_size = Math.Min(stream.Length - stream.Position, BLOCK_SIZE);

                        if (new_size != data.Length)
                        {
                            data = new byte[new_size];
                        }

                        await stream.ReadAsync(data, 0, data.Length);

                        response = await client.PutAsync(UploadURL + $"/{key}/data/{old_offset}",
                                                         new ByteArrayContent(data));

                        if (!response.IsSuccessStatusCode)
                        {
                            tcs.SetResult("FAILED");
                            return;
                        }

                        var val = long.Parse(await response.Content.ReadAsStringAsync());
                        if (val != old_offset + data.Length)
                        {
                            tcs.SetResult("Sync Error");
                            return;
                        }
                    }

                    response = await client.PutAsync(UploadURL + $"/{key}/finish", new StringContent(""));
                    if (response.IsSuccessStatusCode)
                    {
                        tcs.SetResult(await response.Content.ReadAsStringAsync());
                    }
                    else
                    {
                        tcs.SetResult("FAILED");
                    }
                }
            });
            return(tcs.Task);
        }
Ejemplo n.º 3
0
        public static Task <string> UploadFile(WorkQueue queue, string filename)
        {
            var tcs = new TaskCompletionSource <string>();

            queue.QueueTask(async() =>
            {
                var client = new HttpClient();
                var fsize  = new FileInfo(filename).Length;
                client.DefaultRequestHeaders.Add("X-API-KEY", AuthorAPI.GetAPIKey());
                var response = await client.PutAsync(UploadURL + $"/{Path.GetFileName(filename)}/start", new StringContent(""));
                if (!response.IsSuccessStatusCode)
                {
                    tcs.SetResult("FAILED");
                    return;
                }

                var key = await response.Content.ReadAsStringAsync();

                using (var iqueue = new WorkQueue(8))
                {
                    await Enumerable.Range(0, (int)(fsize / BLOCK_SIZE))
                    .PMap(iqueue, async block_idx =>
                    {
                        var block_offset = block_idx * BLOCK_SIZE;
                        var block_size   = block_offset + BLOCK_SIZE > fsize
                                ? fsize - block_offset
                                : BLOCK_SIZE;

                        using (var fs = File.OpenRead(filename))
                        {
                            fs.Position = block_offset;
                            var data    = new byte[block_size];
                            await fs.ReadAsync(data, 0, data.Length);

                            response = await client.PutAsync(UploadURL + $"/{key}/data/{block_offset}",
                                                             new ByteArrayContent(data));

                            if (!response.IsSuccessStatusCode)
                            {
                                tcs.SetResult("FAILED");
                                return;
                            }

                            var val = long.Parse(await response.Content.ReadAsStringAsync());
                            if (val != block_offset + data.Length)
                            {
                                tcs.SetResult("Sync Error");
                                return;
                            }
                        }
                    });
                }

                response = await client.PutAsync(UploadURL + $"/{key}/finish", new StringContent(""));
                if (response.IsSuccessStatusCode)
                {
                    tcs.SetResult(await response.Content.ReadAsStringAsync());
                }
                else
                {
                    tcs.SetResult("FAILED");
                }
            });
            return(tcs.Task);
        }
Ejemplo n.º 4
0
        public static Task <string> UploadFile(WorkQueue queue, string filename, Action <double> progressFn)
        {
            var tcs = new TaskCompletionSource <string>();

            Task.Run(async() =>
            {
                var handler = new HttpClientHandler {
                    MaxConnectionsPerServer = MAX_CONNECTIONS
                };
                var client = new HttpClient(handler);
                var fsize  = new FileInfo(filename).Length;

                var hash_task = filename.FileHashAsync();

                client.DefaultRequestHeaders.Add("X-API-KEY", AuthorAPI.GetAPIKey());
                var response = await client.PutAsync(UploadURL + $"/{Path.GetFileName(filename)}/start", new StringContent(""));
                if (!response.IsSuccessStatusCode)
                {
                    tcs.SetException(new Exception($"Start Error: {response.StatusCode} {response.ReasonPhrase}"));
                    return;
                }

                IEnumerable <long> Blocks(long fsize)
                {
                    for (long block = 0; block *BLOCK_SIZE < fsize; block++)
                    {
                        yield return(block);
                    }
                }

                var key   = await response.Content.ReadAsStringAsync();
                long sent = 0;
                using (var iqueue = new WorkQueue(MAX_CONNECTIONS))
                {
                    iqueue.Report("Starting Upload", 1);
                    await Blocks(fsize)
                    .PMap(iqueue, async block_idx =>
                    {
                        if (tcs.Task.IsFaulted)
                        {
                            return;
                        }
                        var block_offset = block_idx * BLOCK_SIZE;
                        var block_size   = block_offset + BLOCK_SIZE > fsize
                            ? fsize - block_offset
                            : BLOCK_SIZE;
                        Interlocked.Add(ref sent, block_size);
                        progressFn((double)sent / fsize);

                        int retries = 0;

                        using (var fs = File.OpenRead(filename))
                        {
                            fs.Position = block_offset;
                            var data    = new byte[block_size];
                            await fs.ReadAsync(data, 0, data.Length);


                            response = await client.PutAsync(UploadURL + $"/{key}/data/{block_offset}",
                                                             new ByteArrayContent(data));

                            if (!response.IsSuccessStatusCode)
                            {
                                tcs.SetException(new Exception($"Put Error: {response.StatusCode} {response.ReasonPhrase}"));
                                return;
                            }

                            var val = long.Parse(await response.Content.ReadAsStringAsync());
                            if (val != block_offset + data.Length)
                            {
                                tcs.SetResult($"Sync Error {val} vs {block_offset + data.Length}");
                                tcs.SetException(new Exception($"Sync Error {val} vs {block_offset + data.Length}"));
                            }
                        }
                    });
                }

                if (!tcs.Task.IsFaulted)
                {
                    progressFn(1.0);
                    var hash = (await hash_task).FromBase64().ToHex();
                    response = await client.PutAsync(UploadURL + $"/{key}/finish/{hash}", new StringContent(""));
                    if (response.IsSuccessStatusCode)
                    {
                        tcs.SetResult(await response.Content.ReadAsStringAsync());
                    }
                    else
                    {
                        tcs.SetException(new Exception($"Finalization Error: {response.StatusCode} {response.ReasonPhrase}"));
                    }
                }

                progressFn(0.0);
            });
            return(tcs.Task);
        }