misc
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
using AobaV2.Models;
|
||||
|
||||
using MaybeError;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
|
||||
@@ -16,4 +14,9 @@ public class AobaService(IMongoDatabase db)
|
||||
{
|
||||
return await _media.Find(m => m.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public Task AddMediaAsync(Media media)
|
||||
{
|
||||
return _media.InsertOneAsync(media);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
global using MaybeError;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
25
AobaCore/MediaService.cs
Normal file
25
AobaCore/MediaService.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using AobaV2.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;
|
||||
public class MediaService(IMongoDatabase db, AobaService aobaService)
|
||||
{
|
||||
private readonly GridFSBucket _gridFs = new(db);
|
||||
|
||||
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);
|
||||
await aobaService.AddMediaAsync(media);
|
||||
return media;
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@ public class Media
|
||||
[BsonId]
|
||||
public ObjectId Id { get; set; }
|
||||
public ObjectId MediaId { get; set; }
|
||||
public required string Filename { get; set; }
|
||||
public string Filename { get; set; }
|
||||
public MediaType MediaType { get; set; }
|
||||
public required string Ext { get; set; }
|
||||
public string Ext { get; set; }
|
||||
public int ViewCount { get; set; }
|
||||
public ObjectId Owner { get; set; }
|
||||
|
||||
@@ -49,6 +49,23 @@ public class Media
|
||||
{ ".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();
|
||||
}
|
||||
|
||||
public string GetMediaUrl()
|
||||
{
|
||||
return this switch
|
||||
|
||||
Reference in New Issue
Block a user