binsaver.agi 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. ArozOS Office Suite - binary FileSaver backend
  3. Writes a base64-encoded payload the web client produced to a file on
  4. the server. Used by the Slides PDF export, which is built in the
  5. browser because only the browser knows how its text laid out.
  6. Required POST parameters:
  7. filepath - virtual path to the target file (e.g. user:/Desktop/deck.pdf)
  8. content - the file's bytes, base64 encoded
  9. contentFile - vpath of an uploaded temp file holding that base64,
  10. used instead of content when the payload is too big for
  11. a POST form body (deleted after reading)
  12. Response: JSON {"error": "..."} on failure, "OK" on success
  13. */
  14. /*
  15. Same oversized-payload path as filesaver.agi: Go's r.ParseForm drops
  16. EVERY parameter once a urlencoded body passes 10MB, so the front end
  17. streams the payload to a temp file and passes its vpath instead.
  18. */
  19. function resolveContent(){
  20. if (typeof contentFile !== "undefined" && contentFile){
  21. if (!requirelib("filelib")){
  22. return "";
  23. }
  24. var loaded = filelib.readFile(contentFile);
  25. filelib.deleteFile(contentFile);
  26. return loaded;
  27. }
  28. return (typeof content === "undefined" || !content) ? "" : content;
  29. }
  30. function main(){
  31. if (!requirelib("office")){
  32. sendJSONResp(JSON.stringify({ error: "office lib unavailable - please update your ArozOS host" }));
  33. return;
  34. }
  35. if (typeof filepath === "undefined" || !filepath || filepath.trim() === ""){
  36. sendJSONResp(JSON.stringify({ error: "filepath is required" }));
  37. return;
  38. }
  39. var payload = resolveContent();
  40. if (!payload){
  41. sendJSONResp(JSON.stringify({ error: "content is required" }));
  42. return;
  43. }
  44. try {
  45. if (!office.writeBinaryFile(payload, filepath)){
  46. sendJSONResp(JSON.stringify({ error: "unable to write file" }));
  47. return;
  48. }
  49. sendResp("OK");
  50. } catch (e) {
  51. sendJSONResp(JSON.stringify({ error: "write failed: " + e.message }));
  52. }
  53. }
  54. main();