@webiny/api-page-builder-import-export 0.0.0-unstable.d4f203fa97 → 0.0.0-unstable.d7f521b032

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 (123) hide show
  1. package/client.d.ts +2 -2
  2. package/client.js +2 -6
  3. package/client.js.map +1 -1
  4. package/export/combine/blocksHandler.d.ts +6 -0
  5. package/export/combine/blocksHandler.js +99 -0
  6. package/export/combine/blocksHandler.js.map +1 -0
  7. package/{exportPages → export}/combine/index.d.ts +3 -2
  8. package/export/combine/index.js +35 -0
  9. package/export/combine/index.js.map +1 -0
  10. package/export/combine/pagesHandler.d.ts +6 -0
  11. package/export/combine/pagesHandler.js +99 -0
  12. package/export/combine/pagesHandler.js.map +1 -0
  13. package/export/combine/templatesHandler.d.ts +6 -0
  14. package/export/combine/templatesHandler.js +99 -0
  15. package/export/combine/templatesHandler.js.map +1 -0
  16. package/export/process/blocksHandler.d.ts +6 -0
  17. package/export/process/blocksHandler.js +162 -0
  18. package/export/process/blocksHandler.js.map +1 -0
  19. package/{exportPages → export}/process/index.d.ts +4 -6
  20. package/export/process/index.js +32 -0
  21. package/export/process/index.js.map +1 -0
  22. package/export/process/pagesHandler.d.ts +6 -0
  23. package/export/process/pagesHandler.js +189 -0
  24. package/export/process/pagesHandler.js.map +1 -0
  25. package/export/process/templatesHandler.d.ts +6 -0
  26. package/export/process/templatesHandler.js +166 -0
  27. package/export/process/templatesHandler.js.map +1 -0
  28. package/{exportPages → export}/s3Stream.js +1 -20
  29. package/{exportPages → export}/s3Stream.js.map +1 -1
  30. package/export/utils.d.ts +22 -0
  31. package/export/utils.js +160 -0
  32. package/export/utils.js.map +1 -0
  33. package/{exportPages → export}/zipper.d.ts +6 -5
  34. package/{exportPages → export}/zipper.js +37 -48
  35. package/export/zipper.js.map +1 -0
  36. package/graphql/crud/blocks.crud.d.ts +4 -0
  37. package/graphql/crud/blocks.crud.js +137 -0
  38. package/graphql/crud/blocks.crud.js.map +1 -0
  39. package/graphql/crud/importExportTasks.crud.d.ts +5 -0
  40. package/graphql/crud/{pageImportExportTasks.crud.js → importExportTasks.crud.js} +57 -105
  41. package/graphql/crud/importExportTasks.crud.js.map +1 -0
  42. package/graphql/crud/pages.crud.d.ts +2 -2
  43. package/graphql/crud/pages.crud.js +39 -53
  44. package/graphql/crud/pages.crud.js.map +1 -1
  45. package/graphql/crud/templates.crud.d.ts +4 -0
  46. package/graphql/crud/templates.crud.js +124 -0
  47. package/graphql/crud/templates.crud.js.map +1 -0
  48. package/graphql/crud.d.ts +2 -2
  49. package/graphql/crud.js +4 -7
  50. package/graphql/crud.js.map +1 -1
  51. package/graphql/graphql/blocks.gql.d.ts +4 -0
  52. package/graphql/graphql/blocks.gql.js +52 -0
  53. package/graphql/graphql/blocks.gql.js.map +1 -0
  54. package/graphql/graphql/importExportTasks.gql.d.ts +4 -0
  55. package/graphql/graphql/{pageImportExportTasks.gql.js → importExportTasks.gql.js} +18 -23
  56. package/graphql/graphql/importExportTasks.gql.js.map +1 -0
  57. package/graphql/graphql/pages.gql.d.ts +2 -2
  58. package/graphql/graphql/pages.gql.js +4 -15
  59. package/graphql/graphql/pages.gql.js.map +1 -1
  60. package/graphql/graphql/templates.gql.d.ts +4 -0
  61. package/graphql/graphql/templates.gql.js +52 -0
  62. package/graphql/graphql/templates.gql.js.map +1 -0
  63. package/graphql/graphql/utils/resolve.js +0 -3
  64. package/graphql/graphql/utils/resolve.js.map +1 -1
  65. package/graphql/graphql.js +4 -6
  66. package/graphql/graphql.js.map +1 -1
  67. package/graphql/index.d.ts +2 -2
  68. package/graphql/index.js +0 -5
  69. package/graphql/index.js.map +1 -1
  70. package/graphql/types.d.ts +60 -23
  71. package/graphql/types.js.map +1 -1
  72. package/import/create/blocksHandler.d.ts +3 -0
  73. package/import/create/blocksHandler.js +100 -0
  74. package/import/create/blocksHandler.js.map +1 -0
  75. package/{importPages → import}/create/index.d.ts +7 -5
  76. package/import/create/index.js +35 -0
  77. package/import/create/index.js.map +1 -0
  78. package/import/create/pagesHandler.d.ts +3 -0
  79. package/import/create/pagesHandler.js +102 -0
  80. package/import/create/pagesHandler.js.map +1 -0
  81. package/import/create/templatesHandler.d.ts +3 -0
  82. package/import/create/templatesHandler.js +98 -0
  83. package/import/create/templatesHandler.js.map +1 -0
  84. package/import/process/blocksHandler.d.ts +3 -0
  85. package/import/process/blocksHandler.js +169 -0
  86. package/import/process/blocksHandler.js.map +1 -0
  87. package/{importPages → import}/process/index.d.ts +5 -3
  88. package/import/process/index.js +32 -0
  89. package/import/process/index.js.map +1 -0
  90. package/import/process/pagesHandler.d.ts +3 -0
  91. package/import/process/pagesHandler.js +177 -0
  92. package/import/process/pagesHandler.js.map +1 -0
  93. package/import/process/templatesHandler.d.ts +3 -0
  94. package/import/process/templatesHandler.js +169 -0
  95. package/import/process/templatesHandler.js.map +1 -0
  96. package/import/utils.d.ts +56 -0
  97. package/{importPages → import}/utils.js +193 -156
  98. package/import/utils.js.map +1 -0
  99. package/mockSecurity.js +0 -2
  100. package/mockSecurity.js.map +1 -1
  101. package/package.json +24 -24
  102. package/types.d.ts +62 -65
  103. package/types.js +17 -22
  104. package/types.js.map +1 -1
  105. package/exportPages/combine/index.js +0 -114
  106. package/exportPages/combine/index.js.map +0 -1
  107. package/exportPages/process/index.js +0 -208
  108. package/exportPages/process/index.js.map +0 -1
  109. package/exportPages/utils.d.ts +0 -13
  110. package/exportPages/utils.js +0 -113
  111. package/exportPages/utils.js.map +0 -1
  112. package/exportPages/zipper.js.map +0 -1
  113. package/graphql/crud/pageImportExportTasks.crud.d.ts +0 -5
  114. package/graphql/crud/pageImportExportTasks.crud.js.map +0 -1
  115. package/graphql/graphql/pageImportExportTasks.gql.d.ts +0 -4
  116. package/graphql/graphql/pageImportExportTasks.gql.js.map +0 -1
  117. package/importPages/create/index.js +0 -118
  118. package/importPages/create/index.js.map +0 -1
  119. package/importPages/process/index.js +0 -185
  120. package/importPages/process/index.js.map +0 -1
  121. package/importPages/utils.d.ts +0 -50
  122. package/importPages/utils.js.map +0 -1
  123. /package/{exportPages → export}/s3Stream.d.ts +0 -0
