diff --git a/api/net/Areas/Editor/Controllers/ContentController.cs b/api/net/Areas/Editor/Controllers/ContentController.cs index 09f7a936b..9930b58bf 100644 --- a/api/net/Areas/Editor/Controllers/ContentController.cs +++ b/api/net/Areas/Editor/Controllers/ContentController.cs @@ -559,12 +559,14 @@ public async Task UploadFile([FromRoute] long id, [FromQuery] lon public async Task DownloadFileAsync(long id) { var fileReference = _fileReferenceService.FindByContentId(id).FirstOrDefault() ?? throw new NoContentException("File does not exist"); - - var (stream, fileName, contentType) = await _fileReferenceService.GetFileStreamAsync(fileReference.Path); - - if (stream == null) throw new NoContentException("File does not exist"); - - return File(stream, contentType ?? "application/octet-stream", fileName ?? fileReference.FileName); + if (fileReference.IsSyncedToS3 && !string.IsNullOrWhiteSpace(fileReference.S3Path)) + { + var s3Stream = await _fileReferenceService.DownloadFromS3Async(fileReference.S3Path); + if (s3Stream != null) + return File(s3Stream, fileReference.ContentType); + } + var stream = _fileReferenceService.Download(fileReference, _storageOptions.GetUploadPath()); + return File(stream, fileReference.ContentType); } /// @@ -580,11 +582,20 @@ public async Task DownloadFileAsync(long id) public async Task StreamAsync([FromQuery] string path) { path = string.IsNullOrWhiteSpace(path) ? "" : HttpUtility.UrlDecode(path).MakeRelativePath(); - var (stream, fileName, contentType) = await _fileReferenceService.GetFileStreamAsync(path); - // log stream file name content type - if (stream == null) throw new NoContentException("File does not exist"); + //find file from s3 + var stream = await _fileReferenceService.DownloadFromS3Async(path); + if (stream != null) + { + return File(stream, "application/octet-stream"); + } + //find file from local + path = string.IsNullOrWhiteSpace(path) ? "" : HttpUtility.UrlDecode(path).MakeRelativePath(); + var safePath = Path.Combine(_storageOptions.GetUploadPath(), path); + if (!safePath.FileExists()) throw new NoContentException("File does not exist"); - return File(stream, contentType ?? "application/octet-stream", fileName); + var info = new ItemModel(safePath); + var fileStream = System.IO.File.OpenRead(safePath); + return File(fileStream, info.MimeType!); } /// diff --git a/api/net/Areas/Editor/Controllers/StorageController.cs b/api/net/Areas/Editor/Controllers/StorageController.cs index ed9c717cf..0d475477c 100644 --- a/api/net/Areas/Editor/Controllers/StorageController.cs +++ b/api/net/Areas/Editor/Controllers/StorageController.cs @@ -265,26 +265,11 @@ public async Task StreamAsync([FromRoute] int? locationId, [FromQ private async Task GetResultAsync(string safePath, string path) { - _logger.LogInformation("Getting stream for path: {Path}", path); - var fileReference = await _fileReferenceService.GetByS3PathAsync(path); - _logger.LogInformation("File reference: {FileReference}", fileReference); - if (fileReference == null) + //find file from s3 + var stream = await _fileReferenceService.DownloadFromS3Async(path); + if (stream != null) { - return NotFound($"Stream does not exist: '{path}'"); - } - - if (fileReference.IsSyncedToS3) - { - try - { - var stream = await _fileReferenceService.DownloadFromS3Async(path); - return File(stream, "application/octet-stream", fileReference.FileName); - } - catch (Exception ex) - { - _logger.LogError(ex, "error on stream file from s3: {Path}", path); - // if the file is not in s3, try to get it from the local file system - } + return File(stream, "application/octet-stream"); } if (!safePath.FileExists()) throw new NoContentException($"Stream does not exist: '{path}'"); @@ -548,25 +533,10 @@ public async Task UploadFileToS3Async([FromQuery] DateTime? updat var uploadedFiles = new List(); var failedUploads = new List(); // check if s3 credentials are set - var accessKey = Environment.GetEnvironmentVariable("S3_ACCESS_KEY"); - var secretKey = Environment.GetEnvironmentVariable("S3_SECRET_KEY"); - var bucketName = Environment.GetEnvironmentVariable("S3_BUCKET_NAME"); - var serviceUrl = Environment.GetEnvironmentVariable("S3_SERVICE_URL"); - var hasS3Credentials = !string.IsNullOrEmpty(accessKey) && !string.IsNullOrEmpty(secretKey) && !string.IsNullOrEmpty(bucketName) && !string.IsNullOrEmpty(serviceUrl); - - if (!hasS3Credentials) + if (!S3Options.IsS3Enabled) { - // make a string shows all the environment variables name if they are not set - var environmentVariables = new List(); - if (string.IsNullOrEmpty(accessKey)) environmentVariables.Add("S3_ACCESS_KEY"); - if (string.IsNullOrEmpty(secretKey)) environmentVariables.Add("S3_SECRET_KEY"); - if (string.IsNullOrEmpty(bucketName)) environmentVariables.Add("S3_BUCKET_NAME"); - if (string.IsNullOrEmpty(serviceUrl)) environmentVariables.Add("S3_SERVICE_URL"); - _logger.LogError("S3 credentials are not set: {EnvironmentVariables}", string.Join(", ", environmentVariables)); - - return BadRequest($"S3 credentials are not set: {string.Join(", ", environmentVariables)}"); + return BadRequest("S3 is not enabled or credentials are not set"); } - foreach (var fileReference in fileReferences) { try @@ -591,14 +561,17 @@ public async Task UploadFileToS3Async([FromQuery] DateTime? updat uploadedFiles.Add(s3Key); - try - { - System.IO.File.Delete(filePath); - _logger.LogInformation("deleted local file: {FilePath}", filePath); - } - catch (Exception ex) + if (fileReference.ContentType.StartsWith("video/") || fileReference.ContentType.StartsWith("audio/")) { - _logger.LogError(ex, "delete local file failed: {FilePath}", filePath); + try + { + System.IO.File.Delete(filePath); + _logger.LogInformation("deleted local file: {FilePath}", filePath); + } + catch (Exception ex) + { + _logger.LogError(ex, "failed to delete local file: {FilePath}", filePath); + } } } else diff --git a/api/net/Areas/Subscriber/Controllers/ContentController.cs b/api/net/Areas/Subscriber/Controllers/ContentController.cs index 4d3beead1..035e1bdbf 100644 --- a/api/net/Areas/Subscriber/Controllers/ContentController.cs +++ b/api/net/Areas/Subscriber/Controllers/ContentController.cs @@ -149,11 +149,14 @@ public IActionResult FindById(long id) public async Task DownloadFileAsync(long id) { var fileReference = _fileReferenceService.FindByContentId(id).FirstOrDefault() ?? throw new NoContentException("File does not exist"); - var (stream, fileName, contentType) = await _fileReferenceService.GetFileStreamAsync(fileReference.Path); - - if (stream == null) throw new NoContentException("File does not exist"); - - return File(stream, contentType ?? "application/octet-stream", fileName ?? fileReference.FileName); + if (fileReference.IsSyncedToS3 && !string.IsNullOrWhiteSpace(fileReference.S3Path)) + { + var s3Stream = await _fileReferenceService.DownloadFromS3Async(fileReference.S3Path); + if (s3Stream != null) + return File(s3Stream, fileReference.ContentType); + } + var stream = _fileReferenceService.Download(fileReference, _storageOptions.GetUploadPath()); + return File(stream, fileReference.ContentType); } /// @@ -170,10 +173,20 @@ public async Task StreamAsync([FromQuery] string path) { path = string.IsNullOrWhiteSpace(path) ? "" : HttpUtility.UrlDecode(path).MakeRelativePath(); + //find file from s3 + var stream = await _fileReferenceService.DownloadFromS3Async(path); + if (stream != null) + { + return File(stream, "application/octet-stream"); + } + //find file from local + var safePath = Path.Combine(_storageOptions.GetUploadPath(), path); + + if (!safePath.FileExists()) throw new NoContentException("File does not exist"); - var (stream, fileName, contentType) = await _fileReferenceService.GetFileStreamAsync(path); - if (stream == null) throw new NoContentException("File does not exist"); - return File(stream, contentType ?? "application/octet-stream", fileName); + var info = new ItemModel(safePath); + var fileStream = System.IO.File.OpenRead(safePath); + return File(fileStream, info.MimeType!); }