public Task <int> RunAsync() { string originalStoreFilePath = string.Empty; string rebuiltStoreFilePath = string.Empty; var fileId = Guid.NewGuid(); try { var processingCancellationToken = _processingCancellationTokenSource.Token; _logger.LogInformation($"Using store locations '{OriginalStorePath}' and '{RebuiltStorePath}' for {fileId}"); originalStoreFilePath = Path.Combine(OriginalStorePath, fileId.ToString()); rebuiltStoreFilePath = Path.Combine(RebuiltStorePath, fileId.ToString()); _logger.LogInformation($"Updating 'Original' store for {fileId}"); File.Copy(_appConfiguration.InputFilepath, originalStoreFilePath, overwrite: true); _adaptationServiceClient.Connect(); var outcome = _adaptationServiceClient.AdaptationRequest(fileId, originalStoreFilePath, rebuiltStoreFilePath, processingCancellationToken); if (outcome == ReturnOutcome.GW_REBUILT) { _logger.LogInformation($"Copy from '{rebuiltStoreFilePath}' to {_appConfiguration.OutputFilepath}"); File.Copy(rebuiltStoreFilePath, _appConfiguration.OutputFilepath, overwrite: true); } ClearStores(originalStoreFilePath, rebuiltStoreFilePath); _logger.LogInformation($"Returning '{outcome}' Outcome for {fileId}"); return(Task.FromResult((int)outcome)); } catch (OperationCanceledException oce) { _logger.LogError(oce, $"Error Processing Timeout 'input' {fileId} exceeded {_processingTimeoutDuration.TotalSeconds}s"); ClearStores(originalStoreFilePath, rebuiltStoreFilePath); return(Task.FromResult((int)ReturnOutcome.GW_ERROR)); } catch (Exception ex) { _logger.LogError(ex, $"Error Processing 'input' {fileId}"); ClearStores(originalStoreFilePath, rebuiltStoreFilePath); return(Task.FromResult((int)ReturnOutcome.GW_ERROR)); } }
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); } }