protected async Task <IActionResult> RebuildZipInfoByFileId(AdaptionDescriptor descriptor) { CloudProxyResponseModel cloudProxyResponseModel = new CloudProxyResponseModel(); if (descriptor.UUID == Guid.Empty) { cloudProxyResponseModel.Errors.Add($"The value {descriptor.UUID} should not be empty."); return(BadRequest(cloudProxyResponseModel)); } string fileIdString = descriptor.UUID.ToString(); string zipFileName = Path.Combine(Constants.VAR_PATH, $"{fileIdString}{Constants.ZIP_EXTENSION}"); string fileIdFolderPath = Path.Combine(Constants.VAR_PATH, fileIdString); string tempFolderPath = Path.Combine(fileIdFolderPath, fileIdString); try { _logger.LogInformation($"[{UserAgentInfo.ClientTypeString}]:: Using store locations '{_storeConfiguration.OriginalStorePath}' and '{_storeConfiguration.RebuiltStorePath}' for {fileIdString}"); string transactionFolderPath = Directory.GetDirectories(Constants.TRANSACTION_STORE_PATH, $"{ fileIdString}", SearchOption.AllDirectories).FirstOrDefault(); (ReportInformation reportInformation, IActionResult Result) = await GetReportAndMetadataInformation(fileIdString, descriptor.AdaptationServiceResponse.ReportPresignedUrl, descriptor.AdaptationServiceResponse.MetaDataPresignedUrl); if (reportInformation.ReportBytes == null) { return(Result); } GWallInfo gwInfo = reportInformation.ReportXmlText.XmlStringToObject <GWallInfo>(); if (!Directory.Exists(tempFolderPath)) { Directory.CreateDirectory(tempFolderPath); } string reportFolderPath = Path.Combine(tempFolderPath, Constants.REPORT_FOLDER_NAME); if (!Directory.Exists(reportFolderPath)) { Directory.CreateDirectory(reportFolderPath); } string cleanFolderPath = Path.Combine(tempFolderPath, Constants.CLEAN_FOLDER_NAME); if (!Directory.Exists(cleanFolderPath)) { Directory.CreateDirectory(cleanFolderPath); } string rebuiltStoreFilePath = Path.Combine(_storeConfiguration.RebuiltStorePath, fileIdString); if (!System.IO.File.Exists(rebuiltStoreFilePath)) { _logger.LogWarning($"[{UserAgentInfo.ClientTypeString}]:: Rebuild file not exist for file {fileIdString}"); cloudProxyResponseModel.Errors.Add($"Rebuild file not exist for file {fileIdString}"); return(NotFound(cloudProxyResponseModel)); } await System.IO.File.WriteAllTextAsync(Path.Combine(reportFolderPath, Constants.REPORT_XML_FILE_NAME), reportInformation.ReportXmlText); await System.IO.File.WriteAllTextAsync(Path.Combine(tempFolderPath, Constants.METADATA_JSON_FILE_NAME), reportInformation.MetadaJsonText); await System.IO.File.WriteAllBytesAsync(Path.Combine(cleanFolderPath, $"{Path.GetFileName(rebuiltStoreFilePath)}.{gwInfo.DocumentStatistics.DocumentSummary.FileType}"), await System.IO.File.ReadAllBytesAsync(rebuiltStoreFilePath)); _zipUtility.CreateZipFile(zipFileName, null, fileIdFolderPath); AddHeaderToResponse(Constants.Header.FILE_ID, fileIdString); return(new FileContentResult(await System.IO.File.ReadAllBytesAsync(zipFileName), Constants.OCTET_STREAM_CONTENT_TYPE) { FileDownloadName = Path.GetFileName(zipFileName) }); } catch (Exception ex) { _logger.LogError(ex, $"[{UserAgentInfo.ClientTypeString}]:: Error Processing 'input' {fileIdString} and error detail is {ex.Message}"); cloudProxyResponseModel.Errors.Add($"Error Processing 'input' {fileIdString} and error detail is {ex.Message}"); cloudProxyResponseModel.Status = ReturnOutcome.GW_ERROR; return(StatusCode(StatusCodes.Status500InternalServerError, cloudProxyResponseModel)); } finally { if (Directory.Exists(fileIdFolderPath)) { Directory.Delete(fileIdFolderPath, true); } if (System.IO.File.Exists(zipFileName)) { System.IO.File.Delete(zipFileName); } } }
protected async Task <IActionResult> RebuildZipFile(Base64Request request = null, IFormFile formFile = null) { string originalStoreFilePath = string.Empty; string rebuiltStoreFilePath = string.Empty; string fileId = string.Empty; CloudProxyResponseModel cloudProxyResponseModel = new CloudProxyResponseModel(); try { byte[] file = null; if (!ModelState.IsValid) { cloudProxyResponseModel.Errors.AddRange(ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage)); return(BadRequest(cloudProxyResponseModel)); } if (request != null) { if (!_fileUtility.TryGetBase64File(request.Base64, out file)) { cloudProxyResponseModel.Errors.Add("Input file could not be decoded from base64."); return(BadRequest(cloudProxyResponseModel)); } } if (formFile != null) { if (!_fileUtility.TryReadFormFile(formFile, out file)) { cloudProxyResponseModel.Errors.Add("Input file could not be parsed."); return(BadRequest(cloudProxyResponseModel)); } } AdaptionDescriptor descriptor = AdaptionCache.Instance.GetDescriptor(file, _cloudSdkConfiguration); if (null == descriptor) { cloudProxyResponseModel.Errors.Add("Cannot create a cache entry for the file."); return(BadRequest(cloudProxyResponseModel)); } fileId = descriptor.UUID.ToString(); CancellationToken processingCancellationToken = new CancellationTokenSource(_processingConfiguration.ProcessingTimeoutDuration).Token; _logger.LogInformation($"[{UserAgentInfo.ClientTypeString}]:: Using store locations '{_storeConfiguration.OriginalStorePath}' and '{_storeConfiguration.RebuiltStorePath}' for {fileId}"); originalStoreFilePath = Path.Combine(_storeConfiguration.OriginalStorePath, fileId); rebuiltStoreFilePath = Path.Combine(_storeConfiguration.RebuiltStorePath, fileId); if (ReturnOutcome.GW_REBUILT != descriptor.AdaptationServiceResponse.FileOutcome) { _logger.LogInformation($"[{UserAgentInfo.ClientTypeString}]:: Updating 'Original' store for {fileId}"); using (Stream fileStream = new FileStream(originalStoreFilePath, FileMode.Create)) { await fileStream.WriteAsync(file, 0, file.Length); } _adaptationServiceClient.Connect(); IAdaptationServiceResponse adaptationServiceResponse = _adaptationServiceClient.AdaptationRequest(descriptor.UUID, originalStoreFilePath, rebuiltStoreFilePath, processingCancellationToken); descriptor.Update(adaptationServiceResponse, originalStoreFilePath, rebuiltStoreFilePath); _logger.LogInformation($"[{UserAgentInfo.ClientTypeString}]:: Returning '{descriptor.AdaptationServiceResponse.FileOutcome}' Outcome for {fileId}"); } switch (descriptor.AdaptationServiceResponse.FileOutcome) { case ReturnOutcome.GW_REBUILT: return(await RebuildZipInfoByFileId(descriptor)); case ReturnOutcome.GW_FAILED: if (System.IO.File.Exists(descriptor.RebuiltStoreFilePath)) { cloudProxyResponseModel.Errors.Add(await System.IO.File.ReadAllTextAsync(descriptor.RebuiltStoreFilePath)); } cloudProxyResponseModel.Status = descriptor.AdaptationServiceResponse.FileOutcome; cloudProxyResponseModel.RebuildProcessingStatus = descriptor.AdaptationServiceResponse.RebuildProcessingStatus; return(BadRequest(cloudProxyResponseModel)); case ReturnOutcome.GW_UNPROCESSED: if (System.IO.File.Exists(descriptor.RebuiltStoreFilePath)) { cloudProxyResponseModel.Errors.Add(await System.IO.File.ReadAllTextAsync(descriptor.RebuiltStoreFilePath)); } cloudProxyResponseModel.Status = descriptor.AdaptationServiceResponse.FileOutcome; cloudProxyResponseModel.RebuildProcessingStatus = descriptor.AdaptationServiceResponse.RebuildProcessingStatus; return(BadRequest(cloudProxyResponseModel)); case ReturnOutcome.GW_ERROR: default: if (System.IO.File.Exists(descriptor.RebuiltStoreFilePath)) { cloudProxyResponseModel.Errors.Add(await System.IO.File.ReadAllTextAsync(descriptor.RebuiltStoreFilePath)); } cloudProxyResponseModel.Status = descriptor.AdaptationServiceResponse.FileOutcome; cloudProxyResponseModel.RebuildProcessingStatus = descriptor.AdaptationServiceResponse.RebuildProcessingStatus; return(BadRequest(cloudProxyResponseModel)); } } catch (OperationCanceledException oce) { _logger.LogError(oce, $"[{UserAgentInfo.ClientTypeString}]:: Error Processing Timeout 'input' {fileId} exceeded {_processingConfiguration.ProcessingTimeoutDuration.TotalSeconds}s"); cloudProxyResponseModel.Errors.Add($"Error Processing Timeout 'input' {fileId} exceeded {_processingConfiguration.ProcessingTimeoutDuration.TotalSeconds}s"); cloudProxyResponseModel.Status = ReturnOutcome.GW_ERROR; return(StatusCode(StatusCodes.Status500InternalServerError, cloudProxyResponseModel)); } catch (Exception ex) { _logger.LogError(ex, $"[{UserAgentInfo.ClientTypeString}]:: Error Processing 'input' {fileId} and error detail is {ex.Message}"); cloudProxyResponseModel.Errors.Add($"Error Processing 'input' {fileId} and error detail is {ex.Message}"); cloudProxyResponseModel.Status = ReturnOutcome.GW_ERROR; return(StatusCode(StatusCodes.Status500InternalServerError, cloudProxyResponseModel)); } finally { ClearStores(originalStoreFilePath, rebuiltStoreFilePath); AddHeaderToResponse(Constants.Header.FILE_ID, fileId); } }