public async Task <IActionResult> DetermineFileTypeFromBase64([FromBody][Required] Base64Request request)
        {
            _logger.LogInformation($"[{UserAgentInfo.ClientTypeString}]:: {nameof(DetermineFileTypeFromBase64)} method invoked");

            string originalStoreFilePath = string.Empty;
            string rebuiltStoreFilePath  = string.Empty;
            string fileId = string.Empty;
            CloudProxyResponseModel cloudProxyResponseModel = new CloudProxyResponseModel();

            ISpanBuilder builder = _tracer.BuildSpan("Post::Data");
            ISpan        span    = builder.Start();

            // Set some context data
            span.Log("File Type Detection base64");
            span.SetTag("Jaeger Testing Client", "POST api/FileTypeDetection/base64 request");

            try
            {
                if (!ModelState.IsValid)
                {
                    cloudProxyResponseModel.Errors.AddRange(ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage));
                    return(BadRequest(cloudProxyResponseModel));
                }

                if (!_fileUtility.TryGetBase64File(request.Base64, out byte[] file))
        public IActionResult DetermineFileTypeFromBase64([FromBody] Base64Request request)
        {
            try
            {
                Logger.LogInformation("'{0}' method invoked", nameof(DetermineFileTypeFromBase64));

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (!TryGetBase64File(request.Base64, out var file))
                {
                    return(BadRequest("Input file could not be decoded from base64."));
                }

                RecordEngineVersion();

                var fileType = DetectFromBytes(file);

                return(Ok(fileType));
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Exception occured processing file: {e.Message}");
                throw;
            }
        }
示例#3
0
        public static async Task <HttpResponseMessage> SendBase64RequiredAsync(this HttpClient client, string uri)
        {
            Base64Request       base64Request = new Base64Request();
            string              json          = JsonConvert.SerializeObject(base64Request);
            StringContent       stringContent = new StringContent(json, Encoding.UTF8, Constants.JSON_MEDIA_TYPE);
            HttpResponseMessage response      = await client.PostAsync(uri, stringContent);

            return(response);
        }
示例#4
0
        public IActionResult RebuildFromBase64([FromBody][Required] Base64Request request)
        {
            try
            {
                Logger.LogInformation("'{0}' method invoked", nameof(RebuildFromBase64));

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (!TryGetBase64File(request.Base64, out var file))
                {
                    return(BadRequest("Input file could not be decoded from base64."));
                }

                RecordEngineVersion();

                var fileType = DetectFromBytes(file);

                if (fileType.FileType == FileType.Unknown)
                {
                    return(UnprocessableEntity("File could not be determined to be a supported file"));
                }

                var protectedFileResponse = RebuildFromBytes(
                    request.ContentManagementFlags, fileType.FileTypeName, file);

                if (!string.IsNullOrWhiteSpace(protectedFileResponse.ErrorMessage))
                {
                    if (protectedFileResponse.IsDisallowed)
                    {
                        return(Ok(protectedFileResponse));
                    }

                    return(UnprocessableEntity(
                               $"File could not be rebuilt. Error Message: {protectedFileResponse.ErrorMessage}"));
                }

                return(Ok(Convert.ToBase64String(protectedFileResponse.ProtectedFile)));
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Exception occured processing file: {e.Message}");
                throw;
            }
        }
示例#5
0
        public async Task <IActionResult> AnalyseFromBase64([FromBody][Required] Base64Request request)
        {
            _logger.LogInformation($"[{UserAgentInfo.ClientTypeString}]:: {nameof(AnalyseFromBase64)} method invoked");
            string originalStoreFilePath = string.Empty;
            string rebuiltStoreFilePath  = string.Empty;
            string fileId = string.Empty;
            CloudProxyResponseModel cloudProxyResponseModel = new CloudProxyResponseModel();

            try
            {
                if (!ModelState.IsValid)
                {
                    cloudProxyResponseModel.Errors.AddRange(ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage));
                    return(BadRequest(cloudProxyResponseModel));
                }

                if (!_fileUtility.TryGetBase64File(request.Base64, out byte[] file))
示例#6
0
        public object Right(int id, Base64Request data)
        {
            var duo = context.Base64Duo.FirstOrDefault(d => d.Id == id);

            if (duo == null)
            {
                duo = new Base64Duo(id, null, new Base64(data.Data));
                context.Base64Duo.Add(duo);
            }
            else
            {
                duo.ChangeRightValue(new Base64(data.Data));
            }

            context.SaveChanges();

            return(Ok());
        }
        public IActionResult AnalyseFromBase64([FromBody] Base64Request request)
        {
            try
            {
                Logger.LogInformation("'{0}' method invoked", nameof(AnalyseFromBase64));

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (!TryGetBase64File(request.Base64, out var file))
                {
                    return(BadRequest("Input file could not be decoded from base64."));
                }

                RecordEngineVersion();

                var fileType = DetectFromBytes(file);

                if (fileType.FileType == FileType.Unknown)
                {
                    return(UnprocessableEntity("File could not be determined to be a supported file"));
                }

                var xmlReport = AnalyseFromBytes(request.ContentManagementFlags, fileType.FileTypeName, file);

                if (string.IsNullOrWhiteSpace(xmlReport))
                {
                    return(UnprocessableEntity("No report could be generated for file."));
                }

                return(Ok(xmlReport));
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Exception occured processing file: {e.Message}");
                throw;
            }
        }
示例#8
0
        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);
            }
        }
示例#9
0
 public async Task <IActionResult> RebuildFromBase64([FromBody][Required] Base64Request request)
 {
     _logger.LogInformation($"[{UserAgentInfo.ClientTypeString}]:: {nameof(RebuildFromBase64)} method invoked");
     return(await RebuildZipFile(request));
 }