video thumbnail generation
still not perfect actually cache thumbnail simplify media id
This commit is contained in:
@@ -24,27 +24,27 @@ public class MediaController(AobaService aobaService, ILogger<MediaController> l
|
||||
return NotFound();
|
||||
}
|
||||
var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename);
|
||||
_ = aobaService.IncrementFileViewCountAsync(id, cancellationToken);
|
||||
_ = aobaService.IncrementViewCountAsync(id, cancellationToken);
|
||||
return File(file, mime, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redirect legacy media urls to the new url
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="legacyId"></param>
|
||||
/// <param name="rest"></param>
|
||||
/// <param name="aoba"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("/i/{id}/{*rest}")]
|
||||
public async Task<IActionResult> LegacyRedirectAsync(ObjectId id, string rest, CancellationToken cancellationToken)
|
||||
[HttpGet("/i/{legacyId}/{*rest}")]
|
||||
public async Task<IActionResult> LegacyRedirectAsync(ObjectId legacyId, string rest, CancellationToken cancellationToken)
|
||||
{
|
||||
var media = await aobaService.GetMediaAsync(id, cancellationToken);
|
||||
var media = await aobaService.GetMediaFromLegacyIdAsync(legacyId, cancellationToken);
|
||||
if (media == null)
|
||||
return NotFound();
|
||||
return LocalRedirectPermanent($"/m/{media.MediaId}/{rest}");
|
||||
}
|
||||
|
||||
[HttpGet("thumb/{id}")]
|
||||
[HttpGet("{id}/thumb")]
|
||||
[ResponseCache(Duration = int.MaxValue)]
|
||||
public async Task<IActionResult> ThumbAsync(ObjectId id, [FromServices] ThumbnailService thumbnailService, [FromQuery] ThumbnailSize size = ThumbnailSize.Medium, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -57,6 +57,15 @@ public class MediaController(AobaService aobaService, ILogger<MediaController> l
|
||||
return File(thumb, "image/webp", true);
|
||||
}
|
||||
|
||||
[HttpGet("/t/{id}")]
|
||||
public async Task<IActionResult> ThumbAsync(ObjectId id, [FromServices] ThumbnailService thumbnailService, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var thumb = await thumbnailService.GetThumbnailByFileIdAsync(id, cancellationToken);
|
||||
if(thumb == null)
|
||||
return NotFound();
|
||||
return File(thumb, "image/webp", true);
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
private IActionResult DefaultThumbnailAsync()
|
||||
{
|
||||
|
||||
@@ -60,12 +60,12 @@ message UserModel {
|
||||
|
||||
message MediaModel {
|
||||
Id id = 1;
|
||||
Id mediaId = 2;
|
||||
string fileName = 3;
|
||||
MediaType mediaType = 4;
|
||||
string ext = 5;
|
||||
int32 viewCount = 6;
|
||||
Id owner = 7;
|
||||
string fileName = 2;
|
||||
MediaType mediaType = 3;
|
||||
string ext = 4;
|
||||
int32 viewCount = 5;
|
||||
Id owner = 6;
|
||||
string thumbUrl = 7;
|
||||
}
|
||||
|
||||
enum MediaType {
|
||||
|
||||
@@ -20,7 +20,7 @@ public class AobaRpcService(AobaService aobaService, AccountsService accountsSer
|
||||
{
|
||||
public override async Task<MediaResponse> GetMedia(Id request, ServerCallContext context)
|
||||
{
|
||||
var media = await aobaService.GetMediaAsync(request.ToObjectId());
|
||||
var media = await aobaService.GetMediaFromLegacyIdAsync(request.ToObjectId());
|
||||
return media.ToResponse();
|
||||
}
|
||||
|
||||
|
||||
@@ -44,15 +44,18 @@ public static class ProtoExtensions
|
||||
|
||||
public static MediaModel ToMediaModel(this Media media)
|
||||
{
|
||||
var thumbUrl = $"/m/{media.MediaId}/thumb?size={ThumbnailSize.Medium}";
|
||||
if (media.Thumbnails.TryGetValue(ThumbnailSize.Medium, out var thumb))
|
||||
thumbUrl = $"/t/{thumb}";
|
||||
return new MediaModel()
|
||||
{
|
||||
Ext = media.Ext,
|
||||
FileName = media.Filename,
|
||||
Id = media.Id.ToId(),
|
||||
MediaId = media.MediaId.ToId(),
|
||||
Id = media.MediaId.ToId(),
|
||||
MediaType = (Aoba.RPC.MediaType)media.MediaType,
|
||||
Owner = media.Owner.ToId(),
|
||||
ViewCount = media.ViewCount,
|
||||
ThumbUrl = thumbUrl,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user