Skip to content

Commit

Permalink
update running time in the endpoint (#2369)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkwangsir authored Dec 18, 2024
1 parent 16bea4e commit 81db079
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions api/net/Areas/Editor/Controllers/ContentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
using TNO.Keycloak;
using TNO.Models.Extensions;
using TNO.Models.Filters;

using TNO.DAL.Helpers;
namespace TNO.API.Areas.Editor.Controllers;

/// <summary>
Expand Down Expand Up @@ -527,13 +527,35 @@ public async Task<IActionResult> UploadFile([FromRoute] long id, [FromQuery] lon
var content = _contentService.FindById(id) ?? throw new NoContentException("Entity does not exist");

if (!files.Any()) throw new InvalidOperationException("No file uploaded");
if (files.Count > 1) throw new InvalidOperationException("Only one file can be uploaded at a time");

var file = files[0];
// If the content has a file reference, then update it. Otherwise, add one.
content.Version = version; // TODO: Handle concurrency before uploading the file as it will result in an orphaned file.
if (content.FileReferences.Any()) await _fileReferenceService.UploadAsync(content, files.First(), _storageOptions.GetUploadPath());
else await _fileReferenceService.UploadCleanUpAsync(new ContentFileReference(content, files.First()), _storageOptions.GetUploadPath());

if (_workOrderHelper.ShouldAutoTranscribe(content.Id)) await _workOrderHelper.RequestTranscriptionAsync(content.Id);
// save file reference
var updatedFileReference = content.FileReferences.Any()
? await _fileReferenceService.UploadAsync(content, file, _storageOptions.GetUploadPath())
: await _fileReferenceService.UploadCleanUpAsync(new ContentFileReference(content, file), _storageOptions.GetUploadPath());

if (_workOrderHelper.ShouldAutoTranscribe(content.Id))
await _workOrderHelper.RequestTranscriptionAsync(content.Id);

if (updatedFileReference.ContentType.StartsWith("video/", StringComparison.OrdinalIgnoreCase) ||
updatedFileReference.ContentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase))
{
try
{
var filePath = Path.Combine(_storageOptions.GetUploadPath(), updatedFileReference.Path);
var duration = await FfmpegHelper.GetVideoDurationAsync(filePath);
updatedFileReference.RunningTime = (int)Math.Round(duration * 1000);
await _fileReferenceService.UpdateAsync(updatedFileReference);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error parsing duration for file: {Path}, contentId: {ContentId}", updatedFileReference.Path, updatedFileReference.ContentId);
}
}

var updatedContent = new ContentModel(content);
if (!String.IsNullOrWhiteSpace(_kafkaOptions.IndexingTopic))
Expand Down

0 comments on commit 81db079

Please sign in to comment.