thumbnail service wip
This commit is contained in:
66
AobaCore/Services/AccountsService.cs
Normal file
66
AobaCore/Services/AccountsService.cs
Normal file
@@ -0,0 +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;
|
||||
}
|
||||
}
|
||||
26
AobaCore/Services/AobaIndexCreationService.cs
Normal file
26
AobaCore/Services/AobaIndexCreationService.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using AobaCore.Models;
|
||||
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
90
AobaCore/Services/AobaService.cs
Normal file
90
AobaCore/Services/AobaService.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
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<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);
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
28
AobaCore/Services/ThumbnailService.cs
Normal file
28
AobaCore/Services/ThumbnailService.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using AobaCore.Models;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.GridFS;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AobaCore.Services;
|
||||
internal 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 GenerateThumbnailAsync(ObjectId id)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user