JSON definition · shared/definitions/creator-uploads.json
View on GitHub
{
"title": "Customer support",
"description": "Tell us how we can help",
"elements": [
{
"type": "image",
"name": "help_image",
"imageLink": "/shared/samples/support-help.svg",
"altText": "Support resources",
"imageHeight": "180",
"imageWidth": "100%"
},
{
"type": "dropdown",
"name": "product",
"title": "Product",
"choices": [
"Form Library",
"Survey Creator",
"Dashboard",
"PDF Generator"
]
},
{
"type": "comment",
"name": "issue",
"title": "How can we help?",
"isRequired": true
}
]
}
Client · shared/client/creator-uploads.js · sjs:creator-uploads.client
View on GitHub
// Store each uploaded image separately and keep its URL in the form schema
creator.onUploadFile.add(async (_, options) => {
const label = options.propertyName === "logo" || options.elementType === "header" ? "Logo" : "Image";
uploads++; updateControls();
showError("");
try {
if (!options.files.length || options.files.some(file =>
file.size > 5242880 || !["image/png", "image/jpeg", "image/webp", "image/gif"].includes(file.type)))
throw new Error("Choose a PNG, JPEG, WebP, or GIF image up to 5 MB.");
const body = new FormData();
options.files.forEach(file => body.append("files[]", file));
const response = await fetch("/api/files", { method: "POST", body });
const ids = response.ok ? await response.json() : null;
if (!Array.isArray(ids) || ids.length !== options.files.length ||
ids.some(id => typeof id !== "string" || !id))
throw new Error(`Couldn't upload the ${label.toLowerCase()}. Please try again.`);
options.callback("success", `/api/files/${encodeURIComponent(ids[0])}`);
note(`${label} uploaded.`, "ok");
} catch (error) {
options.callback("error");
showError(error.message);
note(error.message, "error");
} finally { uploads--; updateControls(); }
});
Server · routes/store-files.js · sjs:store-files.server
View on GitHub
// POST /api/files — store uploads, return their ids in the same order
// Field "files[]", the name on every platform: PHP keeps only the last of several fields named "files"
app.post("/api/files", upload.array("files[]"), (req, res) => {
const files = req.files ?? [];
if (files.length === 0) throw httpError(422, 'Upload one or more files in a field named "files[]"');
const types = files.map((file) => detectType(file.buffer)); // by the bytes, never the client's Content-Type
const refused = files.find((file, i) => !TYPES.includes(types[i]));
if (refused) throw httpError(422, `${refused.originalname} is not a PNG, JPEG, WebP, GIF or PDF file`);
const ids = files.map((file, i) => {
const id = randomUUID(); // stored by id, never by the client's file name
writeFileSync(join(filesDir(req), id), file.buffer);
db(req).prepare("INSERT INTO files (id, name, type) VALUES (?, ?, ?)").run(id, file.originalname, types[i]);
return id;
});
res.json(ids);
}, uploadErrors);
Server · routes/creator-load-save.js · sjs:creator-load-save.server
View on GitHub
// PUT /api/forms/:id — store the definition as-is: it's one JSON document
app.put("/api/forms/:id", (req, res) => {
const denied = can(req.user, "edit-forms"); // editing is an admin action: 403 otherwise
if (denied) throw httpError(403, denied);
if (req.body === undefined) throw httpError(400, "Expected a form definition"); // not a JSON body
// req.body is the parsed raw body: {} stays {}; store it with JSON.stringify, never a copy
db(req).prepare("INSERT INTO forms (key, json) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET json = excluded.json")
.run(req.params.id, JSON.stringify(req.body));
res.sendStatus(204); // the next page load renders the new version
});