Avoid repeated appearances of parse_date_string to reduce the amount of data downloaded

This commit is contained in:
2026-03-10 01:59:47 +08:00
parent caf6ae9416
commit 6cb4d56e3d
8 changed files with 112 additions and 17 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

+1 -1
View File
@@ -1,5 +1,5 @@
{
"name": "express-jtw-update-server",
"name": "express-storage-site",
"version": "1.0.0",
"description": "",
"main": "index.js",
+14
View File
@@ -0,0 +1,14 @@
import dotenv from 'dotenv'
dotenv.config()
const header_string = (page_title: string | boolean, notitle = false, add: string = null) => {
return `
<head>
${notitle ? "" : `<title>${process.env.servicename || "File Server"}${page_title ? ` | ${page_title}` : ""}</title>`}
${/*<link rel="icon" type="image/x-icon" href="/favicon.ico">*/""}
${add ? add : ""}
</head>
`
}
export default header_string;
+11 -2
View File
@@ -162,13 +162,22 @@ async function posts_string(user?: UserSession): Promise<string> {
out += `
<div class="postlist" onClick="location.href='/posts/${p.hash_id}'">
<a href="/posts/${p.hash_id}">${title}</a> ${p.visibility == "public" ? "" : `(${p.visibility})`} <br>
<small>by ${await getUsernameById(p.user_id)} (<span id="time_span${times}"></span><script>${parse_date_string}; document.getElementById("time_span${times}").innerHTML = parseDate(new Date(toIso("${p.created_at.replace(" ", "T") + ".000Z"}")));</script>)</small><br><br>
<small>by ${await getUsernameById(p.user_id)} (<span class="time" data-date="${p.created_at}" id="time_span${times}"></span>)</small><br><br>
${content}
</div>
`
};
resolve(out || "No posts yet.");
resolve(out + `
<script>
document.addEventsListener("DOMContentLoaded", () => {
document.querySelectorAll(".time").forEach(span => {
const raw = span.dataset.date.replace(" ", "T") + ".000Z";
span.textContent = parseDate(new Date(toIso(raw)), false, true);
})
})
</script>
` || "No posts yet.");
});
});
}
+9 -9
View File
@@ -78,20 +78,20 @@ export async function Main(app: Express) {
db.run(
`INSERT INTO users (username, password_hash) VALUES (?, ?)`,
[username, passwordHash],
(err: any) => {
var isnameerror = false;
function (err: any) {
if (err) {
if (err.message.includes("UNIQUE")) {
isnameerror = true;
return res.send(register_string(`The username ${username} is already taken!`))
}
}
if (!isnameerror)
c.log(`User registered: ${username}`);
if (isnameerror)
res.send(register_string(`The username ${username} is already taken!`))
else
res.redirect(req.query?.next?.toString() ?? "/");
req.session.user = {
id: this.lastID,
username: username
}
c.log(`User registered: ${username}`);
res.redirect(req.query?.next?.toString() ?? "/");
}
);
} catch (err) {
+12 -5
View File
@@ -72,11 +72,7 @@ async function files_string(user: UserSession): Promise<string> {
out += `
<form method="POST" action="/delete" style="display:inline;">
${filect}) <a href="/uploads/${f.stored_name}" target="_blank">${f.original_name}</a> - uploaded on
<span id="time_span${times}"></span>
<script>
${parse_date_string}
document.getElementById("time_span${times}").innerHTML = parseDate(new Date(toIso("${f.upload_date.replace(" ", "T") + ".000Z"}")), false, true);
</script>
<span class=".time" id="time_span${times}" data-date="${f.upload_date}></span>
<input type="hidden" name="fileId" value="${f.id}">
| <button class="tonly" onclick="return confirm('Delete ${f.original_name}?')" type="submit">Delete</button>
</form><br>
@@ -84,6 +80,17 @@ async function files_string(user: UserSession): Promise<string> {
filect++;
});
out += `
<script>
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll(".time").forEach(span => {
const raw = span.dataset.date.replace(" ", "T") + ".000Z";
span.textContent = parseDate(new Date(toIso(raw)), false, true);
});
});
</script>
`
resolve(out);
}
);
+1
View File
@@ -31,6 +31,7 @@ function getUploadByFilename(filename: string): Promise<Upload> {
})
} catch (err) {
// if the cb fails somehow
// its not actually somehow it IS possible, like when a user is deleted from the users table or when a file is manually placed in the /uploads directory
return null
}
}
+64
View File
@@ -0,0 +1,64 @@
const p_404 = {
page: "The file you were looking for does not exist on the server."
}
const p_login = {
title: "Log in",
login: "Log in",
missing_credentials: "Missing username or password!",
wrong_credentials: "Invalid username or password."
}
const p_posts = {
title: "Posts",
by_user: "by",
new: {
post_title: "Post title:",
post_content: "Post content:",
new_post: "New Post",
visibility: "Visibility:",
public: "Public",
unlisted: "Unlisted",
private: "Private",
font: "Font:",
login: "You must be logged in to create a post.",
content_placeholder: "...",
post: "Post"
},
not_found: "Post not found.",
no_visible: "No posts yet.",
login: `<a href="/login?next=/posts/new">Log in</a> to create posts.`,
delete_post: "Delete Post"
}
const p_register = {
title: "Registration",
register: "Register",
name_taken: "The username $(var) is already taken!",
register_disabled: "$(var) does not support account registration."
}
const p_root = {
existing_session: "Logged in as $(var).",
logout_confirm: "Are you sure you want to log out of $(var)?"
}
const loc = {
username: "Username",
password: "Password",
default_title: "File Server",
default_description: "A simple file server built with Express.",
unknown_user: "Unknown",
log_in: "Login",
log_out: "Log out",
register: "Register",
back: "Back",
p_404, p_login, p_posts, p_register, p_root
}
export default loc