@@ -0,0 +1,22 @@
1
+ import S3 from "aws-sdk/clients/s3";
2
+ import { Page, PageBlock, PageTemplate } from "@webiny/api-page-builder/types";
3
+ import { FileManagerContext, File } from "@webiny/api-file-manager/types";
4
+ export declare const EXPORT_PAGES_FOLDER_KEY = "WEBINY_PB_EXPORT_PAGES";
5
+ export declare const EXPORT_BLOCKS_FOLDER_KEY = "WEBINY_PB_EXPORT_BLOCK";
6
+ export declare const EXPORT_TEMPLATES_FOLDER_KEY = "WEBINY_PB_EXPORT_TEMPLATE";
7
+ export interface ExportedPageData {
8
+ page: Pick<Page, "content" | "title" | "version" | "status" | "settings" | "path">;
9
+ files: File[];
10
+ }
11
+ export declare function exportPage(page: Page, exportPagesDataKey: string, fileManager: FileManagerContext["fileManager"]): Promise<S3.ManagedUpload.SendData>;
12
+ export interface ExportedBlockData {
13
+ block: Pick<PageBlock, "name" | "content" | "preview">;
14
+ files: File[];
15
+ }
16
+ export declare function exportBlock(block: PageBlock, exportBlocksDataKey: string, fileManager: FileManagerContext["fileManager"]): Promise<S3.ManagedUpload.SendData>;
17
+ export interface ExportedTemplateData {
18
+ template: Pick<PageTemplate, "title" | "slug" | "tags" | "description" | "content" | "layout">;
19
+ files: File[];
20
+ }
21
+ export declare function exportTemplate(template: PageTemplate, exportTemplatesDataKey: string, fileManager: FileManagerContext["fileManager"]): Promise<S3.ManagedUpload.SendData>;
22
+ export declare function extractFilesFromData(data: Record<string, any>, files?: any[]): File[];
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.EXPORT_TEMPLATES_FOLDER_KEY = exports.EXPORT_PAGES_FOLDER_KEY = exports.EXPORT_BLOCKS_FOLDER_KEY = void 0;
8
+ exports.exportBlock = exportBlock;
9
+ exports.exportPage = exportPage;
10
+ exports.exportTemplate = exportTemplate;
11
+ exports.extractFilesFromData = extractFilesFromData;
12
+ var _get = _interopRequireDefault(require("lodash/get"));
13
+ var _zipper = _interopRequireDefault(require("./zipper"));
14
+ const EXPORT_PAGES_FOLDER_KEY = "WEBINY_PB_EXPORT_PAGES";
15
+ exports.EXPORT_PAGES_FOLDER_KEY = EXPORT_PAGES_FOLDER_KEY;
16
+ const EXPORT_BLOCKS_FOLDER_KEY = "WEBINY_PB_EXPORT_BLOCK";
17
+ exports.EXPORT_BLOCKS_FOLDER_KEY = EXPORT_BLOCKS_FOLDER_KEY;
18
+ const EXPORT_TEMPLATES_FOLDER_KEY = "WEBINY_PB_EXPORT_TEMPLATE";
19
+ exports.EXPORT_TEMPLATES_FOLDER_KEY = EXPORT_TEMPLATES_FOLDER_KEY;
20
+ async function exportPage(page, exportPagesDataKey, fileManager) {
21
+ // Extract all files
22
+ const files = extractFilesFromData(page.content || {});
23
+ // Extract images from page settings
24
+ const pageSettingsImages = [(0, _get.default)(page, "settings.general.image"), (0, _get.default)(page, "settings.social.image")].filter(image => image && image.src);
25
+ const fileIds = [...files, ...pageSettingsImages].map(imageFile => imageFile.id);
26
+ // Get file data for all images
27
+ const imageFilesData = [];
28
+ if (fileIds.length > 0) {
29
+ const [filesData] = await fileManager.files.listFiles({
30
+ ids: fileIds
31
+ });
32
+ imageFilesData.push(...filesData);
33
+ }
34
+
35
+ // Extract the page data in a json file and upload it to S3
36
+ const pageData = {
37
+ page: {
38
+ content: page.content,
39
+ title: page.title,
40
+ path: page.path,
41
+ version: page.version,
42
+ status: page.status,
43
+ settings: page.settings
44
+ },
45
+ files: imageFilesData
46
+ };
47
+ const pageDataBuffer = Buffer.from(JSON.stringify(pageData));
48
+ const zipper = new _zipper.default({
49
+ exportInfo: {
50
+ files: imageFilesData,
51
+ name: page.title,
52
+ dataBuffer: pageDataBuffer
53
+ },
54
+ archiveFileKey: exportPagesDataKey
55
+ });
56
+ return zipper.process();
57
+ }
58
+ async function exportBlock(block, exportBlocksDataKey, fileManager) {
59
+ // Extract all files
60
+ const files = extractFilesFromData(block.content || {});
61
+ const fileIds = files.map(imageFile => imageFile.id);
62
+ // Get file data for all images
63
+ const imageFilesData = [];
64
+ if (fileIds.length > 0) {
65
+ const [filesData] = await fileManager.files.listFiles({
66
+ ids: fileIds
67
+ });
68
+ imageFilesData.push(...filesData);
69
+ }
70
+ // Add block preview image file data
71
+ if (block.preview.id) {
72
+ imageFilesData.push(await fileManager.files.getFile(block.preview.id));
73
+ }
74
+
75
+ // Extract the block data in a json file and upload it to S3
76
+ const blockData = {
77
+ block: {
78
+ name: block.name,
79
+ content: block.content,
80
+ preview: block.preview
81
+ },
82
+ files: imageFilesData
83
+ };
84
+ const blockDataBuffer = Buffer.from(JSON.stringify(blockData));
85
+ const zipper = new _zipper.default({
86
+ exportInfo: {
87
+ files: imageFilesData,
88
+ name: block.name,
89
+ dataBuffer: blockDataBuffer
90
+ },
91
+ archiveFileKey: exportBlocksDataKey
92
+ });
93
+ return zipper.process();
94
+ }
95
+ async function exportTemplate(template, exportTemplatesDataKey, fileManager) {
96
+ // Extract all files
97
+ const files = extractFilesFromData(template.content || {});
98
+ const fileIds = files.map(imageFile => imageFile.id);
99
+ // Get file data for all images
100
+ const imageFilesData = [];
101
+ if (fileIds.length > 0) {
102
+ const [filesData] = await fileManager.files.listFiles({
103
+ ids: fileIds
104
+ });
105
+ imageFilesData.push(...filesData);
106
+ }
107
+
108
+ // Extract the template data in a json file and upload it to S3
109
+ const templateData = {
110
+ template: {
111
+ title: template.title,
112
+ slug: template.slug,
113
+ tags: template.tags,
114
+ description: template.description,
115
+ content: template.content,
116
+ layout: template.layout
117
+ },
118
+ files: imageFilesData
119
+ };
120
+ const templateDataBuffer = Buffer.from(JSON.stringify(templateData));
121
+ const zipper = new _zipper.default({
122
+ exportInfo: {
123
+ files: imageFilesData,
124
+ name: template.title,
125
+ dataBuffer: templateDataBuffer
126
+ },
127
+ archiveFileKey: exportTemplatesDataKey
128
+ });
129
+ return zipper.process();
130
+ }
131
+ function extractFilesFromData(data, files = []) {
132
+ // Base case: termination
133
+ if (!data || typeof data !== "object") {
134
+ return files;
135
+ }
136
+ // Recursively call function for each element
137
+ if (Array.isArray(data)) {
138
+ for (let i = 0; i < data.length; i++) {
139
+ const element = data[i];
140
+ extractFilesFromData(element, files);
141
+ }
142
+ return files;
143
+ }
144
+
145
+ // Main
146
+ const tuple = Object.entries(data);
147
+ for (let i = 0; i < tuple.length; i++) {
148
+ const [key, value] = tuple[i];
149
+ // TODO: @ashutosh extract it to plugins, so that, we can handle cases for other components too.
150
+ if (key === "file" && value) {
151
+ files.push(value);
152
+ } else if (key === "images" && Array.isArray(value)) {
153
+ // Handle case for "images-list" component
154
+ files.push(...value);
155
+ } else {
156
+ extractFilesFromData(value, files);
157
+ }
158
+ }
159
+ return files;
160
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"names":["EXPORT_PAGES_FOLDER_KEY","EXPORT_BLOCKS_FOLDER_KEY","EXPORT_TEMPLATES_FOLDER_KEY","exportPage","page","exportPagesDataKey","fileManager","files","extractFilesFromData","content","pageSettingsImages","get","filter","image","src","fileIds","map","imageFile","id","imageFilesData","length","filesData","listFiles","ids","push","pageData","title","path","version","status","settings","pageDataBuffer","Buffer","from","JSON","stringify","zipper","Zipper","exportInfo","name","dataBuffer","archiveFileKey","process","exportBlock","block","exportBlocksDataKey","preview","getFile","blockData","blockDataBuffer","exportTemplate","template","exportTemplatesDataKey","templateData","slug","tags","description","layout","templateDataBuffer","data","Array","isArray","i","element","tuple","Object","entries","key","value"],"sources":["utils.ts"],"sourcesContent":["import S3 from \"aws-sdk/clients/s3\";\nimport { Page, PageBlock, PageTemplate } from \"@webiny/api-page-builder/types\";\nimport { FileManagerContext, File } from \"@webiny/api-file-manager/types\";\nimport get from \"lodash/get\";\nimport Zipper from \"./zipper\";\n\nexport const EXPORT_PAGES_FOLDER_KEY = \"WEBINY_PB_EXPORT_PAGES\";\nexport const EXPORT_BLOCKS_FOLDER_KEY = \"WEBINY_PB_EXPORT_BLOCK\";\nexport const EXPORT_TEMPLATES_FOLDER_KEY = \"WEBINY_PB_EXPORT_TEMPLATE\";\n\nexport interface ExportedPageData {\n page: Pick<Page, \"content\" | \"title\" | \"version\" | \"status\" | \"settings\" | \"path\">;\n files: File[];\n}\n\nexport async function exportPage(\n page: Page,\n exportPagesDataKey: string,\n fileManager: FileManagerContext[\"fileManager\"]\n): Promise<S3.ManagedUpload.SendData> {\n // Extract all files\n const files = extractFilesFromData(page.content || {});\n // Extract images from page settings\n const pageSettingsImages = [\n get(page, \"settings.general.image\") as unknown as File,\n get(page, \"settings.social.image\") as unknown as File\n ].filter(image => image && image.src);\n\n const fileIds = [...files, ...pageSettingsImages].map(imageFile => imageFile.id);\n // Get file data for all images\n const imageFilesData = [];\n if (fileIds.length > 0) {\n const [filesData] = await fileManager.files.listFiles({ ids: fileIds });\n imageFilesData.push(...filesData);\n }\n\n // Extract the page data in a json file and upload it to S3\n const pageData = {\n page: {\n content: page.content,\n title: page.title,\n path: page.path,\n version: page.version,\n status: page.status,\n settings: page.settings\n },\n files: imageFilesData\n };\n const pageDataBuffer = Buffer.from(JSON.stringify(pageData));\n\n const zipper = new Zipper({\n exportInfo: {\n files: imageFilesData,\n name: page.title,\n dataBuffer: pageDataBuffer\n },\n archiveFileKey: exportPagesDataKey\n });\n\n return zipper.process();\n}\n\nexport interface ExportedBlockData {\n block: Pick<PageBlock, \"name\" | \"content\" | \"preview\">;\n files: File[];\n}\n\nexport async function exportBlock(\n block: PageBlock,\n exportBlocksDataKey: string,\n fileManager: FileManagerContext[\"fileManager\"]\n): Promise<S3.ManagedUpload.SendData> {\n // Extract all files\n const files = extractFilesFromData(block.content || {});\n const fileIds = files.map(imageFile => imageFile.id);\n // Get file data for all images\n const imageFilesData = [];\n if (fileIds.length > 0) {\n const [filesData] = await fileManager.files.listFiles({ ids: fileIds });\n imageFilesData.push(...filesData);\n }\n // Add block preview image file data\n if (block.preview.id) {\n imageFilesData.push(await fileManager.files.getFile(block.preview.id));\n }\n\n // Extract the block data in a json file and upload it to S3\n const blockData = {\n block: {\n name: block.name,\n content: block.content,\n preview: block.preview\n },\n files: imageFilesData\n };\n const blockDataBuffer = Buffer.from(JSON.stringify(blockData));\n\n const zipper = new Zipper({\n exportInfo: {\n files: imageFilesData,\n name: block.name,\n dataBuffer: blockDataBuffer\n },\n archiveFileKey: exportBlocksDataKey\n });\n\n return zipper.process();\n}\n\nexport interface ExportedTemplateData {\n template: Pick<PageTemplate, \"title\" | \"slug\" | \"tags\" | \"description\" | \"content\" | \"layout\">;\n files: File[];\n}\n\nexport async function exportTemplate(\n template: PageTemplate,\n exportTemplatesDataKey: string,\n fileManager: FileManagerContext[\"fileManager\"]\n): Promise<S3.ManagedUpload.SendData> {\n // Extract all files\n const files = extractFilesFromData(template.content || {});\n const fileIds = files.map(imageFile => imageFile.id);\n // Get file data for all images\n const imageFilesData = [];\n if (fileIds.length > 0) {\n const [filesData] = await fileManager.files.listFiles({ ids: fileIds });\n imageFilesData.push(...filesData);\n }\n\n // Extract the template data in a json file and upload it to S3\n const templateData = {\n template: {\n title: template.title,\n slug: template.slug,\n tags: template.tags,\n description: template.description,\n content: template.content,\n layout: template.layout\n },\n files: imageFilesData\n };\n const templateDataBuffer = Buffer.from(JSON.stringify(templateData));\n\n const zipper = new Zipper({\n exportInfo: {\n files: imageFilesData,\n name: template.title,\n dataBuffer: templateDataBuffer\n },\n archiveFileKey: exportTemplatesDataKey\n });\n\n return zipper.process();\n}\n\nexport function extractFilesFromData(data: Record<string, any>, files: any[] = []): File[] {\n // Base case: termination\n if (!data || typeof data !== \"object\") {\n return files;\n }\n // Recursively call function for each element\n if (Array.isArray(data)) {\n for (let i = 0; i < data.length; i++) {\n const element = data[i];\n extractFilesFromData(element, files);\n }\n return files;\n }\n\n // Main\n const tuple = Object.entries(data);\n for (let i = 0; i < tuple.length; i++) {\n const [key, value] = tuple[i];\n // TODO: @ashutosh extract it to plugins, so that, we can handle cases for other components too.\n if (key === \"file\" && value) {\n files.push(value);\n } else if (key === \"images\" && Array.isArray(value)) {\n // Handle case for \"images-list\" component\n files.push(...value);\n } else {\n extractFilesFromData(value, files);\n }\n }\n return files;\n}\n"],"mappings":";;;;;;;;;;;AAGA;AACA;AAEO,MAAMA,uBAAuB,GAAG,wBAAwB;AAAC;AACzD,MAAMC,wBAAwB,GAAG,wBAAwB;AAAC;AAC1D,MAAMC,2BAA2B,GAAG,2BAA2B;AAAC;AAOhE,eAAeC,UAAU,CAC5BC,IAAU,EACVC,kBAA0B,EAC1BC,WAA8C,EACZ;EAClC;EACA,MAAMC,KAAK,GAAGC,oBAAoB,CAACJ,IAAI,CAACK,OAAO,IAAI,CAAC,CAAC,CAAC;EACtD;EACA,MAAMC,kBAAkB,GAAG,CACvB,IAAAC,YAAG,EAACP,IAAI,EAAE,wBAAwB,CAAC,EACnC,IAAAO,YAAG,EAACP,IAAI,EAAE,uBAAuB,CAAC,CACrC,CAACQ,MAAM,CAACC,KAAK,IAAIA,KAAK,IAAIA,KAAK,CAACC,GAAG,CAAC;EAErC,MAAMC,OAAO,GAAG,CAAC,GAAGR,KAAK,EAAE,GAAGG,kBAAkB,CAAC,CAACM,GAAG,CAACC,SAAS,IAAIA,SAAS,CAACC,EAAE,CAAC;EAChF;EACA,MAAMC,cAAc,GAAG,EAAE;EACzB,IAAIJ,OAAO,CAACK,MAAM,GAAG,CAAC,EAAE;IACpB,MAAM,CAACC,SAAS,CAAC,GAAG,MAAMf,WAAW,CAACC,KAAK,CAACe,SAAS,CAAC;MAAEC,GAAG,EAAER;IAAQ,CAAC,CAAC;IACvEI,cAAc,CAACK,IAAI,CAAC,GAAGH,SAAS,CAAC;EACrC;;EAEA;EACA,MAAMI,QAAQ,GAAG;IACbrB,IAAI,EAAE;MACFK,OAAO,EAAEL,IAAI,CAACK,OAAO;MACrBiB,KAAK,EAAEtB,IAAI,CAACsB,KAA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AC,CAAC;IAC7B;IACA,IAAIK,GAAG,KAAK,MAAM,IAAIC,KAAK,EAAE;MACzB7D,KAAK,CAACiB,IAAI,CAAC4C,KAAK,CAAC;IACrB,CAAC,MAAM,IAAID,GAAG,KAAK,QAAQ,IAAIP,KAAK,CAACC,OAAO,CAACO,KAAK,CAAC,EAAE;MACjD;MACA7D,KAAK,CAACiB,IAAI,CAAC,GAAG4C,KAAK,CAAC;IACxB,CAAC,MAAM;MACH5D,oBAAoB,CAAC4D,KAAK,EAAE7D,KAAK,CAAC;IACtC;EACJ;EACA,OAAOA,KAAK;AAChB"}
@@ -2,15 +2,15 @@
2
2
  /// <reference types="node" />
