diff --git a/.dockerignore b/.dockerignore index fe1152b..b2da574 100644 --- a/.dockerignore +++ b/.dockerignore @@ -27,4 +27,9 @@ README.md !.git/HEAD !.git/config !.git/packed-refs -!.git/refs/heads/** \ No newline at end of file +!.git/refs/heads/** + +SharpbinV2.Server/*.db +SharpbinV2.Server/logs +SharpbinV2.Server/pastes +SharpbinV2.Server/.env \ No newline at end of file diff --git a/.gitignore b/.gitignore index d23d46a..c5eeaab 100644 --- a/.gitignore +++ b/.gitignore @@ -70,29 +70,7 @@ artifacts/ # ASP.NET Scaffolding ScaffoldingReadMe.txt -# StyleCop -StyleCopReport.xml - -# Files built by Visual Studio -*_i.c -*_p.c -*_h.h -*.ilk -*.meta -*.obj -*.iobj -*.pch -*.pdb -*.ipdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj +# StyleCop6 *_wpftmp.csproj *.log *.vspscc @@ -204,22 +182,7 @@ PublishScripts/ **/[Pp]ackages/* # except build/, which is used as an MSBuild target. !**/[Pp]ackages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/[Pp]ackages/repositories.config -# NuGet v3's project.json files produces more ignorable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ +# Uncomment if necessar6 BundleArtifacts/ Package.StoreAssociation.xml _pkginfo.txt @@ -320,18 +283,7 @@ paket-files/ __pycache__/ *.pyc -# Cake - Uncomment if you are using it -# tools/** -# !tools/packages.config - -# Tabs Studio -*.tss - -# Telerik's JustMock configuration file -*.jmconfig - -# BizTalk build output -*.btp.cs +# Cake - Uncomment i6 *.btm.cs *.odx.cs *.xsd.cs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..eabdc4f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + + + + ] +} \ No newline at end of file diff --git a/SharpbinV2.Server/Program.cs b/SharpbinV2.Server/Program.cs index aa12c3f..fddbd22 100644 --- a/SharpbinV2.Server/Program.cs +++ b/SharpbinV2.Server/Program.cs @@ -16,7 +16,7 @@ namespace SharpbinV2.Server class Program { public static string MainDatabaseConnection = "Data Source=data.db"; - public static long MaxFileSize = 5000000; // 5MB + public static long MaxFileSize = 1_000_000; // 1MB public static string[] ValidSyntaxLanguages = { "none", @@ -104,7 +104,9 @@ namespace SharpbinV2.Server #region Static Endpoints app.MapGet("/", (HttpContext context) => { - context.Response.Redirect("/index.html"); + context.Response.StatusCode = 200; + context.Response.Headers.Add("Content-Type", "text/html"); + return context.Response.SendFileAsync("wwwroot/index.html"); }); app.MapGet("/{pasteid}", async (HttpContext context, IWebHostEnvironment env) => { @@ -185,6 +187,10 @@ namespace SharpbinV2.Server context.Response.Headers.Add("Content-Type", "text/html"); await context.Response.SendFileAsync("wwwroot/error.html"); }); + app.MapGet("/error.html", (HttpContext context) => + { + context.Response.Redirect("/error"); + }); app.MapGet("/raw/{pasteid}", async (HttpContext context) => { var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null; @@ -217,6 +223,12 @@ namespace SharpbinV2.Server { context.Response.Redirect("/"); }); + app.MapGet("/archive", async (HttpContext context) => + { + context.Response.StatusCode = 200; + context.Response.Headers.Add("Content-Type", "text/html"); + await context.Response.SendFileAsync("wwwroot/archive.html"); + }); #endregion #region API @@ -458,6 +470,13 @@ namespace SharpbinV2.Server var user = await Database.UserFromToken(requestdetails.Token); var queries = context.Request.Query; + if (queries.ContainsKey("title") && queries["title"].ToString().Length > 500) + { + context.Response.StatusCode = 400; + await context.Response.WriteAsJsonAsync(new { success = false, message = "Title too long." }); + return; + } + paste.Created = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); paste.Size = body.Length; @@ -467,7 +486,7 @@ namespace SharpbinV2.Server paste.AuthorUUID = user?.UUID ?? "0"; // 0 means the user is anonymous paste.FilePath = $"pastes/{paste.ID}.txt"; paste.Visibility = queries.ContainsKey("visibility") ? Convert.ToInt32(queries["visibility"]) : 0; - paste.Title = queries.ContainsKey("title") ? queries["title"].ToString() : $"Untitled-{await Database.EnumeratePastes()}"; + paste.Title = queries.ContainsKey("title") && !string.IsNullOrEmpty(queries["title"].ToString()) ? queries["title"].ToString() : $"Untitled-{await Database.EnumeratePastes()}"; paste.Edited = 0; paste.Views = 0; paste.Syntax = queries.ContainsKey("syntax") ? queries["syntax"].ToString() : null; @@ -573,6 +592,74 @@ namespace SharpbinV2.Server } return; }); + app.MapGet("/api/pastes/archive", async (HttpContext context) => + { + var queries = context.Request.Query; + if (!queries.ContainsKey("page") && !queries.ContainsKey("limit")) + { + context.Response.StatusCode = 400; + await context.Response.WriteAsJsonAsync(new { success = false, message = "Missing required fields." }); + return; + } + var page = queries.ContainsKey("page") ? Convert.ToInt32(queries["page"]) : 0; + var limit = queries.ContainsKey("limit") ? Convert.ToInt32(queries["limit"]) : 10; + int pages = await Database.EnumeratePastes() / limit; + + if (page < 0 || limit <= 0 || limit > 25) + { + context.Response.StatusCode = 400; + await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid page or limit." }); + return; + } + if (page > pages) + { + context.Response.StatusCode = 400; + await context.Response.WriteAsJsonAsync(new { success = false, message = "Page out of range.", pages }); + return; + } + + using (var connection = new SqliteConnection(MainDatabaseConnection)) + { + await connection.OpenAsync(); + using (var command = connection.CreateCommand()) + { + command.CommandText = "SELECT * FROM pastes WHERE Visibility NOT IN (1, 2) ORDER BY Created DESC LIMIT @Limit OFFSET @Offset;"; + command.Parameters.AddWithValue("@Limit", limit); + command.Parameters.AddWithValue("@Offset", page * limit); + using (var reader = await command.ExecuteReaderAsync()) + { + if (!reader.HasRows) + { + context.Response.StatusCode = 400; + await context.Response.WriteAsJsonAsync(new { success = false, message = "No pastes found." }); + return; + } + var pastes = new List(); + while (await reader.ReadAsync()) + { + pastes.Add(new Paste + { + UUID = reader.GetString(1), + ID = reader.GetString(2), + Visibility = reader.GetInt32(3), + Title = reader.IsDBNull(4) ? null : reader.GetString(4), + AuthorUUID = reader.GetString(5), + Created = reader.GetInt64(7), + Edited = reader.GetInt64(8), + Size = reader.GetInt32(9), + TrueSize = reader.GetInt32(10), + Views = reader.GetInt32(11), + Syntax = reader.IsDBNull(12) ? null : reader.GetString(12) + }); + } + + context.Response.StatusCode = 200; + await context.Response.WriteAsJsonAsync(new { success = true, pastes, pages }); + return; + } + } + } + }); #endregion diff --git a/SharpbinV2.Server/wwwroot/archive.html b/SharpbinV2.Server/wwwroot/archive.html new file mode 100644 index 0000000..ca8824a --- /dev/null +++ b/SharpbinV2.Server/wwwroot/archive.html @@ -0,0 +1,44 @@ + + + + + + Paste Archives - Sharpbin + + + + + + + + + + +
+

