mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: paste creation, utilities, revert to gzip, grammar fixes etc
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user