JJ Colocate

This commit is contained in:
2025-06-30 14:23:20 -04:00
parent 360fa53439
commit 24abf5607f
77 changed files with 5700 additions and 5700 deletions

View File

@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FFMpegCore" Version="5.2.0" />
<PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.5" />
<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.1.0" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.6" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.11.0" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FFMpegCore" Version="5.2.0" />
<PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.5" />
<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.1.0" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.6" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.11.0" />
</ItemGroup>
</Project>

View File

@@ -1,47 +1,47 @@
global using MaybeError;
using AobaCore.Services;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using MongoDB.Driver.Core.Extensions.DiagnosticSources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AobaCore;
public static class Extensions
{
public static IServiceCollection AddAoba(this IServiceCollection services, string dbString)
{
var settings = MongoClientSettings.FromConnectionString(dbString);
settings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber());
var dbClient = new MongoClient(settings);
var db = dbClient.GetDatabase("Aoba");
services.AddSingleton(dbClient);
services.AddSingleton<IMongoDatabase>(db);
services.AddSingleton<AobaService>();
services.AddSingleton<ThumbnailService>();
services.AddSingleton<AccountsService>();
services.AddHostedService<AobaIndexCreationService>();
return services;
}
public static async Task EnsureIndexAsync<T>(this IMongoCollection<T> collection, CreateIndexModel<T> indexModel)
{
try
{
await collection.Indexes.CreateOneAsync(indexModel);
}
catch (MongoCommandException e) when (e.Code == 85 || e.Code == 86) //CodeName "IndexOptionsConflict" or "NameConflict"
{
await collection.Indexes.DropOneAsync(indexModel.Options.Name);
await collection.Indexes.CreateOneAsync(indexModel);
}
}
}
global using MaybeError;
using AobaCore.Services;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using MongoDB.Driver.Core.Extensions.DiagnosticSources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AobaCore;
public static class Extensions
{
public static IServiceCollection AddAoba(this IServiceCollection services, string dbString)
{
var settings = MongoClientSettings.FromConnectionString(dbString);
settings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber());
var dbClient = new MongoClient(settings);
var db = dbClient.GetDatabase("Aoba");
services.AddSingleton(dbClient);
services.AddSingleton<IMongoDatabase>(db);
services.AddSingleton<AobaService>();
services.AddSingleton<ThumbnailService>();
services.AddSingleton<AccountsService>();
services.AddHostedService<AobaIndexCreationService>();
return services;
}
public static async Task EnsureIndexAsync<T>(this IMongoCollection<T> collection, CreateIndexModel<T> indexModel)
{
try
{
await collection.Indexes.CreateOneAsync(indexModel);
}
catch (MongoCommandException e) when (e.Code == 85 || e.Code == 86) //CodeName "IndexOptionsConflict" or "NameConflict"
{
await collection.Indexes.DropOneAsync(indexModel.Options.Name);
await collection.Indexes.CreateOneAsync(indexModel);
}
}
}

View File

