Пример #1
0
        public Task <Result <CabinetFileInfo> > Handle(GetDocumentQuery request, CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                var dPath = Path.Combine(cs.DataDir, request.ContainerName);
                if (!Directory.Exists(dPath))
                {
                    var message = $"container name '{request.ContainerName}' does not exist";
                    return Result.Fail <CabinetFileInfo>(new Error(message));
                }

                var fPath = Path.Combine(dPath, request.DocumentName);
                if (!File.Exists(fPath))
                {
                    var message = $"file name '{request.ContainerName}' does not exist";
                    return Result.Fail <CabinetFileInfo>(new Error(message));
                }

                var file = new FileInfo(fPath);

                var cfInfo = new CabinetFileInfo
                {
                    Name = request.DocumentName,
                    Path = fPath,
                    ContainerName = request.ContainerName,
                    DocumentName = request.DocumentName,
                    MimeType = file.Extension.ToMimeType(cs.MimeTypes),
                };

                return Result.Ok(cfInfo);
            }));
        }
Пример #2
0
        public async Task <IActionResult> GetDocument(
            [FromQuery] string containerName,
            [FromQuery] string documentName)
        {
            var query  = new GetDocumentQuery(containerName, documentName);
            var result = await _mediator.Send(query);

            if (!result.IsSuccess)
            {
                return(BadRequest());
            }

            var file = new FileStream(result.Value.Path, FileMode.Open);

            return(File(file, result.Value.MimeType, fileDownloadName: result.Value.Name));
        }