implement media serving
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user