mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-08 12:58:41 +00:00
Move servers to LBPU.PL.Servers
This commit is contained in:
parent
545b5a0709
commit
b2ec7eae57
116 changed files with 173 additions and 162 deletions
|
@ -0,0 +1,165 @@
|
|||
#nullable enable
|
||||
using System.Xml.Serialization;
|
||||
using Discord;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Resources;
|
||||
|
||||
[ApiController]
|
||||
[Route("LITTLEBIGPLANETPS3_XML/")]
|
||||
[Produces("text/xml")]
|
||||
public class PhotosController : ControllerBase
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public PhotosController(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpPost("uploadPhoto")]
|
||||
public async Task<IActionResult> UploadPhoto()
|
||||
{
|
||||
User? user = await this.database.UserFromGameRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
if (user.PhotosByMe >= ServerConfiguration.Instance.UserGeneratedContentLimits.PhotosQuota) return this.BadRequest();
|
||||
|
||||
this.Request.Body.Position = 0;
|
||||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
|
||||
XmlSerializer serializer = new(typeof(Photo));
|
||||
Photo? photo = (Photo?)serializer.Deserialize(new StringReader(bodyString));
|
||||
if (photo == null) return this.BadRequest();
|
||||
|
||||
SanitizationHelper.SanitizeStringsInClass(photo);
|
||||
|
||||
foreach (Photo p in this.database.Photos.Where(p => p.CreatorId == user.UserId))
|
||||
{
|
||||
if (p.LargeHash == photo.LargeHash) return this.Ok(); // photo already uplaoded
|
||||
if (p.MediumHash == photo.MediumHash) return this.Ok();
|
||||
if (p.SmallHash == photo.SmallHash) return this.Ok();
|
||||
if (p.PlanHash == photo.PlanHash) return this.Ok();
|
||||
}
|
||||
|
||||
photo.CreatorId = user.UserId;
|
||||
photo.Creator = user;
|
||||
|
||||
if (photo.Subjects.Count > 4) return this.BadRequest();
|
||||
|
||||
if (photo.Timestamp > TimestampHelper.Timestamp) photo.Timestamp = TimestampHelper.Timestamp;
|
||||
|
||||
foreach (PhotoSubject subject in photo.Subjects)
|
||||
{
|
||||
subject.User = await this.database.Users.FirstOrDefaultAsync(u => u.Username == subject.Username);
|
||||
|
||||
if (subject.User == null) continue;
|
||||
|
||||
subject.UserId = subject.User.UserId;
|
||||
Logger.LogDebug($"Adding PhotoSubject (userid {subject.UserId}) to db", LogArea.Photos);
|
||||
|
||||
this.database.PhotoSubjects.Add(subject);
|
||||
}
|
||||
|
||||
await this.database.SaveChangesAsync();
|
||||
|
||||
// Check for duplicate photo subjects
|
||||
List<int> subjectUserIds = new(4);
|
||||
foreach (PhotoSubject subject in photo.Subjects)
|
||||
{
|
||||
if (subjectUserIds.Contains(subject.UserId)) return this.BadRequest();
|
||||
|
||||
subjectUserIds.Add(subject.UserId);
|
||||
}
|
||||
|
||||
photo.PhotoSubjectIds = photo.Subjects.Select(subject => subject.PhotoSubjectId.ToString()).ToArray();
|
||||
|
||||
// photo.Slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == photo.SlotId);
|
||||
|
||||
Logger.LogDebug($"Adding PhotoSubjectCollection ({photo.PhotoSubjectCollection}) to photo", LogArea.Photos);
|
||||
|
||||
this.database.Photos.Add(photo);
|
||||
|
||||
await this.database.SaveChangesAsync();
|
||||
|
||||
await WebhookHelper.SendWebhook
|
||||
(
|
||||
new EmbedBuilder
|
||||
{
|
||||
Title = "New photo uploaded!",
|
||||
Description = $"{user.Username} uploaded a new photo.",
|
||||
ImageUrl = $"{ServerConfiguration.Instance.ExternalUrl}/gameAssets/{photo.LargeHash}",
|
||||
Color = WebhookHelper.UnionColor,
|
||||
}
|
||||
);
|
||||
|
||||
return this.Ok();
|
||||
}
|
||||
|
||||
[HttpGet("photos/user/{id:int}")]
|
||||
public async Task<IActionResult> SlotPhotos(int id)
|
||||
{
|
||||
List<Photo> photos = await this.database.Photos.Include(p => p.Creator).Take(10).ToListAsync();
|
||||
string response = photos.Aggregate(string.Empty, (s, photo) => s + photo.Serialize(id));
|
||||
return this.Ok(LbpSerializer.StringElement("photos", response));
|
||||
}
|
||||
|
||||
[HttpGet("photos/by")]
|
||||
public async Task<IActionResult> UserPhotosBy([FromQuery] string user, [FromQuery] int pageStart, [FromQuery] int pageSize)
|
||||
{
|
||||
User? userFromQuery = await this.database.Users.FirstOrDefaultAsync(u => u.Username == user);
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
||||
if (userFromQuery == null) return this.NotFound();
|
||||
|
||||
List<Photo> photos = await this.database.Photos.Include
|
||||
(p => p.Creator)
|
||||
.Where(p => p.CreatorId == userFromQuery.UserId)
|
||||
.OrderByDescending(s => s.Timestamp)
|
||||
.Skip(pageStart - 1)
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.ToListAsync();
|
||||
string response = photos.Aggregate(string.Empty, (s, photo) => s + photo.Serialize(0));
|
||||
return this.Ok(LbpSerializer.StringElement("photos", response));
|
||||
}
|
||||
|
||||
[HttpGet("photos/with")]
|
||||
public async Task<IActionResult> UserPhotosWith([FromQuery] string user, [FromQuery] int pageStart, [FromQuery] int pageSize)
|
||||
{
|
||||
User? userFromQuery = await this.database.Users.FirstOrDefaultAsync(u => u.Username == user);
|
||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
||||
if (userFromQuery == null) return this.NotFound();
|
||||
|
||||
List<Photo> photos = new();
|
||||
foreach (Photo photo in this.database.Photos.Include
|
||||
(p => p.Creator)) photos.AddRange(photo.Subjects.Where(subject => subject.User.UserId == userFromQuery.UserId).Select(_ => photo));
|
||||
|
||||
string response = photos.OrderByDescending
|
||||
(s => s.Timestamp)
|
||||
.Skip(pageStart - 1)
|
||||
.Take(Math.Min(pageSize, 30))
|
||||
.Aggregate(string.Empty, (s, photo) => s + photo.Serialize(0));
|
||||
|
||||
return this.Ok(LbpSerializer.StringElement("photos", response));
|
||||
}
|
||||
|
||||
[HttpPost("deletePhoto/{id:int}")]
|
||||
public async Task<IActionResult> DeletePhoto(int id)
|
||||
{
|
||||
User? user = await this.database.UserFromGameRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
Photo? photo = await this.database.Photos.FirstOrDefaultAsync(p => p.PhotoId == id);
|
||||
if (photo == null) return this.NotFound();
|
||||
if (photo.CreatorId != user.UserId) return this.StatusCode(401, "");
|
||||
|
||||
this.database.Photos.Remove(photo);
|
||||
await this.database.SaveChangesAsync();
|
||||
return this.Ok();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
#nullable enable
|
||||
using System.Xml.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Files;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using IOFile = System.IO.File;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Resources;
|
||||
|
||||
[ApiController]
|
||||
[Produces("text/xml")]
|
||||
[Route("LITTLEBIGPLANETPS3_XML")]
|
||||
public class ResourcesController : ControllerBase
|
||||
{
|
||||
private readonly Database database;
|
||||
|
||||
public ResourcesController(Database database)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpPost("showModerated")]
|
||||
public IActionResult ShowModerated() => this.Ok(LbpSerializer.BlankElement("resources"));
|
||||
|
||||
[HttpPost("filterResources")]
|
||||
[HttpPost("showNotUploaded")]
|
||||
public async Task<IActionResult> FilterResources()
|
||||
{
|
||||
User? user = await this.database.UserFromGameRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
|
||||
XmlSerializer serializer = new(typeof(ResourceList));
|
||||
ResourceList? resourceList = (ResourceList?)serializer.Deserialize(new StringReader(bodyString));
|
||||
|
||||
if (resourceList == null) return this.BadRequest();
|
||||
|
||||
string resources = resourceList.Resources.Where
|
||||
(s => !FileHelper.ResourceExists(s))
|
||||
.Aggregate("", (current, hash) => current + LbpSerializer.StringElement("resource", hash));
|
||||
|
||||
return this.Ok(LbpSerializer.StringElement("resources", resources));
|
||||
}
|
||||
|
||||
[HttpGet("r/{hash}")]
|
||||
public async Task<IActionResult> GetResource(string hash)
|
||||
{
|
||||
User? user = await this.database.UserFromGameRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
string path = FileHelper.GetResourcePath(hash);
|
||||
|
||||
if (FileHelper.ResourceExists(hash)) return this.File(IOFile.OpenRead(path), "application/octet-stream");
|
||||
|
||||
return this.NotFound();
|
||||
}
|
||||
|
||||
// TODO: check if this is a valid hash
|
||||
[HttpPost("upload/{hash}/unattributed")]
|
||||
[HttpPost("upload/{hash}")]
|
||||
public async Task<IActionResult> UploadResource(string hash)
|
||||
{
|
||||
User? user = await this.database.UserFromGameRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
string assetsDirectory = FileHelper.ResourcePath;
|
||||
string path = FileHelper.GetResourcePath(hash);
|
||||
|
||||
FileHelper.EnsureDirectoryCreated(assetsDirectory);
|
||||
// lbp treats code 409 as success and as an indicator that the file is already present
|
||||
if (FileHelper.ResourceExists(hash)) this.Conflict();
|
||||
|
||||
Logger.LogInfo($"Processing resource upload (hash: {hash})", LogArea.Resources);
|
||||
LbpFile file = new(await BinaryHelper.ReadFromPipeReader(this.Request.BodyReader));
|
||||
|
||||
if (!FileHelper.IsFileSafe(file))
|
||||
{
|
||||
Logger.LogWarn($"File is unsafe (hash: {hash}, type: {file.FileType})", LogArea.Resources);
|
||||
return this.Conflict();
|
||||
}
|
||||
|
||||
string calculatedHash = file.Hash;
|
||||
if (calculatedHash != hash)
|
||||
{
|
||||
Logger.LogWarn
|
||||
($"File hash does not match the uploaded file! (hash: {hash}, calculatedHash: {calculatedHash}, type: {file.FileType})", LogArea.Resources);
|
||||
return this.Conflict();
|
||||
}
|
||||
|
||||
Logger.LogSuccess($"File is OK! (hash: {hash}, type: {file.FileType})", LogArea.Resources);
|
||||
await IOFile.WriteAllBytesAsync(path, file.Data);
|
||||
return this.Ok();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue