Skip to content

Commit bd5b51a

Browse files
committed
feat: add v3 file stream endpoint
1 parent 952110e commit bd5b51a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Shoko.Server/API/v3/Controllers/FileController.cs

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text.RegularExpressions;
66
using Microsoft.AspNetCore.Authorization;
77
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.AspNetCore.StaticFiles;
89
using Shoko.Models.Enums;
910
using Shoko.Server.API.Annotations;
1011
using Shoko.Server.API.ModelBinders;
@@ -160,6 +161,31 @@ public ActionResult<File> GetFileByAnidbFileID([FromRoute] int anidbFileID, [Fro
160161
return new File(HttpContext, file, includeXRefs, includeDataFrom, includeMediaInfo);
161162
}
162163

164+
/// <summary>
165+
/// Returns a file stream for the specified file ID.
166+
/// </summary>
167+
/// <param name="fileID">Shoko ID</param>
168+
/// <returns>A file stream for the specified file.</returns>
169+
[HttpGet("{fileID}/Stream")]
170+
public ActionResult GetFileStream([FromRoute] int fileID)
171+
{
172+
var file = RepoFactory.VideoLocal.GetByID(fileID);
173+
if (file == null)
174+
return NotFound(FileNotFoundWithFileID);
175+
176+
var bestLocation = file.GetBestVideoLocalPlace();
177+
178+
var fileInfo = bestLocation.GetFile();
179+
if (fileInfo == null)
180+
return InternalError("Unable to find physical file for reading the stream data.");
181+
182+
var provider = new FileExtensionContentTypeProvider();
183+
if (!provider.TryGetContentType(fileInfo.FullName, out var contentType))
184+
contentType = "application/octet-stream";
185+
186+
return PhysicalFile(fileInfo.FullName, contentType, enableRangeProcessing: true);
187+
}
188+
163189
/// <summary>
164190
/// Get the MediaInfo model for file with VideoLocal ID
165191
/// </summary>

0 commit comments

Comments
 (0)