convert.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. Office format converters for the WebAssembly bridge
  3. ===================================================
  4. The conversion table the browser build exposes. It lives in its own file
  5. with no build constraint and no syscall/js import for two reasons:
  6. - it is the part with actual behaviour, so it can be table-tested by
  7. `go test ./...` on any platform (main.go is js/wasm-only glue that
  8. just walks these maps);
  9. - it documents, in one place, exactly which of mod/office's converters
  10. the standalone web edition can run.
  11. Everything here is a pure []byte/string transformation - mod/office does
  12. no file I/O and keeps no globals, which is what makes running it in a
  13. browser possible at all.
  14. The suite's own documents are .docx / .xlsx / .pptx (mod/office native.go),
  15. so the three "<app>File" converters are what every open and save of the
  16. web edition runs: in, a file becomes the editor envelope (the embedded copy
  17. when it is current, an import otherwise); out, the envelope becomes the
  18. file. The per-format body converters beside them serve the other formats
  19. (ODF) and keep the table symmetrical with the office.* AGI library.
  20. The Office 97-2003 formats (.doc / .xls / .ppt) are import only. The three
  21. "<app>File" importers take them as they are - office.ReadNativeFile
  22. converts a legacy file into OOXML first - and docToDocument and friends
  23. give their body JSON directly.
  24. PDF is deliberately absent. The web edition renders PDF in the front end
  25. (see apps/arozos_office/README.md); pulling pdf.BuildDoc and friends
  26. in here would only add fpdf to the module for nothing.
  27. */
  28. package main
  29. import (
  30. "errors"
  31. "imuslab.com/arozos/mod/office"
  32. "imuslab.com/arozos/mod/office/doc"
  33. "imuslab.com/arozos/mod/office/docx"
  34. "imuslab.com/arozos/mod/office/model"
  35. "imuslab.com/arozos/mod/office/odf"
  36. "imuslab.com/arozos/mod/office/ppt"
  37. "imuslab.com/arozos/mod/office/pptx"
  38. "imuslab.com/arozos/mod/office/xls"
  39. "imuslab.com/arozos/mod/office/xlsx"
  40. )
  41. // ImportFunc turns a foreign office file into the body JSON of the matching
  42. // app (the schemas in src/web/Office/README.md).
  43. type ImportFunc func(data []byte) (string, error)
  44. // ExportFunc turns an app's body JSON into a foreign office file. The second
  45. // []byte is the .pptx media sidecar zip - nil for every other format, and
  46. // nil for a deck with no video or audio (see pptx.BuildMedia).
  47. type ExportFunc func(jsonStr string) ([]byte, []byte, error)
  48. // nativeImport / nativeExport are the suite's own open and save. Media
  49. // stays inline as data URLs: there is no file system here to link into.
  50. func nativeImport(app string) ImportFunc {
  51. return func(data []byte) (string, error) {
  52. return office.ReadNativeFile(app, data, nil)
  53. }
  54. }
  55. func nativeExport(app string) ExportFunc {
  56. return func(envelope string) ([]byte, []byte, error) {
  57. data, err := office.BuildNativeFile(app, envelope, nil)
  58. return data, nil, err
  59. }
  60. }
  61. // Importers are named after the office.* AGI functions they mirror, so a
  62. // call site can name one string for both hosts.
  63. var Importers = map[string]ImportFunc{
  64. "documentFile": nativeImport(office.AppDocument),
  65. "spreadsheetFile": nativeImport(office.AppSpreadsheet),
  66. "presentationFile": nativeImport(office.AppPresentation),
  67. "docxToDocument": func(data []byte) (string, error) {
  68. doc, err := docx.Parse(data)
  69. if err != nil {
  70. return "", err
  71. }
  72. return model.DocumentToJSON(doc)
  73. },
  74. "docToDocument": func(data []byte) (string, error) {
  75. d, err := doc.Parse(data)
  76. if err != nil {
  77. return "", err
  78. }
  79. return model.DocumentToJSON(d)
  80. },
  81. "xlsToWorkbook": func(data []byte) (string, error) {
  82. wb, err := xls.Parse(data)
  83. if err != nil {
  84. return "", err
  85. }
  86. return model.WorkbookToJSON(wb)
  87. },
  88. "pptToPresentation": func(data []byte) (string, error) {
  89. p, err := ppt.Parse(data)
  90. if err != nil {
  91. return "", err
  92. }
  93. return model.PresentationToJSON(p)
  94. },
  95. "odtToDocument": func(data []byte) (string, error) {
  96. doc, err := odf.ParseOdt(data)
  97. if err != nil {
  98. return "", err
  99. }
  100. return model.DocumentToJSON(doc)
  101. },
  102. "xlsxToWorkbook": func(data []byte) (string, error) {
  103. wb, err := xlsx.Parse(data)
  104. if err != nil {
  105. return "", err
  106. }
  107. return model.WorkbookToJSON(wb)
  108. },
  109. "odsToWorkbook": func(data []byte) (string, error) {
  110. wb, err := odf.ParseOds(data)
  111. if err != nil {
  112. return "", err
  113. }
  114. return model.WorkbookToJSON(wb)
  115. },
  116. "pptxToPresentation": func(data []byte) (string, error) {
  117. pres, err := pptx.Parse(data)
  118. if err != nil {
  119. return "", err
  120. }
  121. return model.PresentationToJSON(pres)
  122. },
  123. "odpToPresentation": func(data []byte) (string, error) {
  124. pres, err := odf.ParseOdp(data)
  125. if err != nil {
  126. return "", err
  127. }
  128. return model.PresentationToJSON(pres)
  129. },
  130. }
  131. var Exporters = map[string]ExportFunc{
  132. "documentFile": nativeExport(office.AppDocument),
  133. "spreadsheetFile": nativeExport(office.AppSpreadsheet),
  134. "presentationFile": nativeExport(office.AppPresentation),
  135. "documentToDocx": func(jsonStr string) ([]byte, []byte, error) {
  136. doc, err := model.ParseDocumentJSON(jsonStr)
  137. if err != nil {
  138. return nil, nil, err
  139. }
  140. data, err := docx.Build(doc)
  141. return data, nil, err
  142. },
  143. "documentToOdt": func(jsonStr string) ([]byte, []byte, error) {
  144. doc, err := model.ParseDocumentJSON(jsonStr)
  145. if err != nil {
  146. return nil, nil, err
  147. }
  148. data, err := odf.BuildOdt(doc)
  149. return data, nil, err
  150. },
  151. "workbookToXlsx": func(jsonStr string) ([]byte, []byte, error) {
  152. wb, err := model.ParseWorkbookJSON(jsonStr)
  153. if err != nil {
  154. return nil, nil, err
  155. }
  156. data, err := xlsx.Build(wb)
  157. return data, nil, err
  158. },
  159. "workbookToOds": func(jsonStr string) ([]byte, []byte, error) {
  160. wb, err := model.ParseWorkbookJSON(jsonStr)
  161. if err != nil {
  162. return nil, nil, err
  163. }
  164. data, err := odf.BuildOds(wb)
  165. return data, nil, err
  166. },
  167. "presentationToPptx": func(jsonStr string) ([]byte, []byte, error) {
  168. pres, err := model.ParsePresentationJSON(jsonStr)
  169. if err != nil {
  170. return nil, nil, err
  171. }
  172. // nil resolver: a deck reaching this build carries its media as data
  173. // URLs, never as media?file= links into an ArozOS file system that
  174. // is not there
  175. return pptx.BuildMedia(pres, nil)
  176. },
  177. "presentationToOdp": func(jsonStr string) ([]byte, []byte, error) {
  178. pres, err := model.ParsePresentationJSON(jsonStr)
  179. if err != nil {
  180. return nil, nil, err
  181. }
  182. data, err := odf.BuildOdp(pres)
  183. return data, nil, err
  184. },
  185. }
  186. // RunImport and RunExport are the single entry points main.go bridges, so
  187. // an unknown name fails the same way on both sides of the wire.
  188. func RunImport(name string, data []byte) (string, error) {
  189. fn, ok := Importers[name]
  190. if !ok {
  191. return "", errors.New("unknown import converter: " + name)
  192. }
  193. if len(data) == 0 {
  194. return "", errors.New("no file content to convert")
  195. }
  196. return fn(data)
  197. }
  198. func RunExport(name string, jsonStr string) ([]byte, []byte, error) {
  199. fn, ok := Exporters[name]
  200. if !ok {
  201. return nil, nil, errors.New("unknown export converter: " + name)
  202. }
  203. if jsonStr == "" {
  204. return nil, nil, errors.New("no document to convert")
  205. }
  206. return fn(jsonStr)
  207. }
  208. // ConverterNames lists what this build can do, for the bridge to publish so
  209. // the front end never has to hardcode the list.
  210. func ConverterNames() ([]string, []string) {
  211. var in, out []string
  212. for k := range Importers {
  213. in = append(in, k)
  214. }
  215. for k := range Exporters {
  216. out = append(out, k)
  217. }
  218. return in, out
  219. }