generate thumbnails
This commit is contained in:
@@ -12,8 +12,9 @@
|
||||
<PackageReference Include="MaybeError" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.5" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="3.4.0" />
|
||||
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="2.0.0" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.10.0" />
|
||||
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="2.1.0" />
|
||||
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.6" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.11.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -18,7 +18,12 @@ public class AobaService(IMongoDatabase db)
|
||||
return await _media.Find(m => m.Id == id).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<PagedResult<Media>> FindMediaAsync(string? query, int page = 1, int pageSize = 100)
|
||||
public async Task<Media?> GetMediaFromFileAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _media.Find(m => m.MediaId == id).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<PagedResult<Media>> FindMediaAsync(string? query, int page = 1, int pageSize = 100)
|
||||
{
|
||||
var filter = string.IsNullOrWhiteSpace(query) ? "{}" : Builders<Media>.Filter.Text(query);
|
||||
var sort = Builders<Media>.Sort.Descending(m => m.UploadDate);
|
||||
|
||||
@@ -4,6 +4,9 @@ using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.GridFS;
|
||||
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -11,18 +14,42 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
internal class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
||||
public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
||||
{
|
||||
private readonly GridFSBucket _gridfs = new GridFSBucket(db);
|
||||
private readonly IMongoCollection<MeidaThumbnail> _thumbnails = db.GetCollection<MeidaThumbnail>("thumbs");
|
||||
|
||||
public async Task<MemoryStream> GetThumbnailAsync(ObjectId id)
|
||||
public async Task<Stream?> GetThumbnailAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var media = await aobaService.GetMediaAsync(id);
|
||||
if (media == null)
|
||||
return null;
|
||||
if (media.MediaType != MediaType.Image)
|
||||
return null;
|
||||
using var file = await _gridfs.OpenDownloadStreamAsync(media.MediaId, new GridFSDownloadOptions { Seekable = true });
|
||||
return await GenerateThumbnailAsync(file, cancellationToken);
|
||||
}
|
||||
public async Task<Stream?> GetThumbnailFromFileAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var media = await aobaService.GetMediaFromFileAsync(id);
|
||||
if (media == null)
|
||||
return null;
|
||||
if (media.MediaType != MediaType.Image)
|
||||
return null;
|
||||
using var file = await _gridfs.OpenDownloadStreamAsync(media.MediaId, new GridFSDownloadOptions { Seekable = true });
|
||||
return await GenerateThumbnailAsync(file, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task GenerateThumbnailAsync(ObjectId id)
|
||||
public async Task<Stream> GenerateThumbnailAsync(Stream stream, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
var img = Image.Load(stream);
|
||||
img.Mutate(o =>
|
||||
{
|
||||
o.Resize(200, 200);
|
||||
});
|
||||
var result = new MemoryStream();
|
||||
await img.SaveAsWebpAsync(result, cancellationToken);
|
||||
result.Position = 0;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user