implement media serving

This commit is contained in:
2025-04-15 23:00:11 -04:00
parent 0e714a7ffe
commit 1c9127ca19
14 changed files with 72 additions and 43 deletions

View File

@@ -1,14 +1,11 @@
using AobaV2.Models;
using AobaV2.Models;
using MaybeError.Errors;
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;
public class MediaService(IMongoDatabase db, AobaService aobaService)
@@ -18,8 +15,20 @@ public class MediaService(IMongoDatabase db, AobaService aobaService)
public async Task<Maybe<Media>> UploadMediaAsync(Stream data, string filename, ObjectId owner, CancellationToken cancellationToken = default)
{
var fileId = await _gridFs.UploadFromStreamAsync(filename, data, cancellationToken: cancellationToken);
var media = new Media(fileId, filename, owner);
var media = new Media(fileId, filename, owner);
await aobaService.AddMediaAsync(media);
return media;
}
public async Task<Maybe<GridFSDownloadStream, ExceptionError<GridFSException>>> GetMediaStreamAsync(ObjectId id, bool seekable = false)
{
try
{
return await _gridFs.OpenDownloadStreamAsync(id, new GridFSDownloadOptions { Seekable = seekable });
}
catch (GridFSException ex)
{
return new ExceptionError<GridFSException>(ex);
}
}
}