3
3
  import S3 from "aws-sdk/clients/s3";
4
4
  import { Readable } from "stream";
5
- import { ImageFile } from "./utils";
5
+ import { File } from "@webiny/api-file-manager/types";
6
6
  interface FileStreamDetails {
7
7
  stream: Readable;
8
8
  filename: string;
9
9
  }
10
10
  interface ExportInfo {
11
- files: ImageFile[];
12
- pageTitle: string;
13
- pageDataBuffer: Buffer;
11
+ files: File[];
12
+ name: string;
13
+ dataBuffer: Buffer;
14
14
  }
15
15
  export interface ZipperConfig {
16
16
  exportInfo: ExportInfo;
@@ -29,7 +29,8 @@ export declare class ZipOfZip {
29
29
  private readonly archiveFormat;
30
30
  private readonly archiveFileName;
31
31
  keys: string[];
32
- constructor(keys: string[]);
32
+ filename: string;
33
+ constructor(keys: string[], filename: string);
33
34
  getFileStreams(): FileStreamDetails[];
34
35
  process(): Promise<S3.ManagedUpload.SendData>;
35
36
  }
@@ -1,29 +1,20 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
-
5
4
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
-
7
5
  Object.defineProperty(exports, "__esModule", {
8
6
  value: true
9
7
  });
10
8
  exports.default = exports.ZipOfZip = void 0;
11
-
12
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
-
14
10
  var _archiver = _interopRequireDefault(require("archiver"));
15
-
16
11
  var _stream = require("stream");
17
-
18
12
  var path = _interopRequireWildcard(require("path"));
19
-
20
13
  var _kebabCase = _interopRequireDefault(require("lodash/kebabCase"));
21
-
22
14
  var _uniqid = _interopRequireDefault(require("uniqid"));
23
-
24
15
  var _s3Stream = require("./s3Stream");
25
-
26
16
  // TODO: Move "archive" in layer
17
+
27
18
  class Zipper {
28
19
  constructor(config) {
29
20
  (0, _defineProperty2.default)(this, "archiveFormat", "zip");
@@ -31,12 +22,11 @@ class Zipper {
31
22
  (0, _defineProperty2.default)(this, "archiveFileName", void 0);
32
23
  (0, _defineProperty2.default)(this, "config", void 0);
33
24
  this.config = config;
34
- this.archiveFileName = (0, _uniqid.default)(`${this.config.archiveFileKey}/`, `-${(0, _kebabCase.default)(this.config.exportInfo.pageTitle)}.zip`);
25
+ this.archiveFileName = (0, _uniqid.default)(`${this.config.archiveFileKey}/`, `-${(0, _kebabCase.default)(this.config.exportInfo.name)}.zip`);
35
26
  }
36
-
37
27
  s3DownloadStreams() {
38
28
  const exportInfo = this.config.exportInfo;
39
- const prefix = (0, _uniqid.default)("", `-${(0, _kebabCase.default)(exportInfo.pageTitle)}`);
29
+ const prefix = (0, _uniqid.default)("", `-${(0, _kebabCase.default)(exportInfo.name)}`);
40
30
  const files = exportInfo.files.map(({
41
31
  key
42
32
  }) => {
@@ -46,53 +36,52 @@ class Zipper {
46
36
  };
47
37
  });
48
38
  return [...files, {
49
- stream: _stream.Readable.from(exportInfo.pageDataBuffer),
50
- filename: `${prefix}\\${exportInfo.pageTitle}.json`
39
+ stream: _stream.Readable.from(exportInfo.dataBuffer),
40
+ filename: `${prefix}\\${exportInfo.name}.json`
51
41
  }];
52
42
  }
53
-
54
43
  process() {
55
44
  const {
56
45
  streamPassThrough,
57
46
  streamPassThroughUploadPromise
58
- } = _s3Stream.s3Stream.writeStream(this.archiveFileName); // 1. Read all files from S3 using stream.
59
-
60
-
61
- const s3FilesStreams = this.s3DownloadStreams(); // 2. Prepare zip from the file stream.
62
-
63
- const archive = _archiver.default.create(this.archiveFormat); // Handle archive events.
47
+ } = _s3Stream.s3Stream.writeStream(this.archiveFileName);
64
48
 
49
+ // 1. Read all files from S3 using stream.
50
+ const s3FilesStreams = this.s3DownloadStreams();
65
51
 
52
+ // 2. Prepare zip from the file stream.
53
+ const archive = _archiver.default.create(this.archiveFormat);
54
+ // Handle archive events.
66
55
  archive.on("error", error => {
67
56
  throw new Error(`${error.name} ${error.code} ${error.message} ${error.path} ${error.stack}`);
68
- }); // Append all file streams to archive.
57
+ });
69
58
 
59
+ // Append all file streams to archive.
70
60
  s3FilesStreams.forEach(streamDetails => archive.append(streamDetails.stream, {
71
61
  name: streamDetails.filename
72
- })); // Pipe archive output to streamPassThrough (Transform Stream) which will be uploaded to S3.
62
+ }));
73
63
 
