@webiny/api-page-builder-import-export 5.34.8 → 5.35.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/client.js +2 -6
  2. package/client.js.map +1 -1
  3. package/export/combine/blocksHandler.js +11 -18
  4. package/export/combine/blocksHandler.js.map +1 -1
  5. package/export/combine/formsHandler.d.ts +6 -0
  6. package/export/combine/formsHandler.js +99 -0
  7. package/export/combine/formsHandler.js.map +1 -0
  8. package/export/combine/index.js +19 -9
  9. package/export/combine/index.js.map +1 -1
  10. package/export/combine/pagesHandler.js +11 -18
  11. package/export/combine/pagesHandler.js.map +1 -1
  12. package/export/combine/templatesHandler.d.ts +6 -0
  13. package/export/combine/templatesHandler.js +99 -0
  14. package/export/combine/templatesHandler.js.map +1 -0
  15. package/export/process/blocksHandler.js +18 -32
  16. package/export/process/blocksHandler.js.map +1 -1
  17. package/export/process/formsHandler.d.ts +6 -0
  18. package/export/process/formsHandler.js +187 -0
  19. package/export/process/formsHandler.js.map +1 -0
  20. package/export/process/index.js +19 -9
  21. package/export/process/index.js.map +1 -1
  22. package/export/process/pagesHandler.js +19 -34
  23. package/export/process/pagesHandler.js.map +1 -1
  24. package/export/process/templatesHandler.d.ts +6 -0
  25. package/export/process/templatesHandler.js +166 -0
  26. package/export/process/templatesHandler.js.map +1 -0
  27. package/export/s3Stream.d.ts +5 -2
  28. package/export/s3Stream.js +1 -20
  29. package/export/s3Stream.js.map +1 -1
  30. package/export/utils.d.ts +14 -1
  31. package/export/utils.js +88 -35
  32. package/export/utils.js.map +1 -1
  33. package/export/zipper.js +31 -43
  34. package/export/zipper.js.map +1 -1
  35. package/graphql/crud/blocks.crud.js +20 -39
  36. package/graphql/crud/blocks.crud.js.map +1 -1
  37. package/graphql/crud/forms.crud.d.ts +4 -0
  38. package/graphql/crud/forms.crud.js +129 -0
  39. package/graphql/crud/forms.crud.js.map +1 -0
  40. package/graphql/crud/importExportTasks.crud.js +9 -57
  41. package/graphql/crud/importExportTasks.crud.js.map +1 -1
  42. package/graphql/crud/pages.crud.js +25 -41
  43. package/graphql/crud/pages.crud.js.map +1 -1
  44. package/graphql/crud/templates.crud.d.ts +4 -0
  45. package/graphql/crud/templates.crud.js +124 -0
  46. package/graphql/crud/templates.crud.js.map +1 -0
  47. package/graphql/crud.js +3 -7
  48. package/graphql/crud.js.map +1 -1
  49. package/graphql/graphql/blocks.gql.js +1 -6
  50. package/graphql/graphql/blocks.gql.js.map +1 -1
  51. package/graphql/graphql/forms.gql.d.ts +4 -0
  52. package/graphql/graphql/forms.gql.js +60 -0
  53. package/graphql/graphql/forms.gql.js.map +1 -0
  54. package/graphql/graphql/importExportTasks.gql.js +1 -6
  55. package/graphql/graphql/importExportTasks.gql.js.map +1 -1
  56. package/graphql/graphql/pages.gql.js +2 -7
  57. package/graphql/graphql/pages.gql.js.map +1 -1
  58. package/graphql/graphql/templates.gql.d.ts +4 -0
  59. package/graphql/graphql/templates.gql.js +52 -0
  60. package/graphql/graphql/templates.gql.js.map +1 -0
  61. package/graphql/graphql/utils/resolve.d.ts +1 -1
  62. package/graphql/graphql/utils/resolve.js +0 -3
  63. package/graphql/graphql/utils/resolve.js.map +1 -1
  64. package/graphql/graphql.js +3 -6
  65. package/graphql/graphql.js.map +1 -1
  66. package/graphql/index.js +0 -5
  67. package/graphql/index.js.map +1 -1
  68. package/graphql/types.d.ts +40 -0
  69. package/graphql/types.js.map +1 -1
  70. package/import/constants.d.ts +3 -0
  71. package/import/constants.js +14 -0
  72. package/import/constants.js.map +1 -0
  73. package/import/create/blocksHandler.js +11 -20
  74. package/import/create/blocksHandler.js.map +1 -1
  75. package/import/create/formsHandler.d.ts +3 -0
  76. package/import/create/formsHandler.js +103 -0
  77. package/import/create/formsHandler.js.map +1 -0
  78. package/import/create/index.d.ts +2 -1
  79. package/import/create/index.js +19 -9
  80. package/import/create/index.js.map +1 -1
  81. package/import/create/pagesHandler.js +13 -20
  82. package/import/create/pagesHandler.js.map +1 -1
  83. package/import/create/templatesHandler.d.ts +3 -0
  84. package/import/create/templatesHandler.js +99 -0
  85. package/import/create/templatesHandler.js.map +1 -0
  86. package/import/process/blocks/blocksHandler.d.ts +3 -0
  87. package/import/process/blocks/blocksHandler.js +169 -0
  88. package/import/process/blocks/blocksHandler.js.map +1 -0
  89. package/import/process/blocks/importBlock.d.ts +11 -0
  90. package/import/process/blocks/importBlock.js +92 -0
  91. package/import/process/blocks/importBlock.js.map +1 -0
  92. package/import/process/forms/formsHandler.d.ts +3 -0
  93. package/import/process/forms/formsHandler.js +176 -0
  94. package/import/process/forms/formsHandler.js.map +1 -0
  95. package/import/process/forms/importForm.d.ts +9 -0
  96. package/import/process/forms/importForm.js +43 -0
  97. package/import/process/forms/importForm.js.map +1 -0
  98. package/import/process/index.d.ts +1 -0
  99. package/import/process/index.js +21 -11
  100. package/import/process/index.js.map +1 -1
  101. package/import/process/pages/importPage.d.ts +11 -0
  102. package/import/process/pages/importPage.js +92 -0
  103. package/import/process/pages/importPage.js.map +1 -0
  104. package/import/process/pages/pagesHandler.d.ts +3 -0
  105. package/import/process/pages/pagesHandler.js +183 -0
  106. package/import/process/pages/pagesHandler.js.map +1 -0
  107. package/import/process/templates/importTemplate.d.ts +11 -0
  108. package/import/process/templates/importTemplate.js +66 -0
  109. package/import/process/templates/importTemplate.js.map +1 -0
  110. package/import/process/templates/templatesHandler.d.ts +3 -0
  111. package/import/process/templates/templatesHandler.js +170 -0
  112. package/import/process/templates/templatesHandler.js.map +1 -0
  113. package/import/utils/deleteS3Folder.d.ts +1 -0
  114. package/import/utils/deleteS3Folder.js +19 -0
  115. package/import/utils/deleteS3Folder.js.map +1 -0
  116. package/import/utils/extractAndUploadZipFileContents.d.ts +7 -0
  117. package/import/utils/extractAndUploadZipFileContents.js +122 -0
  118. package/import/utils/extractAndUploadZipFileContents.js.map +1 -0
  119. package/import/utils/extractZipAndUploadToS3.d.ts +2 -0
  120. package/import/utils/extractZipAndUploadToS3.js +98 -0
  121. package/import/utils/extractZipAndUploadToS3.js.map +1 -0
  122. package/import/utils/getFileNameWithoutExt.d.ts +1 -0
  123. package/import/utils/getFileNameWithoutExt.js +11 -0
  124. package/import/utils/getFileNameWithoutExt.js.map +1 -0
  125. package/import/utils/index.d.ts +9 -0
  126. package/import/utils/index.js +104 -0
  127. package/import/utils/index.js.map +1 -0
  128. package/import/utils/initialStats.d.ts +7 -0
  129. package/import/utils/initialStats.js +16 -0
  130. package/import/utils/initialStats.js.map +1 -0
  131. package/import/utils/prepareDataDirMap.d.ts +6 -0
  132. package/import/utils/prepareDataDirMap.js +29 -0
  133. package/import/utils/prepareDataDirMap.js.map +1 -0
  134. package/import/utils/updateFilesInData.d.ts +8 -0
  135. package/import/utils/updateFilesInData.js +48 -0
  136. package/import/utils/updateFilesInData.js.map +1 -0
  137. package/import/utils/uploadAssets.d.ts +10 -0
  138. package/import/utils/uploadAssets.js +51 -0
  139. package/import/utils/uploadAssets.js.map +1 -0
  140. package/import/utils/uploadFilesFromS3.d.ts +3 -0
  141. package/import/utils/uploadFilesFromS3.js +19 -0
  142. package/import/utils/uploadFilesFromS3.js.map +1 -0
  143. package/mockSecurity.js +0 -2
  144. package/mockSecurity.js.map +1 -1
  145. package/package.json +25 -24
  146. package/types.d.ts +21 -0
  147. package/types.js +0 -5
  148. package/types.js.map +1 -1