@@ -1,98 +1,98 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace AobaCore.Models;
[BsonIgnoreExtraElements]
public class Media
{
[BsonId]
public ObjectId Id { get; set; }
public ObjectId MediaId { get; set; }
public string Filename { get; set; }
public MediaType MediaType { get; set; }
public string Ext { get; set; }
public int ViewCount { get; set; }
public ObjectId Owner { get; set; }
public DateTime UploadDate { get; set; }
public static readonly Dictionary<string, MediaType> KnownTypes = new()
{
{ ".jpg", MediaType.Image },
{ ".avif", MediaType.Image },
{ ".jpeg", MediaType.Image },
{ ".png", MediaType.Image },
{ ".apng", MediaType.Image },
{ ".webp", MediaType.Image },
{ ".ico", MediaType.Image },
{ ".gif", MediaType.Image },
{ ".mp3", MediaType.Audio },
{ ".flac", MediaType.Audio },
{ ".alac", MediaType.Audio },
{ ".mp4", MediaType.Video },
{ ".webm", MediaType.Video },
{ ".mov", MediaType.Video },
{ ".avi", MediaType.Video },
{ ".mkv", MediaType.Video },
{ ".txt", MediaType.Text },
{ ".log", MediaType.Text },
{ ".css", MediaType.Code },
{ ".cs", MediaType.Code },
{ ".cpp", MediaType.Code },
{ ".lua", MediaType.Code },
{ ".js", MediaType.Code },
{ ".htm", MediaType.Code },
{ ".html", MediaType.Code },
{ ".cshtml", MediaType.Code },
{ ".xml", MediaType.Code },
{ ".json", MediaType.Code },
{ ".py", MediaType.Code },
};
[BsonConstructor]
private Media()
{
Filename = string.Empty;
Ext = string.Empty;
}
public Media(ObjectId fileId, string filename, ObjectId owner)
{
MediaType = GetMediaType(filename);
Ext = Path.GetExtension(filename);
Filename = filename;
MediaId = fileId;
Owner = owner;
Id = ObjectId.GenerateNewId();
UploadDate = DateTime.UtcNow;
}
public string GetMediaUrl()
{
return this switch
{
//Media { MediaType: MediaType.Raw or MediaType.Text or MediaType.Code} => $"/i/dl/{MediaId}/{Filename}",
_ => $"/m/{MediaId}"
};
}
public static MediaType GetMediaType(string filename)
{
string ext = Path.GetExtension(filename);
if (KnownTypes.TryGetValue(ext, out MediaType mType))
return mType;
else
return MediaType.Raw;
}
}
public enum MediaType
{
Image,
Audio,
Video,
Text,
Code,
Raw
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace AobaCore.Models;
[BsonIgnoreExtraElements]
public class Media
{
[BsonId]
public ObjectId Id { get; set; }
public ObjectId MediaId { get; set; }
public string Filename { get; set; }
public MediaType MediaType { get; set; }
public string Ext { get; set; }
public int ViewCount { get; set; }
public ObjectId Owner { get; set; }
public DateTime UploadDate { get; set; }
public static readonly Dictionary<string, MediaType> KnownTypes = new()
{
{ ".jpg", MediaType.Image },
{ ".avif", MediaType.Image },
{ ".jpeg", MediaType.Image },
{ ".png", MediaType.Image },
{ ".apng", MediaType.Image },
{ ".webp", MediaType.Image },
{ ".ico", MediaType.Image },
{ ".gif", MediaType.Image },
{ ".mp3", MediaType.Audio },
{ ".flac", MediaType.Audio },
{ ".alac", MediaType.Audio },
{ ".mp4", MediaType.Video },
{ ".webm", MediaType.Video },
{ ".mov", MediaType.Video },
{ ".avi", MediaType.Video },
{ ".mkv", MediaType.Video },
{ ".txt", MediaType.Text },
{ ".log", MediaType.Text },
{ ".css", MediaType.Code },
{ ".cs", MediaType.Code },
{ ".cpp", MediaType.Code },
{ ".lua", MediaType.Code },
{ ".js", MediaType.Code },
{ ".htm", MediaType.Code },
{ ".html", MediaType.Code },
{ ".cshtml", MediaType.Code },
{ ".xml", MediaType.Code },
{ ".json", MediaType.Code },
{ ".py", MediaType.Code },
};
[BsonConstructor]
private Media()
{
Filename = string.Empty;
Ext = string.Empty;
}
public Media(ObjectId fileId, string filename, ObjectId owner)
{
MediaType = GetMediaType(filename);
Ext = Path.GetExtension(filename);
Filename = filename;
MediaId = fileId;
Owner = owner;
Id = ObjectId.GenerateNewId();
UploadDate = DateTime.UtcNow;
}
public string GetMediaUrl()
{
return this switch
{
//Media { MediaType: MediaType.Raw or MediaType.Text or MediaType.Code} => $"/i/dl/{MediaId}/{Filename}",
_ => $"/m/{MediaId}"
};
}
public static MediaType GetMediaType(string filename)
{
string ext = Path.GetExtension(filename);
if (KnownTypes.TryGetValue(ext, out MediaType mType))
return mType;
else
return MediaType.Raw;
}
}
public enum MediaType
{
Image,
Audio,
Video,
Text,
Code,
Raw
}

View File

@@ -1,19 +1,19 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace AobaCore.Models;
public record MediaThumbnail
{
[BsonId]
public required ObjectId Id { get; init; }
public Dictionary<ThumbnailSize, ObjectId> Sizes { get; set; } = [];
}
public enum ThumbnailSize
{
Small = 128,
Medium = 256,
Large = 512,
ExtraLarge = 1024
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace AobaCore.Models;
public record MediaThumbnail
{
[BsonId]
public required ObjectId Id { get; init; }
public Dictionary<ThumbnailSize, ObjectId> Sizes { get; set; } = [];
}
public enum ThumbnailSize
{
Small = 128,
Medium = 256,
Large = 512,
ExtraLarge = 1024
}

View File

@@ -1,18 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AobaCore.Models;
public class PagedResult<T>(List<T> items, int page, int pageSize, long totalItems)
{
public List<T> Items { get; set; } = items;
public int Page { get; set; } = page;
public int PageSize { get; set; } = pageSize;
public long TotalItems { get; set; } = totalItems;
public long TotalPages { get; set; } = totalItems / pageSize;
public string? Query { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AobaCore.Models;
public class PagedResult<T>(List<T> items, int page, int pageSize, long totalItems)
{
public List<T> Items { get; set; } = items;
public int Page { get; set; } = page;
public int PageSize { get; set; } = pageSize;
public long TotalItems { get; set; } = totalItems;
public long TotalPages { get; set; } = totalItems / pageSize;
public string? Query { get; set; }
}

View File

@@ -1,36 +1,36 @@
using Microsoft.IdentityModel.Tokens;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace AobaCore.Models;
public class User
{
[BsonId]
public ObjectId Id { get; set; }
public required string Username { get; set; }
public required string PasswordHash { get; set; }
public required string Role { get; set; }
public bool IsArgon { get; set; }
public ObjectId[] ApiKeys { get; set; } = [];
public List<ObjectId> RegTokens { get; set; } = [];
public ClaimsIdentity GetIdentity()
{
var id = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.NameIdentifier, Id.ToString()),
new Claim(ClaimTypes.Name, Username),
});
if (Role != null)
id.AddClaim(new Claim(ClaimTypes.Role, Role));
return id;
}
}
using Microsoft.IdentityModel.Tokens;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace AobaCore.Models;
public class User
{
[BsonId]
public ObjectId Id { get; set; }
public required string Username { get; set; }
public required string PasswordHash { get; set; }
public required string Role { get; set; }
public bool IsArgon { get; set; }
public ObjectId[] ApiKeys { get; set; } = [];
public List<ObjectId> RegTokens { get; set; } = [];
public ClaimsIdentity GetIdentity()
{
var id = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.NameIdentifier, Id.ToString()),
new Claim(ClaimTypes.Name, Username),
});
if (Role != null)
id.AddClaim(new Claim(ClaimTypes.Role, Role));
return id;
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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
}
}
}

View File

@@ -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();
}
}