74
- archive.pipe(streamPassThrough); // Finalize the archive (ie we are done appending files but streams have to finish yet)
64
+ // Pipe archive output to streamPassThrough (Transform Stream) which will be uploaded to S3.
65
+ archive.pipe(streamPassThrough);
66
+ // Finalize the archive (ie we are done appending files but streams have to finish yet)
75
67
  // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
68
+ archive.finalize();
76
69
 
77
- archive.finalize(); // 3. Return upload stream promise.
78
-
70
+ // 3. Return upload stream promise.
79
71
  return streamPassThroughUploadPromise;
80
72
  }
81
-
82
73
  }
83
-
84
74
  exports.default = Zipper;
85
- const PAGE_EXPORT_BASENAME = `WEBINY_PAGE_EXPORT.zip`;
86
-
87
75
  class ZipOfZip {
88
- constructor(keys) {
76
+ constructor(keys, filename) {
89
77
  (0, _defineProperty2.default)(this, "archiveFormat", "zip");
90
78
  (0, _defineProperty2.default)(this, "archiveFileName", void 0);
91
79
  (0, _defineProperty2.default)(this, "keys", void 0);
80
+ (0, _defineProperty2.default)(this, "filename", void 0);
92
81
  this.keys = keys;
93
- this.archiveFileName = (0, _uniqid.default)("", `-${PAGE_EXPORT_BASENAME}`);
82
+ this.filename = filename;
83
+ this.archiveFileName = (0, _uniqid.default)("", `-${filename}`);
94
84
  }
95
-
96
85
  getFileStreams() {
97
86
  return this.keys.map(key => {
98
87
  return {
@@ -101,35 +90,35 @@ class ZipOfZip {
101
90
  };
102
91
  });
103
92
  }
104
-
105
93
  process() {
106
94
  const {
107
95
  streamPassThrough,
108
96
  streamPassThroughUploadPromise
109
- } = _s3Stream.s3Stream.writeStream(this.archiveFileName); // 1. Read all files from S3 using stream.
110
-
111
-
112
- const fileStreamDetails = this.getFileStreams(); // 2. Prepare zip from the file stream.
113
-
114
- const archive = _archiver.default.create(this.archiveFormat); // Handle archive events.
97
+ } = _s3Stream.s3Stream.writeStream(this.archiveFileName);
115
98
 
99
+ // 1. Read all files from S3 using stream.
100
+ const fileStreamDetails = this.getFileStreams();
116
101
 
102
+ // 2. Prepare zip from the file stream.
103
+ const archive = _archiver.default.create(this.archiveFormat);
104
+ // Handle archive events.
117
105
  archive.on("error", error => {
118
106
  throw new Error(`${error.name} ${error.code} ${error.message} ${error.path} ${error.stack}`);
119
- }); // Append all file streams to archive.
107
+ });
120
108
 
109
+ // Append all file streams to archive.
121
110
  fileStreamDetails.forEach(streamDetails => archive.append(streamDetails.stream, {
122
111
  name: streamDetails.filename
123
- })); // Pipe archive output to streamPassThrough (Transform Stream) which will be uploaded to S3.
112
+ }));
124
113
 