@@ -1 +1 @@
1
- {"version":3,"names":["pagesHandler","configuration","payload","context","log","console","subTask","noPendingTask","prevStatusOfSubTask","ImportExportTaskStatus","PENDING","pageBuilder","fileManager","taskId","subTaskIndex","type","identity","mockSecurity","importExportTask","getSubTask","zeroPad","status","data","error","id","input","pageId","exportPagesDataKey","revisionType","page","ExportRevisionType","PUBLISHED","getPublishedPageById","getPage","e","NotFoundError","version","updateSubTask","PROCESSING","updateStats","prevStatus","nextStatus","pageDataZip","exportPage","COMPLETED","message","key","Key","FAILED","name","stack","code","invokeHandlerClient","handlers","combine","security","getIdentity","description","process"],"sources":["pagesHandler.ts"],"sourcesContent":["import { ExportRevisionType, ImportExportTaskStatus, PbImportExportContext } from \"~/types\";\nimport { invokeHandlerClient } from \"~/client\";\nimport { NotFoundError } from \"@webiny/handler-graphql\";\nimport { exportPage } from \"~/export/utils\";\nimport { Payload as ExtractPayload } from \"../combine\";\nimport { mockSecurity } from \"~/mockSecurity\";\nimport { SecurityIdentity } from \"@webiny/api-security/types\";\nimport { zeroPad } from \"@webiny/utils\";\nimport { Configuration, Payload, Response } from \"~/export/process\";\n\n/**\n * Handles the export pages process workflow.\n */\nexport const pagesHandler = async (\n configuration: Configuration,\n payload: Payload,\n context: PbImportExportContext\n): Promise<Response> => {\n const log = console.log;\n let subTask;\n let noPendingTask = true;\n let prevStatusOfSubTask = ImportExportTaskStatus.PENDING;\n\n log(\"RUNNING Export Pages Process Handler\");\n const { pageBuilder, fileManager } = context;\n const { taskId, subTaskIndex, type, identity } = payload;\n // Disable authorization; this is necessary because we call Page Builder CRUD methods which include authorization checks\n // and this Lambda is invoked internally, without credentials.\n mockSecurity(identity as SecurityIdentity, context);\n\n try {\n /*\n * Note: We're not going to DB for finding the next sub-task to process,\n * because the data might be out of sync due to GSI eventual consistency.\n */\n subTask = await pageBuilder.importExportTask.getSubTask(taskId, zeroPad(subTaskIndex, 5));\n /**\n * Base condition!!\n * Bail out early, if task not found or task's status is not \"pending\".\n */\n if (!subTask || subTask.status !== ImportExportTaskStatus.PENDING) {\n noPendingTask = true;\n return {\n data: \"\",\n error: null\n };\n } else {\n noPendingTask = false;\n }\n\n log(`Fetched sub task => ${subTask.id}`);\n\n const { input } = subTask;\n const { pageId, exportPagesDataKey, revisionType } = input;\n\n /**\n * At the moment, we only export a single revision of the page.\n * It could be \"published\" or \"latest\" depending upon user input.\n *\n * Note: In case of no \"published\" revision available, we use the latest revision.\n */\n let page;\n try {\n if (revisionType === ExportRevisionType.PUBLISHED) {\n // Get \"published\" page.\n page = await pageBuilder.getPublishedPageById({ id: pageId });\n } else {\n // Get \"latest\" page.\n page = await pageBuilder.getPage(pageId);\n }\n } catch (e) {\n // If we're looking for \"published\" page and doesn't found it, get latest page.\n if (revisionType === ExportRevisionType.PUBLISHED && e instanceof NotFoundError) {\n page = await pageBuilder.getPage(pageId);\n } else {\n throw e;\n }\n }\n\n if (!page) {\n log(`Unable to load page \"${pageId}\"`);\n throw new NotFoundError(`Unable to load page \"${pageId}\"`);\n }\n\n log(`Processing page key \"${pageId}\" | version ${page.version} | ${page.status}`);\n\n // Mark task status as PROCESSING\n subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {\n status: ImportExportTaskStatus.PROCESSING\n });\n // Update stats in main task\n await pageBuilder.importExportTask.updateStats(taskId, {\n prevStatus: prevStatusOfSubTask,\n nextStatus: ImportExportTaskStatus.PROCESSING\n });\n prevStatusOfSubTask = subTask.status;\n\n log(`Extracting page data and uploading to storage...`);\n // Extract Page\n const pageDataZip = await exportPage(page, exportPagesDataKey, fileManager);\n log(`Finish uploading zip...`);\n // Update task record in DB\n subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {\n status: ImportExportTaskStatus.COMPLETED,\n data: {\n message: `Finish uploading data for page \"${page.id}\" v${page.version} (${page.status}).`,\n key: pageDataZip.Key\n }\n });\n // Update stats in main task\n await pageBuilder.importExportTask.updateStats(taskId, {\n prevStatus: prevStatusOfSubTask,\n nextStatus: ImportExportTaskStatus.COMPLETED\n });\n prevStatusOfSubTask = subTask.status;\n } catch (e) {\n log(\"[EXPORT_PAGES_PROCESS] Error => \", e);\n\n if (subTask && subTask.id) {\n /**\n * In case of error, we'll update the task status to \"failed\",\n * so that, client can show notify the user appropriately.\n */\n subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {\n status: ImportExportTaskStatus.FAILED,\n error: {\n name: e.name,\n message: e.message,\n stack: e.stack,\n code: \"EXPORT_FAILED\"\n }\n });\n\n // Update stats in main task\n await pageBuilder.importExportTask.updateStats(taskId, {\n prevStatus: prevStatusOfSubTask,\n nextStatus: ImportExportTaskStatus.FAILED\n });\n prevStatusOfSubTask = subTask.status;\n }\n\n return {\n data: null,\n error: {\n message: e.message\n }\n };\n } finally {\n // Base condition!\n if (noPendingTask) {\n log(`No pending sub-task for task ${taskId}`);\n // Combine individual page zip files.\n await invokeHandlerClient<ExtractPayload>({\n context,\n name: configuration.handlers.combine,\n payload: {\n taskId,\n type,\n identity: context.security.getIdentity()\n },\n description: \"Export pages - combine\"\n });\n } else {\n console.log(`Invoking PROCESS for task \"${subTaskIndex + 1}\"`);\n // We want to continue with Self invocation no matter if current page error out.\n await invokeHandlerClient<Payload>({\n context,\n name: configuration.handlers.process,\n payload: {\n taskId,\n type,\n subTaskIndex: subTaskIndex + 1,\n identity: context.security.getIdentity()\n },\n description: \"Export pages - process - subtask\"\n });\n }\n }\n return {\n data: \"\",\n error: null\n };\n};\n"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AAEA;;AAEA;;AAGA;AACA;AACA;AACO,MAAMA,YAAY,GAAG,OACxBC,aADwB,EAExBC,OAFwB,EAGxBC,OAHwB,KAIJ;EACpB,MAAMC,GAAG,GAAGC,OAAO,CAACD,GAApB;EACA,IAAIE,OAAJ;EACA,IAAIC,aAAa,GAAG,IAApB;EACA,IAAIC,mBAAmB,GAAGC,6BAAA,CAAuBC,OAAjD;EAEAN,GAAG,CAAC,sCAAD,CAAH;EACA,MAAM;IAAEO,WAAF;IAAeC;EAAf,IAA+BT,OAArC;EACA,MAAM;IAAEU,MAAF;IAAUC,YAAV;IAAwBC,IAAxB;IAA8BC;EAA9B,IAA2Cd,OAAjD,CARoB,CASpB;EACA;;EACA,IAAAe,0BAAA,EAAaD,QAAb,EAA2Cb,OAA3C;;EAEA,IAAI;IACA;AACR;AACA;AACA;IACQG,OAAO,GAAG,MAAMK,WAAW,CAACO,gBAAZ,CAA6BC,UAA7B,CAAwCN,MAAxC,EAAgD,IAAAO,eAAA,EAAQN,YAAR,EAAsB,CAAtB,CAAhD,CAAhB;IACA;AACR;AACA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A3D,EADG,CAEH;;MACA,MAAM,IAAAsC,2BAAA,EAA6B;QAC/BjD,OAD+B;QAE/B8C,IAAI,EAAEhD,aAAa,CAACoD,QAAd,CAAuBK,OAFE;QAG/BxD,OAAO,EAAE;UACLW,MADK;UAELE,IAFK;UAGLD,YAAY,EAAEA,YAAY,GAAG,CAHxB;UAILE,QAAQ,EAAEb,OAAO,CAACoD,QAAR,CAAiBC,WAAjB;QAJL,CAHsB;QAS/BC,WAAW,EAAE;MATkB,CAA7B,CAAN;IAWH;EACJ;;EACD,OAAO;IACHnC,IAAI,EAAE,EADH;IAEHC,KAAK,EAAE;EAFJ,CAAP;AAIH,CAzKM"}
1
+ {"version":3,"names":["pagesHandler","configuration","payload","context","log","console","subTask","noPendingTask","prevStatusOfSubTask","ImportExportTaskStatus","PENDING","pageBuilder","fileManager","taskId","subTaskIndex","type","identity","mockSecurity","importExportTask","getSubTask","zeroPad","status","data","error","id","input","pageId","exportPagesDataKey","revisionType","page","ExportRevisionType","PUBLISHED","getPublishedPageById","getPage","e","NotFoundError","version","updateSubTask","PROCESSING","updateStats","prevStatus","nextStatus","pageDataZip","exportPage","COMPLETED","message","key","Key","FAILED","name","stack","code","invokeHandlerClient","handlers","combine","security","getIdentity","description","process"],"sources":["pagesHandler.ts"],"sourcesContent":["import { ExportRevisionType, ImportExportTaskStatus, PbImportExportContext } from \"~/types\";\nimport { invokeHandlerClient } from \"~/client\";\nimport { NotFoundError } from \"@webiny/handler-graphql\";\nimport { exportPage } from \"~/export/utils\";\nimport { Payload as ExtractPayload } from \"../combine\";\nimport { mockSecurity } from \"~/mockSecurity\";\nimport { SecurityIdentity } from \"@webiny/api-security/types\";\nimport { zeroPad } from \"@webiny/utils\";\nimport { Configuration, Payload, Response } from \"~/export/process\";\n\n/**\n * Handles the export pages process workflow.\n */\nexport const pagesHandler = async (\n configuration: Configuration,\n payload: Payload,\n context: PbImportExportContext\n): Promise<Response> => {\n const log = console.log;\n let subTask;\n let noPendingTask = true;\n let prevStatusOfSubTask = ImportExportTaskStatus.PENDING;\n\n log(\"RUNNING Export Pages Process Handler\");\n const { pageBuilder, fileManager } = context;\n const { taskId, subTaskIndex, type, identity } = payload;\n // Disable authorization; this is necessary because we call Page Builder CRUD methods which include authorization checks\n // and this Lambda is invoked internally, without credentials.\n mockSecurity(identity as SecurityIdentity, context);\n\n try {\n /*\n * Note: We're not going to DB for finding the next sub-task to process,\n * because the data might be out of sync due to GSI eventual consistency.\n */\n subTask = await pageBuilder.importExportTask.getSubTask(taskId, zeroPad(subTaskIndex, 5));\n /**\n * Base condition!!\n * Bail out early, if task not found or task's status is not \"pending\".\n */\n if (!subTask || subTask.status !== ImportExportTaskStatus.PENDING) {\n noPendingTask = true;\n return {\n data: \"\",\n error: null\n };\n } else {\n noPendingTask = false;\n }\n\n log(`Fetched sub task => ${subTask.id}`);\n\n const { input } = subTask;\n const { pageId, exportPagesDataKey, revisionType } = input;\n\n /**\n * At the moment, we only export a single revision of the page.\n * It could be \"published\" or \"latest\" depending upon user input.\n *\n * Note: In case of no \"published\" revision available, we use the latest revision.\n */\n let page;\n try {\n if (revisionType === ExportRevisionType.PUBLISHED) {\n // Get \"published\" page.\n page = await pageBuilder.getPublishedPageById({ id: pageId });\n } else {\n // Get \"latest\" page.\n page = await pageBuilder.getPage(pageId);\n }\n } catch (e) {\n // If we're looking for \"published\" page and doesn't found it, get latest page.\n if (revisionType === ExportRevisionType.PUBLISHED && e instanceof NotFoundError) {\n page = await pageBuilder.getPage(pageId);\n } else {\n throw e;\n }\n }\n\n if (!page) {\n log(`Unable to load page \"${pageId}\"`);\n throw new NotFoundError(`Unable to load page \"${pageId}\"`);\n }\n\n log(`Processing page key \"${pageId}\" | version ${page.version} | ${page.status}`);\n\n // Mark task status as PROCESSING\n subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {\n status: ImportExportTaskStatus.PROCESSING\n });\n // Update stats in main task\n await pageBuilder.importExportTask.updateStats(taskId, {\n prevStatus: prevStatusOfSubTask,\n nextStatus: ImportExportTaskStatus.PROCESSING\n });\n prevStatusOfSubTask = subTask.status;\n\n log(`Extracting page data and uploading to storage...`);\n // Extract Page\n const pageDataZip = await exportPage(page, exportPagesDataKey, fileManager);\n log(`Finish uploading zip...`);\n // Update task record in DB\n subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {\n status: ImportExportTaskStatus.COMPLETED,\n data: {\n message: `Finish uploading data for page \"${page.id}\" v${page.version} (${page.status}).`,\n key: pageDataZip.Key\n }\n });\n // Update stats in main task\n await pageBuilder.importExportTask.updateStats(taskId, {\n prevStatus: prevStatusOfSubTask,\n nextStatus: ImportExportTaskStatus.COMPLETED\n });\n prevStatusOfSubTask = subTask.status;\n } catch (e) {\n log(\"[EXPORT_PAGES_PROCESS] Error => \", e);\n\n if (subTask && subTask.id) {\n /**\n * In case of error, we'll update the task status to \"failed\",\n * so that, client can show notify the user appropriately.\n */\n subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {\n status: ImportExportTaskStatus.FAILED,\n error: {\n name: e.name,\n message: e.message,\n stack: e.stack,\n code: \"EXPORT_FAILED\"\n }\n });\n\n // Update stats in main task\n await pageBuilder.importExportTask.updateStats(taskId, {\n prevStatus: prevStatusOfSubTask,\n nextStatus: ImportExportTaskStatus.FAILED\n });\n prevStatusOfSubTask = subTask.status;\n }\n\n return {\n data: null,\n error: {\n message: e.message\n }\n };\n } finally {\n // Base condition!\n if (noPendingTask) {\n log(`No pending sub-task for task ${taskId}`);\n // Combine individual page zip files.\n await invokeHandlerClient<ExtractPayload>({\n context,\n name: configuration.handlers.combine,\n payload: {\n taskId,\n type,\n identity: context.security.getIdentity()\n },\n description: \"Export pages - combine\"\n });\n } else {\n console.log(`Invoking PROCESS for task \"${subTaskIndex + 1}\"`);\n // We want to continue with Self invocation no matter if current page error out.\n await invokeHandlerClient<Payload>({\n context,\n name: configuration.handlers.process,\n payload: {\n taskId,\n type,\n subTaskIndex: subTaskIndex + 1,\n identity: context.security.getIdentity()\n },\n description: \"Export pages - process - subtask\"\n });\n }\n }\n return {\n data: \"\",\n error: null\n };\n};\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AAEA;AAEA;AAGA;AACA;AACA;AACO,MAAMA,YAAY,GAAG,OACxBC,aAA4B,EAC5BC,OAAgB,EAChBC,OAA8B,KACV;EACpB,MAAMC,GAAG,GAAGC,OAAO,CAACD,GAAG;EACvB,IAAIE,OAAO;EACX,IAAIC,aAAa,GAAG,IAAI;EACxB,IAAIC,mBAAmB,GAAGC,6BAAsB,CAACC,OAAO;EAExDN,GAAG,CAAC,sCAAsC,CAAC;EAC3C,MAAM;IAAEO,WAAW;IAAEC;EAAY,CAAC,GAAGT,OAAO;EAC5C,MAAM;IAAEU,MAAM;IAAEC,YAAY;IAAEC,IAAI;IAAEC;EAAS,CAAC,GAAGd,OAAO;EACxD;EACA;EACA,IAAAe,0BAAY,EAACD,QAAQ,EAAsBb,OAAO,CAAC;EAEnD,IAAI;IACA;AACR;AACA;AACA;IACQG,OAAO,GAAG,MAAMK,WAAW,CAACO,gBAAgB,CAACC,UAAU,CAACN,MAAM,EAAE,IAAAO,eAAO,EAACN,YAAY,EAAE,CAAC,CAAC,CAAC;IACzF;AACR;AACA;AACA;IACQ,IAAI,CAACR,OAAO,IAAIA,OAAO,CAACe,MAAM,KAAKZ,6BAAsB,CAACC,OAAO,EAAE;MAC/DH,aAAa,GAAG,IAAI;MACpB,OAAO;QACHe,IAAI,EAAE,EAAE;QACRC,KAAK,EAAE;MACX,CAAC;IACL,CAAC,MAAM;MACHhB,aAAa,GAAG,KAAK;IACzB;IAEAH,GAAG,CAAE,uBAAsBE,OAAO,CAACkB,EAAG,EAAC,CAAC;IAExC,MAAM;MAAEC;IAAM,CAAC,GAAGnB,OAAO;IACzB,MAAM;MAAEoB,MAAM;MAAEC,kBAAkB;MAAEC;IAAa,CAAC,GAAGH,KAAK;;IAE1D;AACR;AACA;AACA;AACA;AACA;IACQ,IAAII,IAAI;IAC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,CAAC,CAAC;MACFxC,mBAAmB,GAAGF,OAAO,CAACe,MAAM;IACxC;IAEA,OAAO;MACHC,IAAI,EAAE,IAAI;MACVC,KAAK,EAAE;QACHsB,OAAO,EAAEX,CAAC,CAACW;MACf;IACJ,CAAC;EACL,CAAC,SAAS;IACN;IACA,IAAItC,aAAa,EAAE;MACfH,GAAG,CAAE,gCAA+BS,MAAO,EAAC,CAAC;MAC7C;MACA,MAAM,IAAAuC,2BAAmB,EAAiB;QACtCjD,OAAO;QACP8C,IAAI,EAAEhD,aAAa,CAACoD,QAAQ,CAACC,OAAO;QACpCpD,OAAO,EAAE;UACLW,MAAM;UACNE,IAAI;UACJC,QAAQ,EAAEb,OAAO,CAACoD,QAAQ,CAACC,WAAW;QAC1C,CAAC;QACDC,WAAW,EAAE;MACjB,CAAC,CAAC;IACN,CAAC,MAAM;MACHpD,OAAO,CAACD,GAAG,CAAE,8BAA6BU,YAAY,GAAG,CAAE,GAAE,CAAC;MAC9D;MACA,MAAM,IAAAsC,2BAAmB,EAAU;QAC/BjD,OAAO;QACP8C,IAAI,EAAEhD,aAAa,CAACoD,QAAQ,CAACK,OAAO;QACpCxD,OAAO,EAAE;UACLW,MAAM;UACNE,IAAI;UACJD,YAAY,EAAEA,YAAY,GAAG,CAAC;UAC9BE,QAAQ,EAAEb,OAAO,CAACoD,QAAQ,CAACC,WAAW;QAC1C,CAAC;QACDC,WAAW,EAAE;MACjB,CAAC,CAAC;IACN;EACJ;EACA,OAAO;IACHnC,IAAI,EAAE,EAAE;IACRC,KAAK,EAAE;EACX,CAAC;AACL,CAAC;AAAC"}
@@ -0,0 +1,6 @@
1
+ import { PbImportExportContext } from "../../types";
2
+ import { Configuration, Payload, Response } from ".";
3
+ /**
4
+ * Handles the export templates process workflow.
5
+ */
6
+ export declare const templatesHandler: (configuration: Configuration, payload: Payload, context: PbImportExportContext) => Promise<Response>;
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.templatesHandler = void 0;
7
+ var _types = require("../../types");
8
+ var _client = require("../../client");
9
+ var _handlerGraphql = require("@webiny/handler-graphql");
10
+ var _utils = require("../utils");
11
+ var _mockSecurity = require("../../mockSecurity");
12
+ var _utils2 = require("@webiny/utils");
13
+ /**
14
+ * Handles the export templates process workflow.
15
+ */
16
+ const templatesHandler = async (configuration, payload, context) => {
17
+ const log = console.log;
18
+ let subTask;
19
+ let noPendingTask = true;
20
+ let prevStatusOfSubTask = _types.ImportExportTaskStatus.PENDING;
21
+ log("RUNNING Export Templates Process Handler");
22
+ const {
23
+ pageBuilder,
24
+ fileManager
25
+ } = context;
26
+ const {
27
+ taskId,
28
+ subTaskIndex,
29
+ type,
30
+ identity
31
+ } = payload;
32
+ // Disable authorization; this is necessary because we call Page Builder CRUD methods which include authorization checks
33
+ // and this Lambda is invoked internally, without credentials.
34
+ (0, _mockSecurity.mockSecurity)(identity, context);
35
+ try {
36
+ /*
37
+ * Note: We're not going to DB for finding the next sub-task to process,
38
+ * because the data might be out of sync due to GSI eventual consistency.
39
+ */
40
+ subTask = await pageBuilder.importExportTask.getSubTask(taskId, (0, _utils2.zeroPad)(subTaskIndex, 5));
41
+ /**
42
+ * Base condition!!
43
+ * Bail out early, if task not found or task's status is not "pending".
44
+ */
45
+ if (!subTask || subTask.status !== _types.ImportExportTaskStatus.PENDING) {
46
+ noPendingTask = true;
47
+ return {
48
+ data: "",
49
+ error: null
50
+ };
51
+ } else {
52
+ noPendingTask = false;
53
+ }
54
+ log(`Fetched sub task => ${subTask.id}`);
55
+ const {
56
+ input
57
+ } = subTask;
58
+ const {
59
+ templateId,
60
+ exportTemplatesDataKey
61
+ } = input;
62
+ const template = await pageBuilder.getPageTemplate({
63
+ where: {
64
+ id: templateId
65
+ }
66
+ });
67
+ if (!template) {
68
+ log(`Unable to load template "${templateId}"`);
69
+ throw new _handlerGraphql.NotFoundError(`Unable to load template "${templateId}"`);
70
+ }
71
+ log(`Processing template key "${templateId}"`);
72
+
73
+ // Mark task status as PROCESSING
74
+ subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {
75
+ status: _types.ImportExportTaskStatus.PROCESSING
76
+ });
77
+ // Update stats in main task
78
+ await pageBuilder.importExportTask.updateStats(taskId, {
79
+ prevStatus: prevStatusOfSubTask,
80
+ nextStatus: _types.ImportExportTaskStatus.PROCESSING
81
+ });
82
+ prevStatusOfSubTask = subTask.status;
83
+ log(`Extracting template data and uploading to storage...`);
84
+ // Extract Template
85
+ const templateDataZip = await (0, _utils.exportTemplate)(template, exportTemplatesDataKey, fileManager);
86
+ log(`Finish uploading zip...`);
87
+ // Update task record in DB
88
+ subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {
89
+ status: _types.ImportExportTaskStatus.COMPLETED,
90
+ data: {
91
+ message: `Finish uploading data for template "${template.id}"`,
92
+ key: templateDataZip.Key
93
+ }
94
+ });
95
+ // Update stats in main task
96
+ await pageBuilder.importExportTask.updateStats(taskId, {
97
+ prevStatus: prevStatusOfSubTask,
98
+ nextStatus: _types.ImportExportTaskStatus.COMPLETED
99
+ });
100
+ prevStatusOfSubTask = subTask.status;
101
+ } catch (e) {
102
+ log("[EXPORT_TEMPLATES_PROCESS] Error => ", e.message);
103
+ if (subTask && subTask.id) {
104
+ /**
105
+ * In case of error, we'll update the task status to "failed",
106
+ * so that, client can show notify the user appropriately.
107
+ */
108
+ subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {
109
+ status: _types.ImportExportTaskStatus.FAILED,
110
+ error: {
111
+ name: e.name,
112
+ message: e.message,
113
+ code: "EXPORT_FAILED"
114
+ }
115
+ });
116
+
117
+ // Update stats in main task
118
+ await pageBuilder.importExportTask.updateStats(taskId, {
119
+ prevStatus: prevStatusOfSubTask,
120
+ nextStatus: _types.ImportExportTaskStatus.FAILED
121
+ });
122
+ prevStatusOfSubTask = subTask.status;
123
+ }
124
+ return {
125
+ data: null,
126
+ error: {
127
+ message: e.message
128
+ }
129
+ };
130
+ } finally {
131
+ // Base condition!
132
+ if (noPendingTask) {
133
+ log(`No pending sub-task for task ${taskId}`);
134
+ // Combine individual template zip files.
135
+ await (0, _client.invokeHandlerClient)({
136
+ context,
137
+ name: configuration.handlers.combine,
138
+ payload: {
139
+ taskId,
140
+ type,
141
+ identity: context.security.getIdentity()
142
+ },
143
+ description: "Export templates - combine"
144
+ });
145
+ } else {
146
+ console.log(`Invoking PROCESS for task "${subTaskIndex + 1}"`);
147
+ // We want to continue with Self invocation no matter if current template error out.
148
+ await (0, _client.invokeHandlerClient)({
149
+ context,
150
+ name: configuration.handlers.process,
151
+ payload: {
152
+ taskId,
153
+ subTaskIndex: subTaskIndex + 1,
154
+ type,
155
+ identity: context.security.getIdentity()
156
+ },
157
+ description: "Export templates - process - subtask"
158
+ });
159
+ }
160
+ }
161
+ return {
162
+ data: "",
163
+ error: null
164
+ };
165
+ };
166
+ exports.templatesHandler = templatesHandler;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["templatesHandler","configuration","payload","context","log","console","subTask","noPendingTask","prevStatusOfSubTask","ImportExportTaskStatus","PENDING","pageBuilder","fileManager","taskId","subTaskIndex","type","identity","mockSecurity","importExportTask","getSubTask","zeroPad","status","data","error","id","input","templateId","exportTemplatesDataKey","template","getPageTemplate","where","NotFoundError","updateSubTask","PROCESSING","updateStats","prevStatus","nextStatus","templateDataZip","exportTemplate","COMPLETED","message","key","Key","e","FAILED","name","code","invokeHandlerClient","handlers","combine","security","getIdentity","description","process"],"sources":["templatesHandler.ts"],"sourcesContent":["import { ImportExportTaskStatus, PbImportExportContext } from \"~/types\";\nimport { invokeHandlerClient } from \"~/client\";\nimport { NotFoundError } from \"@webiny/handler-graphql\";\nimport { exportTemplate } from \"~/export/utils\";\nimport { Payload as ExtractPayload } from \"../combine\";\nimport { mockSecurity } from \"~/mockSecurity\";\nimport { SecurityIdentity } from \"@webiny/api-security/types\";\nimport { zeroPad } from \"@webiny/utils\";\nimport { Configuration, Payload, Response } from \"~/export/process\";\n\n/**\n * Handles the export templates process workflow.\n */\nexport const templatesHandler = async (\n configuration: Configuration,\n payload: Payload,\n context: PbImportExportContext\n): Promise<Response> => {\n const log = console.log;\n let subTask;\n let noPendingTask = true;\n let prevStatusOfSubTask = ImportExportTaskStatus.PENDING;\n\n log(\"RUNNING Export Templates Process Handler\");\n const { pageBuilder, fileManager } = context;\n const { taskId, subTaskIndex, type, identity } = payload;\n // Disable authorization; this is necessary because we call Page Builder CRUD methods which include authorization checks\n // and this Lambda is invoked internally, without credentials.\n mockSecurity(identity as SecurityIdentity, context);\n try {\n /*\n * Note: We're not going to DB for finding the next sub-task to process,\n * because the data might be out of sync due to GSI eventual consistency.\n */\n subTask = await pageBuilder.importExportTask.getSubTask(taskId, zeroPad(subTaskIndex, 5));\n /**\n * Base condition!!\n * Bail out early, if task not found or task's status is not \"pending\".\n */\n if (!subTask || subTask.status !== ImportExportTaskStatus.PENDING) {\n noPendingTask = true;\n return {\n data: \"\",\n error: null\n };\n } else {\n noPendingTask = false;\n }\n\n log(`Fetched sub task => ${subTask.id}`);\n\n const { input } = subTask;\n const { templateId, exportTemplatesDataKey } = input;\n\n const template = await pageBuilder.getPageTemplate({ where: { id: templateId } });\n\n if (!template) {\n log(`Unable to load template \"${templateId}\"`);\n throw new NotFoundError(`Unable to load template \"${templateId}\"`);\n }\n\n log(`Processing template key \"${templateId}\"`);\n\n // Mark task status as PROCESSING\n subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {\n status: ImportExportTaskStatus.PROCESSING\n });\n // Update stats in main task\n await pageBuilder.importExportTask.updateStats(taskId, {\n prevStatus: prevStatusOfSubTask,\n nextStatus: ImportExportTaskStatus.PROCESSING\n });\n prevStatusOfSubTask = subTask.status;\n\n log(`Extracting template data and uploading to storage...`);\n // Extract Template\n const templateDataZip = await exportTemplate(template, exportTemplatesDataKey, fileManager);\n log(`Finish uploading zip...`);\n // Update task record in DB\n subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {\n status: ImportExportTaskStatus.COMPLETED,\n data: {\n message: `Finish uploading data for template \"${template.id}\"`,\n key: templateDataZip.Key\n }\n });\n // Update stats in main task\n await pageBuilder.importExportTask.updateStats(taskId, {\n prevStatus: prevStatusOfSubTask,\n nextStatus: ImportExportTaskStatus.COMPLETED\n });\n prevStatusOfSubTask = subTask.status;\n } catch (e) {\n log(\"[EXPORT_TEMPLATES_PROCESS] Error => \", e.message);\n\n if (subTask && subTask.id) {\n /**\n * In case of error, we'll update the task status to \"failed\",\n * so that, client can show notify the user appropriately.\n */\n subTask = await pageBuilder.importExportTask.updateSubTask(taskId, subTask.id, {\n status: ImportExportTaskStatus.FAILED,\n error: {\n name: e.name,\n message: e.message,\n code: \"EXPORT_FAILED\"\n }\n });\n\n // Update stats in main task\n await pageBuilder.importExportTask.updateStats(taskId, {\n prevStatus: prevStatusOfSubTask,\n nextStatus: ImportExportTaskStatus.FAILED\n });\n prevStatusOfSubTask = subTask.status;\n }\n\n return {\n data: null,\n error: {\n message: e.message\n }\n };\n } finally {\n // Base condition!\n if (noPendingTask) {\n log(`No pending sub-task for task ${taskId}`);\n // Combine individual template zip files.\n await invokeHandlerClient<ExtractPayload>({\n context,\n name: configuration.handlers.combine,\n payload: {\n taskId,\n type,\n identity: context.security.getIdentity()\n },\n description: \"Export templates - combine\"\n });\n } else {\n console.log(`Invoking PROCESS for task \"${subTaskIndex + 1}\"`);\n // We want to continue with Self invocation no matter if current template error out.\n await invokeHandlerClient<Payload>({\n context,\n name: configuration.handlers.process,\n payload: {\n taskId,\n subTaskIndex: subTaskIndex + 1,\n type,\n identity: context.security.getIdentity()\n },\n description: \"Export templates - process - subtask\"\n });\n }\n }\n return {\n data: \"\",\n error: null\n };\n};\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AAEA;AAEA;AAGA;AACA;AACA;AACO,MAAMA,gBAAgB,GAAG,OAC5BC,aAA4B,EAC5BC,OAAgB,EAChBC,OAA8B,KACV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GAAG,CAAC;UAC9BC,IAAI;UACJC,QAAQ,EAAEb,OAAO,CAAC+C,QAAQ,CAACC,WAAW;QAC1C,CAAC;QACDC,WAAW,EAAE;MACjB,CAAC,CAAC;IACN;EACJ;EACA,OAAO;IACH9B,IAAI,EAAE,EAAE;IACRC,KAAK,EAAE;EACX,CAAC;AACL,CAAC;AAAC"}
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
- import { PassThrough, Readable } from "stream";
3
+ import { Readable } from "stream";
4
4
  import S3 from "aws-sdk/clients/s3";
