JJ Colocate
This commit is contained in:
@@ -1,66 +1,66 @@
|
||||
using AobaCore.Models;
|
||||
|
||||
using Isopoh.Cryptography.Argon2;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
public class AccountsService(IMongoDatabase db)
|
||||
{
|
||||
public readonly IMongoCollection<User> _users = db.GetCollection<User>("users");
|
||||
|
||||
public async Task<User?> GetUserAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _users.Find(u => u.Id == id).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<User?> VerifyLoginAsync(string username, string password, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await _users.Find(u => u.Username == username).FirstOrDefaultAsync(cancellationToken);
|
||||
if(user == null)
|
||||
return null;
|
||||
|
||||
if(user.IsArgon && Argon2.Verify(user.PasswordHash, password))
|
||||
return user;
|
||||
|
||||
if(LegacyVerifyPassword( password, user.PasswordHash))
|
||||
{
|
||||
#if !DEBUG
|
||||
var argon2Hash = Argon2.Hash(password);
|
||||
var update = Builders<User>.Update.Set(u => u.PasswordHash, argon2Hash).Set(u => u.IsArgon, true);
|
||||
await _users.UpdateOneAsync(u => u.Id == user.Id, update, cancellationToken: cancellationToken);
|
||||
#endif
|
||||
return user;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static bool LegacyVerifyPassword(string password, string passwordHash)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(passwordHash))
|
||||
return false;
|
||||
/* Extract the bytes */
|
||||
byte[] hashBytes = Convert.FromBase64String(passwordHash);
|
||||
/* Get the salt */
|
||||
byte[] salt = new byte[16];
|
||||
Array.Copy(hashBytes, 0, salt, 0, 16);
|
||||
/* Compute the hash on the password the user entered */
|
||||
var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 10000, HashAlgorithmName.SHA1);
|
||||
byte[] hash = pbkdf2.GetBytes(20);
|
||||
/* Compare the results */
|
||||
for (int i = 0; i < 20; i++)
|
||||
if (hashBytes[i + 16] != hash[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
using AobaCore.Models;
|
||||
|
||||
using Isopoh.Cryptography.Argon2;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
public class AccountsService(IMongoDatabase db)
|
||||
{
|
||||
public readonly IMongoCollection<User> _users = db.GetCollection<User>("users");
|
||||
|
||||
public async Task<User?> GetUserAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _users.Find(u => u.Id == id).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<User?> VerifyLoginAsync(string username, string password, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await _users.Find(u => u.Username == username).FirstOrDefaultAsync(cancellationToken);
|
||||
if(user == null)
|
||||
return null;
|
||||
|
||||
if(user.IsArgon && Argon2.Verify(user.PasswordHash, password))
|
||||
return user;
|
||||
|
||||
if(LegacyVerifyPassword( password, user.PasswordHash))
|
||||
{
|
||||
#if !DEBUG
|
||||
var argon2Hash = Argon2.Hash(password);
|
||||
var update = Builders<User>.Update.Set(u => u.PasswordHash, argon2Hash).Set(u => u.IsArgon, true);
|
||||
await _users.UpdateOneAsync(u => u.Id == user.Id, update, cancellationToken: cancellationToken);
|
||||
#endif
|
||||
return user;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static bool LegacyVerifyPassword(string password, string passwordHash)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(passwordHash))
|
||||
return false;
|
||||
/* Extract the bytes */
|
||||
byte[] hashBytes = Convert.FromBase64String(passwordHash);
|
||||
/* Get the salt */
|
||||
byte[] salt = new byte[16];
|
||||
Array.Copy(hashBytes, 0, salt, 0, 16);
|
||||
/* Compute the hash on the password the user entered */
|
||||
var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 10000, HashAlgorithmName.SHA1);
|
||||
byte[] hash = pbkdf2.GetBytes(20);
|
||||
/* Compare the results */
|
||||
for (int i = 0; i < 20; i++)
|
||||
if (hashBytes[i + 16] != hash[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,30 @@
|
||||
using AobaCore.Models;
|
||||
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Bson.Serialization.Serializers;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
|
||||
public class AobaIndexCreationService(IMongoDatabase db): BackgroundService
|
||||
{
|
||||
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
BsonSerializer.RegisterSerializer(new EnumSerializer<ThumbnailSize>(BsonType.String));
|
||||
var textKeys = Builders<Media>.IndexKeys
|
||||
.Text(m => m.Filename);
|
||||
|
||||
var textModel = new CreateIndexModel<Media>(textKeys, new CreateIndexOptions
|
||||
{
|
||||
Name = "Text",
|
||||
Background = true
|
||||
});
|
||||
|
||||
await _media.EnsureIndexAsync(textModel);
|
||||
}
|
||||
using AobaCore.Models;
|
||||
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Bson.Serialization.Serializers;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
|
||||
public class AobaIndexCreationService(IMongoDatabase db): BackgroundService
|
||||
{
|
||||
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
BsonSerializer.RegisterSerializer(new EnumSerializer<ThumbnailSize>(BsonType.String));
|
||||
var textKeys = Builders<Media>.IndexKeys
|
||||
.Text(m => m.Filename);
|
||||
|
||||
var textModel = new CreateIndexModel<Media>(textKeys, new CreateIndexOptions
|
||||
{
|
||||
Name = "Text",
|
||||
Background = true
|
||||
});
|
||||
|
||||
await _media.EnsureIndexAsync(textModel);
|
||||
}
|
||||
}
|
||||
@@ -1,98 +1,98 @@
|
||||
using AobaCore.Models;
|
||||
|
||||
using MaybeError.Errors;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.GridFS;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
|
||||
public class AobaService(IMongoDatabase db)
|
||||
{
|
||||
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
|
||||
private readonly GridFSBucket _gridFs = new(db);
|
||||
|
||||
public async Task<Media?> GetMediaAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _media.Find(m => m.Id == id).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
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, ObjectId userId, int page = 1, int pageSize = 100)
|
||||
{
|
||||
var filter = Builders<Media>.Filter.And([
|
||||
string.IsNullOrWhiteSpace(query) ? "{}" : Builders<Media>.Filter.Text(query),
|
||||
Builders<Media>.Filter.Eq(m => m.Owner, userId)
|
||||
]);
|
||||
var sort = Builders<Media>.Sort.Descending(m => m.UploadDate);
|
||||
var find = _media.Find(filter);
|
||||
|
||||
var total = await find.CountDocumentsAsync();
|
||||
page -= 1;
|
||||
var items = await find.Sort(sort).Skip(page * pageSize).Limit(pageSize).ToListAsync();
|
||||
return new PagedResult<Media>(items, page, pageSize, total);
|
||||
}
|
||||
|
||||
|
||||
public Task AddMediaAsync(Media media, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _media.InsertOneAsync(media, null, cancellationToken);
|
||||
}
|
||||
|
||||
public Task IncrementViewCountAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _media.UpdateOneAsync(m => m.Id == id, Builders<Media>.Update.Inc(m => m.ViewCount, 1), cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public Task IncrementFileViewCountAsync(ObjectId fileId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _media.UpdateOneAsync(m => m.MediaId == fileId, Builders<Media>.Update.Inc(m => m.ViewCount, 1), cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
public async Task<Maybe<Media>> UploadFileAsync(Stream data, string filename, ObjectId owner, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileId = await _gridFs.UploadFromStreamAsync(filename, data, cancellationToken: cancellationToken);
|
||||
var media = new Media(fileId, filename, owner);
|
||||
await AddMediaAsync(media, cancellationToken);
|
||||
return media;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MaybeEx<GridFSDownloadStream, GridFSException>> GetFileStreamAsync(ObjectId id, bool seekable = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _gridFs.OpenDownloadStreamAsync(id, new GridFSDownloadOptions { Seekable = seekable }, cancellationToken);
|
||||
}
|
||||
catch (GridFSException ex)
|
||||
{
|
||||
return ex;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteFileAsync(ObjectId fileId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await _gridFs.DeleteAsync(fileId, CancellationToken.None);
|
||||
await _media.DeleteOneAsync(m => m.MediaId == fileId, CancellationToken.None);
|
||||
}
|
||||
catch (GridFSFileNotFoundException)
|
||||
{
|
||||
//ignore if file was not found
|
||||
}
|
||||
}
|
||||
}
|
||||
using AobaCore.Models;
|
||||
|
||||
using MaybeError.Errors;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.GridFS;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
|
||||
public class AobaService(IMongoDatabase db)
|
||||
{
|
||||
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
|
||||
private readonly GridFSBucket _gridFs = new(db);
|
||||
|
||||
public async Task<Media?> GetMediaAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _media.Find(m => m.Id == id).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
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, ObjectId userId, int page = 1, int pageSize = 100)
|
||||
{
|
||||
var filter = Builders<Media>.Filter.And([
|
||||
string.IsNullOrWhiteSpace(query) ? "{}" : Builders<Media>.Filter.Text(query),
|
||||
Builders<Media>.Filter.Eq(m => m.Owner, userId)
|
||||
]);
|
||||
var sort = Builders<Media>.Sort.Descending(m => m.UploadDate);
|
||||
var find = _media.Find(filter);
|
||||
|
||||
var total = await find.CountDocumentsAsync();
|
||||
page -= 1;
|
||||
var items = await find.Sort(sort).Skip(page * pageSize).Limit(pageSize).ToListAsync();
|
||||
return new PagedResult<Media>(items, page, pageSize, total);
|
||||
}
|
||||
|
||||
|
||||
public Task AddMediaAsync(Media media, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _media.InsertOneAsync(media, null, cancellationToken);
|
||||
}
|
||||
|
||||
public Task IncrementViewCountAsync(ObjectId id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _media.UpdateOneAsync(m => m.Id == id, Builders<Media>.Update.Inc(m => m.ViewCount, 1), cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public Task IncrementFileViewCountAsync(ObjectId fileId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _media.UpdateOneAsync(m => m.MediaId == fileId, Builders<Media>.Update.Inc(m => m.ViewCount, 1), cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
public async Task<Maybe<Media>> UploadFileAsync(Stream data, string filename, ObjectId owner, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileId = await _gridFs.UploadFromStreamAsync(filename, data, cancellationToken: cancellationToken);
|
||||
var media = new Media(fileId, filename, owner);
|
||||
await AddMediaAsync(media, cancellationToken);
|
||||
return media;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ex;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<MaybeEx<GridFSDownloadStream, GridFSException>> GetFileStreamAsync(ObjectId id, bool seekable = false, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _gridFs.OpenDownloadStreamAsync(id, new GridFSDownloadOptions { Seekable = seekable }, cancellationToken);
|
||||
}
|
||||
catch (GridFSException ex)
|
||||
{
|
||||
return ex;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteFileAsync(ObjectId fileId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await _gridFs.DeleteAsync(fileId, CancellationToken.None);
|
||||
await _media.DeleteOneAsync(m => m.MediaId == fileId, CancellationToken.None);
|
||||
}
|
||||
catch (GridFSFileNotFoundException)
|
||||
{
|
||||
//ignore if file was not found
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,145 +1,145 @@
|
||||
using AobaCore.Models;
|
||||
|
||||
using FFMpegCore;
|
||||
using FFMpegCore.Pipes;
|
||||
|
||||
using MaybeError.Errors;
|
||||
|
||||
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;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
||||
{
|
||||
private readonly GridFSBucket _gridfs = new GridFSBucket(db);
|
||||
private readonly IMongoCollection<MediaThumbnail> _thumbnails = db.GetCollection<MediaThumbnail>("thumbs");
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="id">File id</param>
|
||||
/// <param name="size"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Maybe<Stream>> GetOrCreateThumbnailAsync(ObjectId id, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var existingThumb = await GetThumbnailAsync(id, size, cancellationToken);
|
||||
if (existingThumb != null)
|
||||
return existingThumb;
|
||||
|
||||
var media = await aobaService.GetMediaFromFileAsync(id, cancellationToken);
|
||||
|
||||
if (media == null)
|
||||
return new Error("Media does not exist");
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
using var mediaData = await _gridfs.OpenDownloadStreamAsync(media.MediaId, new GridFSDownloadOptions { Seekable = true }, cancellationToken);
|
||||
var thumb = await GenerateThumbnailAsync(mediaData, size, media.MediaType, media.Ext, cancellationToken);
|
||||
|
||||
if (thumb.HasError)
|
||||
return thumb.Error;
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
#if !DEBUG
|
||||
var thumbId = await _gridfs.UploadFromStreamAsync($"{media.Filename}.webp", thumb, cancellationToken: CancellationToken.None);
|
||||
var update = Builders<MediaThumbnail>.Update.Set(t => t.Sizes[size], thumbId);
|
||||
await _thumbnails.UpdateOneAsync(t => t.Id == id, update, cancellationToken: CancellationToken.None);
|
||||
#endif
|
||||
thumb.Value.Position = 0;
|
||||
return thumb;
|
||||
} catch (Exception ex) {
|
||||
return ex;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="id">File Id</param>
|
||||
/// <param name="size"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Stream?> GetThumbnailAsync(ObjectId id, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var thumb = await _thumbnails.Find(t => t.Id == id).FirstOrDefaultAsync(cancellationToken);
|
||||
if (thumb == null)
|
||||
return null;
|
||||
|
||||
if (!thumb.Sizes.TryGetValue(size, out var tid))
|
||||
return null;
|
||||
|
||||
var thumbData = await _gridfs.OpenDownloadStreamAsync(tid, cancellationToken: cancellationToken);
|
||||
return thumbData;
|
||||
}
|
||||
|
||||
|
||||
public async Task<Maybe<Stream>> GenerateThumbnailAsync(Stream stream, ThumbnailSize size, MediaType type, string ext, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
MediaType.Image => await GenerateImageThumbnailAsync(stream, size, cancellationToken),
|
||||
MediaType.Video => await GenerateVideoThumbnailAsync(stream, size, cancellationToken),
|
||||
MediaType.Text or MediaType.Code => await GenerateDocumentThumbnailAsync(stream, size, cancellationToken),
|
||||
_ => new Error($"No Thumbnail for {type}"),
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Stream> GenerateImageThumbnailAsync(Stream stream, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var img = Image.Load(stream);
|
||||
img.Mutate(o =>
|
||||
{
|
||||
var size =
|
||||
o.Resize(new ResizeOptions
|
||||
{
|
||||
Position = AnchorPositionMode.Center,
|
||||
Mode = ResizeMode.Crop,
|
||||
Size = new Size(300, 300)
|
||||
});
|
||||
});
|
||||
var result = new MemoryStream();
|
||||
await img.SaveAsWebpAsync(result, cancellationToken);
|
||||
result.Position = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Maybe<Stream>> GenerateVideoThumbnailAsync(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var w = (int)size;
|
||||
var source = new MemoryStream();
|
||||
data.CopyTo(source);
|
||||
source.Position = 0;
|
||||
var output = new MemoryStream();
|
||||
await FFMpegArguments.FromPipeInput(new StreamPipeSource(source))
|
||||
.OutputToPipe(new StreamPipeSink(output), opt =>
|
||||
{
|
||||
opt.WithCustomArgument($"-t 5 -vf \"crop='min(in_w,in_h)':'min(in_w,in_h)',scale={w}:{w}\" -loop 0")
|
||||
.ForceFormat("webp");
|
||||
}).ProcessAsynchronously();
|
||||
output.Position = 0;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public async Task<Maybe<Stream>> GenerateDocumentThumbnailAsync(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new NotImplementedException();
|
||||
}
|
||||
}
|
||||
using AobaCore.Models;
|
||||
|
||||
using FFMpegCore;
|
||||
using FFMpegCore.Pipes;
|
||||
|
||||
using MaybeError.Errors;
|
||||
|
||||
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;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
||||
{
|
||||
private readonly GridFSBucket _gridfs = new GridFSBucket(db);
|
||||
private readonly IMongoCollection<MediaThumbnail> _thumbnails = db.GetCollection<MediaThumbnail>("thumbs");
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="id">File id</param>
|
||||
/// <param name="size"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Maybe<Stream>> GetOrCreateThumbnailAsync(ObjectId id, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var existingThumb = await GetThumbnailAsync(id, size, cancellationToken);
|
||||
if (existingThumb != null)
|
||||
return existingThumb;
|
||||
|
||||
var media = await aobaService.GetMediaFromFileAsync(id, cancellationToken);
|
||||
|
||||
if (media == null)
|
||||
return new Error("Media does not exist");
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
using var mediaData = await _gridfs.OpenDownloadStreamAsync(media.MediaId, new GridFSDownloadOptions { Seekable = true }, cancellationToken);
|
||||
var thumb = await GenerateThumbnailAsync(mediaData, size, media.MediaType, media.Ext, cancellationToken);
|
||||
|
||||
if (thumb.HasError)
|
||||
return thumb.Error;
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
#if !DEBUG
|
||||
var thumbId = await _gridfs.UploadFromStreamAsync($"{media.Filename}.webp", thumb, cancellationToken: CancellationToken.None);
|
||||
var update = Builders<MediaThumbnail>.Update.Set(t => t.Sizes[size], thumbId);
|
||||
await _thumbnails.UpdateOneAsync(t => t.Id == id, update, cancellationToken: CancellationToken.None);
|
||||
#endif
|
||||
thumb.Value.Position = 0;
|
||||
return thumb;
|
||||
} catch (Exception ex) {
|
||||
return ex;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="id">File Id</param>
|
||||
/// <param name="size"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Stream?> GetThumbnailAsync(ObjectId id, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var thumb = await _thumbnails.Find(t => t.Id == id).FirstOrDefaultAsync(cancellationToken);
|
||||
if (thumb == null)
|
||||
return null;
|
||||
|
||||
if (!thumb.Sizes.TryGetValue(size, out var tid))
|
||||
return null;
|
||||
|
||||
var thumbData = await _gridfs.OpenDownloadStreamAsync(tid, cancellationToken: cancellationToken);
|
||||
return thumbData;
|
||||
}
|
||||
|
||||
|
||||
public async Task<Maybe<Stream>> GenerateThumbnailAsync(Stream stream, ThumbnailSize size, MediaType type, string ext, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
MediaType.Image => await GenerateImageThumbnailAsync(stream, size, cancellationToken),
|
||||
MediaType.Video => await GenerateVideoThumbnailAsync(stream, size, cancellationToken),
|
||||
MediaType.Text or MediaType.Code => await GenerateDocumentThumbnailAsync(stream, size, cancellationToken),
|
||||
_ => new Error($"No Thumbnail for {type}"),
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<Stream> GenerateImageThumbnailAsync(Stream stream, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var img = Image.Load(stream);
|
||||
img.Mutate(o =>
|
||||
{
|
||||
var size =
|
||||
o.Resize(new ResizeOptions
|
||||
{
|
||||
Position = AnchorPositionMode.Center,
|
||||
Mode = ResizeMode.Crop,
|
||||
Size = new Size(300, 300)
|
||||
});
|
||||
});
|
||||
var result = new MemoryStream();
|
||||
await img.SaveAsWebpAsync(result, cancellationToken);
|
||||
result.Position = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Maybe<Stream>> GenerateVideoThumbnailAsync(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var w = (int)size;
|
||||
var source = new MemoryStream();
|
||||
data.CopyTo(source);
|
||||
source.Position = 0;
|
||||
var output = new MemoryStream();
|
||||
await FFMpegArguments.FromPipeInput(new StreamPipeSource(source))
|
||||
.OutputToPipe(new StreamPipeSink(output), opt =>
|
||||
{
|
||||
opt.WithCustomArgument($"-t 5 -vf \"crop='min(in_w,in_h)':'min(in_w,in_h)',scale={w}:{w}\" -loop 0")
|
||||
.ForceFormat("webp");
|
||||
}).ProcessAsynchronously();
|
||||
output.Position = 0;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public async Task<Maybe<Stream>> GenerateDocumentThumbnailAsync(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user