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 void CheckIfPackageExistsTestShouldCheckForRightFile() { var date = DateTime.UtcNow; ZipFileInfo zipFileInfo = new ZipFileInfo() { BatchNumber = 2, Origin = "dk", PackageDate = date }; string zipFilesFolder = "ZipFilesFolder"; _fileSystem.Setup(x => x.FileExists(Path.Join(zipFilesFolder, zipFileInfo.Origin.ToLower(), zipFileInfo.FileName))).Returns(true); bool actualValue = _fileInfoService.CheckIfPackageExists(zipFileInfo, zipFilesFolder); Assert.IsTrue(actualValue); }
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)); } }