Example #1
0
        public async Task <ResponseModel <FileContentResultModel> > Handle(GetByFileNameQuery request, CancellationToken cancellationToken)
        {
            var fileExtension     = Path.GetExtension(request.FileName);
            var categoryPath      = Path.Combine(_contentRootPath, "Archive", request.CategoryName);
            var fileAbsolutePath  = Path.Combine(categoryPath, request.FileName);
            var fileContentResult = new FileContentResultModel();

            if (fileExtension != FileConstants.AesExtension && fileExtension != FileConstants.RijndaelExtension)
            {
                if (!Directory.Exists(categoryPath))
                {
                    return(ResponseProvider.NotFound <FileContentResultModel>(nameof(request.CategoryName)));
                }
                if (!File.Exists(fileAbsolutePath))
                {
                    return(ResponseProvider.NotFound <FileContentResultModel>(nameof(request.FileName)));
                }

                fileContentResult.StreamData = await FileHelpers.ReadFileToMemoryStream(fileAbsolutePath);

                fileContentResult.FileName = request.FileName;
            }

            using (FileStream fs = File.Open(fileAbsolutePath, FileMode.Open))
            {
                byte[] data = new BinaryReader(fs).ReadBytes((int)fs.Length);

                fileContentResult.StreamData = await DecryptFile(data, fileExtension);

                fileContentResult.FileName = Path.GetFileNameWithoutExtension(request.FileName);

                return(ResponseProvider.Ok(fileContentResult));
            }
        }