Paste Archives

+
+

Title

+

Date

+

Syntax

+
+
    + +
+
+ +

0/0

+ +
+
+ + + + \ No newline at end of file diff --git a/SharpbinV2.Server/wwwroot/css/archive.css b/SharpbinV2.Server/wwwroot/css/archive.css new file mode 100644 index 0000000..d1961ca --- /dev/null +++ b/SharpbinV2.Server/wwwroot/css/archive.css @@ -0,0 +1,62 @@ +.archive-list { + list-style-type: none; + margin: 0; + padding: 0; + width: 100%; + max-height: 100%; + overflow-y: scroll; +} +.archive-list li { + margin: 5px 0 5px 0; + padding: 0; + width: 100%; +} +.archive-list li a { + display: block; + padding: 1rem; + background-color: var(--neutral-800); + color: var(--neutral-50); + text-decoration: none; + transition: background-color 0.3s; + border-radius: 5px; + border: 1px solid var(--neutral-700); + width: 100%; + display: flex; + justify-content: space-between; +} +#archive-title { + min-width: 50%; + max-width: 50%; +} +.archive-information { + padding: 0 2px 0 2px; + width: 100%; + display: flex; + justify-content: space-between; +} +.main-container { + max-height: 80% !important; +} +.paste-actions { + width: 100%; + margin-top: 5px; + display: flex; + justify-content: space-between; + align-items: center; +} + +button { + padding: 10px 40px 10px 40px; + margin: 5px 0; + border-radius: 5px; + border: 1px solid var(--neutral-800); + background-color: var(--neutral-800); + color: var(--neutral-50); + cursor: pointer; + transition: background-color 0.3s; + font-size: 1rem; +} + +button:hover { + background-color: var(--neutral-700); +} \ No newline at end of file diff --git a/SharpbinV2.Server/wwwroot/js/archive.js b/SharpbinV2.Server/wwwroot/js/archive.js new file mode 100644 index 0000000..d8d85f2 --- /dev/null +++ b/SharpbinV2.Server/wwwroot/js/archive.js @@ -0,0 +1,111 @@ +pages = 0; + +document.addEventListener('DOMContentLoaded', async () => { + var queries = new URLSearchParams(window.location.search); + var page = queries.get("page"); + var limit = queries.get("limit"); + + if (parseInt(page).isNaN) { + page = 0; + } + + var archiveList = document.getElementById("archives"); + var archiveListHTML = ""; + var pagesElement = document.getElementById("paste-pages"); + + if (page == null || page < 0) { + page = 0; + } + if (limit == null || limit < 1 || limit > 25) { + limit = 25; + } + + var archiveData = await getPastes(page, limit); + + var pastes = archiveData.pastes; + pages = archiveData.pages; + pagesElement.innerText = `${parseInt(page) + 1}/${pages + 1}`; + + + pastes.forEach(paste => { + var title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title; + archiveListHTML += ` +
  • + +

    ${title}

    +

    ${formatDate(paste.created)}

    +

    ${paste.syntax}

    +
    +
  • + `; + }); + + archiveList.innerHTML = archiveListHTML; +}); + +async function getPastes(page, limit) { + const response = await fetch(`/api/pastes/archive?page=${page}&limit=${limit}`); + const data = await response.json(); + return data; +} + +function formatDate(unix) { + if (typeof unix !== 'number' || isNaN(unix)) { + return 'Invalid date'; + } + var nowunix = Math.floor(Date.now() / 1000); + var diff = nowunix - unix; + + if (diff < 60) { + return `${diff} second${diff !== 1 ? 's' : ''} ago`; + } else if (diff < 3600) { + const minutes = Math.floor(diff / 60); + return `${minutes} minute${minutes !== 1 ? 's' : ''} ago`; + } else if (diff < 86400) { + const hours = Math.floor(diff / 3600); + return `${hours} hour${hours !== 1 ? 's' : ''} ago`; + } else { + const days = Math.floor(diff / 86400); + return `${days} day${days !== 1 ? 's' : ''} ago`; + } +} + +function nextPage() { + var queries = new URLSearchParams(window.location.search); + var page = queries.get("page"); + var limit = queries.get("limit"); + + if (page == null || page < 0) { + page = 0; + } + if (limit == null || limit < 1 || limit > 25) { + limit = 25; + } + + if (page + 1 > pages) { + return; + } + + page++; + window.location.href = `/archive?page=${page}&limit=${limit}`; +} + +function previousPage() { + var queries = new URLSearchParams(window.location.search); + var page = queries.get("page"); + var limit = queries.get("limit"); + + if (page == null || page < 0) { + page = 0; + } + if (limit == null || limit < 1 || limit > 25) { + limit = 25; + } + + if (page < pages) { + return; + } + + page--; + window.location.href = `/archive?page=${page}&limit=${limit}`; +} \ No newline at end of file diff --git a/SharpbinV2.Server/wwwroot/js/index.js b/SharpbinV2.Server/wwwroot/js/index.js index c21cd2f..e16894c 100644 --- a/SharpbinV2.Server/wwwroot/js/index.js +++ b/SharpbinV2.Server/wwwroot/js/index.js @@ -15,7 +15,6 @@ document.getElementById("create-form").addEventListener("submit", async function if (visibility === "2" && password !== "" && password.length >= 6) { content = await encryptAES(content, password); - console.log(content); } else if (visibility === "2") { alert("Password must be at least 6 characters long"); diff --git a/SharpbinV2.Server/wwwroot/js/navbar.js b/SharpbinV2.Server/wwwroot/js/navbar.js index 0af76e5..53c5c05 100644 --- a/SharpbinV2.Server/wwwroot/js/navbar.js +++ b/SharpbinV2.Server/wwwroot/js/navbar.js @@ -2,8 +2,8 @@ const navbarHTML = `