mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: paste cleanup service; sveltekit frontend; paste editing; paste metadata modifying
refactor: rename SharpbinV3 -> SharpbinV3.Server; run cleanup
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using SharpbinV3.Data;
|
||||
using SharpbinV3.Data.Entities;
|
||||
using SharpbinV3.DTOs;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Bcrypt = BCrypt.Net.BCrypt;
|
||||
|
||||
namespace SharpbinV3.Services
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public sealed class AuthService(AppDbContext db, IConfiguration configuration) : IAuthService
|
||||
{
|
||||
@@ -125,7 +125,7 @@ namespace SharpbinV3.Services
|
||||
ValidateAudience = true,
|
||||
ValidAudience = _configuration["JwtConfig:Audience"],
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
ValidateLifetime = true
|
||||
ValidateLifetime = false
|
||||
}, out SecurityToken validatedToken);
|
||||
var jwtId = validatedToken.Id;
|
||||
var storedRefreshToken = await _db.RefreshTokens
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.IO.Compression;
|
||||
|
||||
|
||||
namespace SharpbinV3.Services
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public class CompressionService : ICompressionService
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using SharpbinV3.Data.Entities;
|
||||
using SharpbinV3.DTOs;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs;
|
||||
|
||||
namespace SharpbinV3.Services
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public interface IAuthService
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace SharpbinV3.Services
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public interface ICompressionService
|
||||
{
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using SharpbinV3.Data.Entities;
|
||||
using SharpbinV3.DTOs;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
|
||||
namespace SharpbinV3.Services
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public interface IPasteService
|
||||
{
|
||||
@@ -13,5 +12,9 @@ namespace SharpbinV3.Services
|
||||
Task<bool> EditText(Paste paste, string text);
|
||||
Task<Paste> Get(string id);
|
||||
Task<List<Paste>> GetList(int offset, int count, bool publicOnly = false);
|
||||
Task<bool> ValidateSyntax(string syntax);
|
||||
Task<bool> ValidateTitle(string title);
|
||||
Task<bool> ValidateVisibility(int visibility);
|
||||
Task<bool> ValidateExpiresAt(long expiresAt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using SharpbinV3.Data.Entities;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
|
||||
namespace SharpbinV3.Services
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public interface IUserService
|
||||
{
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SharpbinV3.Server.Data;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public class PasteCleanUpService(IServiceProvider serviceProvider, ILogger<PasteCleanUpService> logger) : BackgroundService
|
||||
{
|
||||
private readonly TimeSpan _period = TimeSpan.FromMinutes(5);
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using var timer = new PeriodicTimer(_period);
|
||||
await Cleanup(stoppingToken);
|
||||
while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken))
|
||||
{
|
||||
try
|
||||
{
|
||||
await Cleanup(stoppingToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error occurred during paste cleanup");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Cleanup(CancellationToken stoppingToken)
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
var expiredPastes = db.Pastes.Where(p => p.ExpiresAt != 0 && p.ExpiresAt <= now);
|
||||
if (await expiredPastes.AnyAsync(stoppingToken))
|
||||
{
|
||||
var deletedCount = await expiredPastes.CountAsync(stoppingToken);
|
||||
db.Pastes.RemoveRange(expiredPastes);
|
||||
await db.SaveChangesAsync(stoppingToken);
|
||||
logger.LogInformation("Deleted {Count} expired pastes", deletedCount);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,41 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SharpbinV3.Data;
|
||||
using SharpbinV3.Data.Entities;
|
||||
using SharpbinV3.DTOs;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpbinV3.Services
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public sealed class PasteService(AppDbContext db, ICompressionService cs) : IPasteService
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly ICompressionService _cs = cs;
|
||||
private readonly static string[] ValidSyntaxLanguages =
|
||||
[
|
||||
"plaintext",
|
||||
"autoHotkey",
|
||||
"autoIt",
|
||||
"bash",
|
||||
"c",
|
||||
"cpp",
|
||||
"csharp",
|
||||
"css",
|
||||
"dart",
|
||||
"html",
|
||||
"java",
|
||||
"javascript",
|
||||
"json",
|
||||
"lua",
|
||||
"markdown",
|
||||
"php",
|
||||
"python",
|
||||
"ruby",
|
||||
"rust",
|
||||
"sql",
|
||||
"swift",
|
||||
"typescript",
|
||||
"toml",
|
||||
"xml"
|
||||
];
|
||||
|
||||
public async Task<Paste> Create(User? author, string content, string title, string syntax, int visibility, long expiresAt)
|
||||
{
|
||||
@@ -61,6 +87,7 @@ namespace SharpbinV3.Services
|
||||
paste.Size = compressedData.Length;
|
||||
paste.TrueSize = Encoding.UTF8.GetByteCount(text);
|
||||
paste.EditedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
paste.IsCompressed = compressedData.Length < Encoding.UTF8.GetByteCount(text);
|
||||
_db.Pastes.Update(paste);
|
||||
var result = await _db.SaveChangesAsync();
|
||||
return result > 0;
|
||||
@@ -83,5 +110,23 @@ namespace SharpbinV3.Services
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> ValidateSyntax(string syntax)
|
||||
{
|
||||
return await Task.FromResult(ValidSyntaxLanguages.Contains(syntax));
|
||||
}
|
||||
public async Task<bool> ValidateTitle(string title)
|
||||
{
|
||||
return await Task.FromResult(title.Length <= 500);
|
||||
}
|
||||
public async Task<bool> ValidateVisibility(int visibility)
|
||||
{
|
||||
return await Task.FromResult(visibility >= 0 && visibility <= 2);
|
||||
}
|
||||
public async Task<bool> ValidateExpiresAt(long expiresAt)
|
||||
{
|
||||
if (expiresAt == 0) return true;
|
||||
var currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
return await Task.FromResult(expiresAt > currentTime - 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SharpbinV3.Data;
|
||||
using SharpbinV3.Data.Entities;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
|
||||
|
||||
namespace SharpbinV3.Services
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public sealed class UserService(AppDbContext db) : IUserService
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user