Пример #1
0
        public async Task <IActionResult> Get(
            Guid id,
            CancellationToken cancellationToken,
            [FromQuery] bool download = false
            )
        {
            UploadFile file;

            try
            {
                file = await _crudService.GetFile(id);

                _logger.LogInformation("Fetched file with id: {Id}", file.Id, file);
            }
            catch (FileNotFoundException e)
            {
                return(BadRequest(e.Message));
            }
            catch (UnauthorizedAccessException e)
            {
                return(Unauthorized(e.Message));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }

            // Check if there is a OnFetch method given by the provider
            var onFetch = _storageProvider.OnFetch(new StorageOnFetchOptions
            {
                Download    = download,
                File        = file,
                HttpContext = HttpContext,
            });

            if (onFetch != null)
            {
                return(await onFetch(cancellationToken));
            }

            // No OnFetch provided, execute the download ourselves
            var fileStream = await _storageProvider.GetAsync(new StorageGetOptions
            {
                Container = file.Container,
                FileName  = file.FileId,
            }, cancellationToken);

            SetFileHeaders(new FileDownloadOptions
            {
                ContentType = file.ContentType,
                FileName    = file.FileName,
                Length      = fileStream.Length,
                Download    = download,
            });

            return(new FileStreamResult(fileStream, file.ContentType ?? "application/octet-stream"));
        }