示例#1
0
        private bool PrepareFile(DistributionFile distributionFile, FileSystemInfo targetDirectory)
        {
            if (distributionFile.Action == FileProcessingAction.Delete)
            {
                _logger.LogTrace($"Skipping file marked for delete: {distributionFile.Name}");
                return(true);
            }

            var compressed   = new FileInfo(Path.Combine(targetDirectory.FullName, distributionFile.InternalPath, distributionFile.CompressedName));
            var decompressed = new FileInfo(Path.Combine(targetDirectory.FullName, distributionFile.InternalPath, distributionFile.Name));

            try
            {
                if (compressed.Exists)
                {
                    compressed.Delete();
                }
                if (decompressed.Exists)
                {
                    decompressed.Delete();
                }

                if (!_downloader.TryDownload(distributionFile, compressed.FullName))
                {
                    _logger.LogError($"Can not download file: {compressed.Name}");
                    return(false);
                }

                if (compressed.Length != distributionFile.CompressedSize)
                {
                    _logger.LogError($"Downloaded file {compressed.Name} has wrong size: {compressed.Length.ToString()}, has to be: {distributionFile.CompressedSize.ToString()}");
                    return(false);
                }

                if (!_decompressor.TryDecompress(distributionFile, compressed, decompressed))
                {
                    _logger.LogError($"Can not decompress file {compressed.Name}");
                    decompressed.DeleteIfExists();
                    return(false);
                }

                if (decompressed.Length != distributionFile.Size)
                {
                    _logger.LogError($"Decompressed file {decompressed.Name} has wrong size: {decompressed.Length.ToString()}, has to be: {distributionFile.Size.ToString()}");
                    decompressed.DeleteIfExists();
                    return(false);
                }

                if (!_verifier.VerifyFile(distributionFile, decompressed))
                {
                    _logger.LogError($"Decompressed file {decompressed.Name} verification failed");
                    decompressed.DeleteIfExists();
                    return(false);
                }

                return(true);
            }
            finally
            {
                compressed.DeleteIfExists();
            }
        }