Skip to content

Commit

Permalink
feat: add v3 file stream endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Apr 6, 2023
1 parent 952110e commit bd5b51a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Shoko.Server/API/v3/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Shoko.Models.Enums;
using Shoko.Server.API.Annotations;
using Shoko.Server.API.ModelBinders;
Expand Down Expand Up @@ -160,6 +161,31 @@ public ActionResult<File> GetFileByAnidbFileID([FromRoute] int anidbFileID, [Fro
return new File(HttpContext, file, includeXRefs, includeDataFrom, includeMediaInfo);
}

/// <summary>
/// Returns a file stream for the specified file ID.
/// </summary>
/// <param name="fileID">Shoko ID</param>
/// <returns>A file stream for the specified file.</returns>
[HttpGet("{fileID}/Stream")]
public ActionResult GetFileStream([FromRoute] int fileID)
{
var file = RepoFactory.VideoLocal.GetByID(fileID);
if (file == null)
return NotFound(FileNotFoundWithFileID);

var bestLocation = file.GetBestVideoLocalPlace();

var fileInfo = bestLocation.GetFile();
if (fileInfo == null)
return InternalError("Unable to find physical file for reading the stream data.");

var provider = new FileExtensionContentTypeProvider();
if (!provider.TryGetContentType(fileInfo.FullName, out var contentType))
contentType = "application/octet-stream";

return PhysicalFile(fileInfo.FullName, contentType, enableRangeProcessing: true);
}

/// <summary>
/// Get the MediaInfo model for file with VideoLocal ID
/// </summary>
Expand Down

0 comments on commit bd5b51a

Please sign in to comment.