@webiny/api-page-builder-import-export 0.0.0-unstable.5e7233243f → 0.0.0-unstable.60a863e033

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 +6 -16
  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 +4 -25
  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 +39 -52
  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} +73 -114
  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 +40 -54
  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 +5 -8
  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} +19 -24
  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 +5 -16
  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 +5 -7
  66. package/graphql/graphql.js.map +1 -1
  67. package/graphql/index.d.ts +2 -2
  68. package/graphql/index.js +1 -6
  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 +194 -157
  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 +28 -28
  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 @@
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;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,0CAA0C,CAAC;EAC/C,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;EACnD,IAAI;IACA;AACR;AACA;AACA;IACQG,OAAO,GAAG,MAAMK,WAAW,CAACO,gBAAgB,CAACC,UAAU,CAACN,MAAM,EAAE,IAAAO,eAAO,EAACN,YAAY,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IAEA,OAAO;MACHC,IAAI,EAAE,IAAI;MACVC,KAAK,EAAE;QACHiB,OAAO,EAAEG,CAAC,CAACH;MACf;IACJ,CAAC;EACL,CAAC,SAAS;IACN;IACA,IAAIjC,aAAa,EAAE;MACfH,GAAG,CAAE,gCAA+BS,MAAO,EAAC,CAAC;MAC7C;MACA,MAAM,IAAAkC,2BAAmB,EAAiB;QACtC5C,OAAO;QACP0C,IAAI,EAAE5C,aAAa,CAAC+C,QAAQ,CAACC,OAAO;QACpC/C,OAAO,EAAE;UACLW,MAAM;UACNE,IAAI;UACJC,QAAQ,EAAEb,OAAO,CAAC+C,QAAQ,CAACC,WAAW;QAC1C,CAAC;QACDC,WAAW,EAAE;MACjB,CAAC,CAAC;IACN,CAAC,MAAM;MACH/C,OAAO,CAACD,GAAG,CAAE,8BAA6BU,YAAY,GAAG,CAAE,GAAE,CAAC;MAC9D;MACA,MAAM,IAAAiC,2BAAmB,EAAU;QAC/B5C,OAAO;QACP0C,IAAI,EAAE5C,aAAa,CAAC+C,QAAQ,CAACK,OAAO;QACpCnD,OAAO,EAAE;UACLW,MAAM;UACNC,YAAY,EAAEA,YAAY,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,24 +1,15 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.s3Stream = void 0;
9
-
8
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
10
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
10
  var _stream = require("stream");
13
-
14
11
  var _s = _interopRequireDefault(require("aws-sdk/clients/s3"));
15
-
16
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
17
-
18
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
19
-
20
12
  const ARCHIVE_CONTENT_TYPE = "application/zip";
21
-
22
13
  class S3Stream {
23
14
  constructor() {
24
15
  (0, _defineProperty2.default)(this, "s3", void 0);
@@ -28,22 +19,19 @@ class S3Stream {
28
19
  });
29
20
  this.bucket = process.env.S3_BUCKET;
30
21
  }
31
-
32
22
  getPresignedUrl(key) {
33
23
  return this.s3.getSignedUrl("getObject", {
34
24
  Bucket: this.bucket,
35
25
  Key: key,
36
26
  Expires: 604800 // 1 week
37
-
38
27
  });
39
28
  }
29
+
40
30
  /**
41
31
  * We're checking if the file is accessible on S3 by getting object meta data.
42
32
  * It help us to filter files that we need to download as part of export data.
43
33
  * @param Key {string}
44
34
  */
