More product setup

This commit is contained in:
2025-04-01 23:52:37 -04:00
parent 636deeb2c0
commit 05afb855be
20 changed files with 295 additions and 48 deletions

18
AobaCore/AobaCore.csproj Normal file
View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.3" />
<PackageReference Include="MongoDB.Bson" Version="3.3.0" />
<PackageReference Include="MaybeError" Version="1.0.5" />
<PackageReference Include="MongoDB.Analyzer" Version="1.5.0" />
<PackageReference Include="MongoDB.Driver" Version="2.30.0" />
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.30.0" />
</ItemGroup>
</Project>

19
AobaCore/AobaService.cs Normal file
View File

@@ -0,0 +1,19 @@
using AobaV2.Models;
using MaybeError;
using MongoDB.Bson;
using MongoDB.Driver;
namespace AobaCore;
public class AobaService(IMongoDatabase db)
{
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
public async Task<Media?> GetMediaAsync(ObjectId id)
{
return await _media.Find(m => m.Id == id).FirstOrDefaultAsync();
}
}

17
AobaCore/Extensions.cs Normal file
View File

@@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
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)
{
services.AddSingleton<AobaService>();
return services;
}
}

79
AobaCore/Models/Media.cs Normal file
View File

@@ -0,0 +1,79 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace AobaV2.Models;
[BsonIgnoreExtraElements]
public class Media
{
[BsonId]
public ObjectId Id { get; set; }
public ObjectId MediaId { get; set; }
public required string Filename { get; set; }
public MediaType MediaType { get; set; }
public required string Ext { get; set; }
public int ViewCount { get; set; }
public ObjectId Owner { 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 },
};
public string GetMediaUrl()
{
return this switch
{
Media { MediaType: MediaType.Raw or MediaType.Text or MediaType.Code} => $"/i/dl/{Id}/{Filename}",
_ => $"/i/{Id}"
};
}
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
}