mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
feat: paste creation, utilities, revert to gzip, grammar fixes etc
This commit is contained in:
@@ -57,7 +57,8 @@ namespace SharpbinV3.Controllers
|
||||
if (!Bcrypt.Verify(request.Password, user.PasswordHash))
|
||||
return BadRequest(new { message = "Invalid username/email or password." });
|
||||
var token = _authService.GenerateJWTToken(user);
|
||||
return Ok(new { token });
|
||||
await _authService.UpdateLoginTime(user);
|
||||
return Ok(new { token.Result });
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("refresh")]
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SharpbinV3.Data.Entities;
|
||||
using SharpbinV3.DTOs;
|
||||
using SharpbinV3.Services;
|
||||
|
||||
namespace SharpbinV3.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PasteController(IPasteService pasteService, IUserService userService) : ControllerBase
|
||||
{
|
||||
private readonly IPasteService _pasteService = pasteService;
|
||||
private readonly IUserService _userService = userService;
|
||||
|
||||
[HttpPost]
|
||||
[Route("create")]
|
||||
public async Task<IActionResult> CreatePaste(string title = "", string syntax = "plaintext", int visibility = 0, long expiresAt = 0)
|
||||
{
|
||||
string content = await new StreamReader(Request.Body).ReadToEndAsync();
|
||||
var httpUser = HttpContext.User;
|
||||
var userUUID = httpUser?.FindFirst("UUID")?.Value;
|
||||
User? user = userUUID is not null ? await _userService.GetByUUID(Guid.Parse(userUUID)) : null;
|
||||
Paste paste = await _pasteService.Create(user, content, title, syntax, visibility, expiresAt);
|
||||
return Ok(new
|
||||
{
|
||||
paste.ID,
|
||||
paste.UUID,
|
||||
paste.IsCompressed,
|
||||
paste.Size,
|
||||
paste.TrueSize,
|
||||
paste.ExpiresAt
|
||||
});
|
||||
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("{id}")]
|
||||
public async Task<IActionResult> GetPasteByID(string id)
|
||||
{
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null) return NotFound();
|
||||
Console.WriteLine(paste.User);
|
||||
return Ok(new
|
||||
{
|
||||
paste.ID,
|
||||
paste.UUID,
|
||||
paste.Title,
|
||||
paste.Size,
|
||||
paste.TrueSize,
|
||||
paste.IsCompressed,
|
||||
paste.Views,
|
||||
paste.Syntax,
|
||||
paste.Visibility,
|
||||
paste.ExpiresAt,
|
||||
Author = paste.User != null && paste.User.Visiblity == 0 ? new
|
||||
{
|
||||
paste.User.UID,
|
||||
paste.User.UUID,
|
||||
paste.User.Username,
|
||||
paste.User.Visiblity
|
||||
} : null
|
||||
});
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("raw/{id}")]
|
||||
public async Task<IActionResult> GetRawPasteByID(string id)
|
||||
{
|
||||
var paste = await _pasteService.Get(id);
|
||||
if (paste == null) return NotFound();
|
||||
if (paste.IsCompressed)
|
||||
{
|
||||
Response.Headers.Append("Content-Encoding", "gzip");
|
||||
return File(paste.Content, "application/octet-stream");
|
||||
}
|
||||
else
|
||||
{
|
||||
return File(paste.Content, "text/plain; charset=utf-8");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,23 +9,29 @@ namespace SharpbinV3.Data.Entities
|
||||
[Index(nameof(AuthorUUID))]
|
||||
[Index(nameof(ExpiresAt))]
|
||||
[Index(nameof(Views))]
|
||||
[Index(nameof(Visiblity))]
|
||||
[Index(nameof(Visibility))]
|
||||
public sealed class Paste
|
||||
{
|
||||
[Key]
|
||||
public int PID { get; set; }
|
||||
public required string UUID { get; set; } // uuid v7 includes timestamp, this counts as created at
|
||||
public required Guid UUID { get; set; } // uuid v7 includes timestamp, this counts as created at
|
||||
public required string ID { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public required string AuthorUUID { get; set; } // uuid of the user who created the paste, or "ANON" for anonymous pastes
|
||||
|
||||
public required Guid AuthorUUID { get; set; } = Guid.Empty; // uuid of the user who created the paste, or Guid.Empty for anonymous pastes
|
||||
public User? User { get; set; }
|
||||
public required string FilePath { get; set; }
|
||||
|
||||
public long? EditedAt { get; set; }
|
||||
|
||||
public required byte[] Content { get; set; }
|
||||
public int Size { get; set; } // stored size (may be compressed)
|
||||
public int TrueSize { get; set; } // uncompressed size
|
||||
public bool IsCompressed { get; set; } = false;
|
||||
|
||||
public int Views { get; set; }
|
||||
public string? Syntax { get; set; }
|
||||
public int Visiblity { get; set; } = 0; // 0 = public, 1 = unlisted, 2 = private
|
||||
public int Visibility { get; set; } = 0; // 0 = public, 1 = unlisted, 2 = private (encrypted pastes handled on frontend)
|
||||
|
||||
public long ExpiresAt { get; set; } // timestamp of when the paste expires, or 0 for never
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SharpbinV3.Data;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SharpbinV3.Migrations
|
||||
{
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20251125053617_ImprovePastes")]
|
||||
partial class ImprovePastes
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "10.0.0");
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.Property<int>("PID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("AuthorUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<byte[]>("Data")
|
||||
.IsRequired()
|
||||
.HasColumnType("BLOB");
|
||||
|
||||
b.Property<long?>("EditedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("ID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("IsCompressed")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Size")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Syntax")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("TrueSize")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("UUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("UserUID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Views")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Visiblity")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("PID");
|
||||
|
||||
b.HasIndex("AuthorUUID");
|
||||
|
||||
b.HasIndex("ExpiresAt");
|
||||
|
||||
b.HasIndex("ID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("PID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UUID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UserUID");
|
||||
|
||||
b.HasIndex("Views");
|
||||
|
||||
b.HasIndex("Visiblity");
|
||||
|
||||
b.ToTable("Pastes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.RefreshToken", b =>
|
||||
{
|
||||
b.Property<Guid>("UserUUID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("JwtId")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Token")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("Used")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int?>("UserUID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("UserUUID");
|
||||
|
||||
b.HasIndex("UserUID");
|
||||
|
||||
b.ToTable("RefreshTokens");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("UID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("DisplayName")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long?>("LastLogin")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.PrimitiveCollection<string>("Roles")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<Guid>("UUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Visiblity")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("UID");
|
||||
|
||||
b.HasIndex("UID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UUID")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Data.Entities.User", "User")
|
||||
.WithMany("Pastes")
|
||||
.HasForeignKey("UserUID");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.RefreshToken", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Data.Entities.User", "User")
|
||||
.WithMany()
|
||||
.HasForeignKey("UserUID");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("Pastes");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SharpbinV3.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class ImprovePastes : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "FilePath",
|
||||
table: "Pastes");
|
||||
|
||||
migrationBuilder.AddColumn<byte[]>(
|
||||
name: "Data",
|
||||
table: "Pastes",
|
||||
type: "BLOB",
|
||||
nullable: false,
|
||||
defaultValue: new byte[0]);
|
||||
|
||||
migrationBuilder.AddColumn<bool>(
|
||||
name: "IsCompressed",
|
||||
table: "Pastes",
|
||||
type: "INTEGER",
|
||||
nullable: false,
|
||||
defaultValue: false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "Data",
|
||||
table: "Pastes");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "IsCompressed",
|
||||
table: "Pastes");
|
||||
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "FilePath",
|
||||
table: "Pastes",
|
||||
type: "TEXT",
|
||||
nullable: false,
|
||||
defaultValue: "");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SharpbinV3.Data;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SharpbinV3.Migrations
|
||||
{
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20251125055340_PasteContent")]
|
||||
partial class PasteContent
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "10.0.0");
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.Property<int>("PID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("AuthorUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<byte[]>("Content")
|
||||
.IsRequired()
|
||||
.HasColumnType("BLOB");
|
||||
|
||||
b.Property<long?>("EditedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("ID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("IsCompressed")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Size")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Syntax")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("TrueSize")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("UUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("UserUID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Views")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Visiblity")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("PID");
|
||||
|
||||
b.HasIndex("AuthorUUID");
|
||||
|
||||
b.HasIndex("ExpiresAt");
|
||||
|
||||
b.HasIndex("ID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("PID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UUID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UserUID");
|
||||
|
||||
b.HasIndex("Views");
|
||||
|
||||
b.HasIndex("Visiblity");
|
||||
|
||||
b.ToTable("Pastes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.RefreshToken", b =>
|
||||
{
|
||||
b.Property<Guid>("UserUUID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("JwtId")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Token")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("Used")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int?>("UserUID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("UserUUID");
|
||||
|
||||
b.HasIndex("UserUID");
|
||||
|
||||
b.ToTable("RefreshTokens");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("UID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("DisplayName")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long?>("LastLogin")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.PrimitiveCollection<string>("Roles")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<Guid>("UUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Visiblity")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("UID");
|
||||
|
||||
b.HasIndex("UID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UUID")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Data.Entities.User", "User")
|
||||
.WithMany("Pastes")
|
||||
.HasForeignKey("UserUID");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.RefreshToken", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Data.Entities.User", "User")
|
||||
.WithMany()
|
||||
.HasForeignKey("UserUID");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("Pastes");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SharpbinV3.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class PasteContent : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.RenameColumn(
|
||||
name: "Data",
|
||||
table: "Pastes",
|
||||
newName: "Content");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.RenameColumn(
|
||||
name: "Content",
|
||||
table: "Pastes",
|
||||
newName: "Data");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SharpbinV3.Data;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SharpbinV3.Migrations
|
||||
{
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20251125061310_GrammarFix")]
|
||||
partial class GrammarFix
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "10.0.0");
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.Property<int>("PID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("AuthorUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<byte[]>("Content")
|
||||
.IsRequired()
|
||||
.HasColumnType("BLOB");
|
||||
|
||||
b.Property<long?>("EditedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("ID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("IsCompressed")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Size")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Syntax")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("TrueSize")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("UUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("UserUID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Views")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Visibility")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("PID");
|
||||
|
||||
b.HasIndex("AuthorUUID");
|
||||
|
||||
b.HasIndex("ExpiresAt");
|
||||
|
||||
b.HasIndex("ID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("PID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UUID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UserUID");
|
||||
|
||||
b.HasIndex("Views");
|
||||
|
||||
b.HasIndex("Visibility");
|
||||
|
||||
b.ToTable("Pastes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.RefreshToken", b =>
|
||||
{
|
||||
b.Property<Guid>("UserUUID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("JwtId")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Token")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("Used")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int?>("UserUID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("UserUUID");
|
||||
|
||||
b.HasIndex("UserUID");
|
||||
|
||||
b.ToTable("RefreshTokens");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("UID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("DisplayName")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long?>("LastLogin")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.PrimitiveCollection<string>("Roles")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<Guid>("UUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Visiblity")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("UID");
|
||||
|
||||
b.HasIndex("UID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UUID")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Data.Entities.User", "User")
|
||||
.WithMany("Pastes")
|
||||
.HasForeignKey("UserUID");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.RefreshToken", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Data.Entities.User", "User")
|
||||
.WithMany()
|
||||
.HasForeignKey("UserUID");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Data.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("Pastes");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SharpbinV3.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class GrammarFix : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.RenameColumn(
|
||||
name: "Visiblity",
|
||||
table: "Pastes",
|
||||
newName: "Visibility");
|
||||
|
||||
migrationBuilder.RenameIndex(
|
||||
name: "IX_Pastes_Visiblity",
|
||||
table: "Pastes",
|
||||
newName: "IX_Pastes_Visibility");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.RenameColumn(
|
||||
name: "Visibility",
|
||||
table: "Pastes",
|
||||
newName: "Visiblity");
|
||||
|
||||
migrationBuilder.RenameIndex(
|
||||
name: "IX_Pastes_Visibility",
|
||||
table: "Pastes",
|
||||
newName: "IX_Pastes_Visiblity");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,23 +23,25 @@ namespace SharpbinV3.Migrations
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("AuthorUUID")
|
||||
.IsRequired()
|
||||
b.Property<Guid>("AuthorUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<byte[]>("Content")
|
||||
.IsRequired()
|
||||
.HasColumnType("BLOB");
|
||||
|
||||
b.Property<long?>("EditedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("FilePath")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("ID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("IsCompressed")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Size")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
@@ -52,8 +54,7 @@ namespace SharpbinV3.Migrations
|
||||
b.Property<int>("TrueSize")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("UUID")
|
||||
.IsRequired()
|
||||
b.Property<Guid>("UUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("UserUID")
|
||||
@@ -62,7 +63,7 @@ namespace SharpbinV3.Migrations
|
||||
b.Property<int>("Views")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Visiblity")
|
||||
b.Property<int>("Visibility")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("PID");
|
||||
@@ -84,7 +85,7 @@ namespace SharpbinV3.Migrations
|
||||
|
||||
b.HasIndex("Views");
|
||||
|
||||
b.HasIndex("Visiblity");
|
||||
b.HasIndex("Visibility");
|
||||
|
||||
b.ToTable("Pastes");
|
||||
});
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace SharpbinV3
|
||||
builder.Services.AddSingleton<ICompressionService, CompressionService>();
|
||||
builder.Services.AddScoped<IUserService, UserService>();
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
builder.Services.AddScoped<IPasteService, PasteService>();
|
||||
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace SharpbinV3.Services
|
||||
existingToken.Used = false;
|
||||
existingToken.CreatedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
existingToken.ExpiresAt = DateTimeOffset.UtcNow.AddMonths(6).ToUnixTimeMilliseconds();
|
||||
existingToken.Token = RandomString(36) + Guid.NewGuid();
|
||||
existingToken.Token = Utilities.GenerateRandomString(36) + Guid.NewGuid();
|
||||
_db.RefreshTokens.Update(existingToken);
|
||||
}
|
||||
else
|
||||
@@ -73,7 +73,7 @@ namespace SharpbinV3.Services
|
||||
UserUUID = user.UUID,
|
||||
CreatedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
||||
ExpiresAt = DateTimeOffset.UtcNow.AddMonths(6).ToUnixTimeMilliseconds(),
|
||||
Token = RandomString(36) + Guid.NewGuid()
|
||||
Token = Utilities.GenerateRandomString(36) + Guid.NewGuid()
|
||||
};
|
||||
await _db.RefreshTokens.AddAsync(refreshToken);
|
||||
}
|
||||
@@ -151,10 +151,25 @@ namespace SharpbinV3.Services
|
||||
}
|
||||
}
|
||||
|
||||
private static string RandomString(int length)
|
||||
public async Task<User> UpdateUser(User user)
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
return new string([.. Enumerable.Repeat(chars, length).Select(s => s[Random.Shared.Next(s.Length)])]);
|
||||
var existingUser = await _db.Users.FirstOrDefaultAsync(u => u.UUID == user.UUID) ?? throw new Exception("User not found");
|
||||
existingUser.Username = user.Username;
|
||||
existingUser.Email = user.Email;
|
||||
existingUser.DisplayName = user.DisplayName;
|
||||
existingUser.Roles = user.Roles;
|
||||
existingUser.LastLogin = user.LastLogin;
|
||||
existingUser.Visiblity = user.Visiblity;
|
||||
_db.Users.Update(existingUser);
|
||||
await _db.SaveChangesAsync();
|
||||
return user;
|
||||
}
|
||||
public async Task UpdateLoginTime(User user)
|
||||
{
|
||||
var existingUser = await _db.Users.FirstOrDefaultAsync(u => u.UUID == user.UUID) ?? throw new Exception("User not found");
|
||||
existingUser.LastLogin = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
_db.Users.Update(existingUser);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,11 @@ namespace SharpbinV3.Services
|
||||
|
||||
return data;
|
||||
}
|
||||
public byte[] Compress(string data)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
return Compress(System.Text.Encoding.UTF8.GetBytes(data));
|
||||
}
|
||||
public byte[] Decompress(byte[] compressedData)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(compressedData);
|
||||
|
||||
@@ -9,5 +9,7 @@ namespace SharpbinV3.Services
|
||||
Task<JWTResult> GenerateJWTToken(User user);
|
||||
Task<bool> ValidateJWTToken(string token);
|
||||
Task<JWTResult> RefreshJWTToken(string token, string refreshToken);
|
||||
Task<User> UpdateUser(User user);
|
||||
Task UpdateLoginTime(User user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
public interface ICompressionService
|
||||
{
|
||||
byte[] Compress(byte[] data);
|
||||
byte[] Compress(string data);
|
||||
byte[] Decompress(byte[] compressedData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
using SharpbinV3.Data.Entities;
|
||||
using SharpbinV3.DTOs;
|
||||
|
||||
namespace SharpbinV3.Services
|
||||
{
|
||||
public interface IPasteService
|
||||
{
|
||||
Task<Paste> Create(Paste paste);
|
||||
Task<Paste> Create(User? author, string content, string title, string syntax, int visibility, long expiresAt);
|
||||
Task<bool> Delete(Paste paste);
|
||||
Task<Paste> Edit(Paste paste);
|
||||
Task<bool> Exists(string id);
|
||||
Task<bool> Exists(Guid uuid);
|
||||
Task<bool> EditText(Paste paste, string text);
|
||||
Task<Paste> Get(string id);
|
||||
Task<List<Paste>> GetList(int offset, int count);
|
||||
Task<List<Paste>> GetList(int offset, int count, bool publicOnly = false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SharpbinV3.Data;
|
||||
using SharpbinV3.Data.Entities;
|
||||
using SharpbinV3.DTOs;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpbinV3.Services
|
||||
{
|
||||
public sealed class PasteService(AppDbContext db, ICompressionService cs) : IPasteService
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly ICompressionService _cs = cs;
|
||||
|
||||
public async Task<Paste> Create(User? author, string content, string title, string syntax, int visibility, long expiresAt)
|
||||
{
|
||||
var compressedData = _cs.Compress(content);
|
||||
var paste = new Paste
|
||||
{
|
||||
UUID = Guid.CreateVersion7(),
|
||||
ID = Utilities.GenerateRandomString(8),
|
||||
Title = title,
|
||||
AuthorUUID = author?.UUID ?? Guid.Empty,
|
||||
User = author,
|
||||
Content = compressedData,
|
||||
Size = compressedData.Length,
|
||||
TrueSize = Encoding.UTF8.GetByteCount(content),
|
||||
IsCompressed = compressedData.Length < Encoding.UTF8.GetByteCount(content),
|
||||
Syntax = syntax,
|
||||
Visibility = visibility,
|
||||
ExpiresAt = expiresAt,
|
||||
};
|
||||
_db.Pastes.Add(paste);
|
||||
await _db.SaveChangesAsync();
|
||||
return paste;
|
||||
}
|
||||
|
||||
public async Task<bool> Delete(Paste paste)
|
||||
{
|
||||
_db.Pastes.Remove(paste);
|
||||
var result = await _db.SaveChangesAsync();
|
||||
return result > 0;
|
||||
}
|
||||
public async Task<Paste> Edit(Paste paste)
|
||||
{
|
||||
_db.Pastes.Update(paste);
|
||||
await _db.SaveChangesAsync();
|
||||
return paste;
|
||||
}
|
||||
public async Task<bool> Exists(string id)
|
||||
{
|
||||
return await _db.Pastes.AnyAsync(p => p.ID == id);
|
||||
}
|
||||
public async Task<bool> Exists(Guid uuid)
|
||||
{
|
||||
return await _db.Pastes.AnyAsync(p => p.UUID == uuid);
|
||||
}
|
||||
public async Task<bool> EditText(Paste paste, string text)
|
||||
{
|
||||
var compressedData = _cs.Compress(text);
|
||||
paste.Content = compressedData;
|
||||
paste.Size = compressedData.Length;
|
||||
paste.TrueSize = Encoding.UTF8.GetByteCount(text);
|
||||
paste.EditedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
_db.Pastes.Update(paste);
|
||||
var result = await _db.SaveChangesAsync();
|
||||
return result > 0;
|
||||
}
|
||||
public async Task<Paste> Get(string id)
|
||||
{
|
||||
var paste = await _db.Pastes.Include(p => p.User).FirstOrDefaultAsync(p => p.ID == id);
|
||||
return paste ?? throw new KeyNotFoundException("Paste not found");
|
||||
}
|
||||
public async Task<List<Paste>> GetList(int offset, int count, bool publicOnly = false)
|
||||
{
|
||||
var query = _db.Pastes.AsQueryable();
|
||||
if (publicOnly)
|
||||
query = query.Where(p => p.Visibility == 0);
|
||||
|
||||
return await query
|
||||
.OrderByDescending(p => p.PID)
|
||||
.Skip(offset)
|
||||
.Take(count)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="K4os.Compression.LZ4" Version="1.3.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.0" />
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace SharpbinV3
|
||||
{
|
||||
public class Utilities
|
||||
{
|
||||
public static string GenerateRandomString(int length)
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
var random = new Random();
|
||||
return new string(Enumerable.Repeat(chars, length)
|
||||
.Select(s => s[random.Next(s.Length)]).ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user