/* ArozOS Office Suite - document backend Opens and saves the suite's documents. Docs, Sheets and Slides live in .docx / .xlsx / .pptx files: office.saveDocument writes the OOXML plus the editor's own envelope embedded in the package, and office.loadDocument hands the envelope back - the embedded copy when nothing else has changed the file, an import of the OOXML when Word, Excel or PowerPoint has (see mod/office native.go). Media handling: documents reference media through media?file= links (either the user's own files or working copies, below). office.saveDocument reads those files server side, so the pictures never travel from the browser on a save, and office.loadDocument extracts a document's media into a working folder and links it, so they are streamed rather than sent as base64 on an open. Working copies belong to one editor window (the "instance" parameter, a random id the page makes) and live under tmp:/, which the nightly tmp sweep empties of anything a day old: tmp:/.appdata/Office/cache// media of the opened documents tmp:/.appdata/Office/uploads// media dropped in from the device tmp:/.appdata/Office/tmp/ request payloads the front end uploaded instead of posting (agirunLarge), gzipped where the browser can compress - consumed and deleted at once The window deletes its folders when it closes ("release") and keeps them fresh while it stays open ("touch"); office.saveDocument refuses to save if a picture's working copy is gone anyway, rather than drop it. Kept for good: user:/.appdata/Office/session/ - the rolling per-app snapshots for "Restore from previous session" (self-contained zips). POST parameters: action = "save" | "load" | "prepare" | "touch" | "release" | "session-save" | "session-load" | "session-delete" save: filepath (.docx/.xlsx/.pptx), content (envelope JSON) -> "OK" contentFile may replace content: vpath of an uploaded payload file (deleted after reading) load: filepath, instance -> {"envelope": {...}} prepare: instance -> "OK" (folders exist) touch: instance (the window is still open) -> "OK" release: instance (the window closed) -> "OK" session-save: app, content(envelope JSON) -> "OK" session-load: app, instance -> {"envelope": {...}} or {"none": true} session-delete: app (drop the snapshot so it stops -> "OK" prompting on next launch) */ var WORKDIR = "user:/.appdata/Office"; var TMPWORK = "tmp:/.appdata/Office"; var DAY = 24 * 60 * 60; /* The editor window's own id. A page made before the working copies moved to tmp:/ sends none; it shares one folder that only the nightly sweep clears. */ function instanceId(){ if (typeof instance === "undefined" || !instance){ return "shared"; } var id = String(instance); if (!/^[a-z0-9]{6,40}$/.test(id)){ return "shared"; } return id; } function cacheDir(){ return TMPWORK + "/cache/" + instanceId(); } function uploadDir(){ return TMPWORK + "/uploads/" + instanceId(); } function ensureWorkdir(){ if (!requirelib("filelib")){ return false; } var dirs = [ "user:/.appdata", WORKDIR, WORKDIR + "/session", "tmp:/.appdata", TMPWORK, TMPWORK + "/cache", TMPWORK + "/uploads", TMPWORK + "/tmp", cacheDir(), uploadDir() ]; for (var i = 0; i < dirs.length; i++){ if (!filelib.fileExists(dirs[i])){ filelib.mkdir(dirs[i]); } } return true; } /* Drop leftovers a day old: agirunLarge payloads from requests that were interrupted before the backend ran, working folders of windows that closed without saying so, and the folders earlier versions kept under user:/ (where nothing ever removed them). The nightly tmp sweep would get the tmp:/ ones too; this keeps them from waiting for it. */ function sweepLeftovers(){ var roots = [ TMPWORK + "/tmp", TMPWORK + "/cache", TMPWORK + "/uploads", WORKDIR + "/tmp", WORKDIR + "/cache", WORKDIR + "/uploads" ]; for (var i = 0; i < roots.length; i++){ try { office.releaseWorkdir(roots[i], DAY); } catch (e) { //non-fatal: a failed sweep must never block the workdir setup } } } function safeAppName(name){ return String(name || "app").replace(/[^a-zA-Z0-9_-]/g, "").substring(0, 32); } /* The front end uploads a big payload as a file instead of putting it in the POST body (OfficeApp.agirunLarge in common/office.js): Go's r.ParseForm drops EVERY parameter once a urlencoded body passes 10MB, and a compressed upload is much smaller on a slow link anyway. Read it back from contentFile when that path was taken (office.readPayload gunzips it), and remove the temp file once it is in. */ function resolveContent(){ if (typeof contentFile !== "undefined" && contentFile){ if (!requirelib("filelib")){ return ""; } var loaded = requirelib("office") ? office.readPayload(contentFile) : filelib.readFile(contentFile); filelib.deleteFile(contentFile); return loaded; } return (typeof content === "undefined" || !content) ? "" : content; } function requireFilepath(){ if (typeof filepath === "undefined" || !filepath || filepath.trim() === ""){ sendJSONResp(JSON.stringify({ error: "filepath is required" })); return false; } return true; } function main(){ if (typeof action === "undefined"){ sendJSONResp(JSON.stringify({ error: "action is required" })); return; } if (!requirelib("office")){ sendJSONResp(JSON.stringify({ error: "office lib unavailable - please update your ArozOS host" })); return; } if (action == "prepare"){ if (!ensureWorkdir()){ sendJSONResp(JSON.stringify({ error: "filelib unavailable" })); return; } sweepLeftovers(); sendResp("OK"); return; } if (action == "touch"){ try { office.touchWorkdir(cacheDir(), uploadDir()); } catch (e) { sendJSONResp(JSON.stringify({ error: "touch failed: " + e.message })); return; } sendResp("OK"); return; } if (action == "release"){ //the shared folder of an old page is left to the sweeps if (instanceId() != "shared"){ try { office.releaseWorkdir(cacheDir()); office.releaseWorkdir(uploadDir()); } catch (e) { sendJSONResp(JSON.stringify({ error: "release failed: " + e.message })); return; } } sendResp("OK"); return; } if (action == "save"){ if (!requireFilepath()){ return; } var payload = resolveContent(); if (!payload){ sendJSONResp(JSON.stringify({ error: "content is required" })); return; } try { if (!office.saveDocument(payload, filepath)){ sendJSONResp(JSON.stringify({ error: "unable to write the document" })); return; } sendResp("OK"); } catch (e) { sendJSONResp(JSON.stringify({ error: "save failed: " + e.message })); } return; } if (action == "load"){ if (!requireFilepath()){ return; } try { ensureWorkdir(); var envelope = office.loadDocument(filepath, cacheDir()); if (!envelope){ sendJSONResp(JSON.stringify({ error: "unable to read the document" })); return; } //envelope is already a JSON string - concat instead of re-encoding sendJSONResp('{"envelope":' + envelope + '}'); } catch (e) { sendJSONResp(JSON.stringify({ error: "load failed: " + e.message })); } return; } if (action == "session-save"){ var sessPayload = resolveContent(); if (typeof app === "undefined" || !sessPayload){ sendJSONResp(JSON.stringify({ error: "app and content are required" })); return; } try { ensureWorkdir(); var dest = WORKDIR + "/session/" + safeAppName(app) + ".osession"; if (!office.packToFile(sessPayload, dest)){ sendJSONResp(JSON.stringify({ error: "unable to write the session snapshot" })); return; } sendResp("OK"); } catch (e) { sendJSONResp(JSON.stringify({ error: "session save failed: " + e.message })); } return; } if (action == "session-load"){ if (typeof app === "undefined"){ sendJSONResp(JSON.stringify({ error: "app is required" })); return; } if (!requirelib("filelib")){ sendJSONResp(JSON.stringify({ none: true })); return; } try { var src = WORKDIR + "/session/" + safeAppName(app) + ".osession"; if (!filelib.fileExists(src)){ sendJSONResp(JSON.stringify({ none: true })); return; } ensureWorkdir(); var sess = office.unpackToWorkdir(src, cacheDir()); if (!sess){ sendJSONResp(JSON.stringify({ none: true })); return; } sendJSONResp('{"envelope":' + sess + '}'); } catch (e) { sendJSONResp(JSON.stringify({ none: true, error: e.message })); } return; } if (action == "session-delete"){ if (typeof app === "undefined"){ sendJSONResp(JSON.stringify({ error: "app is required" })); return; } if (!requirelib("filelib")){ sendJSONResp(JSON.stringify({ error: "filelib unavailable" })); return; } try { var target = WORKDIR + "/session/" + safeAppName(app) + ".osession"; //deleting a non-existent snapshot is a no-op success if (filelib.fileExists(target)){ filelib.deleteFile(target); } sendResp("OK"); } catch (e) { sendJSONResp(JSON.stringify({ error: "session delete failed: " + e.message })); } return; } sendJSONResp(JSON.stringify({ error: "unknown action: " + action })); } main();