private void RemoveOldZipFilesFromSubfolder(string path, int daysToInvalidateZipFile)
        {
            var deleteFilesOlderThanDate = DateTime.UtcNow.Date.AddDays(-daysToInvalidateZipFile);
            var allFilenames             = _fileSystem.GetFilenamesFromDirectory(path);
            var oldZipFilenames          = allFilenames.Where(x => _zipFileInfoService.CreateZipFileInfoFromPackageName(new FileInfo(x).Name).PackageDate < deleteFilesOlderThanDate);

            foreach (var filename in oldZipFilenames)
            {
                _fileSystem.DeleteFile(filename);
            }
        }
Exemplo n.º 2
0
        public void CreateZipFileInfoFromPackageNameTestShouldReturnCorrectPackageName()
        {
            string filename            = "2020-09-22_2_dk.zip";
            var    actualZipFileInfo   = _fileInfoService.CreateZipFileInfoFromPackageName(filename);
            var    expectedZipFileInfo = new ZipFileInfo()
            {
                BatchNumber = 2,
                PackageDate = DateTime.Parse("2020-09-22"),
                Origin      = "dk"
            };

            Assert.AreEqual(expectedZipFileInfo.Origin, actualZipFileInfo.Origin);
            Assert.AreEqual(expectedZipFileInfo.BatchNumber, actualZipFileInfo.BatchNumber);
            Assert.AreEqual(expectedZipFileInfo.PackageDate, actualZipFileInfo.PackageDate);
        }
Exemplo n.º 3
0
        public IActionResult DownloadDiagnosisKeysFile(string packageName)
        {
            _logger.LogInformation("DownloadDiagnosisKeysFile endpoint called");
            try
            {
                ZipFileInfo packageInfo    = _zipFileInfoService.CreateZipFileInfoFromPackageName(packageName);
                string      zipFilesFolder = _configuration["ZipFilesFolder"];

                _logger.LogInformation("Package Date: " + packageInfo.PackageDate);
                _logger.LogInformation("Add days: " + DateTime.UtcNow.Date.AddDays(-14));
                _logger.LogInformation("Utc now:" + DateTime.UtcNow);

                if (!IsDateValid(packageInfo.PackageDate, packageName))
                {
                    return(BadRequest("Package Date is invalid"));
                }

                var packageExists = _zipFileInfoService.CheckIfPackageExists(packageInfo, zipFilesFolder);
                if (packageExists)
                {
                    var zipFileContent     = _zipFileInfoService.ReadPackage(packageInfo, zipFilesFolder);
                    var currentBatchNumber = packageInfo.BatchNumber;
                    packageInfo.BatchNumber++;
                    var nextPackageExists = _zipFileInfoService.CheckIfPackageExists(packageInfo, zipFilesFolder);

                    AddResponseHeader(nextPackageExists, currentBatchNumber);
                    _logger.LogInformation("Zip package fetched successfully");
                    return(File(zipFileContent, System.Net.Mime.MediaTypeNames.Application.Zip));
                }
                else
                {
                    _logger.LogInformation("Package does not exist");
                    return(NoContent());
                }
            }
            catch (FormatException e)
            {
                _logger.LogError("Error when parsing data: " + e);
                return(BadRequest(e.Message));
            }
            catch (Exception e)
            {
                _logger.LogError("Error when downloading package: " + e);
                return(StatusCode(500));
            }
        }
        public async Task <IActionResult> DownloadDiagnosisKeysFile(string packageName)
        {
            _logger.LogInformation("DownloadDiagnosisKeysFile endpoint called");
            try
            {
                if (packageName == "today")
                {
                    packageName = ReplacePackageNameWithToday();
                }

                ZipFileInfo packageInfo    = _zipFileInfoService.CreateZipFileInfoFromPackageName(packageName);
                string      zipFilesFolder = _appSettingsConfig.ZipFilesFolder;

                _logger.LogInformation("Package Date: " + packageInfo.PackageDate);
                _logger.LogInformation("Add days: " + DateTime.UtcNow.Date.AddDays(-14));
                _logger.LogInformation("Utc now:" + DateTime.UtcNow);

                if (!IsDateValid(packageInfo.PackageDate, packageName))
                {
                    return(BadRequest("Package Date is invalid"));
                }

                var packageExists = _zipFileInfoService.CheckIfPackageExists(packageInfo, zipFilesFolder);
                if (packageExists)
                {
                    byte[] zipFileContent  = null;
                    bool   invalidateCache = false;
                    if (Request.Headers.ContainsKey("Cache-Control") && Request.Headers["Cache-Control"] == "no-cache")
                    {
                        invalidateCache = true;
                        zipFileContent  = await _cacheOperations.GetCacheValue(packageInfo, zipFilesFolder, invalidateCache);
                    }
                    else
                    {
                        zipFileContent = await _cacheOperations.GetCacheValue(packageInfo, zipFilesFolder, invalidateCache);
                    }
                    var currentBatchNumber = packageInfo.BatchNumber;
                    packageInfo.BatchNumber++;
                    var nextPackageExists = _zipFileInfoService.CheckIfPackageExists(packageInfo, zipFilesFolder);

                    AddResponseHeader(nextPackageExists, currentBatchNumber);
                    _logger.LogInformation("Zip package fetched successfully");
                    return(File(zipFileContent, System.Net.Mime.MediaTypeNames.Application.Zip));
                }
                else
                {
                    _logger.LogInformation("Package does not exist");
                    return(NoContent());
                }
            }
            catch (FormatException e)
            {
                _logger.LogError("Error when parsing data: " + e);
                return(BadRequest(e.Message));
            }
            catch (Exception e)
            {
                _logger.LogError("Error when downloading package: " + e);
                return(StatusCode(500));
            }
        }