125
- archive.pipe(streamPassThrough); // Finalize the archive (ie we are done appending files but streams have to finish yet)
114
+ // Pipe archive output to streamPassThrough (Transform Stream) which will be uploaded to S3.
115
+ archive.pipe(streamPassThrough);
116
+ // Finalize the archive (ie we are done appending files but streams have to finish yet)
126
117
  // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
118
+ archive.finalize();
127
119
 
128
- archive.finalize(); // 3. Return upload stream promise.
129
-
120
+ // 3. Return upload stream promise.
130
121
  return streamPassThroughUploadPromise;
131
122
  }
132
-
133
123
  }
134
-
135
124
  exports.ZipOfZip = ZipOfZip;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Zipper","constructor","config","archiveFileName","uniqueId","archiveFileKey","kebabCase","exportInfo","name","s3DownloadStreams","prefix","files","map","key","stream","s3Stream","readStream","filename","filesDirName","path","basename","Readable","from","dataBuffer","process","streamPassThrough","streamPassThroughUploadPromise","writeStream","s3FilesStreams","archive","vending","create","archiveFormat","on","error","Error","code","message","stack","forEach","streamDetails","append","pipe","finalize","ZipOfZip","keys","getFileStreams","fileStreamDetails"],"sources":["zipper.ts"],"sourcesContent":["// TODO: Move \"archive\" in layer\nimport vending, { ArchiverError } from \"archiver\";\nimport S3 from \"aws-sdk/clients/s3\";\nimport { Readable } from \"stream\";\nimport * as path from \"path\";\nimport kebabCase from \"lodash/kebabCase\";\nimport uniqueId from \"uniqid\";\nimport { s3Stream } from \"./s3Stream\";\nimport { File } from \"@webiny/api-file-manager/types\";\n\ninterface FileStreamDetails {\n stream: Readable;\n filename: string;\n}\n\ninterface ExportInfo {\n files: File[];\n name: string;\n dataBuffer: Buffer;\n}\n\nexport interface ZipperConfig {\n exportInfo: ExportInfo;\n archiveFileKey: string;\n}\n\nexport default class Zipper {\n private readonly archiveFormat = \"zip\";\n private readonly filesDirName = \"assets\";\n private readonly archiveFileName: string;\n config: ZipperConfig;\n\n constructor(config: ZipperConfig) {\n this.config = config;\n this.archiveFileName = uniqueId(\n `${this.config.archiveFileKey}/`,\n `-${kebabCase(this.config.exportInfo.name)}.zip`\n );\n }\n\n s3DownloadStreams(): FileStreamDetails[] {\n const exportInfo = this.config.exportInfo;\n const prefix = uniqueId(\"\", `-${kebabCase(exportInfo.name)}`);\n const files = exportInfo.files.map(({ key }) => {\n return {\n stream: s3Stream.readStream(key),\n filename: `${prefix}\\\\${this.filesDirName}\\\\${path.basename(key)}`\n };\n });\n\n return [\n ...files,\n {\n stream: Readable.from(exportInfo.dataBuffer),\n filename: `${prefix}\\\\${exportInfo.name}.json`\n }\n ];\n }\n\n process(): Promise<S3.ManagedUpload.SendData> {\n const { streamPassThrough, streamPassThroughUploadPromise } = s3Stream.writeStream(\n this.archiveFileName\n );\n\n // 1. Read all files from S3 using stream.\n const s3FilesStreams = this.s3DownloadStreams();\n\n // 2. Prepare zip from the file stream.\n const archive = vending.create(this.archiveFormat);\n // Handle archive events.\n archive.on(\"error\", (error: ArchiverError) => {\n throw new Error(\n `${error.name} ${error.code} ${error.message} ${error.path} ${error.stack}`\n );\n });\n\n // Append all file streams to archive.\n s3FilesStreams.forEach((streamDetails: FileStreamDetails) =>\n archive.append(streamDetails.stream, { name: streamDetails.filename })\n );\n\n // Pipe archive output to streamPassThrough (Transform Stream) which will be uploaded to S3.\n archive.pipe(streamPassThrough);\n // Finalize the archive (ie we are done appending files but streams have to finish yet)\n // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand\n archive.finalize();\n\n // 3. Return upload stream promise.\n return streamPassThroughUploadPromise;\n }\n}\n\nexport class ZipOfZip {\n private readonly archiveFormat = \"zip\";\n private readonly archiveFileName: string;\n keys: string[];\n filename: string;\n\n constructor(keys: string[], filename: string) {\n this.keys = keys;\n this.filename = filename;\n this.archiveFileName = uniqueId(\"\", `-${filename}`);\n }\n\n getFileStreams(): FileStreamDetails[] {\n return this.keys.map(key => {\n return {\n stream: s3Stream.readStream(key),\n filename: `${path.basename(key)}`\n };\n });\n }\n\n process(): Promise<S3.ManagedUpload.SendData> {\n const { streamPassThrough, streamPassThroughUploadPromise } = s3Stream.writeStream(\n this.archiveFileName\n );\n\n // 1. Read all files from S3 using stream.\n const fileStreamDetails = this.getFileStreams();\n\n // 2. Prepare zip from the file stream.\n const archive = vending.create(this.archiveFormat);\n // Handle archive events.\n archive.on(\"error\", (error: ArchiverError) => {\n throw new Error(\n `${error.name} ${error.code} ${error.message} ${error.path} ${error.stack}`\n );\n });\n\n // Append all file streams to archive.\n fileStreamDetails.forEach((streamDetails: FileStreamDetails) =>\n archive.append(streamDetails.stream, { name: streamDetails.filename })\n );\n\n // Pipe archive output to streamPassThrough (Transform Stream) which will be uploaded to S3.\n archive.pipe(streamPassThrough);\n // Finalize the archive (ie we are done appending files but streams have to finish yet)\n // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand\n archive.finalize();\n\n // 3. Return upload stream promise.\n return streamPassThroughUploadPromise;\n }\n}\n"],"mappings":";;;;;;;;;AACA;AAEA;AACA;AACA;AACA;AACA;AAPA;;AA0Be,MAAMA,MAAM,CAAC;EAMxBC,WAAW,CAACC,MAAoB,EAAE;IAAA,qDALD,KAAK;IAAA,oDACN,QAAQ;IAAA;IAAA;IAKpC,IAAI,CAACA,MAAM,GAAGA,MAAM;IACpB,IAAI,CAACC,eAAe,GAAG,IAAAC,eAAQ,EAC1B,GAAE,IAAI,CAACF,MAAM,CAACG,cAAe,GAAE,EAC/B,IAAG,IAAAC,kBAAS,EAAC,IAAI,CAACJ,MAAM,CAACK,UAAU,CAACC,IAAI,CAAE,MAAK,CACnD;EACL;EAEAC,iBAAiB,GAAwB;IACrC,MAAMF,UAAU,GAAG,IAAI,CAACL,MAAM,CAACK,UAAU;IACzC,MAAMG,MAAM,GAAG,IAAAN,eAAQ,EAAC,EAAE,EAAG,IAAG,IAAAE,kBAAS,EAACC,UAAU,CAACC,IAAI,CAAE,EAAC,CAAC;IAC7D,MAAMG,KAAK,GAAGJ,UAAU,CAACI,KAAK,CAACC,GAAG,CAAC,CAAC;MAAEC;IAAI,CAAC,KAAK;MAC5C,OAAO;QACHC,MAAM,EAAEC,kBAAQ,CAACC,UAAU,CAACH,GAAG,CAAC;QAChCI,QAAQ,EAAG,GAAEP,MAAO,KAAI,IAAI,CAACQ,YAAa,KAAIC,IAAI,CAACC,QAAQ,CAACP,GAAG,CAAE;MACrE,CAAC;IACL,CAAC,CAAC;IAEF,OAAO,CACH,GAAGF,KAAK,EACR;MACIG,MAAM,EAAEO,gBAAQ,CAACC,IAAI,CAACf,UAAU,CAACgB,UAAU,CAAC;MAC5CN,QAAQ,EAAG,GAAEP,MAAO,KAAIH,UAAU,CAACC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,IAAI;MACxB,OAAO;QACHC,MAAM,EAAEC,kBAAQ,CAACC,UAAU,CAACH,GAAG,CAAC;QAChCI,QAAQ,EAAG,GAAEE,IAAI,CAACC,QAAQ,CAACP,GAAG,CAAE;MACpC,CAAC;IACL,CAAC,CAAC;EACN;EAEAW,OAAO,GAAuC;IAC1C,MAAM;MAAEC,iBAAiB;MAAEC;IAA+B,CAAC,GAAGX,kBAAQ,CAACY,WAAW,CAC9E,IAAI,CAACxB,eAAe,CACvB;;IAED;IACA,MAAM4C,iBAAiB,GAAG,IAAI,CAACD,cAAc,EAAE;;IAE/C;IACA,MAAMjB,OAAO,GAAGC,iBAAO,CAACC,MAAM,CAAC,IAAI,CAACC,aAAa,CAAC;IAClD;IACAH,OAAO,CAACI,EAAE,CAAC,OAAO,EAAGC,KAAoB,IAAK;MAC1C,MAAM,IAAIC,KAAK,CACV,GAAED,KAAK,CAAC1B,IAAK,IAAG0B,KAAK,CAACE,IAAK,IAAGF,KAAK,CAACG,OAAQ,IAAGH,KAAK,CAACf,IAAK,IAAGe,KAAK,CAACI,KAAM,EAAC,CAC9E;IACL,CAAC,CAAC;;IAEF;IACAS,iBAAiB,CAACR,OAAO,CAAEC,aAAgC,IACvDX,OAAO,CAACY,MAAM,CAACD,aAAa,CAAC1B,MAAM,EAAE;MAAEN,IAAI,EAAEgC,aAAa,CAACvB;IAAS,CAAC,CAAC,CACzE;;IAED;IACAY,OAAO,CAACa,IAAI,CAACjB,iBAAiB,CAAC;IAC/B;IACA;IACAI,OAAO,CAACc,QAAQ,EAAE;;IAElB;IACA,OAAOjB,8BAA8B;EACzC;AACJ;AAAC"}
