예제 #1
0
        public IFileProtectResponse GetProtectedFile(ContentManagementFlags contentManagementFlags, string fileType, byte[] fileBytes)
        {
            var response = new FileProtectResponse {
                ProtectedFile = Enumerable.Empty <byte>().ToArray()
            };

            var glasswallConfiguration = _glasswallConfigurationAdaptor.Adapt(contentManagementFlags);
            var configurationOutcome   = _glasswallFileOperations.SetConfiguration(glasswallConfiguration);

            if (configurationOutcome != EngineOutcome.Success)
            {
                response.Outcome = configurationOutcome;
                return(response);
            }

            var version = _glasswallFileOperations.GetLibraryVersion();

            var engineOutcome = _glasswallFileOperations.ProtectFile(fileBytes, fileType, out var protectedFile);

            response.Outcome       = engineOutcome;
            response.ProtectedFile = protectedFile;

            if (engineOutcome != EngineOutcome.Success)
            {
                response.ErrorMessage = _glasswallFileOperations.GetEngineError();
            }

            return(response);
        }
        public IFileProtectResponse GetProtectedFile(ContentManagementFlags contentManagementFlags, string fileType, byte[] fileBytes)
        {
            var response = new FileProtectResponse {
                ProtectedFile = Enumerable.Empty <byte>().ToArray()
            };

            var glasswallConfiguration = _glasswallConfigurationAdaptor.Adapt(contentManagementFlags);
            var configurationOutcome   = _glasswallFileOperations.SetConfiguration(glasswallConfiguration);

            if (configurationOutcome != EngineOutcome.Success)
            {
                _logger.Log(LogLevel.Error, "Error processing configuration");
                response.Outcome = configurationOutcome;
                return(response);
            }

            var version = _glasswallFileOperations.GetLibraryVersion();

            _logger.LogInformation($"Engine version: {version}");

            var engineOutcome = _glasswallFileOperations.ProtectFile(fileBytes, fileType, out var protectedFile);

            response.Outcome       = engineOutcome;
            response.ProtectedFile = protectedFile;

            if (engineOutcome != EngineOutcome.Success)
            {
                _logger.Log(LogLevel.Error, $"Unable to protect file, reason: {engineOutcome}.");
            }

            return(response);
        }
예제 #3
0
        public IActionResult RebuildFromFormFile([FromForm] string contentManagementFlagJson, [FromForm][Required] IFormFile file)
        {
            try
            {
                Logger.LogInformation("'{0}' method invoked", nameof(RebuildFromFormFile));

                ContentManagementFlags contentManagementFlags = null;
                if (!string.IsNullOrWhiteSpace(contentManagementFlagJson))
                {
                    contentManagementFlags = Newtonsoft.Json.JsonConvert.DeserializeObject <ContentManagementFlags>(contentManagementFlagJson);
                }

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

                if (!TryReadFormFile(file, out var fileBytes))
                {
                    return(BadRequest("Input file could not be read."));
                }

                RecordEngineVersion();

                var fileType = DetectFromBytes(fileBytes);

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

                var protectedFileResponse = RebuildFromBytes(
                    contentManagementFlags, fileType.FileTypeName, fileBytes);

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

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

                return(new FileContentResult(protectedFileResponse.ProtectedFile, "application/octet-stream")
                {
                    FileDownloadName = file.FileName ?? "Unknown"
                });
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Exception occured processing file: {e.Message}");
                throw;
            }
        }
예제 #4
0
        private IFileProtectResponse RebuildFromBytes(ContentManagementFlags contentManagementFlags, string fileType, byte[] bytes)
        {
            contentManagementFlags = contentManagementFlags.ValidatedOrDefault();

            TimeMetricTracker.Restart();
            var response = _fileProtector.GetProtectedFile(contentManagementFlags, fileType, bytes);

            TimeMetricTracker.Stop();

            MetricService.Record(Metric.RebuildTime, TimeMetricTracker.Elapsed);
            return(response);
        }
        private string AnalyseFromBytes(ContentManagementFlags contentManagementFlags, string fileType, byte[] bytes)
        {
            contentManagementFlags = contentManagementFlags.ValidatedOrDefault();

            TimeMetricTracker.Restart();
            var response = _fileAnalyser.GetReport(contentManagementFlags, fileType, bytes);

            TimeMetricTracker.Stop();

            MetricService.Record(Metric.AnalyseTime, TimeMetricTracker.Elapsed);
            return(response);
        }
        public string GetReport(ContentManagementFlags flags, string fileType, byte[] fileBytes)
        {
            var analysisReport = string.Empty;

            var glasswallConfiguration = _glasswallConfigurationAdaptor.Adapt(flags);

            if (glasswallConfiguration == null)
            {
                return(analysisReport);
            }

            var setConfigurationEngineOutcome = _glasswallFileOperations.SetConfiguration(glasswallConfiguration);

            if (setConfigurationEngineOutcome != EngineOutcome.Success)
            {
                return(analysisReport);
            }

            _glasswallFileOperations.AnalyseFile(fileBytes, fileType, out analysisReport);


            return(analysisReport);
        }