Пример #1
0
        public async Task <bool> CheckAuthorizationSuccessAsync(string response, IStatus status)
        {
            if (response.Contains(gogData))
            {
                await statusController.InformAsync(status, successfullyAuthorized);

                await statusController.CompleteAsync(status);

                return(true);
            }

            return(false);
        }
        public async Task DownloadProductFileAsync(long id, string title, string sourceUri, string destination, IStatus status)
        {
            if (string.IsNullOrEmpty(sourceUri))
            {
                return;
            }

            var sourceUriSansSession = formatUriRemoveSessionDelegate.Format(sourceUri);
            var destinationUri       = formatValidationFileDelegate.Format(sourceUriSansSession);

            // return early if validation is not expected for this file
            if (!confirmValidationExpectedDelegate.Confirm(sourceUriSansSession))
            {
                return;
            }

            if (fileController.Exists(destinationUri))
            {
                await statusController.InformAsync(status, "Validation file already exists, will not be redownloading");

                return;
            }

            var validationSourceUri = formatValidationUriDelegate.Format(sourceUri);

            var downloadValidationFileTask = await statusController.CreateAsync(status, "Download validation file");

            await downloadFromUriAsyncDelegate.DownloadFromUriAsync(
                validationSourceUri,
                validationDirectoryDelegate.GetDirectory(string.Empty),
                downloadValidationFileTask);

            await statusController.CompleteAsync(downloadValidationFileTask);
        }
        public async Task DownloadFromResponseAsync(HttpResponseMessage response, string destination, IStatus status)
        {
            response.EnsureSuccessStatusCode();

            var filename = response.RequestMessage.RequestUri.Segments.Last();
            var fullPath = Path.Combine(destination, filename);

            int bufferSize = 1024 * 1024; // 1M
            byte[] buffer = new byte[bufferSize];
            int bytesRead = 0;
            long totalBytesRead = 0;

            // don't redownload file with the same name and size
            if (fileController.Exists(fullPath) &&
                fileController.GetSize(fullPath) == response.Content.Headers.ContentLength)
            {
                await statusController.InformAsync(
                    status, 
                    $"File {fullPath} already exists and matches response size, will not be redownloading");
                return;
            }

            using (var writeableStream = streamController.OpenWritable(fullPath))
            using (var responseStream = await response.Content.ReadAsStreamAsync())
            {
                while ((bytesRead = await responseStream.ReadAsync(buffer, 0, bufferSize)) > 0)
                {
                    totalBytesRead += bytesRead;
                    var contentLength = response.Content.Headers.ContentLength != null ?
                        (long) response.Content.Headers.ContentLength :
                        totalBytesRead;
                    await writeableStream.WriteAsync(buffer, 0, bytesRead);
                    await statusController.UpdateProgressAsync(
                        status,
                        totalBytesRead,
                        contentLength,
                        filename,
                        DataUnits.Bytes);
                }
            }
        }
Пример #4
0
        public async Task <IFileValidationResult> ValidateFileAsync(string productFileUri, string validationUri, IStatus status)
        {
            var fileValidation = new FileValidation
            {
                Filename = productFileUri
            };

            if (string.IsNullOrEmpty(productFileUri))
            {
                throw new ArgumentNullException();
            }

            if (string.IsNullOrEmpty(validationUri))
            {
                throw new ArgumentNullException();
            }

            fileValidation.ValidationExpected = confirmValidationExpectedDelegate.Confirm(productFileUri);

            if (!fileValidation.ValidationExpected)
            {
                return(fileValidation);
            }

            fileValidation.ValidationFileExists = VerifyValidationFileExists(validationUri);
            fileValidation.ProductFileExists    = VerifyProductFileExists(productFileUri);

            if (!fileValidation.ValidationFileExists ||
                !fileValidation.ProductFileExists)
            {
                return(fileValidation);
            }

            try
            {
                using (var xmlStream = streamController.OpenReadable(validationUri))
                    validationXml.Load(xmlStream);

                fileValidation.ValidationFileIsValid = true;
            }
            catch
            {
                fileValidation.ValidationFileIsValid = false;
                return(fileValidation);
            }

            var fileElement = validationXml.GetElementsByTagName("file");

            if (fileElement == null ||
                fileElement.Count < 1 ||
                fileElement[0] == null ||
                fileElement[0].Attributes == null)
            {
                fileValidation.ValidationFileIsValid = false;
                return(fileValidation);
            }

            long   expectedSize;
            string expectedName;
            int    chunks;
            bool   available;

            try
            {
                expectedSize = long.Parse(fileElement[0].Attributes["total_size"]?.Value);
                expectedName = fileElement[0].Attributes["name"]?.Value;
                chunks       = int.Parse(fileElement[0].Attributes["chunks"]?.Value);
                available    = fileElement[0].Attributes["available"]?.Value == "1";

                if (!available)
                {
                    var notAvailableMessage = fileElement[0].Attributes["notavailablemsg"]?.Value;
                    throw new Exception(notAvailableMessage);
                }
            }
            catch
            {
                fileValidation.ValidationFileIsValid = false;
                return(fileValidation);
            }

            fileValidation.FilenameVerified = VerifyFilename(productFileUri, expectedName);
            fileValidation.SizeVerified     = VerifySize(productFileUri, expectedSize);

            var chunksValidation = new List <IChunkValidation>();

            using (var fileStream = streamController.OpenReadable(productFileUri))
            {
                long length = 0;

                foreach (XmlNode chunkElement in fileElement[0].ChildNodes)
                {
                    if (chunkElement.Name != "chunk")
                    {
                        continue;
                    }

                    long   from, to = 0;
                    string expectedMd5 = string.Empty;

                    from        = long.Parse(chunkElement.Attributes["from"]?.Value);
                    to          = long.Parse(chunkElement.Attributes["to"]?.Value);
                    length     += (to - from);
                    expectedMd5 = chunkElement.FirstChild.Value;

                    chunksValidation.Add(await VerifyChunkAsync(fileStream, from, to, expectedMd5, status));

                    await statusController.UpdateProgressAsync(
                        status,
                        length,
                        expectedSize,
                        productFileUri,
                        DataUnits.Bytes);
                }

                fileValidation.Chunks = chunksValidation.ToArray();

                await statusController.UpdateProgressAsync(status, length, expectedSize, productFileUri);
            }

            await statusController.InformAsync(status, $"Validation result: {productFileUri} is valid: " +
                                               $"{validationResultController.ProductFileIsValid(fileValidation)}");

            return(fileValidation);
        }