@@ -0,0 +1,4 @@
1
+ import { ContextPlugin } from "@webiny/api";
2
+ import { PbImportExportContext } from "../../types";
3
+ declare const _default: ContextPlugin<PbImportExportContext>;
4
+ export default _default;
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = void 0;
8
+ var _error = _interopRequireDefault(require("@webiny/error"));
9
+ var _handlerGraphql = require("@webiny/handler-graphql");
10
+ var _api = require("@webiny/api");
11
+ var _checkBasePermissions = _interopRequireDefault(require("@webiny/api-page-builder/graphql/crud/utils/checkBasePermissions"));
12
+ var _types = require("../../types");
13
+ var _client = require("../../client");
14
+ var _utils = require("../../import/utils");
15
+ var _utils2 = require("../../export/utils");
16
+ var _utils3 = require("@webiny/utils");
17
+ const PERMISSION_NAME = "pb.block";
18
+ const EXPORT_BLOCKS_PROCESS_HANDLER = process.env.EXPORT_PROCESS_HANDLER;
19
+ const IMPORT_BLOCKS_CREATE_HANDLER = process.env.IMPORT_CREATE_HANDLER;
20
+ var _default = new _api.ContextPlugin(context => {
21
+ const importExportCrud = {
22
+ async importBlocks({
23
+ category: categorySlug,
24
+ zipFileUrl
25
+ }) {
26
+ await (0, _checkBasePermissions.default)(context, PERMISSION_NAME, {
27
+ rwd: "w"
28
+ });
29
+
30
+ // Bail out early if category not found
31
+ const category = await context.pageBuilder.getBlockCategory(categorySlug);
32
+ if (!category) {
33
+ throw new _handlerGraphql.NotFoundError(`Category with slug "${categorySlug}" not found.`);
34
+ }
35
+
36
+ // Create a task for import block
37
+ const task = await context.pageBuilder.importExportTask.createTask({
38
+ status: _types.ImportExportTaskStatus.PENDING,
39
+ input: {
40
+ category: categorySlug,
41
+ zipFileUrl
42
+ }
43
+ });
44
+ /**
45
+ * Import Blocks
46
+ * ImportBlocks
47
+ * importBlocks
48
+ */
49
+ await (0, _client.invokeHandlerClient)({
50
+ context,
51
+ name: IMPORT_BLOCKS_CREATE_HANDLER,
52
+ payload: {
53
+ category: categorySlug,
54
+ zipFileUrl,
55
+ task,
56
+ type: "block",
57
+ identity: context.security.getIdentity()
58
+ },
59
+ description: "Import Blocks - create"
60
+ });
61
+ return {
62
+ task
63
+ };
64
+ },
65
+ async exportBlocks({
66
+ ids: initialBlockIds,
67
+ where
68
+ }) {
69
+ await (0, _checkBasePermissions.default)(context, PERMISSION_NAME, {
70
+ rwd: "w"
71
+ });
72
+ let blockIds = initialBlockIds || [];
73
+ // If no ids are provided then it means we want to export all blocks
74
+ if (!initialBlockIds || Array.isArray(initialBlockIds) && initialBlockIds.length === 0) {
75
+ blockIds = [];
76
+ const blocks = await context.pageBuilder.listPageBlocks({
77
+ where
78
+ });
79
+ // Save block ids
80
+ blocks.forEach(block => blockIds.push(block.id));
81
+ }
82
+ if (blockIds.length === 0) {
83
+ throw new _error.default("Cannot export blocks - no blocks found for provided inputs.", "EMPTY_EXPORT_NO_BLOCKS_FOUND");
84
+ }
85
+
86
+ // Create the main task for blocks export.
87
+ const task = await context.pageBuilder.importExportTask.createTask({
88
+ status: _types.ImportExportTaskStatus.PENDING
89
+ });
90
+ const exportBlocksDataKey = `${_utils2.EXPORT_BLOCKS_FOLDER_KEY}/${task.id}`;
91
+ // For each block create a sub task and invoke the process handler.
92
+ for (let i = 0; i < blockIds.length; i++) {
93
+ const blockId = blockIds[i];
94
+ // Create sub task.
95
+ await context.pageBuilder.importExportTask.createSubTask(task.id, (0, _utils3.zeroPad)(i + 1, 5), {
96
+ status: _types.ImportExportTaskStatus.PENDING,
97
+ input: {
98
+ blockId,
99
+ exportBlocksDataKey
100
+ }
101
+ });
102
+ }
103
+ // Update main task status.
104
+ await context.pageBuilder.importExportTask.updateTask(task.id, {
105
+ status: _types.ImportExportTaskStatus.PROCESSING,
106
+ stats: (0, _utils.initialStats)(blockIds.length),
107
+ input: {
108
+ exportBlocksDataKey
109
+ }
110
+ });
111
+
112
+ /**
113
+ * Export Blocks
114
+ * ExportBlocks
115
+ * exportBlocks
116
+ */
117
+ // Invoke handler.
118
+ await (0, _client.invokeHandlerClient)({
119
+ context,
120
+ name: EXPORT_BLOCKS_PROCESS_HANDLER,
121
+ payload: {
122
+ taskId: task.id,
123
+ subTaskIndex: 1,
124
+ type: "block",
125
+ identity: context.security.getIdentity()
126
+ },
127
+ description: "Export blocks - process"
128
+ });
129
+ return {
130
+ task
131
+ };
132
+ }
133
+ };
134
+ // Modify context
135
+ context.pageBuilder.blocks = importExportCrud;
136
+ });
137
+ exports.default = _default;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["PERMISSION_NAME","EXPORT_BLOCKS_PROCESS_HANDLER","process","env","EXPORT_PROCESS_HANDLER","IMPORT_BLOCKS_CREATE_HANDLER","IMPORT_CREATE_HANDLER","ContextPlugin","context","importExportCrud","importBlocks","category","categorySlug","zipFileUrl","checkBasePermissions","rwd","pageBuilder","getBlockCategory","NotFoundError","task","importExportTask","createTask","status","ImportExportTaskStatus","PENDING","input","invokeHandlerClient","name","payload","type","identity","security","getIdentity","description","exportBlocks","ids","initialBlockIds","where","blockIds","Array","isArray","length","blocks","listPageBlocks","forEach","block","push","id","WebinyError","exportBlocksDataKey","EXPORT_BLOCKS_FOLDER_KEY","i","blockId","createSubTask","zeroPad","updateTask","PROCESSING","stats","initialStats","taskId","subTaskIndex"],"sources":["blocks.crud.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { NotFoundError } from \"@webiny/handler-graphql\";\nimport { ContextPlugin } from \"@webiny/api\";\nimport checkBasePermissions from \"@webiny/api-page-builder/graphql/crud/utils/checkBasePermissions\";\nimport { ImportExportTaskStatus, BlocksImportExportCrud, PbImportExportContext } from \"~/types\";\nimport { invokeHandlerClient } from \"~/client\";\nimport { Payload as CreateHandlerPayload } from \"~/import/create\";\nimport { initialStats } from \"~/import/utils\";\nimport { Payload as ExportBlocksProcessHandlerPayload } from \"~/export/process\";\nimport { EXPORT_BLOCKS_FOLDER_KEY } from \"~/export/utils\";\nimport { zeroPad } from \"@webiny/utils\";\n\nconst PERMISSION_NAME = \"pb.block\";\nconst EXPORT_BLOCKS_PROCESS_HANDLER = process.env.EXPORT_PROCESS_HANDLER as string;\nconst IMPORT_BLOCKS_CREATE_HANDLER = process.env.IMPORT_CREATE_HANDLER as string;\n\nexport default new ContextPlugin<PbImportExportContext>(context => {\n const importExportCrud: BlocksImportExportCrud = {\n async importBlocks({ category: categorySlug, zipFileUrl }) {\n await checkBasePermissions(context, PERMISSION_NAME, {\n rwd: \"w\"\n });\n\n // Bail out early if category not found\n const category = await context.pageBuilder.getBlockCategory(categorySlug);\n if (!category) {\n throw new NotFoundError(`Category with slug \"${categorySlug}\" not found.`);\n }\n\n // Create a task for import block\n const task = await context.pageBuilder.importExportTask.createTask({\n status: ImportExportTaskStatus.PENDING,\n input: {\n category: categorySlug,\n zipFileUrl\n }\n });\n /**\n * Import Blocks\n * ImportBlocks\n * importBlocks\n */\n await invokeHandlerClient<CreateHandlerPayload>({\n context,\n name: IMPORT_BLOCKS_CREATE_HANDLER,\n payload: {\n category: categorySlug,\n zipFileUrl,\n task,\n type: \"block\",\n identity: context.security.getIdentity()\n },\n description: \"Import Blocks - create\"\n });\n\n return {\n task\n };\n },\n\n async exportBlocks({ ids: initialBlockIds, where }) {\n await checkBasePermissions(context, PERMISSION_NAME, {\n rwd: \"w\"\n });\n let blockIds: string[] = initialBlockIds || [];\n // If no ids are provided then it means we want to export all blocks\n if (\n !initialBlockIds ||\n (Array.isArray(initialBlockIds) && initialBlockIds.length === 0)\n ) {\n blockIds = [];\n const blocks = await context.pageBuilder.listPageBlocks({ where });\n // Save block ids\n blocks.forEach(block => blockIds.push(block.id));\n }\n\n if (blockIds.length === 0) {\n throw new WebinyError(\n \"Cannot export blocks - no blocks found for provided inputs.\",\n \"EMPTY_EXPORT_NO_BLOCKS_FOUND\"\n );\n }\n\n // Create the main task for blocks export.\n const task = await context.pageBuilder.importExportTask.createTask({\n status: ImportExportTaskStatus.PENDING\n });\n const exportBlocksDataKey = `${EXPORT_BLOCKS_FOLDER_KEY}/${task.id}`;\n // For each block create a sub task and invoke the process handler.\n for (let i = 0; i < blockIds.length; i++) {\n const blockId = blockIds[i];\n // Create sub task.\n await context.pageBuilder.importExportTask.createSubTask(\n task.id,\n zeroPad(i + 1, 5),\n {\n status: ImportExportTaskStatus.PENDING,\n input: {\n blockId,\n exportBlocksDataKey\n }\n }\n );\n }\n // Update main task status.\n await context.pageBuilder.importExportTask.updateTask(task.id, {\n status: ImportExportTaskStatus.PROCESSING,\n stats: initialStats(blockIds.length),\n input: {\n exportBlocksDataKey\n }\n });\n\n /**\n * Export Blocks\n * ExportBlocks\n * exportBlocks\n */\n // Invoke handler.\n await invokeHandlerClient<ExportBlocksProcessHandlerPayload>({\n context,\n name: EXPORT_BLOCKS_PROCESS_HANDLER,\n payload: {\n taskId: task.id,\n subTaskIndex: 1,\n type: \"block\",\n identity: context.security.getIdentity()\n },\n description: \"Export blocks - process\"\n });\n\n return { task };\n }\n };\n // Modify context\n context.pageBuilder.blocks = importExportCrud;\n});\n"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AAEA,MAAMA,eAAe,GAAG,UAAU;AAClC,MAAMC,6BAA6B,GAAGC,OAAO,CAACC,GAAG,CAACC,sBAAgC;AAClF,MAAMC,4BAA4B,GAAGH,OAAO,CAACC,GAAG,CAACG,qBAA+B;AAAC,eAElE,IAAIC,kBAAa,CAAwBC,OAAO,IAAI;EAC/D,MAAMC,gBAAwC,GAAG;IAC7C,MAAMC,YAAY,CAAC;MAAEC,QAAQ,EAAEC,YAAY;MAAEC;IAAW,CAAC,EAAE;MACvD,MAAM,IAAAC,6BAAoB,EAACN,OAAO,EAAER,eAAe,EAAE;QACjDe,GAAG,EAAE;MACT,CAAC,CAAC;;MAEF;MACA,MAAMJ,QAAQ,GAAG,MAAMH,OAAO,CAACQ,WAAW,CAACC,gBAAgB,CAACL,YAAY,CAAC;MACzE,IAAI,CAACD,QAAQ,EAAE;QACX,MAAM,IAAIO,6BAAa,CAAE,uBAAsBN,YAAa,cAAa,CAAC;MAC9E;;MAEA;MACA,MAAMO,IAAI,GAAG,MAAMX,OAAO,CAACQ,WAAW,CAACI,gBAAgB,CAACC,UAAU,CAAC;QAC/DC,MAAM,EAAEC,6BAAs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AAgB;AACjD,CAAC,CAAC;AAAA"}
@@ -0,0 +1,5 @@
1
+ import { ContextPlugin } from "@webiny/api";
2
+ import { ImportExportPluginsParams } from "../../types";
3
+ import { PbImportExportContext } from "../types";
4
+ declare const _default: ({ storageOperations }: ImportExportPluginsParams) => ContextPlugin<PbImportExportContext>;
5
+ export default _default;