document.agi 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. ArozOS Office Suite - document backend
  3. Opens and saves the suite's documents. Docs, Sheets and Slides live in
  4. .docx / .xlsx / .pptx files: office.saveDocument writes the OOXML plus
  5. the editor's own envelope embedded in the package, and office.loadDocument
  6. hands the envelope back - the embedded copy when nothing else has changed
  7. the file, an import of the OOXML when Word, Excel or PowerPoint has (see
  8. mod/office native.go).
  9. Media handling: documents reference media through media?file= links
  10. (either the user's own files or working copies, below). office.saveDocument
  11. reads those files server side, so the pictures never travel from the
  12. browser on a save, and office.loadDocument extracts a document's media
  13. into a working folder and links it, so they are streamed rather than sent
  14. as base64 on an open.
  15. Working copies belong to one editor window (the "instance" parameter, a
  16. random id the page makes) and live under tmp:/, which the nightly tmp
  17. sweep empties of anything a day old:
  18. tmp:/.appdata/Office/cache/<instance>/ media of the opened documents
  19. tmp:/.appdata/Office/uploads/<instance>/ media dropped in from the device
  20. tmp:/.appdata/Office/tmp/ request payloads the front end
  21. uploaded instead of posting (agirunLarge), gzipped where the
  22. browser can compress - consumed and deleted at once
  23. The window deletes its folders when it closes ("release") and keeps them
  24. fresh while it stays open ("touch"); office.saveDocument refuses to save
  25. if a picture's working copy is gone anyway, rather than drop it.
  26. Kept for good: user:/.appdata/Office/session/ - the rolling per-app
  27. snapshots for "Restore from previous session" (self-contained zips).
  28. POST parameters:
  29. action = "save" | "load" | "prepare" | "touch" | "release" |
  30. "session-save" | "session-load" | "session-delete"
  31. save: filepath (.docx/.xlsx/.pptx), content (envelope JSON) -> "OK"
  32. contentFile may replace content: vpath of an uploaded
  33. payload file (deleted after reading)
  34. load: filepath, instance -> {"envelope": {...}}
  35. prepare: instance -> "OK" (folders exist)
  36. touch: instance (the window is still open) -> "OK"
  37. release: instance (the window closed) -> "OK"
  38. session-save: app, content(envelope JSON) -> "OK"
  39. session-load: app, instance -> {"envelope": {...}} or {"none": true}
  40. session-delete: app (drop the snapshot so it stops -> "OK"
  41. prompting on next launch)
  42. */
  43. var WORKDIR = "user:/.appdata/Office";
  44. var TMPWORK = "tmp:/.appdata/Office";
  45. var DAY = 24 * 60 * 60;
  46. /*
  47. The editor window's own id. A page made before the working copies moved
  48. to tmp:/ sends none; it shares one folder that only the nightly sweep
  49. clears.
  50. */
  51. function instanceId(){
  52. if (typeof instance === "undefined" || !instance){
  53. return "shared";
  54. }
  55. var id = String(instance);
  56. if (!/^[a-z0-9]{6,40}$/.test(id)){
  57. return "shared";
  58. }
  59. return id;
  60. }
  61. function cacheDir(){
  62. return TMPWORK + "/cache/" + instanceId();
  63. }
  64. function uploadDir(){
  65. return TMPWORK + "/uploads/" + instanceId();
  66. }
  67. function ensureWorkdir(){
  68. if (!requirelib("filelib")){
  69. return false;
  70. }
  71. var dirs = [
  72. "user:/.appdata",
  73. WORKDIR,
  74. WORKDIR + "/session",
  75. "tmp:/.appdata",
  76. TMPWORK,
  77. TMPWORK + "/cache",
  78. TMPWORK + "/uploads",
  79. TMPWORK + "/tmp",
  80. cacheDir(),
  81. uploadDir()
  82. ];
  83. for (var i = 0; i < dirs.length; i++){
  84. if (!filelib.fileExists(dirs[i])){
  85. filelib.mkdir(dirs[i]);
  86. }
  87. }
  88. return true;
  89. }
  90. /*
  91. Drop leftovers a day old: agirunLarge payloads from requests that were
  92. interrupted before the backend ran, working folders of windows that
  93. closed without saying so, and the folders earlier versions kept under
  94. user:/ (where nothing ever removed them). The nightly tmp sweep would
  95. get the tmp:/ ones too; this keeps them from waiting for it.
  96. */
  97. function sweepLeftovers(){
  98. var roots = [
  99. TMPWORK + "/tmp", TMPWORK + "/cache", TMPWORK + "/uploads",
  100. WORKDIR + "/tmp", WORKDIR + "/cache", WORKDIR + "/uploads"
  101. ];
  102. for (var i = 0; i < roots.length; i++){
  103. try {
  104. office.releaseWorkdir(roots[i], DAY);
  105. } catch (e) {
  106. //non-fatal: a failed sweep must never block the workdir setup
  107. }
  108. }
  109. }
  110. function safeAppName(name){
  111. return String(name || "app").replace(/[^a-zA-Z0-9_-]/g, "").substring(0, 32);
  112. }
  113. /*
  114. The front end uploads a big payload as a file instead of putting it in
  115. the POST body (OfficeApp.agirunLarge in common/office.js): Go's
  116. r.ParseForm drops EVERY parameter once a urlencoded body passes 10MB, and
  117. a compressed upload is much smaller on a slow link anyway. Read it back
  118. from contentFile when that path was taken (office.readPayload gunzips it),
  119. and remove the temp file once it is in.
  120. */
  121. function resolveContent(){
  122. if (typeof contentFile !== "undefined" && contentFile){
  123. if (!requirelib("filelib")){
  124. return "";
  125. }
  126. var loaded = requirelib("office") ? office.readPayload(contentFile) : filelib.readFile(contentFile);
  127. filelib.deleteFile(contentFile);
  128. return loaded;
  129. }
  130. return (typeof content === "undefined" || !content) ? "" : content;
  131. }
  132. function requireFilepath(){
  133. if (typeof filepath === "undefined" || !filepath || filepath.trim() === ""){
  134. sendJSONResp(JSON.stringify({ error: "filepath is required" }));
  135. return false;
  136. }
  137. return true;
  138. }
  139. function main(){
  140. if (typeof action === "undefined"){
  141. sendJSONResp(JSON.stringify({ error: "action is required" }));
  142. return;
  143. }
  144. if (!requirelib("office")){
  145. sendJSONResp(JSON.stringify({ error: "office lib unavailable - please update your ArozOS host" }));
  146. return;
  147. }
  148. if (action == "prepare"){
  149. if (!ensureWorkdir()){
  150. sendJSONResp(JSON.stringify({ error: "filelib unavailable" }));
  151. return;
  152. }
  153. sweepLeftovers();
  154. sendResp("OK");
  155. return;
  156. }
  157. if (action == "touch"){
  158. try {
  159. office.touchWorkdir(cacheDir(), uploadDir());
  160. } catch (e) {
  161. sendJSONResp(JSON.stringify({ error: "touch failed: " + e.message }));
  162. return;
  163. }
  164. sendResp("OK");
  165. return;
  166. }
  167. if (action == "release"){
  168. //the shared folder of an old page is left to the sweeps
  169. if (instanceId() != "shared"){
  170. try {
  171. office.releaseWorkdir(cacheDir());
  172. office.releaseWorkdir(uploadDir());
  173. } catch (e) {
  174. sendJSONResp(JSON.stringify({ error: "release failed: " + e.message }));
  175. return;
  176. }
  177. }
  178. sendResp("OK");
  179. return;
  180. }
  181. if (action == "save"){
  182. if (!requireFilepath()){
  183. return;
  184. }
  185. var payload = resolveContent();
  186. if (!payload){
  187. sendJSONResp(JSON.stringify({ error: "content is required" }));
  188. return;
  189. }
  190. try {
  191. if (!office.saveDocument(payload, filepath)){
  192. sendJSONResp(JSON.stringify({ error: "unable to write the document" }));
  193. return;
  194. }
  195. sendResp("OK");
  196. } catch (e) {
  197. sendJSONResp(JSON.stringify({ error: "save failed: " + e.message }));
  198. }
  199. return;
  200. }
  201. if (action == "load"){
  202. if (!requireFilepath()){
  203. return;
  204. }
  205. try {
  206. ensureWorkdir();
  207. var envelope = office.loadDocument(filepath, cacheDir());
  208. if (!envelope){
  209. sendJSONResp(JSON.stringify({ error: "unable to read the document" }));
  210. return;
  211. }
  212. //envelope is already a JSON string - concat instead of re-encoding
  213. sendJSONResp('{"envelope":' + envelope + '}');
  214. } catch (e) {
  215. sendJSONResp(JSON.stringify({ error: "load failed: " + e.message }));
  216. }
  217. return;
  218. }
  219. if (action == "session-save"){
  220. var sessPayload = resolveContent();
  221. if (typeof app === "undefined" || !sessPayload){
  222. sendJSONResp(JSON.stringify({ error: "app and content are required" }));
  223. return;
  224. }
  225. try {
  226. ensureWorkdir();
  227. var dest = WORKDIR + "/session/" + safeAppName(app) + ".osession";
  228. if (!office.packToFile(sessPayload, dest)){
  229. sendJSONResp(JSON.stringify({ error: "unable to write the session snapshot" }));
  230. return;
  231. }
  232. sendResp("OK");
  233. } catch (e) {
  234. sendJSONResp(JSON.stringify({ error: "session save failed: " + e.message }));
  235. }
  236. return;
  237. }
  238. if (action == "session-load"){
  239. if (typeof app === "undefined"){
  240. sendJSONResp(JSON.stringify({ error: "app is required" }));
  241. return;
  242. }
  243. if (!requirelib("filelib")){
  244. sendJSONResp(JSON.stringify({ none: true }));
  245. return;
  246. }
  247. try {
  248. var src = WORKDIR + "/session/" + safeAppName(app) + ".osession";
  249. if (!filelib.fileExists(src)){
  250. sendJSONResp(JSON.stringify({ none: true }));
  251. return;
  252. }
  253. ensureWorkdir();
  254. var sess = office.unpackToWorkdir(src, cacheDir());
  255. if (!sess){
  256. sendJSONResp(JSON.stringify({ none: true }));
  257. return;
  258. }
  259. sendJSONResp('{"envelope":' + sess + '}');
  260. } catch (e) {
  261. sendJSONResp(JSON.stringify({ none: true, error: e.message }));
  262. }
  263. return;
  264. }
  265. if (action == "session-delete"){
  266. if (typeof app === "undefined"){
  267. sendJSONResp(JSON.stringify({ error: "app is required" }));
  268. return;
  269. }
  270. if (!requirelib("filelib")){
  271. sendJSONResp(JSON.stringify({ error: "filelib unavailable" }));
  272. return;
  273. }
  274. try {
  275. var target = WORKDIR + "/session/" + safeAppName(app) + ".osession";
  276. //deleting a non-existent snapshot is a no-op success
  277. if (filelib.fileExists(target)){
  278. filelib.deleteFile(target);
  279. }
  280. sendResp("OK");
  281. } catch (e) {
  282. sendJSONResp(JSON.stringify({ error: "session delete failed: " + e.message }));
  283. }
  284. return;
  285. }
  286. sendJSONResp(JSON.stringify({ error: "unknown action: " + action }));
  287. }
  288. main();