Beispiel #1
0
        public static async Task <VirusScanner.Result> GetVirusScanResult(AbsolutePath path)
        {
            var client = await GetClient();

            Utils.Log($"Checking virus result for {path}");

            var hash = await path.FileHashAsync();

            using var result = await client.GetAsync($"{Consts.WabbajackBuildServerUri}virus_scan/{hash.ToHex()}", errorsAsExceptions : false);

            if (result.StatusCode == HttpStatusCode.OK)
            {
                var data = await result.Content.ReadAsStringAsync();

                return(Enum.Parse <VirusScanner.Result>(data));
            }

            if (result.StatusCode == HttpStatusCode.NotFound)
            {
                await using var input = await path.OpenRead();

                using var postResult = await client.PostAsync($"{Consts.WabbajackBuildServerUri}virus_scan", errorsAsExceptions : false, content : new StreamContent(input));

                if (postResult.StatusCode == HttpStatusCode.OK)
                {
                    var data = await postResult.Content.ReadAsStringAsync();

                    return(Enum.Parse <VirusScanner.Result>(data));
                }
                throw new HttpException(result);
            }
            throw new HttpException(result);
        }
Beispiel #2
0
        public static async Task <Hash> FileHashCachedAsync(this AbsolutePath file, bool nullOnIOError = false)
        {
            if (TryGetHashCache(file, out var foundHash))
            {
                return(foundHash);
            }

            var hash = await file.FileHashAsync(nullOnIOError);

            if (hash != Hash.Empty)
            {
                WriteHashCache(file, hash);
            }
            return(hash);
        }
Beispiel #3
0
        public async Task <AbsolutePath> Ingest(AbsolutePath file)
        {
            var hash = await file.FileHashAsync();

            if (HaveArchive(hash))
            {
                file.Delete();
                return(_archives[hash]);
            }

            var newPath = _settings.ArchivePath.Combine(hash.ToHex());
            await file.MoveToAsync(newPath);

            _archives[hash] = newPath;
            return(_archives[hash]);
        }
        public async Task Ingest(AbsolutePath file)
        {
            var hash = await file.FileHashAsync();

            if (hash == null)
            {
                return;
            }

            var path = ArchivePath(hash.Value);

            if (HaveArchive(hash.Value))
            {
                await file.DeleteAsync();

                return;
            }

            var newPath = _settings.ArchivePath.Combine(hash.Value.ToHex());
            await file.MoveToAsync(newPath);
        }
 public async Task <Hash> HashAsync()
 {
     return(await _path.FileHashAsync());
 }
Beispiel #6
0
        public static Task <string> UploadFile(AbsolutePath filename, Action <double> progressFn, string?apikey = null)
        {
            var tcs = new TaskCompletionSource <string>();

            Task.Run(async() =>
            {
                var client = await GetAuthorizedClient(apikey);

                var fsize    = filename.Size;
                var hashTask = filename.FileHashAsync();

                Utils.Log($"{UploadURL}/{filename.FileName.ToString()}/start");
                using var response = await client.PutAsync($"{UploadURL}/{filename.FileName.ToString()}/start", new StringContent(""));
                if (!response.IsSuccessStatusCode)
                {
                    Utils.Log("Error starting upload");
                    Utils.Log(await response.Content.ReadAsStringAsync());
                    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", Percent.One);
                    await Blocks(fsize)
                    .PMap(iqueue, async blockIdx =>
                    {
                        if (tcs.Task.IsFaulted)
                        {
                            return;
                        }
                        var blockOffset = blockIdx * BLOCK_SIZE;
                        var blockSize   = blockOffset + BLOCK_SIZE > fsize
                            ? fsize - blockOffset
                            : BLOCK_SIZE;
                        Interlocked.Add(ref sent, blockSize);
                        progressFn((double)sent / fsize);

                        var data = new byte[blockSize];
                        await using (var fs = filename.OpenRead())
                        {
                            fs.Position = blockOffset;
                            await fs.ReadAsync(data, 0, data.Length);
                        }


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

                        if (!offsetResponse.IsSuccessStatusCode)
                        {
                            Utils.Log(await offsetResponse.Content.ReadAsStringAsync());
                            tcs.SetException(new Exception($"Put Error: {offsetResponse.StatusCode} {offsetResponse.ReasonPhrase}"));
                            return;
                        }

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

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

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