video thumbnail generation
still not perfect actually cache thumbnail simplify media id
This commit is contained in:
@@ -12,12 +12,11 @@ pub fn MediaItem(props: MediaItemProps) -> Element {
|
|||||||
if let Some(item) = props.item {
|
if let Some(item) = props.item {
|
||||||
let mtype = item.media_type().as_str_name();
|
let mtype = item.media_type().as_str_name();
|
||||||
let filename = item.file_name;
|
let filename = item.file_name;
|
||||||
let id = item.media_id.unwrap().value;
|
let id = item.id.unwrap().value;
|
||||||
|
let thumb = item.thumb_url;
|
||||||
let src = format!("{HOST}/m/thumb/{id}");
|
|
||||||
return rsx! {
|
return rsx! {
|
||||||
a { class: "mediaItem", href: "{HOST}/m/{id}", target: "_blank",
|
a { class: "mediaItem", href: "{HOST}/m/{id}", target: "_blank",
|
||||||
img { src }
|
img { src: "{HOST}{thumb}" }
|
||||||
span { class: "info",
|
span { class: "info",
|
||||||
span { class: "name", "{filename}" }
|
span { class: "name", "{filename}" }
|
||||||
span { class: "details",
|
span { class: "details",
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace AobaCore.Models;
|
|||||||
public class Media
|
public class Media
|
||||||
{
|
{
|
||||||
[BsonId]
|
[BsonId]
|
||||||
public ObjectId Id { get; set; }
|
public ObjectId LegacyId { get; set; }
|
||||||
public ObjectId MediaId { get; set; }
|
public ObjectId MediaId { get; set; }
|
||||||
public string Filename { get; set; }
|
public string Filename { get; set; }
|
||||||
public MediaType MediaType { get; set; }
|
public MediaType MediaType { get; set; }
|
||||||
@@ -16,6 +16,7 @@ public class Media
|
|||||||
public ObjectId Owner { get; set; }
|
public ObjectId Owner { get; set; }
|
||||||
public DateTime UploadDate { get; set; }
|
public DateTime UploadDate { get; set; }
|
||||||
public string[] Tags { get; set; } = [];
|
public string[] Tags { get; set; } = [];
|
||||||
|
public Dictionary<ThumbnailSize, ObjectId> Thumbnails { get; set; } = [];
|
||||||
|
|
||||||
|
|
||||||
public static readonly Dictionary<string, MediaType> KnownTypes = new()
|
public static readonly Dictionary<string, MediaType> KnownTypes = new()
|
||||||
@@ -65,7 +66,7 @@ public class Media
|
|||||||
Filename = filename;
|
Filename = filename;
|
||||||
MediaId = fileId;
|
MediaId = fileId;
|
||||||
Owner = owner;
|
Owner = owner;
|
||||||
Id = ObjectId.GenerateNewId();
|
LegacyId = ObjectId.GenerateNewId();
|
||||||
UploadDate = DateTime.UtcNow;
|
UploadDate = DateTime.UtcNow;
|
||||||
Tags = DeriveTags(filename);
|
Tags = DeriveTags(filename);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,16 @@ public class AobaIndexCreationService(IMongoDatabase db): BackgroundService
|
|||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
BsonSerializer.RegisterSerializer(new EnumSerializer<ThumbnailSize>(BsonType.String));
|
BsonSerializer.RegisterSerializer(new EnumSerializer<ThumbnailSize>(BsonType.String));
|
||||||
|
|
||||||
|
var mediaId = Builders<Media>.IndexKeys.Ascending(m => m.MediaId);
|
||||||
|
|
||||||
|
var mediaIdModel = new CreateIndexModel<Media>(mediaId, new CreateIndexOptions
|
||||||
|
{
|
||||||
|
Name = "Media",
|
||||||
|
Unique = true,
|
||||||
|
Background = true
|
||||||
|
});
|
||||||
|
|
||||||
var textKeys = Builders<Media>.IndexKeys
|
var textKeys = Builders<Media>.IndexKeys
|
||||||
.Text(m => m.Filename)
|
.Text(m => m.Filename)
|
||||||
.Text(m => m.Ext)
|
.Text(m => m.Ext)
|
||||||
@@ -27,6 +37,7 @@ public class AobaIndexCreationService(IMongoDatabase db): BackgroundService
|
|||||||
Background = true
|
Background = true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await _media.EnsureIndexAsync(mediaIdModel);
|
||||||
await _media.EnsureIndexAsync(textModel);
|
await _media.EnsureIndexAsync(textModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,9 +13,9 @@ public class AobaService(IMongoDatabase db)
|
|||||||
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
|
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
|
||||||
private readonly GridFSBucket _gridFs = new(db);
|
private readonly GridFSBucket _gridFs = new(db);
|
||||||
|
|
||||||
public async Task<Media?> GetMediaAsync(ObjectId id, CancellationToken cancellationToken = default)
|
public async Task<Media?> GetMediaFromLegacyIdAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return await _media.Find(m => m.Id == id).FirstOrDefaultAsync(cancellationToken);
|
return await _media.Find(m => m.LegacyId == id).FirstOrDefaultAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Media?> GetMediaFromFileAsync(ObjectId id, CancellationToken cancellationToken = default)
|
public async Task<Media?> GetMediaFromFileAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||||
@@ -44,14 +44,22 @@ public class AobaService(IMongoDatabase db)
|
|||||||
return _media.InsertOneAsync(media, null, cancellationToken);
|
return _media.InsertOneAsync(media, null, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task IncrementViewCountAsync(ObjectId id, CancellationToken cancellationToken = default)
|
public async Task AddThumbnailAsync(ObjectId mediaId, ObjectId thumbId, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return _media.UpdateOneAsync(m => m.Id == id, Builders<Media>.Update.Inc(m => m.ViewCount, 1), cancellationToken: cancellationToken);
|
var upate = Builders<Media>.Update.Set(m => m.Thumbnails[size], thumbId);
|
||||||
|
|
||||||
|
await _media.UpdateOneAsync(m => m.MediaId == mediaId, upate, cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task IncrementFileViewCountAsync(ObjectId fileId, CancellationToken cancellationToken = default)
|
public async Task<ObjectId> GetThumbnailIdAsync(ObjectId mediaId, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return _media.UpdateOneAsync(m => m.MediaId == fileId, Builders<Media>.Update.Inc(m => m.ViewCount, 1), cancellationToken: cancellationToken);
|
var thumb = await _media.Find(m => m.MediaId == mediaId).Project(m => m.Thumbnails[size]).FirstOrDefaultAsync(cancellationToken);
|
||||||
|
return thumb;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task IncrementViewCountAsync(ObjectId mediaId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return _media.UpdateOneAsync(m => m.MediaId == mediaId, Builders<Media>.Update.Inc(m => m.ViewCount, 1), cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -70,11 +78,11 @@ public class AobaService(IMongoDatabase db)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<MaybeEx<GridFSDownloadStream, GridFSException>> GetFileStreamAsync(ObjectId id, bool seekable = false, CancellationToken cancellationToken = default)
|
public async Task<MaybeEx<GridFSDownloadStream, GridFSException>> GetFileStreamAsync(ObjectId mediaId, bool seekable = false, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return await _gridFs.OpenDownloadStreamAsync(id, new GridFSDownloadOptions { Seekable = seekable }, cancellationToken);
|
return await _gridFs.OpenDownloadStreamAsync(mediaId, new GridFSDownloadOptions { Seekable = seekable }, cancellationToken);
|
||||||
}
|
}
|
||||||
catch (GridFSException ex)
|
catch (GridFSException ex)
|
||||||
{
|
{
|
||||||
@@ -82,13 +90,13 @@ public class AobaService(IMongoDatabase db)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteFileAsync(ObjectId fileId, CancellationToken cancellationToken = default)
|
public async Task DeleteFileAsync(ObjectId mediaId, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
await _gridFs.DeleteAsync(fileId, CancellationToken.None);
|
await _gridFs.DeleteAsync(mediaId, CancellationToken.None);
|
||||||
await _media.DeleteOneAsync(m => m.MediaId == fileId, CancellationToken.None);
|
await _media.DeleteOneAsync(m => m.MediaId == mediaId, CancellationToken.None);
|
||||||
}
|
}
|
||||||
catch (GridFSFileNotFoundException)
|
catch (GridFSFileNotFoundException)
|
||||||
{
|
{
|
||||||
@@ -96,6 +104,8 @@ public class AobaService(IMongoDatabase db)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public async Task DeriveTagsAsync(CancellationToken cancellationToken = default)
|
public async Task DeriveTagsAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var mediaItems = await _media.Find(Builders<Media>.Filter.Exists(m => m.Tags, false))
|
var mediaItems = await _media.Find(Builders<Media>.Filter.Exists(m => m.Tags, false))
|
||||||
@@ -104,7 +114,7 @@ public class AobaService(IMongoDatabase db)
|
|||||||
foreach (var mediaItem in mediaItems)
|
foreach (var mediaItem in mediaItems)
|
||||||
{
|
{
|
||||||
mediaItem.Tags = Media.DeriveTags(mediaItem.Filename);
|
mediaItem.Tags = Media.DeriveTags(mediaItem.Filename);
|
||||||
await _media.UpdateOneAsync(m => m.Id == mediaItem.Id, Builders<Media>.Update.Set(m => m.Tags, mediaItem.Tags), null, cancellationToken);
|
await _media.UpdateOneAsync(m => m.MediaId == mediaItem.MediaId, Builders<Media>.Update.Set(m => m.Tags, mediaItem.Tags), null, cancellationToken);
|
||||||
}
|
}
|
||||||
Console.WriteLine("All Tags Derived");
|
Console.WriteLine("All Tags Derived");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,33 +20,32 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace AobaCore.Services;
|
namespace AobaCore.Services;
|
||||||
|
|
||||||
public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
||||||
{
|
{
|
||||||
private readonly GridFSBucket _gridfs = new GridFSBucket(db);
|
private readonly GridFSBucket _gridfs = new GridFSBucket(db);
|
||||||
private readonly IMongoCollection<MediaThumbnail> _thumbnails = db.GetCollection<MediaThumbnail>("thumbs");
|
private Lock _lock = new();
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">File id</param>
|
/// <param name="mediaId">Media id</param>
|
||||||
/// <param name="size"></param>
|
/// <param name="size"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<Maybe<Stream>> GetOrCreateThumbnailAsync(ObjectId id, ThumbnailSize size, CancellationToken cancellationToken = default)
|
public async Task<Maybe<Stream>> GetOrCreateThumbnailAsync(ObjectId mediaId, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var existingThumb = await GetThumbnailAsync(id, size, cancellationToken);
|
var existingThumb = await GetThumbnailAsync(mediaId, size, cancellationToken);
|
||||||
if (existingThumb != null)
|
if (existingThumb != null)
|
||||||
return existingThumb;
|
return existingThumb;
|
||||||
|
|
||||||
var media = await aobaService.GetMediaFromFileAsync(id, cancellationToken);
|
var media = await aobaService.GetMediaFromFileAsync(mediaId, cancellationToken);
|
||||||
|
|
||||||
if (media == null)
|
if (media == null)
|
||||||
return new Error("Media does not exist");
|
return new Error("Media does not exist");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
using var mediaData = await _gridfs.OpenDownloadStreamAsync(media.MediaId, new GridFSDownloadOptions { Seekable = true }, cancellationToken);
|
using var mediaData = await _gridfs.OpenDownloadStreamAsync(media.MediaId, new GridFSDownloadOptions { Seekable = true }, cancellationToken);
|
||||||
var thumb = await GenerateThumbnailAsync(mediaData, size, media.MediaType, media.Ext, cancellationToken);
|
var thumb = await GenerateThumbnailAsync(mediaData, size, media.MediaType, media.Ext, cancellationToken);
|
||||||
|
|
||||||
@@ -54,59 +53,58 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
|||||||
return thumb.Error;
|
return thumb.Error;
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
#if !DEBUG
|
|
||||||
var thumbId = await _gridfs.UploadFromStreamAsync($"{media.Filename}.webp", thumb, cancellationToken: CancellationToken.None);
|
var thumbId = await _gridfs.UploadFromStreamAsync($"{media.Filename}.webp", thumb, cancellationToken: CancellationToken.None);
|
||||||
var update = Builders<MediaThumbnail>.Update.Set(t => t.Sizes[size], thumbId);
|
await aobaService.AddThumbnailAsync(mediaId, thumbId, size, cancellationToken);
|
||||||
await _thumbnails.UpdateOneAsync(t => t.Id == id, update, cancellationToken: CancellationToken.None);
|
|
||||||
#endif
|
|
||||||
thumb.Value.Position = 0;
|
thumb.Value.Position = 0;
|
||||||
return thumb;
|
return thumb;
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
return ex;
|
return ex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">File Id</param>
|
/// <param name="mediaId">Media Id</param>
|
||||||
/// <param name="size"></param>
|
/// <param name="size"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<Stream?> GetThumbnailAsync(ObjectId id, ThumbnailSize size, CancellationToken cancellationToken = default)
|
public async Task<Stream?> GetThumbnailAsync(ObjectId mediaId, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var thumb = await _thumbnails.Find(t => t.Id == id).FirstOrDefaultAsync(cancellationToken);
|
var thumb = await aobaService.GetThumbnailIdAsync(mediaId, size, cancellationToken);
|
||||||
if (thumb == null)
|
if (thumb == default)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (!thumb.Sizes.TryGetValue(size, out var tid))
|
var thumbData = await _gridfs.OpenDownloadStreamAsync(thumb, cancellationToken: cancellationToken);
|
||||||
return null;
|
|
||||||
|
|
||||||
var thumbData = await _gridfs.OpenDownloadStreamAsync(tid, cancellationToken: cancellationToken);
|
|
||||||
return thumbData;
|
return thumbData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Stream?> GetThumbnailByFileIdAsync(ObjectId thumbId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var thumbData = await _gridfs.OpenDownloadStreamAsync(thumbId, cancellationToken: cancellationToken);
|
||||||
|
return thumbData;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<Maybe<Stream>> GenerateThumbnailAsync(Stream stream, ThumbnailSize size, MediaType type, string ext, CancellationToken cancellationToken = default)
|
public async Task<Maybe<Stream>> GenerateThumbnailAsync(Stream stream, ThumbnailSize size, MediaType type, string ext, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
return type switch
|
return type switch
|
||||||
{
|
{
|
||||||
MediaType.Image => await GenerateImageThumbnailAsync(stream, size, cancellationToken),
|
MediaType.Image => await GenerateImageThumbnailAsync(stream, size, cancellationToken),
|
||||||
MediaType.Video => await GenerateVideoThumbnailAsync(stream, size, cancellationToken),
|
MediaType.Video => GenerateVideoThumbnail(stream, size, cancellationToken),
|
||||||
MediaType.Text or MediaType.Code => await GenerateDocumentThumbnailAsync(stream, size, cancellationToken),
|
MediaType.Text or MediaType.Code => await GenerateDocumentThumbnailAsync(stream, size, cancellationToken),
|
||||||
_ => new Error($"No Thumbnail for {type}"),
|
_ => new Error($"No Thumbnail for {type}"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Stream> GenerateImageThumbnailAsync(Stream stream, ThumbnailSize size, CancellationToken cancellationToken = default)
|
public static async Task<Stream> GenerateImageThumbnailAsync(Stream stream, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var img = Image.Load(stream);
|
var img = Image.Load(stream);
|
||||||
img.Mutate(o =>
|
img.Mutate(o =>
|
||||||
{
|
{
|
||||||
var size =
|
var size =
|
||||||
o.Resize(new ResizeOptions
|
o.Resize(new ResizeOptions
|
||||||
{
|
{
|
||||||
Position = AnchorPositionMode.Center,
|
Position = AnchorPositionMode.Center,
|
||||||
@@ -120,21 +118,22 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Maybe<Stream>> GenerateVideoThumbnailAsync(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default)
|
public Maybe<Stream> GenerateVideoThumbnail(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var w = (int)size;
|
var w = (int)size;
|
||||||
var source = new MemoryStream();
|
var source = new MemoryStream();
|
||||||
data.CopyTo(source);
|
data.CopyTo(source);
|
||||||
source.Position = 0;
|
source.Position = 0;
|
||||||
var output = new MemoryStream();
|
var output = new MemoryStream();
|
||||||
await FFMpegArguments.FromPipeInput(new StreamPipeSource(source))
|
FFMpegArguments.FromPipeInput(new StreamPipeSource(source), opt =>
|
||||||
.OutputToPipe(new StreamPipeSink(output), opt =>
|
{
|
||||||
{
|
opt.WithCustomArgument("-t 5");
|
||||||
opt.WithCustomArgument($"-t 5 -vf \"crop='min(in_w,in_h)':'min(in_w,in_h)',scale={w}:{w}\" -loop 0")
|
}).OutputToPipe(new StreamPipeSink(output), opt =>
|
||||||
.ForceFormat("webp");
|
{
|
||||||
}).ProcessAsynchronously();
|
opt.WithCustomArgument($"-vf \"crop='min(in_w,in_h)':'min(in_w,in_h)',scale={w}:{w}\" -loop 0 -r 15")
|
||||||
|
.ForceFormat("webp");
|
||||||
|
}).ProcessSynchronously();
|
||||||
output.Position = 0;
|
output.Position = 0;
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,4 +141,4 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
|||||||
{
|
{
|
||||||
return new NotImplementedException();
|
return new NotImplementedException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,27 +24,27 @@ public class MediaController(AobaService aobaService, ILogger<MediaController> l
|
|||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename);
|
var mime = MimeTypesMap.GetMimeType(file.Value.FileInfo.Filename);
|
||||||
_ = aobaService.IncrementFileViewCountAsync(id, cancellationToken);
|
_ = aobaService.IncrementViewCountAsync(id, cancellationToken);
|
||||||
return File(file, mime, true);
|
return File(file, mime, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Redirect legacy media urls to the new url
|
/// Redirect legacy media urls to the new url
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="legacyId"></param>
|
||||||
/// <param name="rest"></param>
|
/// <param name="rest"></param>
|
||||||
/// <param name="aoba"></param>
|
/// <param name="aoba"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("/i/{id}/{*rest}")]
|
[HttpGet("/i/{legacyId}/{*rest}")]
|
||||||
public async Task<IActionResult> LegacyRedirectAsync(ObjectId id, string rest, CancellationToken cancellationToken)
|
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)
|
if (media == null)
|
||||||
return NotFound();
|
return NotFound();
|
||||||
return LocalRedirectPermanent($"/m/{media.MediaId}/{rest}");
|
return LocalRedirectPermanent($"/m/{media.MediaId}/{rest}");
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("thumb/{id}")]
|
[HttpGet("{id}/thumb")]
|
||||||
[ResponseCache(Duration = int.MaxValue)]
|
[ResponseCache(Duration = int.MaxValue)]
|
||||||
public async Task<IActionResult> ThumbAsync(ObjectId id, [FromServices] ThumbnailService thumbnailService, [FromQuery] ThumbnailSize size = ThumbnailSize.Medium, CancellationToken cancellationToken = default)
|
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);
|
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]
|
[NonAction]
|
||||||
private IActionResult DefaultThumbnailAsync()
|
private IActionResult DefaultThumbnailAsync()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -60,12 +60,12 @@ message UserModel {
|
|||||||
|
|
||||||
message MediaModel {
|
message MediaModel {
|
||||||
Id id = 1;
|
Id id = 1;
|
||||||
Id mediaId = 2;
|
string fileName = 2;
|
||||||
string fileName = 3;
|
MediaType mediaType = 3;
|
||||||
MediaType mediaType = 4;
|
string ext = 4;
|
||||||
string ext = 5;
|
int32 viewCount = 5;
|
||||||
int32 viewCount = 6;
|
Id owner = 6;
|
||||||
Id owner = 7;
|
string thumbUrl = 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum MediaType {
|
enum MediaType {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class AobaRpcService(AobaService aobaService, AccountsService accountsSer
|
|||||||
{
|
{
|
||||||
public override async Task<MediaResponse> GetMedia(Id request, ServerCallContext context)
|
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();
|
return media.ToResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,15 +44,18 @@ public static class ProtoExtensions
|
|||||||
|
|
||||||
public static MediaModel ToMediaModel(this Media media)
|
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()
|
return new MediaModel()
|
||||||
{
|
{
|
||||||
Ext = media.Ext,
|
Ext = media.Ext,
|
||||||
FileName = media.Filename,
|
FileName = media.Filename,
|
||||||
Id = media.Id.ToId(),
|
Id = media.MediaId.ToId(),
|
||||||
MediaId = media.MediaId.ToId(),
|
|
||||||
MediaType = (Aoba.RPC.MediaType)media.MediaType,
|
MediaType = (Aoba.RPC.MediaType)media.MediaType,
|
||||||
Owner = media.Owner.ToId(),
|
Owner = media.Owner.ToId(),
|
||||||
ViewCount = media.ViewCount,
|
ViewCount = media.ViewCount,
|
||||||
|
ThumbUrl = thumbUrl,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user