5
5
  declare class S3Stream {
6
6
  s3: S3;
@@ -16,7 +16,10 @@ declare class S3Stream {
16
16
  getObjectHead(Key: string): Promise<S3.HeadObjectOutput>;
17
17
  readStream(Key: string): Readable;
18
18
  writeStream(Key: string, contentType?: string): {
19
- streamPassThrough: PassThrough;
19
+ streamPassThrough: import("stream").PassThrough;
20
+ /**
21
+ * We're not using the `FileManager` storage plugin here because it currently doesn't support streams.
22
+ */
20
23
  streamPassThroughUploadPromise: Promise<S3.ManagedUpload.SendData>;
21
24
  };
22
25
  upload(params: {
@@ -1,22 +1,15 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.s3Stream = void 0;
9
-
10
8
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
-
12
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
-
14
10
  var _stream = require("stream");
15
-
16
11
  var _s = _interopRequireDefault(require("aws-sdk/clients/s3"));
17
-
18
12
  const ARCHIVE_CONTENT_TYPE = "application/zip";
19
-
20
13
  class S3Stream {
21
14
  constructor() {
22
15
  (0, _defineProperty2.default)(this, "s3", void 0);
@@ -26,22 +19,19 @@ class S3Stream {
26
19
  });
27
20
  this.bucket = process.env.S3_BUCKET;
28
21
  }
29
-
30
22
  getPresignedUrl(key) {
31
23
  return this.s3.getSignedUrl("getObject", {
32
24
  Bucket: this.bucket,
33
25
  Key: key,
34
26
  Expires: 604800 // 1 week
35
-
36
27
  });
37
28
  }
29
+
38
30
  /**
39
31
  * We're checking if the file is accessible on S3 by getting object meta data.
40
32
  * It help us to filter files that we need to download as part of export data.
41
33
  * @param Key {string}
42
34
  */
43
-
44
-
45
35
  async isFileAccessible(Key) {
46
36
  try {
47
37
  await this.getObjectHead(Key);
@@ -52,21 +42,18 @@ class S3Stream {
52
42
  return false;
53
43
  }
54
44
  }
55
-
56
45
  getObjectHead(Key) {
57
46
  return this.s3.headObject({
58
47
  Bucket: this.bucket,
59
48
  Key
60
49
  }).promise();
61
50
  }
62
-
63
51
  readStream(Key) {
64
52
  return this.s3.getObject({
65
53
  Bucket: this.bucket,
66
54
  Key
67
55
  }).createReadStream();
68
56
  }
69
-
70
57
  writeStream(Key, contentType = ARCHIVE_CONTENT_TYPE) {
71
58
  const streamPassThrough = new _stream.Stream.PassThrough();
72
59
  const params = {
@@ -78,36 +65,30 @@ class S3Stream {
78
65
  };
79
66
  return {
80
67
  streamPassThrough: streamPassThrough,
81
-
82
68
  /**
83
69
  * We're not using the `FileManager` storage plugin here because it currently doesn't support streams.
84
70
  */
85
71
  streamPassThroughUploadPromise: this.s3.upload(params).promise()
86
72
  };
87
73
  }
88
-
89
74
  upload(params) {
90
75
  return this.s3.upload((0, _objectSpread2.default)({
91
76
  ACL: "private",
92
77
  Bucket: this.bucket
93
78
  }, params)).promise();
94
79
  }
95
-
96
80
  listObject(prefix) {
97
81
  return this.s3.listObjects({
98
82
  Bucket: this.bucket,
99
83
  Prefix: prefix
100
84
  }).promise();
101
85
  }
102
-
103
86
  deleteObject(key) {
104
87
  return this.s3.deleteObject({
105
88
  Key: key,
106
89
  Bucket: this.bucket
107
90
  }).promise();
108
91
  }
109
-
110
92
  }
111
-
112
93
  const s3Stream = new S3Stream();
113
94
  exports.s3Stream = s3Stream;
@@ -1 +1 @@
1
- {"version":3,"names":["ARCHIVE_CONTENT_TYPE","S3Stream","constructor","s3","S3","region","process","env","AWS_REGION","bucket","S3_BUCKET","getPresignedUrl","key","getSignedUrl","Bucket","Key","Expires","isFileAccessible","getObjectHead","error","console","warn","log","headObject","promise","readStream","getObject","createReadStream","writeStream","contentType","streamPassThrough","Stream","PassThrough","params","ACL","Body","ContentType","streamPassThroughUploadPromise","upload","listObject","prefix","listObjects","Prefix","deleteObject","s3Stream"],"sources":["s3Stream.ts"],"sourcesContent":["import { Stream, PassThrough, Readable } from \"stream\";\nimport S3 from \"aws-sdk/clients/s3\";\n\nconst ARCHIVE_CONTENT_TYPE = \"application/zip\";\n\nclass S3Stream {\n s3: S3;\n bucket: string;\n\n constructor() {\n this.s3 = new S3({\n region: process.env.AWS_REGION as string\n });\n this.bucket = process.env.S3_BUCKET as string;\n }\n\n getPresignedUrl(key: string) {\n return this.s3.getSignedUrl(\"getObject\", {\n Bucket: this.bucket,\n Key: key,\n Expires: 604800 // 1 week\n });\n }\n\n /**\n * We're checking if the file is accessible on S3 by getting object meta data.\n * It help us to filter files that we need to download as part of export data.\n * @param Key {string}\n */\n async isFileAccessible(Key: string): Promise<boolean> {\n try {\n await this.getObjectHead(Key);\n return true;\n } catch (error) {\n console.warn(`Error while fetching meta data for file \"${Key}\"`);\n console.log(error);\n return false;\n }\n }\n\n getObjectHead(Key: string): Promise<S3.HeadObjectOutput> {\n return this.s3.headObject({ Bucket: this.bucket, Key }).promise();\n }\n\n readStream(Key: string): Readable {\n return this.s3.getObject({ Bucket: this.bucket, Key }).createReadStream();\n }\n\n writeStream(\n Key: string,\n contentType: string = ARCHIVE_CONTENT_TYPE\n ): {\n streamPassThrough: PassThrough;\n streamPassThroughUploadPromise: Promise<S3.ManagedUpload.SendData>;\n } {\n const streamPassThrough = new Stream.PassThrough();\n\n const params: S3.PutObjectRequest = {\n ACL: \"private\",\n Body: streamPassThrough,\n Bucket: this.bucket,\n ContentType: contentType,\n Key\n };\n\n return {\n streamPassThrough: streamPassThrough,\n /**\n * We're not using the `FileManager` storage plugin here because it currently doesn't support streams.\n */\n streamPassThroughUploadPromise: this.s3.upload(params).promise()\n };\n }\n\n upload(params: {\n Key: string;\n ContentType: string;\n Body: Buffer;\n }): Promise<S3.ManagedUpload.SendData> {\n return this.s3\n .upload({\n ACL: \"private\",\n Bucket: this.bucket,\n ...params\n })\n .promise();\n }\n\n listObject(prefix: string): Promise<S3.ListObjectsOutput> {\n return this.s3\n .listObjects({\n Bucket: this.bucket,\n Prefix: prefix\n })\n .promise();\n }\n\n deleteObject(key: string): Promise<S3.DeleteObjectOutput> {\n return this.s3.deleteObject({ Key: key, Bucket: this.bucket }).promise();\n }\n}\n\nexport const s3Stream = new S3Stream();\n"],"mappings":";;;;;;;;;;;;;AAAA;;AACA;;AAEA,MAAMA,oBAAoB,GAAG,iBAA7B;;AAEA,MAAMC,QAAN,CAAe;EAIXC,WAAW,GAAG;IAAA;IAAA;IACV,KAAKC,EAAL,GAAU,IAAIC,UAAJ,CAAO;MACbC,MAAM,EAAEC,OAAO,CAACC,GAAR,CAAYC;IADP,CAAP,CAAV;IAGA,KAAKC,MAAL,GAAcH,OAAO,CAACC,GAAR,CAAYG,SAA1B;EACH;;EAEDC,eAAe,CAACC,GAAD,EAAc;IACzB,OAAO,KAAKT,EAAL,CAAQU,YAAR,CAAqB,WAArB,EAAkC;MACrCC,MAAM,EAAE,KAAKL,MADwB;MAErCM,GAAG,EAAEH,GAFgC;MAGrCI,OAAO,EAAE,MAH4B,CAGrB;;IAHqB,CAAlC,CAAP;EAKH;EAED;AACJ;AACA;AACA;AACA;;;EAC0B,MAAhBC,gBAAgB,CAACF,GAAD,EAAgC;IAClD,IAAI;MACA,MAAM,KAAKG,aAAL,CAAmBH,GAAnB,CAAN;MACA,OAAO,IAAP;IACH,CAHD,CAGE,OAAOI,KAAP,EAAc;MACZC,OAAO,CAACC,IAAR,CAAc,4CAA2CN,GAAI,G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AO,KAAKT,EAAL,CAAQwC,YAAR,CAAqB;MAAE5B,GAAG,EAAEH,GAAP;MAAYE,MAAM,EAAE,KAAKL;IAAzB,CAArB,EAAwDe,OAAxD,EAAP;EACH;;AA9FU;;AAiGR,MAAMoB,QAAQ,GAAG,IAAI3C,QAAJ,EAAjB"}
1
+ {"version":3,"names":["ARCHIVE_CONTENT_TYPE","S3Stream","constructor","s3","S3","region","process","env","AWS_REGION","bucket","S3_BUCKET","getPresignedUrl","key","getSignedUrl","Bucket","Key","Expires","isFileAccessible","getObjectHead","error","console","warn","log","headObject","promise","readStream","getObject","createReadStream","writeStream","contentType","streamPassThrough","Stream","PassThrough","params","ACL","Body","ContentType","streamPassThroughUploadPromise","upload","listObject","prefix","listObjects","Prefix","deleteObject","s3Stream"],"sources":["s3Stream.ts"],"sourcesContent":["import { Stream, Readable } from \"stream\";\nimport S3 from \"aws-sdk/clients/s3\";\n\nconst ARCHIVE_CONTENT_TYPE = \"application/zip\";\n\nclass S3Stream {\n s3: S3;\n bucket: string;\n\n constructor() {\n this.s3 = new S3({\n region: process.env.AWS_REGION as string\n });\n this.bucket = process.env.S3_BUCKET as string;\n }\n\n getPresignedUrl(key: string) {\n return this.s3.getSignedUrl(\"getObject\", {\n Bucket: this.bucket,\n Key: key,\n Expires: 604800 // 1 week\n });\n }\n\n /**\n * We're checking if the file is accessible on S3 by getting object meta data.\n * It help us to filter files that we need to download as part of export data.\n * @param Key {string}\n */\n async isFileAccessible(Key: string): Promise<boolean> {\n try {\n await this.getObjectHead(Key);\n return true;\n } catch (error) {\n console.warn(`Error while fetching meta data for file \"${Key}\"`);\n console.log(error);\n return false;\n }\n }\n\n getObjectHead(Key: string): Promise<S3.HeadObjectOutput> {\n return this.s3.headObject({ Bucket: this.bucket, Key }).promise();\n }\n\n readStream(Key: string): Readable {\n return this.s3.getObject({ Bucket: this.bucket, Key }).createReadStream();\n }\n\n writeStream(Key: string, contentType: string = ARCHIVE_CONTENT_TYPE) {\n const streamPassThrough = new Stream.PassThrough();\n\n const params: S3.PutObjectRequest = {\n ACL: \"private\",\n Body: streamPassThrough,\n Bucket: this.bucket,\n ContentType: contentType,\n Key\n };\n\n return {\n streamPassThrough: streamPassThrough,\n /**\n * We're not using the `FileManager` storage plugin here because it currently doesn't support streams.\n */\n streamPassThroughUploadPromise: this.s3.upload(params).promise()\n };\n }\n\n upload(params: {\n Key: string;\n ContentType: string;\n Body: Buffer;\n }): Promise<S3.ManagedUpload.SendData> {\n return this.s3\n .upload({\n ACL: \"private\",\n Bucket: this.bucket,\n ...params\n })\n .promise();\n }\n\n listObject(prefix: string): Promise<S3.ListObjectsOutput> {\n return this.s3\n .listObjects({\n Bucket: this.bucket,\n Prefix: prefix\n })\n .promise();\n }\n\n deleteObject(key: string): Promise<S3.DeleteObjectOutput> {\n return this.s3.deleteObject({ Key: key, Bucket: this.bucket }).promise();\n }\n}\n\nexport const s3Stream = new S3Stream();\n"],"mappings":";;;;;;;;;AAAA;AACA;AAEA,MAAMA,oBAAoB,GAAG,iBAAiB;AAE9C,MAAMC,QAAQ,CAAC;EAIXC,WAAW,GAAG;IAAA;IAAA;IACV,IAAI,CAACC,EAAE,GAAG,IAAIC,UAAE,CAAC;MACbC,MAAM,EAAEC,OAAO,CAACC,GAAG,CAACC;IACxB,CAAC,CAAC;IACF,IAAI,CAACC,MAAM,GAAGH,OAAO,CAACC,GAAG,CAACG,SAAmB;EACjD;EAEAC,eAAe,CAACC,GAAW,EAAE;IACzB,OAAO,IAAI,CAACT,EAAE,CAACU,YAAY,CAAC,WAAW,EAAE;MACrCC,MAAM,EAAE,IAAI,CAACL,MAAM;MACnBM,GAAG,EAAEH,GAAG;MACRI,OAAO,EAAE,MAAM,CAAC;IACpB,CAAC,CAAC;EACN;;EAEA;AACJ;AACA;AACA;AACA;EACI,MAAMC,gBAAgB,CAACF,GAAW,EAAoB;IAClD,IAAI;MACA,MAAM,IAAI,CAACG,aAAa,CAACH,GAAG,CAAC;MAC7B,OAAO,IAAI;IAC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,EAAE,IAAI,CAACL,MAAM;MACnBiC,MAAM,EAAEF;IACZ,CAAC,CAAC,CACDhB,OAAO,EAAE;EAClB;EAEAmB,YAAY,CAAC/B,GAAW,EAAkC;IACtD,OAAO,IAAI,CAACT,EAAE,CAACwC,YAAY,CAAC;MAAE5B,GAAG,EAAEH,GAAG;MAAEE,MAAM,EAAE,IAAI,CAACL;IAAO,CAAC,CAAC,CAACe,OAAO,EAAE;EAC5E;AACJ;AAEO,MAAMoB,QAAQ,GAAG,IAAI3C,QAAQ,EAAE;AAAC"}
package/export/utils.d.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  import S3 from "aws-sdk/clients/s3";
2
- import { Page, PageBlock } from "@webiny/api-page-builder/types";
2
+ import { Page, PageBlock, PageTemplate } from "@webiny/api-page-builder/types";
3
+ import { FbForm } from "@webiny/api-form-builder/types";
3
4
  import { FileManagerContext, File } from "@webiny/api-file-manager/types";
4
5
  export declare const EXPORT_PAGES_FOLDER_KEY = "WEBINY_PB_EXPORT_PAGES";
5
6
  export declare const EXPORT_BLOCKS_FOLDER_KEY = "WEBINY_PB_EXPORT_BLOCK";
7
+ export declare const EXPORT_TEMPLATES_FOLDER_KEY = "WEBINY_PB_EXPORT_TEMPLATE";
8
+ export declare const EXPORT_FORMS_FOLDER_KEY = "WEBINY_FB_EXPORT_FORM";
6
9
  export interface ExportedPageData {
7
10
  page: Pick<Page, "content" | "title" | "version" | "status" | "settings" | "path">;
8
11
  files: File[];
@@ -13,4 +16,14 @@ export interface ExportedBlockData {
13
16
  files: File[];
14
17
  }
15
18
  export declare function exportBlock(block: PageBlock, exportBlocksDataKey: string, fileManager: FileManagerContext["fileManager"]): Promise<S3.ManagedUpload.SendData>;
19
+ export interface ExportedTemplateData {
20
+ template: Pick<PageTemplate, "title" | "slug" | "tags" | "description" | "content" | "layout" | "pageCategory">;
21
+ files: File[];
22
+ }
23
+ export declare function exportTemplate(template: PageTemplate, exportTemplatesDataKey: string, fileManager: FileManagerContext["fileManager"]): Promise<S3.ManagedUpload.SendData>;
16
24
  export declare function extractFilesFromData(data: Record<string, any>, files?: any[]): File[];
25
+ export interface ExportedFormData {
26
+ form: Pick<FbForm, "name" | "status" | "version" | "fields" | "layout" | "settings" | "triggers">;
27
+ files: File[];
28
+ }
29
+ export declare function exportForm(form: FbForm, exportFormsDataKey: string): Promise<S3.ManagedUpload.SendData>;
package/export/utils.js CHANGED
@@ -1,41 +1,41 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
- exports.EXPORT_PAGES_FOLDER_KEY = exports.EXPORT_BLOCKS_FOLDER_KEY = void 0;
7
+ exports.EXPORT_TEMPLATES_FOLDER_KEY = exports.EXPORT_PAGES_FOLDER_KEY = exports.EXPORT_FORMS_FOLDER_KEY = exports.EXPORT_BLOCKS_FOLDER_KEY = void 0;
9
8
  exports.exportBlock = exportBlock;
9
+ exports.exportForm = exportForm;
10
10
  exports.exportPage = exportPage;
11
+ exports.exportTemplate = exportTemplate;
11
12
  exports.extractFilesFromData = extractFilesFromData;
12
-
13
13
  var _get = _interopRequireDefault(require("lodash/get"));
14
-
15
14
  var _zipper = _interopRequireDefault(require("./zipper"));
16
-
17
15
  const EXPORT_PAGES_FOLDER_KEY = "WEBINY_PB_EXPORT_PAGES";
18
16
  exports.EXPORT_PAGES_FOLDER_KEY = EXPORT_PAGES_FOLDER_KEY;
19
17
  const EXPORT_BLOCKS_FOLDER_KEY = "WEBINY_PB_EXPORT_BLOCK";
20
18
  exports.EXPORT_BLOCKS_FOLDER_KEY = EXPORT_BLOCKS_FOLDER_KEY;
21
-
19
+ const EXPORT_TEMPLATES_FOLDER_KEY = "WEBINY_PB_EXPORT_TEMPLATE";
20
+ exports.EXPORT_TEMPLATES_FOLDER_KEY = EXPORT_TEMPLATES_FOLDER_KEY;
21
+ const EXPORT_FORMS_FOLDER_KEY = "WEBINY_FB_EXPORT_FORM";
22
+ exports.EXPORT_FORMS_FOLDER_KEY = EXPORT_FORMS_FOLDER_KEY;
22
23
  async function exportPage(page, exportPagesDataKey, fileManager) {
23
24
  // Extract all files
24
- const files = extractFilesFromData(page.content || {}); // Extract images from page settings
25
-
25
+ const files = extractFilesFromData(page.content || {});
26
+ // Extract images from page settings
26
27
  const pageSettingsImages = [(0, _get.default)(page, "settings.general.image"), (0, _get.default)(page, "settings.social.image")].filter(image => image && image.src);
27
- const fileIds = [...files, ...pageSettingsImages].map(imageFile => imageFile.id); // Get file data for all images
28
-
28
+ const fileIds = [...files, ...pageSettingsImages].map(imageFile => imageFile.id);
29
+ // Get file data for all images
29
30
  const imageFilesData = [];
30
-
31
31
  if (fileIds.length > 0) {
32
- const [filesData] = await fileManager.files.listFiles({
32
+ const [filesData] = await fileManager.listFiles({
33
33
  ids: fileIds
34
34
  });
35
35
  imageFilesData.push(...filesData);
36
- } // Extract the page data in a json file and upload it to S3
37
-
36
+ }
38
37
 
38
+ // Extract the page data in a json file and upload it to S3
39
39
  const pageData = {
40
40
  page: {
41
41
  content: page.content,
@@ -58,27 +58,24 @@ async function exportPage(page, exportPagesDataKey, fileManager) {
58
58
  });
59
59
  return zipper.process();
60
60
  }
61
-
62
61
  async function exportBlock(block, exportBlocksDataKey, fileManager) {
63
62
  // Extract all files
64
63
  const files = extractFilesFromData(block.content || {});
65
- const fileIds = files.map(imageFile => imageFile.id); // Get file data for all images
66
-
64
+ const fileIds = files.map(imageFile => imageFile.id);
65
+ // Get file data for all images
67
66
  const imageFilesData = [];
68
-
69
67
  if (fileIds.length > 0) {
70
- const [filesData] = await fileManager.files.listFiles({
68
+ const [filesData] = await fileManager.listFiles({
71
69
  ids: fileIds
72
70
  });
73
71
  imageFilesData.push(...filesData);
74
- } // Add block preview image file data
75
-
76
-
72
+ }
73
+ // Add block preview image file data
77
74
  if (block.preview.id) {
78
- imageFilesData.push(await fileManager.files.getFile(block.preview.id));
79
- } // Extract the block data in a json file and upload it to S3
80
-
75
+ imageFilesData.push(await fileManager.getFile(block.preview.id));
76
+ }
81
77
 
78
+ // Extract the block data in a json file and upload it to S3
82
79
  const blockData = {
83
80
  block: {
84
81
  name: block.name,
@@ -98,29 +95,62 @@ async function exportBlock(block, exportBlocksDataKey, fileManager) {
98
95
  });
99
96
  return zipper.process();
100
97
  }
98
+ async function exportTemplate(template, exportTemplatesDataKey, fileManager) {
99
+ // Extract all files
100
+ const files = extractFilesFromData(template.content || {});
101
+ const fileIds = files.map(imageFile => imageFile.id);
102
+ // Get file data for all images
103
+ const imageFilesData = [];
104
+ if (fileIds.length > 0) {
105
+ const [filesData] = await fileManager.listFiles({
106
+ ids: fileIds
107
+ });
108
+ imageFilesData.push(...filesData);
109
+ }
101
110
 
111
+ // Extract the template data in a json file and upload it to S3
112
+ const templateData = {
113
+ template: {
114
+ title: template.title,
115
+ slug: template.slug,
116
+ tags: template.tags,
117
+ description: template.description,
118
+ content: template.content,
119
+ layout: template.layout,
120
+ pageCategory: template.pageCategory
121
+ },
122
+ files: imageFilesData
123
+ };
124
+ const templateDataBuffer = Buffer.from(JSON.stringify(templateData));
125
+ const zipper = new _zipper.default({
126
+ exportInfo: {
127
+ files: imageFilesData,
128
+ name: template.title,
129
+ dataBuffer: templateDataBuffer
130
+ },
131
+ archiveFileKey: exportTemplatesDataKey
132
+ });
133
+ return zipper.process();
134
+ }
102
135
  function extractFilesFromData(data, files = []) {
103
136
  // Base case: termination
104
137
  if (!data || typeof data !== "object") {
105
138
  return files;
106
- } // Recursively call function for each element
107
-
108
-
139
+ }
140
+ // Recursively call function for each element
109
141
  if (Array.isArray(data)) {
110
142
  for (let i = 0; i < data.length; i++) {
111
143
  const element = data[i];
112
144
  extractFilesFromData(element, files);
113
145
  }
114
-
115
146
  return files;
116
- } // Main
117
-
147
+ }
118
148
 
149
+ // Main
119
150
  const tuple = Object.entries(data);
120
-
121
151
  for (let i = 0; i < tuple.length; i++) {
122
- const [key, value] = tuple[i]; // TODO: @ashutosh extract it to plugins, so that, we can handle cases for other components too.
123
-
152
+ const [key, value] = tuple[i];
153
+ // TODO: @ashutosh extract it to plugins, so that, we can handle cases for other components too.
124
154
  if (key === "file" && value) {
125
155
  files.push(value);
126
156
  } else if (key === "images" && Array.isArray(value)) {
@@ -130,6 +160,29 @@ function extractFilesFromData(data, files = []) {
130
160
  extractFilesFromData(value, files);
131
161
  }
132
162
  }
133
-
134
163
  return files;
164
+ }
165
+ async function exportForm(form, exportFormsDataKey) {
166
+ // Extract the form data in a json file and upload it to S3
167
+ const formData = {
168
+ form: {
169
+ name: form.name,
170
+ status: form.status,
171
+ version: form.version,
172
+ fields: form.fields,
173
+ layout: form.layout,
174
+ settings: form.settings,
175
+ triggers: form.triggers
176
+ }
177
+ };
178
+ const formDataBuffer = Buffer.from(JSON.stringify(formData));
179
+ const zipper = new _zipper.default({
180
+ exportInfo: {
181
+ files: [],
182
+ name: form.name,
183
+ dataBuffer: formDataBuffer
184
+ },
185
+ archiveFileKey: exportFormsDataKey
186
+ });
187
+ return zipper.process();
135
188
  }