Skip to content

Commit

Permalink
update controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
kkwangsir committed Sep 20, 2024
1 parent 954cb2a commit 936fe3b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 61 deletions.
31 changes: 21 additions & 10 deletions api/net/Areas/Editor/Controllers/ContentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,14 @@ public async Task<IActionResult> UploadFile([FromRoute] long id, [FromQuery] lon
public async Task<IActionResult> 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);
}

/// <summary>
Expand All @@ -580,11 +582,20 @@ public async Task<IActionResult> DownloadFileAsync(long id)
public async Task<IActionResult> 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!);
}

/// <summary>
Expand Down
59 changes: 16 additions & 43 deletions api/net/Areas/Editor/Controllers/StorageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,11 @@ public async Task<IActionResult> StreamAsync([FromRoute] int? locationId, [FromQ

private async Task<IActionResult> 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}'");
Expand Down Expand Up @@ -548,25 +533,10 @@ public async Task<IActionResult> UploadFileToS3Async([FromQuery] DateTime? updat
var uploadedFiles = new List<string>();
var failedUploads = new List<string>();
// 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<string>();
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
Expand All @@ -591,14 +561,17 @@ public async Task<IActionResult> 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
Expand Down
29 changes: 21 additions & 8 deletions api/net/Areas/Subscriber/Controllers/ContentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@ public IActionResult FindById(long id)
public async Task<IActionResult> 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);
}

/// <summary>
Expand All @@ -170,10 +173,20 @@ public async Task<IActionResult> 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!);

}

Expand Down

0 comments on commit 936fe3b

Please sign in to comment.