45
-
46
-
47
35
  async isFileAccessible(Key) {
48
36
  try {
49
37
  await this.getObjectHead(Key);
@@ -54,21 +42,18 @@ class S3Stream {
54
42
  return false;
55
43
  }
56
44
  }
57
-
58
45
  getObjectHead(Key) {
59
46
  return this.s3.headObject({
60
47
  Bucket: this.bucket,
61
48
  Key
62
49
  }).promise();
63
50
  }
64
-
65
51
  readStream(Key) {
66
52
  return this.s3.getObject({
67
53
  Bucket: this.bucket,
68
54
  Key
69
55
  }).createReadStream();
70
56
  }
71
-
72
57
  writeStream(Key, contentType = ARCHIVE_CONTENT_TYPE) {
73
58
  const streamPassThrough = new _stream.Stream.PassThrough();
74
59
  const params = {
@@ -80,36 +65,30 @@ class S3Stream {
80
65
  };
81
66
  return {
82
67
  streamPassThrough: streamPassThrough,
83
-
84
68
  /**
85
69
  * We're not using the `FileManager` storage plugin here because it currently doesn't support streams.
86
70
  */
87
71
  streamPassThroughUploadPromise: this.s3.upload(params).promise()
88
72
  };
89
73
  }
90
-
91
74
  upload(params) {
92
- return this.s3.upload(_objectSpread({
75
+ return this.s3.upload((0, _objectSpread2.default)({
93
76
  ACL: "private",
94
77
  Bucket: this.bucket
95
78
  }, params)).promise();
96
79
  }
97
-
98
80
  listObject(prefix) {
99
81
  return this.s3.listObjects({
100
82
  Bucket: this.bucket,
101
83
  Prefix: prefix
102
84
  }).promise();
103
85
  }
104
-
105
86
  deleteObject(key) {
106
87
  return this.s3.deleteObject({
107
88
  Key: key,
108
89
  Bucket: this.bucket
109
90
  }).promise();
110
91
  }
111
-
112
92
  }
113
-
114
93
  const s3Stream = new S3Stream();
115
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}\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T;IACE,MAAM8B,iBAAiB,GAAG,IAAIC,cAAA,CAAOC,WAAX,EAA1B;IAEA,MAAMC,MAA2B,GAAG;MAChCC,GAAG,EAAE,SAD2B;MAEhCC,IAAI,EAAEL,iBAF0B;MAGhChB,MAAM,EAAE,KAAKL,MAHmB;MAIhC2B,WAAW,EAAEP,WAJmB;MAKhCd;IALgC,CAApC;IAQA,OAAO;MACHe,iBAAiB,EAAEA,iBADhB;;MAEH;AACZ;AACA;MACYO,8BAA8B,EAAE,KAAKlC,EAAL,CAAQmC,MAAR,CAAeL,MAAf,EAAuBT,OAAvB;IAL7B,CAAP;EAOH;;EAEDc,MAAM,CAACL,MAAD,EAIiC;IACnC,OAAO,KAAK9B,EAAL,CACFmC,MADE;MAECJ,GAAG,EAAE,SAFN;MAGCpB,MAAM,EAAE,KAAKL;IAHd,GAIIwB,MAJJ,GAMFT,OANE,EAAP;EAOH;;EAEDe,UAAU,CAACC,MAAD,EAAgD;IACtD,OAAO,KAAKrC,EAAL,CACFsC,WADE,CACU;MACT3B,MAAM,EAAE,KAAKL,MADJ;MAETiC,MAAM,EAAEF;IAFC,CADV,EAKFhB,OALE,EAAP;EAMH;;EAEDmB,YAAY,CAAC/B,GAAD,EAA8C;IACtD,OAAO,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, 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,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;IACf,CAAC,CAAC,OAAOI,KAAK,EAAE;MACZC,OAAO,CAACC,IAAI,CAAE,4CAA2CN,GAAI,GAAE,CAAC;MAChEK,OAAO,CAACE,GAAG,CAACH,KAAK,CAAC;MAClB,OAAO,KAAK;IAChB;EACJ;EAEAD,aAAa,CAACH,GAAW,EAAgC;IACrD,OAAO,IAAI,CAACZ,EAAE,CAACoB,UAAU,CAAC;MAAET,MAAM,EAAE,IAAI,CAACL,MAAM;MAAEM;IAAI,CAAC,CAAC,CAACS,OAAO,EAAE;EACrE;EAEAC,UAAU,CAACV,GAAW,EAAY;IAC9B,OAAO,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k/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,KAAK;MACjBC,IAAI,EAAEvB,IAAI,CAACuB,IAAI;MACfC,OAAO,EAAExB,IAAI,CAACwB,OAAO;MACrBC,MAAM,EAAEzB,IAAI,CAACyB,MAAM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AAC,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,31 +1,20 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
3
+ var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
8
  exports.default = exports.ZipOfZip = void 0;
9
-
10
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
10
  var _archiver = _interopRequireDefault(require("archiver"));
13
-
14
11
  var _stream = require("stream");
15
-
16
12
  var path = _interopRequireWildcard(require("path"));
17
-
18
13
  var _kebabCase = _interopRequireDefault(require("lodash/kebabCase"));
19
-
20
14
  var _uniqid = _interopRequireDefault(require("uniqid"));
21
-
22
15
  var _s3Stream = require("./s3Stream");
23
-
24
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
25
-
26
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
27
-
28
16
  // TODO: Move "archive" in layer
17
+
29
18
  class Zipper {
30
19
  constructor(config) {
31
20
  (0, _defineProperty2.default)(this, "archiveFormat", "zip");
@@ -33,12 +22,11 @@ class Zipper {
33
22
  (0, _defineProperty2.default)(this, "archiveFileName", void 0);
34
23
  (0, _defineProperty2.default)(this, "config", void 0);
35
24
  this.config = config;
36
- 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`);
37
26
  }
38
-
39
27
  s3DownloadStreams() {
40
28
  const exportInfo = this.config.exportInfo;
41
- const prefix = (0, _uniqid.default)("", `-${(0, _kebabCase.default)(exportInfo.pageTitle)}`);
29
+ const prefix = (0, _uniqid.default)("", `-${(0, _kebabCase.default)(exportInfo.name)}`);
42
30
  const files = exportInfo.files.map(({
43
31
  key
44
32
  }) => {
@@ -48,53 +36,52 @@ class Zipper {
48
36
  };
49
37
  });
50
38
  return [...files, {
51
- stream: _stream.Readable.from(exportInfo.pageDataBuffer),
52
- filename: `${prefix}\\${exportInfo.pageTitle}.json`
39
+ stream: _stream.Readable.from(exportInfo.dataBuffer),
40
+ filename: `${prefix}\\${exportInfo.name}.json`
53
41
  }];
54
42
  }
55
-
56
43
  process() {
57
44
  const {
58
45
  streamPassThrough,
59
46
  streamPassThroughUploadPromise
60
- } = _s3Stream.s3Stream.writeStream(this.archiveFileName); // 1. Read all files from S3 using stream.
61
-
62
-
63
- const s3FilesStreams = this.s3DownloadStreams(); // 2. Prepare zip from the file stream.
64
-
65
- const archive = _archiver.default.create(this.archiveFormat); // Handle archive events.
47
+ } = _s3Stream.s3Stream.writeStream(this.archiveFileName);
66
48
 
49
+ // 1. Read all files from S3 using stream.
50
+ const s3FilesStreams = this.s3DownloadStreams();
67
51
 
52
+ // 2. Prepare zip from the file stream.
53
+ const archive = _archiver.default.create(this.archiveFormat);
54
+ // Handle archive events.
68
55
  archive.on("error", error => {
69
56
  throw new Error(`${error.name} ${error.code} ${error.message} ${error.path} ${error.stack}`);
70
- }); // Append all file streams to archive.
57
+ });
71
58
 
59
+ // Append all file streams to archive.
72
60
  s3FilesStreams.forEach(streamDetails => archive.append(streamDetails.stream, {
73
61
  name: streamDetails.filename
74
- })); // Pipe archive output to streamPassThrough (Transform Stream) which will be uploaded to S3.
62
+ }));
75
63
 
76
- 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)
77
67
  // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
68
+ archive.finalize();
78
69
 
79
- archive.finalize(); // 3. Return upload stream promise.
80
-
70
+ // 3. Return upload stream promise.
81
71
  return streamPassThroughUploadPromise;
82
72
  }
83
-
84
73
  }
85
-
86
74
  exports.default = Zipper;
87
- const PAGE_EXPORT_BASENAME = `WEBINY_PAGE_EXPORT.zip`;
88
-
89
75
  class ZipOfZip {
90
- constructor(keys) {
76
+ constructor(keys, filename) {
91
77
  (0, _defineProperty2.default)(this, "archiveFormat", "zip");
92
78
  (0, _defineProperty2.default)(this, "archiveFileName", void 0);
93
79
  (0, _defineProperty2.default)(this, "keys", void 0);
80
+ (0, _defineProperty2.default)(this, "filename", void 0);
94
81
  this.keys = keys;
95
- this.archiveFileName = (0, _uniqid.default)("", `-${PAGE_EXPORT_BASENAME}`);
82
+ this.filename = filename;
83
+ this.archiveFileName = (0, _uniqid.default)("", `-${filename}`);
96
84
  }
97
-
98
85
  getFileStreams() {
99
86
  return this.keys.map(key => {
100
87
  return {
@@ -103,35 +90,35 @@ class ZipOfZip {
103
90
  };
104
91
  });
105
92
  }
106
-
107
93
  process() {
108
94
  const {
109
95
  streamPassThrough,
110
96
  streamPassThroughUploadPromise
111
- } = _s3Stream.s3Stream.writeStream(this.archiveFileName); // 1. Read all files from S3 using stream.
112
-
113
-
114
- const fileStreamDetails = this.getFileStreams(); // 2. Prepare zip from the file stream.
115
-
116
- const archive = _archiver.default.create(this.archiveFormat); // Handle archive events.
97
+ } = _s3Stream.s3Stream.writeStream(this.archiveFileName);
117
98
 
99
+ // 1. Read all files from S3 using stream.
100
+ const fileStreamDetails = this.getFileStreams();
118
101
 
102
+ // 2. Prepare zip from the file stream.
103
+ const archive = _archiver.default.create(this.archiveFormat);
104
+ // Handle archive events.
119
105
  archive.on("error", error => {
120
106
  throw new Error(`${error.name} ${error.code} ${error.message} ${error.path} ${error.stack}`);
121
- }); // Append all file streams to archive.
107
+ });
122
108
 
109
+ // Append all file streams to archive.
123
110
  fileStreamDetails.forEach(streamDetails => archive.append(streamDetails.stream, {
124
111
  name: streamDetails.filename
125
- })); // Pipe archive output to streamPassThrough (Transform Stream) which will be uploaded to S3.
112
+ }));
126
113
 
127
- 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)
128
117
  // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
118
+ archive.finalize();
129
119
 
130
- archive.finalize(); // 3. Return upload stream promise.
131
-
120
+ // 3. Return upload stream promise.
132
121
  return streamPassThroughUploadPromise;
133
122
  }
134
-
135
123
  }
136
-
137
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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AC;;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;