@webiny/api-file-manager 0.0.0-mt-1

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 (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +17 -0
  3. package/handlers/download/index.d.ts +3 -0
  4. package/handlers/download/index.js +125 -0
  5. package/handlers/manage/index.d.ts +3 -0
  6. package/handlers/manage/index.js +70 -0
  7. package/handlers/transform/index.d.ts +3 -0
  8. package/handlers/transform/index.js +96 -0
  9. package/handlers/transform/loaders/imageLoader.d.ts +13 -0
  10. package/handlers/transform/loaders/imageLoader.js +106 -0
  11. package/handlers/transform/loaders/index.d.ts +13 -0
  12. package/handlers/transform/loaders/index.js +13 -0
  13. package/handlers/transform/loaders/sanitizeImageTransformations.d.ts +7 -0
  14. package/handlers/transform/loaders/sanitizeImageTransformations.js +51 -0
  15. package/handlers/transform/managers/imageManager.d.ts +9 -0
  16. package/handlers/transform/managers/imageManager.js +55 -0
  17. package/handlers/transform/managers/index.d.ts +9 -0
  18. package/handlers/transform/managers/index.js +13 -0
  19. package/handlers/transform/optimizeImage.d.ts +2 -0
  20. package/handlers/transform/optimizeImage.js +45 -0
  21. package/handlers/transform/transformImage.d.ts +9 -0
  22. package/handlers/transform/transformImage.js +30 -0
  23. package/handlers/transform/utils.d.ts +12 -0
  24. package/handlers/transform/utils.js +46 -0
  25. package/handlers/utils/createHandler.d.ts +24 -0
  26. package/handlers/utils/createHandler.js +68 -0
  27. package/handlers/utils/getEnvironment.d.ts +5 -0
  28. package/handlers/utils/getEnvironment.js +13 -0
  29. package/handlers/utils/getObjectParams.d.ts +10 -0
  30. package/handlers/utils/getObjectParams.js +27 -0
  31. package/handlers/utils/index.d.ts +3 -0
  32. package/handlers/utils/index.js +31 -0
  33. package/package.json +75 -0
  34. package/plugins/crud/files/validation.d.ts +3 -0
  35. package/plugins/crud/files/validation.js +41 -0
  36. package/plugins/crud/files.crud.d.ts +4 -0
  37. package/plugins/crud/files.crud.js +427 -0
  38. package/plugins/crud/settings.crud.d.ts +5 -0
  39. package/plugins/crud/settings.crud.js +115 -0
  40. package/plugins/crud/system.crud.d.ts +4 -0
  41. package/plugins/crud/system.crud.js +145 -0
  42. package/plugins/crud/utils/checkBasePermissions.d.ts +5 -0
  43. package/plugins/crud/utils/checkBasePermissions.js +33 -0
  44. package/plugins/crud/utils/createFileModel.d.ts +2 -0
  45. package/plugins/crud/utils/createFileModel.js +64 -0
  46. package/plugins/crud/utils/lifecycleEvents.d.ts +6 -0
  47. package/plugins/crud/utils/lifecycleEvents.js +33 -0
  48. package/plugins/definitions/FilePhysicalStoragePlugin.d.ts +22 -0
  49. package/plugins/definitions/FilePhysicalStoragePlugin.js +42 -0
  50. package/plugins/definitions/FilePlugin.d.ts +133 -0
  51. package/plugins/definitions/FilePlugin.js +64 -0
  52. package/plugins/definitions/FileStorageTransformPlugin.d.ts +34 -0
  53. package/plugins/definitions/FileStorageTransformPlugin.js +51 -0
  54. package/plugins/definitions/FilesStorageOperationsProviderPlugin.d.ts +9 -0
  55. package/plugins/definitions/FilesStorageOperationsProviderPlugin.js +17 -0
  56. package/plugins/definitions/InstallationPlugin.d.ts +19 -0
  57. package/plugins/definitions/InstallationPlugin.js +40 -0
  58. package/plugins/definitions/SettingsStorageOperationsProviderPlugin.d.ts +9 -0
  59. package/plugins/definitions/SettingsStorageOperationsProviderPlugin.js +17 -0
  60. package/plugins/definitions/SystemStorageOperationsProviderPlugin.d.ts +9 -0
  61. package/plugins/definitions/SystemStorageOperationsProviderPlugin.js +17 -0
  62. package/plugins/graphql.d.ts +4 -0
  63. package/plugins/graphql.js +274 -0
  64. package/plugins/index.d.ts +2 -0
  65. package/plugins/index.js +24 -0
  66. package/plugins/storage/FileStorage.d.ts +31 -0
  67. package/plugins/storage/FileStorage.js +95 -0
  68. package/plugins/storage/index.d.ts +4 -0
  69. package/plugins/storage/index.js +22 -0
  70. package/types.d.ts +357 -0
  71. package/types.js +5 -0
  72. package/utils.d.ts +4 -0
  73. package/utils.js +16 -0
@@ -0,0 +1,427 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _mdbid = _interopRequireDefault(require("mdbid"));
13
+
14
+ var _handlerGraphql = require("@webiny/handler-graphql");
15
+
16
+ var _apiSecurity = require("@webiny/api-security");
17
+
18
+ var _error = _interopRequireDefault(require("@webiny/error"));
19
+
20
+ var _checkBasePermissions = _interopRequireDefault(require("./utils/checkBasePermissions"));
21
+
22
+ var _ContextPlugin = require("@webiny/handler/plugins/ContextPlugin");
23
+
24
+ var _FilePlugin = require("../definitions/FilePlugin");
25
+
26
+ var _FilesStorageOperationsProviderPlugin = require("../definitions/FilesStorageOperationsProviderPlugin");
27
+
28
+ var _lifecycleEvents = require("./utils/lifecycleEvents");
29
+
30
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
31
+
32
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
33
+
34
+ const BATCH_CREATE_MAX_FILES = 20;
35
+ /**
36
+ * If permission is limited to "own" files only, check that current identity owns the file.
37
+ */
38
+
39
+ const checkOwnership = (file, permission, context) => {
40
+ if ((permission === null || permission === void 0 ? void 0 : permission.own) === true) {
41
+ const identity = context.security.getIdentity();
42
+
43
+ if (file.createdBy.id !== identity.id) {
44
+ throw new _apiSecurity.NotAuthorizedError();
45
+ }
46
+ }
47
+ };
48
+
49
+ const getLocaleCode = context => {
50
+ if (!context.i18nContent) {
51
+ throw new _error.default("Missing i18nContent on the FileManagerContext.", "MISSING_I18N_CONTENT");
52
+ } else if (!context.i18nContent.locale) {
53
+ throw new _error.default("Missing i18nContent.locale on the FileManagerContext.", "MISSING_I18N_CONTENT_LOCALE");
54
+ } else if (!context.i18nContent.locale.code) {
55
+ throw new _error.default("Missing i18nContent.locale.code on the FileManagerContext.", "MISSING_I18N_CONTENT_LOCALE_CODE");
56
+ }
57
+
58
+ return context.i18nContent.locale.code;
59
+ };
60
+
61
+ const filesContextCrudPlugin = new _ContextPlugin.ContextPlugin(async context => {
62
+ const pluginType = _FilesStorageOperationsProviderPlugin.FilesStorageOperationsProviderPlugin.type;
63
+ const providerPlugin = context.plugins.byType(pluginType).find(() => true);
64
+
65
+ if (!providerPlugin) {
66
+ throw new _error.default(`Missing "${pluginType}" plugin.`, "PLUGIN_NOT_FOUND", {
67
+ type: pluginType
68
+ });
69
+ }
70
+
71
+ const storageOperations = await providerPlugin.provide({
72
+ context
73
+ });
74
+
75
+ if (!context.fileManager) {
76
+ context.fileManager = {};
77
+ }
78
+
79
+ const filePlugins = context.plugins.byType(_FilePlugin.FilePlugin.type);
80
+ context.fileManager.files = {
81
+ async getFile(id) {
82
+ const permission = await (0, _checkBasePermissions.default)(context, {
83
+ rwd: "r"
84
+ });
85
+ const file = await storageOperations.get({
86
+ where: {
87
+ id,
88
+ tenant: context.tenancy.getCurrentTenant().id,
89
+ locale: getLocaleCode(context)
90
+ }
91
+ });
92
+
93
+ if (!file) {
94
+ throw new _handlerGraphql.NotFoundError(`File with id "${id}" does not exists.`);
95
+ }
96
+
97
+ checkOwnership(file, permission, context);
98
+ return file;
99
+ },
100
+
101
+ async createFile(input) {
102
+ await (0, _checkBasePermissions.default)(context, {
103
+ rwd: "w"
104
+ });
105
+ const identity = context.security.getIdentity();
106
+ const tenant = context.tenancy.getCurrentTenant();
107
+ const id = (0, _mdbid.default)();
108
+
109
+ const file = _objectSpread(_objectSpread({}, input), {}, {
110
+ id,
111
+ meta: _objectSpread({
112
+ private: false
113
+ }, input.meta || {}),
114
+ tenant: tenant.id,
115
+ createdOn: new Date().toISOString(),
116
+ createdBy: {
117
+ id: identity.id,
118
+ displayName: identity.displayName,
119
+ type: identity.type
120
+ },
121
+ locale: getLocaleCode(context),
122
+ webinyVersion: context.WEBINY_VERSION
123
+ });
124
+
125
+ try {
126
+ await (0, _lifecycleEvents.runLifecycleEvent)("beforeCreate", {
127
+ context,
128
+ plugins: filePlugins,
129
+ data: file
130
+ });
131
+ const result = await storageOperations.create({
132
+ file
133
+ });
134
+ await (0, _lifecycleEvents.runLifecycleEvent)("afterCreate", {
135
+ context,
136
+ plugins: filePlugins,
137
+ data: file,
138
+ file: result
139
+ });
140
+ return result;
141
+ } catch (ex) {
142
+ throw new _error.default(ex.message || "Could not create a file.", ex.code || "CREATE_FILE_ERROR", _objectSpread(_objectSpread({}, ex.data || {}), {}, {
143
+ file
144
+ }));
145
+ }
146
+ },
147
+
148
+ async updateFile(id, input) {
149
+ const permission = await (0, _checkBasePermissions.default)(context, {
150
+ rwd: "w"
151
+ });
152
+ const original = await storageOperations.get({
153
+ where: {
154
+ id,
155
+ tenant: context.tenancy.getCurrentTenant().id,
156
+ locale: getLocaleCode(context)
157
+ }
158
+ });
159
+
160
+ if (!original) {
161
+ throw new _handlerGraphql.NotFoundError(`File with id "${id}" does not exists.`);
162
+ }
163
+
164
+ checkOwnership(original, permission, context);
165
+
166
+ const file = _objectSpread(_objectSpread(_objectSpread({}, original), input), {}, {
167
+ id: original.id,
168
+ webinyVersion: context.WEBINY_VERSION
169
+ });
170
+
171
+ try {
172
+ await (0, _lifecycleEvents.runLifecycleEvent)("beforeUpdate", {
173
+ context,
174
+ plugins: filePlugins,
175
+ original,
176
+ data: file
177
+ });
178
+ const result = await storageOperations.update({
179
+ original,
180
+ file
181
+ });
182
+ await (0, _lifecycleEvents.runLifecycleEvent)("afterUpdate", {
183
+ context,
184
+ plugins: filePlugins,
185
+ original,
186
+ data: file,
187
+ file: result
188
+ });
189
+ return result;
190
+ } catch (ex) {
191
+ throw new _error.default(ex.message || "Could not update a file.", ex.code || "UPDATE_FILE_ERROR", _objectSpread(_objectSpread({}, ex.data || {}), {}, {
192
+ original,
193
+ file
194
+ }));
195
+ }
196
+ },
197
+
198
+ async deleteFile(id) {
199
+ const permission = await (0, _checkBasePermissions.default)(context, {
200
+ rwd: "d"
201
+ });
202
+ const file = await storageOperations.get({
203
+ where: {
204
+ id,
205
+ tenant: context.tenancy.getCurrentTenant().id,
206
+ locale: getLocaleCode(context)
207
+ }
208
+ });
209
+
210
+ if (!file) {
211
+ throw new _handlerGraphql.NotFoundError(`File with id "${id}" does not exists.`);
212
+ }
213
+
214
+ checkOwnership(file, permission, context);
215
+
216
+ try {
217
+ await (0, _lifecycleEvents.runLifecycleEvent)("beforeDelete", {
218
+ context,
219
+ plugins: filePlugins,
220
+ file
221
+ });
222
+ await storageOperations.delete({
223
+ file
224
+ });
225
+ await (0, _lifecycleEvents.runLifecycleEvent)("afterDelete", {
226
+ context,
227
+ plugins: filePlugins,
228
+ file
229
+ });
230
+ } catch (ex) {
231
+ throw new _error.default(ex.message || "Could not delete a file.", ex.code || "DELETE_FILE_ERROR", _objectSpread(_objectSpread({}, ex.data || {}), {}, {
232
+ id,
233
+ file
234
+ }));
235
+ }
236
+
237
+ return true;
238
+ },
239
+
240
+ async createFilesInBatch(inputs) {
241
+ if (!Array.isArray(inputs)) {
242
+ throw new _error.default(`"data" must be an array.`, "CREATE_FILES_NON_ARRAY");
243
+ }
244
+
245
+ if (inputs.length === 0) {
246
+ throw new _error.default(`"data" argument must contain at least one file.`, "CREATE_FILES_MIN_FILES");
247
+ }
248
+
249
+ if (inputs.length > BATCH_CREATE_MAX_FILES) {
250
+ throw new _error.default(`"data" argument must not contain more than ${BATCH_CREATE_MAX_FILES} files.`, "CREATE_FILES_MAX_FILES");
251
+ }
252
+
253
+ await (0, _checkBasePermissions.default)(context, {
254
+ rwd: "w"
255
+ });
256
+ const identity = context.security.getIdentity();
257
+ const tenant = context.tenancy.getCurrentTenant();
258
+ const createdBy = {
259
+ id: identity.id,
260
+ displayName: identity.displayName,
261
+ type: identity.type
262
+ };
263
+ const files = inputs.map(input => {
264
+ return _objectSpread(_objectSpread({}, input), {}, {
265
+ meta: _objectSpread({
266
+ private: false
267
+ }, input.meta || {}),
268
+ id: (0, _mdbid.default)(),
269
+ tenant: tenant.id,
270
+ createdOn: new Date().toISOString(),
271
+ createdBy,
272
+ locale: getLocaleCode(context),
273
+ webinyVersion: context.WEBINY_VERSION
274
+ });
275
+ });
276
+
277
+ try {
278
+ await (0, _lifecycleEvents.runLifecycleEvent)("beforeBatchCreate", {
279
+ context,
280
+ plugins: filePlugins,
281
+ data: files
282
+ });
283
+ const results = await storageOperations.createBatch({
284
+ files
285
+ });
286
+ await (0, _lifecycleEvents.runLifecycleEvent)("afterBatchCreate", {
287
+ context,
288
+ plugins: filePlugins,
289
+ data: files,
290
+ files: results
291
+ });
292
+ return results;
293
+ } catch (ex) {
294
+ throw new _error.default(ex.message || "Could not create a batch of files.", ex.code || "CREATE_FILES_ERROR", _objectSpread(_objectSpread({}, ex.data || {}), {}, {
295
+ files
296
+ }));
297
+ }
298
+ },
299
+
300
+ async listFiles(params = {}) {
301
+ const permission = await (0, _checkBasePermissions.default)(context, {
302
+ rwd: "r"
303
+ });
304
+ const {
305
+ limit = 40,
306
+ search = "",
307
+ types = [],
308
+ tags = [],
309
+ ids = [],
310
+ after = null,
311
+ where: initialWhere,
312
+ sort: initialSort
313
+ } = params;
314
+
315
+ const where = _objectSpread(_objectSpread({}, initialWhere), {}, {
316
+ private: false,
317
+ locale: context.i18nContent.getLocale().code,
318
+ tenant: context.tenancy.getCurrentTenant().id
319
+ });
320
+ /**
321
+ * Always override the createdBy received from the user, if any.
322
+ */
323
+
324
+
325
+ if (permission.own === true) {
326
+ const identity = context.security.getIdentity();
327
+ where.createdBy = identity.id;
328
+ }
329
+ /**
330
+ * We need to map the old GraphQL definition to the new one.
331
+ * That GQL definition is marked as deprecated.
332
+ */
333
+
334
+ /**
335
+ * To have standardized where objects across the applications, we transform the types into type_in.
336
+ */
337
+
338
+
339
+ if (Array.isArray(types) && types.length > 0 && !where.type_in) {
340
+ where.type_in = types;
341
+ }
342
+ /**
343
+ * We are assigning search to tag and name search.
344
+ * This should be treated as OR condition in the storage operations.
345
+ */
346
+
347
+
348
+ if (search && !where.search) {
349
+ where.search = search;
350
+ }
351
+ /**
352
+ * Same as on types/type_in.
353
+ */
354
+
355
+
356
+ if (Array.isArray(tags) && tags.length > 0 && !where.tag_in) {
357
+ where.tag_in = tags.map(tag => tag.toLowerCase());
358
+ }
359
+ /**
360
+ * Same as on types/type_in.
361
+ */
362
+
363
+
364
+ if (Array.isArray(ids) && ids.length > 0 && !where.id_in) {
365
+ where.id_in = ids;
366
+ }
367
+
368
+ const sort = Array.isArray(initialSort) && initialSort.length > 0 ? initialSort : ["id_DESC"];
369
+
370
+ try {
371
+ return await storageOperations.list({
372
+ where,
373
+ after,
374
+ limit,
375
+ sort
376
+ });
377
+ } catch (ex) {
378
+ throw new _error.default(ex.message || "Could not list files by given parameters.", ex.code || "FILE_TAG_SEARCH_ERROR", _objectSpread(_objectSpread({}, ex.data || {}), {}, {
379
+ where,
380
+ after,
381
+ limit,
382
+ sort
383
+ }));
384
+ }
385
+ },
386
+
387
+ async listTags({
388
+ after,
389
+ limit
390
+ }) {
391
+ await (0, _checkBasePermissions.default)(context);
392
+ const {
393
+ i18nContent
394
+ } = context;
395
+ const where = {
396
+ tenant: context.tenancy.getCurrentTenant().id,
397
+ locale: i18nContent.locale.code
398
+ };
399
+ const params = {
400
+ where,
401
+ limit: limit || 100000,
402
+ after
403
+ };
404
+
405
+ try {
406
+ /**
407
+ * There is a meta object on the second key.
408
+ * TODO: use when changing GraphQL output of the tags.
409
+ */
410
+ const [tags] = await storageOperations.tags(params);
411
+ /**
412
+ * just to keep it standardized, sort by the tag ASC
413
+ */
414
+
415
+ return tags.sort();
416
+ } catch (ex) {
417
+ throw new _error.default(ex.message || "Could not search for tags.", ex.code || "FILE_TAG_SEARCH_ERROR", _objectSpread(_objectSpread({}, ex.data || {}), {}, {
418
+ params
419
+ }));
420
+ }
421
+ }
422
+
423
+ };
424
+ });
425
+ filesContextCrudPlugin.name = "FileManagerFilesCrud";
426
+ var _default = filesContextCrudPlugin;
427
+ exports.default = _default;
@@ -0,0 +1,5 @@
1
+ import { FileManagerContext } from "../../types";
2
+ import { ContextPlugin } from "@webiny/handler/plugins/ContextPlugin";
3
+ export declare const SETTINGS_KEY = "file-manager";
4
+ declare const settingsCrudContextPlugin: ContextPlugin<FileManagerContext>;
5
+ export default settingsCrudContextPlugin;
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = exports.SETTINGS_KEY = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _fields = require("@commodo/fields");
13
+
14
+ var _validation = require("@webiny/validation");
15
+
16
+ var _SettingsStorageOperationsProviderPlugin = require("../definitions/SettingsStorageOperationsProviderPlugin");
17
+
18
+ var _error = _interopRequireDefault(require("@webiny/error"));
19
+
20
+ var _ContextPlugin = require("@webiny/handler/plugins/ContextPlugin");
21
+
22
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
23
+
24
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
25
+
26
+ const SETTINGS_KEY = "file-manager";
27
+ exports.SETTINGS_KEY = SETTINGS_KEY;
28
+ const CreateDataModel = (0, _fields.withFields)({
29
+ uploadMinFileSize: (0, _fields.number)({
30
+ value: 0,
31
+ validation: _validation.validation.create("gte:0")
32
+ }),
33
+ uploadMaxFileSize: (0, _fields.number)({
34
+ value: 26214401
35
+ }),
36
+ srcPrefix: (0, _fields.onSet)(value => {
37
+ // Make sure srcPrefix always ends with forward slash.
38
+ if (typeof value === "string") {
39
+ return value.endsWith("/") ? value : value + "/";
40
+ }
41
+
42
+ return value;
43
+ })((0, _fields.string)({
44
+ value: "/files/"
45
+ }))
46
+ })();
47
+ const UpdateDataModel = (0, _fields.withFields)({
48
+ uploadMinFileSize: (0, _fields.number)({
49
+ validation: _validation.validation.create("gte:0")
50
+ }),
51
+ uploadMaxFileSize: (0, _fields.number)(),
52
+ srcPrefix: (0, _fields.onSet)(value => {
53
+ // Make sure srcPrefix always ends with forward slash.
54
+ if (typeof value === "string") {
55
+ return value.endsWith("/") ? value : value + "/";
56
+ }
57
+
58
+ return value;
59
+ })((0, _fields.string)())
60
+ })();
61
+ const settingsCrudContextPlugin = new _ContextPlugin.ContextPlugin(async context => {
62
+ const pluginType = _SettingsStorageOperationsProviderPlugin.SettingsStorageOperationsProviderPlugin.type;
63
+ const providerPlugin = context.plugins.byType(pluginType).find(() => true);
64
+
65
+ if (!providerPlugin) {
66
+ throw new _error.default(`Missing "${pluginType}" plugin.`, "PLUGIN_NOT_FOUND", {
67
+ type: pluginType
68
+ });
69
+ }
70
+
71
+ const storageOperations = await providerPlugin.provide({
72
+ context
73
+ });
74
+
75
+ if (!context.fileManager) {
76
+ context.fileManager = {};
77
+ }
78
+
79
+ context.fileManager.settings = {
80
+ async getSettings() {
81
+ return storageOperations.get();
82
+ },
83
+
84
+ async createSettings(data) {
85
+ const settings = new CreateDataModel().populate(data);
86
+ await settings.validate();
87
+ const settingsData = await settings.toJSON();
88
+ return storageOperations.create({
89
+ data: settingsData
90
+ });
91
+ },
92
+
93
+ async updateSettings(data) {
94
+ const updatedValue = new UpdateDataModel().populate(data);
95
+ await updatedValue.validate();
96
+ const existingSettings = await storageOperations.get();
97
+ const updatedSettings = await updatedValue.toJSON({
98
+ onlyDirty: true
99
+ });
100
+ return storageOperations.update({
101
+ original: existingSettings,
102
+ data: _objectSpread(_objectSpread({}, existingSettings), updatedSettings)
103
+ });
104
+ },
105
+
106
+ async deleteSettings() {
107
+ await storageOperations.delete();
108
+ return true;
109
+ }
110
+
111
+ };
112
+ });
113
+ settingsCrudContextPlugin.name = "FileMangerSettingsCrud";
114
+ var _default = settingsCrudContextPlugin;
115
+ exports.default = _default;
@@ -0,0 +1,4 @@
1
+ import { FileManagerContext } from "../../types";
2
+ import { ContextPlugin } from "@webiny/handler/plugins/ContextPlugin";
3
+ declare const systemCrudContextPlugin: ContextPlugin<FileManagerContext>;
4
+ export default systemCrudContextPlugin;
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _apiSecurity = require("@webiny/api-security");
13
+
14
+ var _apiUpgrade = require("@webiny/api-upgrade");
15
+
16
+ var _error = _interopRequireDefault(require("@webiny/error"));
17
+
18
+ var _ContextPlugin = require("@webiny/handler/plugins/ContextPlugin");
19
+
20
+ var _utils = require("../../utils");
21
+
22
+ var _InstallationPlugin = require("../definitions/InstallationPlugin");
23
+
24
+ var _SystemStorageOperationsProviderPlugin = require("../definitions/SystemStorageOperationsProviderPlugin");
25
+
26
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
27
+
28
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
29
+
30
+ const systemCrudContextPlugin = new _ContextPlugin.ContextPlugin(async context => {
31
+ const pluginType = _SystemStorageOperationsProviderPlugin.SystemStorageOperationsProviderPlugin.type;
32
+ const providerPlugin = context.plugins.byType(pluginType).find(() => true);
33
+
34
+ if (!providerPlugin) {
35
+ throw new _error.default(`Missing "${pluginType}" plugin.`, "PLUGIN_NOT_FOUND", {
36
+ type: pluginType
37
+ });
38
+ }
39
+
40
+ const storageOperations = await providerPlugin.provide({
41
+ context
42
+ });
43
+
44
+ if (!context.fileManager) {
45
+ context.fileManager = {};
46
+ }
47
+
48
+ context.fileManager.system = {
49
+ async getVersion() {
50
+ const system = await storageOperations.get();
51
+ return system ? system.version : null;
52
+ },
53
+
54
+ async setVersion(version) {
55
+ const system = await storageOperations.get();
56
+
57
+ if (system) {
58
+ const data = _objectSpread(_objectSpread({}, system), {}, {
59
+ version
60
+ });
61
+
62
+ try {
63
+ await storageOperations.update({
64
+ original: system,
65
+ data
66
+ });
67
+ return;
68
+ } catch (ex) {
69
+ throw new _error.default("Could not update the system data.", "SYSTEM_UPDATE_ERROR", {
70
+ data
71
+ });
72
+ }
73
+ }
74
+
75
+ const data = {
76
+ version
77
+ };
78
+
79
+ try {
80
+ await storageOperations.create({
81
+ data
82
+ });
83
+ return;
84
+ } catch (ex) {
85
+ throw new _error.default("Could not create the system data.", "SYSTEM_CREATE_ERROR", {
86
+ data
87
+ });
88
+ }
89
+ },
90
+
91
+ async install({
92
+ srcPrefix
93
+ }) {
94
+ const {
95
+ fileManager
96
+ } = context;
97
+ const version = await fileManager.system.getVersion();
98
+
99
+ if (version) {
100
+ throw new _error.default("File Manager is already installed.", "FILES_INSTALL_ABORTED");
101
+ }
102
+
103
+ const data = {};
104
+
105
+ if (srcPrefix) {
106
+ data.srcPrefix = srcPrefix;
107
+ }
108
+
109
+ const installationPlugins = context.plugins.byType(_InstallationPlugin.InstallationPlugin.type);
110
+ await (0, _utils.executeCallbacks)(installationPlugins, "beforeInstall", {
111
+ context
112
+ });
113
+ await fileManager.settings.createSettings(data);
114
+ await fileManager.system.setVersion(context.WEBINY_VERSION);
115
+ await (0, _utils.executeCallbacks)(installationPlugins, "afterInstall", {
116
+ context
117
+ });
118
+ return true;
119
+ },
120
+
121
+ async upgrade(version) {
122
+ const identity = context.security.getIdentity();
123
+
124
+ if (!identity) {
125
+ throw new _apiSecurity.NotAuthorizedError();
126
+ }
127
+
128
+ const upgradePlugins = context.plugins.byType("api-upgrade").filter(pl => pl.app === "file-manager");
129
+ const plugin = (0, _apiUpgrade.getApplicablePlugin)({
130
+ deployedVersion: context.WEBINY_VERSION,
131
+ installedAppVersion: await this.getVersion(),
132
+ upgradePlugins,
133
+ upgradeToVersion: version
134
+ });
135
+ await plugin.apply(context); // Store new app version
136
+
137
+ await context.fileManager.system.setVersion(version);
138
+ return true;
139
+ }
140
+
141
+ };
142
+ });
143
+ systemCrudContextPlugin.name = "FileManagerSystemCrud";
144
+ var _default = systemCrudContextPlugin;
145
+ exports.default = _default;
@@ -0,0 +1,5 @@
1
+ import { FileManagerContext, FilePermission } from "../../../types";
2
+ declare const _default: (context: FileManagerContext, check?: {
3
+ rwd?: string;
4
+ }) => Promise<FilePermission>;
5
+ export default _default;