Update index.ts
This commit is contained in:
+7
-20
@@ -76,7 +76,7 @@ console.log("Server starting with valid 'secrets.json'.")
|
|||||||
|
|
||||||
function checkHeaderKey(headers: Record<string, string | undefined>, expectList: string[] = [INSTANT_KEY]) {
|
function checkHeaderKey(headers: Record<string, string | undefined>, expectList: string[] = [INSTANT_KEY]) {
|
||||||
const key = headers["x-instant-key"]
|
const key = headers["x-instant-key"]
|
||||||
if (!expectList.includes(key ?? "")) throw new HttpError(401, "Invalid authentication key")
|
if (!expectList.includes(key ?? "")) done(401, "Invalid authentication key")
|
||||||
}
|
}
|
||||||
|
|
||||||
export const app = new Elysia()
|
export const app = new Elysia()
|
||||||
@@ -189,25 +189,19 @@ export const app = new Elysia()
|
|||||||
|
|
||||||
// ----- 4. POST /edit -----
|
// ----- 4. POST /edit -----
|
||||||
// Edits a specific field in the metadata.
|
// Edits a specific field in the metadata.
|
||||||
.post("/edit", async ({ body, status }) => {
|
.post("/edit", async ({ headers, body, status }) => {
|
||||||
const { id, key, field, value } = body
|
const { id, key, field, value } = body
|
||||||
const metadata = await getMetadata()
|
const metadata = await getMetadata()
|
||||||
|
|
||||||
const photoIndex = metadata.findIndex((p) => p.id === id)
|
const photoIndex = metadata.findIndex((p) => p.id === id)
|
||||||
if (photoIndex === -1) throw
|
if (photoIndex === -1) done(404, "Photo not found")
|
||||||
|
|
||||||
const photo = metadata[photoIndex]
|
const photo = metadata[photoIndex]
|
||||||
|
checkHeaderKey(headers, [photo.owner_key, INSTANT_KEY])
|
||||||
// Check authentication (owner key OR site key)
|
|
||||||
if (key !== photo.owner_key && key !== INSTANT_KEY) {
|
|
||||||
throw new HttpError(401, "Invalid authentication key")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent editing core, protected fields
|
// Prevent editing core, protected fields
|
||||||
const protectedFields = ["id", "owner_key", "upload_time", "original_photo", "edited_photo"]
|
const protectedFields = ["id", "owner_key", "upload_time", "original_photo", "edited_photo"]
|
||||||
if (protectedFields.includes(field)) {
|
if (protectedFields.includes(field)) done(400, `Cannot edit protected field: ${field}`)
|
||||||
throw new HttpError(400, `Cannot edit protected field: ${field}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply the edit
|
// Apply the edit
|
||||||
console.log(`Editing photo ${id}: Set ${field} = ${value}`)
|
console.log(`Editing photo ${id}: Set ${field} = ${value}`)
|
||||||
@@ -215,16 +209,9 @@ export const app = new Elysia()
|
|||||||
metadata[photoIndex] = photo // Update the photo in the main array
|
metadata[photoIndex] = photo // Update the photo in the main array
|
||||||
|
|
||||||
await saveMetadata(metadata)
|
await saveMetadata(metadata)
|
||||||
|
done(200, { success: true, id, updated: { [field]: value } })
|
||||||
set.status = 200 // OK
|
|
||||||
return { success: true, id, updated: { [field]: value } }
|
|
||||||
}, {
|
}, {
|
||||||
body: t.Object({
|
body: t.Object({ id: t.String(), key: t.String(), field: t.String(), value: t.Any() })
|
||||||
id: t.String(),
|
|
||||||
key: t.String(),
|
|
||||||
field: t.String(),
|
|
||||||
value: t.Any() // Allow any type of value (boolean, string, null, etc.)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
.listen(3000)
|
.listen(3000)
|
||||||
|
|||||||
Reference in New Issue
Block a user