From a9860b28f2be123d1f0bfad504165992a4c841ed Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Tue, 10 Jan 2023 21:46:03 +0100 Subject: feat: feat --- BlobBin/BlobBin.csproj | 8 +- BlobBin/DB.cs | 41 ++++++++ .../20230110204008_InitialCreate.Designer.cs | 105 +++++++++++++++++++++ BlobBin/Migrations/20230110204008_InitialCreate.cs | 65 +++++++++++++ BlobBin/Migrations/DBModelSnapshot.cs | 102 ++++++++++++++++++++ BlobBin/Program.cs | 26 +++-- 6 files changed, 336 insertions(+), 11 deletions(-) create mode 100644 BlobBin/DB.cs create mode 100644 BlobBin/Migrations/20230110204008_InitialCreate.Designer.cs create mode 100644 BlobBin/Migrations/20230110204008_InitialCreate.cs create mode 100644 BlobBin/Migrations/DBModelSnapshot.cs (limited to 'BlobBin') diff --git a/BlobBin/BlobBin.csproj b/BlobBin/BlobBin.csproj index cd8d47d..263a517 100644 --- a/BlobBin/BlobBin.csproj +++ b/BlobBin/BlobBin.csproj @@ -8,8 +8,12 @@ - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + diff --git a/BlobBin/DB.cs b/BlobBin/DB.cs new file mode 100644 index 0000000..5146304 --- /dev/null +++ b/BlobBin/DB.cs @@ -0,0 +1,41 @@ +using Microsoft.EntityFrameworkCore; + +namespace BlobBin; + +public class DB : DbContext +{ + public DB(DbContextOptions options) : base(options) { } + + public DbSet Files { get; set; } + public DbSet Pastes { get; set; } +} + +public class UploadEntityBase +{ + public UploadEntityBase() { + Id = Guid.NewGuid(); + CreatedAt = DateTime.UtcNow; + } + + public Guid Id { get; set; } + public DateTime CreatedAt { get; set; } + public string CreatedBy { get; set; } + public DateTime? DeletedAt { get; set; } + public string? PasswordHash { get; set; } + public bool Singleton { get; set; } + public string? AutoDeleteAfter { get; set; } + public string? MimeType { get; set; } +} + +public class File : UploadEntityBase +{ + public string? Name { get; set; } + public long Length { get; set; } +} + +public class Paste : UploadEntityBase +{ + public string? Name { get; set; } + public string? Content { get; set; } + public long Length { get; set; } +} \ No newline at end of file diff --git a/BlobBin/Migrations/20230110204008_InitialCreate.Designer.cs b/BlobBin/Migrations/20230110204008_InitialCreate.Designer.cs new file mode 100644 index 0000000..f461df5 --- /dev/null +++ b/BlobBin/Migrations/20230110204008_InitialCreate.Designer.cs @@ -0,0 +1,105 @@ +// +using System; +using BlobBin; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlobBin.Migrations +{ + [DbContext(typeof(DB))] + [Migration("20230110204008_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("BlobBin.File", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AutoDeleteAfter") + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("DeletedAt") + .HasColumnType("TEXT"); + + b.Property("Length") + .HasColumnType("INTEGER"); + + b.Property("MimeType") + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("PasswordHash") + .HasColumnType("TEXT"); + + b.Property("Singleton") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("Files"); + }); + + modelBuilder.Entity("BlobBin.Paste", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AutoDeleteAfter") + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("DeletedAt") + .HasColumnType("TEXT"); + + b.Property("Length") + .HasColumnType("INTEGER"); + + b.Property("MimeType") + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("PasswordHash") + .HasColumnType("TEXT"); + + b.Property("Singleton") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("Pastes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlobBin/Migrations/20230110204008_InitialCreate.cs b/BlobBin/Migrations/20230110204008_InitialCreate.cs new file mode 100644 index 0000000..1614b01 --- /dev/null +++ b/BlobBin/Migrations/20230110204008_InitialCreate.cs @@ -0,0 +1,65 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BlobBin.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Files", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + Name = table.Column(type: "TEXT", nullable: false), + Length = table.Column(type: "INTEGER", nullable: false), + CreatedAt = table.Column(type: "TEXT", nullable: false), + CreatedBy = table.Column(type: "TEXT", nullable: false), + DeletedAt = table.Column(type: "TEXT", nullable: true), + PasswordHash = table.Column(type: "TEXT", nullable: true), + Singleton = table.Column(type: "INTEGER", nullable: false), + AutoDeleteAfter = table.Column(type: "TEXT", nullable: true), + MimeType = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Files", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Pastes", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + Name = table.Column(type: "TEXT", nullable: false), + Length = table.Column(type: "INTEGER", nullable: false), + CreatedAt = table.Column(type: "TEXT", nullable: false), + CreatedBy = table.Column(type: "TEXT", nullable: false), + DeletedAt = table.Column(type: "TEXT", nullable: true), + PasswordHash = table.Column(type: "TEXT", nullable: true), + Singleton = table.Column(type: "INTEGER", nullable: false), + AutoDeleteAfter = table.Column(type: "TEXT", nullable: true), + MimeType = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Pastes", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Files"); + + migrationBuilder.DropTable( + name: "Pastes"); + } + } +} diff --git a/BlobBin/Migrations/DBModelSnapshot.cs b/BlobBin/Migrations/DBModelSnapshot.cs new file mode 100644 index 0000000..b81feae --- /dev/null +++ b/BlobBin/Migrations/DBModelSnapshot.cs @@ -0,0 +1,102 @@ +// +using System; +using BlobBin; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlobBin.Migrations +{ + [DbContext(typeof(DB))] + partial class DBModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("BlobBin.File", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AutoDeleteAfter") + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("DeletedAt") + .HasColumnType("TEXT"); + + b.Property("Length") + .HasColumnType("INTEGER"); + + b.Property("MimeType") + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("PasswordHash") + .HasColumnType("TEXT"); + + b.Property("Singleton") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("Files"); + }); + + modelBuilder.Entity("BlobBin.Paste", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("AutoDeleteAfter") + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("CreatedBy") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("DeletedAt") + .HasColumnType("TEXT"); + + b.Property("Length") + .HasColumnType("INTEGER"); + + b.Property("MimeType") + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("PasswordHash") + .HasColumnType("TEXT"); + + b.Property("Singleton") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("Pastes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlobBin/Program.cs b/BlobBin/Program.cs index 085bd6e..acf3e5f 100644 --- a/BlobBin/Program.cs +++ b/BlobBin/Program.cs @@ -1,25 +1,33 @@ -var builder = WebApplication.CreateBuilder(args); +global using BlobBin; +using Microsoft.EntityFrameworkCore; +using File = BlobBin.File; +var builder = WebApplication.CreateBuilder(args); +builder.Services.AddDbContext(opt => opt.UseSqlite("data source=main.db")); var app = builder.Build(); app.UseFileServer(); app.UseStatusCodePages(); -app.MapPost("/upload", Upload); +app.MapGet("/upload-link", GetUploadLink); +app.MapPost("/upload/{id}", GetUploadLink); app.MapPost("/text", UploadText); app.MapGet("/b/{id}", GetBlob); app.Run(); -IResult Upload(HttpContext context) { - var request = new UploadRequest() { - Singleton = context.Request.Form["singleton"] == "on", - File = context.Request.Form.Files.FirstOrDefault(), - Password = context.Request.Form["password"], - AutoDeleteAfter = context.Request.Form["autoDeleteAfter"] +IResult GetUploadLink(HttpContext context, DB db) { + var file = new File { + CreatedBy = context.Request.Headers["X-Forwarded-For"].ToString() }; + db.Files.Add(file); + db.SaveChanges(); + return Results.Ok(context.Request.Host.Value + "/upload/" + file.Id); +} + +IResult Upload(HttpContext context, DB db) { return Results.Ok(); } -IResult UploadText(PasteRequest request) { +IResult UploadText(HttpContext context, DB db) { return Results.Ok(); } -- cgit v1.3