@payloadcms/plugin-cloud-storage 1.0.15 → 1.0.17-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/.editorconfig +10 -10
  2. package/.eslintrc.js +14 -14
  3. package/.gitignore +248 -248
  4. package/.prettierignore +1 -1
  5. package/.prettierrc.js +8 -8
  6. package/.vscode/launch.json +40 -28
  7. package/.vscode/settings.json +9 -9
  8. package/LICENSE.md +22 -22
  9. package/README.md +196 -178
  10. package/azure.d.ts +1 -1
  11. package/azure.js +1 -1
  12. package/dev/.env +11 -15
  13. package/dev/.env.example +21 -21
  14. package/dev/nodemon.json +8 -8
  15. package/dev/package.json +34 -32
  16. package/dev/src/collections/Media.ts +56 -56
  17. package/dev/src/collections/Users.ts +23 -23
  18. package/dev/src/mocks/fsMock.js +1 -1
  19. package/dev/src/mocks/promisifyMock.js +1 -1
  20. package/dev/src/payload.config.ts +111 -111
  21. package/dev/src/server.ts +26 -26
  22. package/dev/tsconfig.json +20 -20
  23. package/dist/adapters/azure/fileStub.d.ts +2 -2
  24. package/dist/adapters/azure/fileStub.js +3 -3
  25. package/dist/adapters/azure/generateURL.d.ts +7 -7
  26. package/dist/adapters/azure/generateURL.js +15 -15
  27. package/dist/adapters/azure/handleDelete.d.ts +9 -9
  28. package/dist/adapters/azure/handleDelete.js +63 -63
  29. package/dist/adapters/azure/handleUpload.d.ts +10 -10
  30. package/dist/adapters/azure/handleUpload.js +80 -80
  31. package/dist/adapters/azure/index.d.ts +8 -8
  32. package/dist/adapters/azure/index.js +42 -42
  33. package/dist/adapters/azure/index.js.map +1 -1
  34. package/dist/adapters/azure/mock.d.ts +13 -13
  35. package/dist/adapters/azure/mock.js +12 -12
  36. package/dist/adapters/azure/staticHandler.d.ts +9 -9
  37. package/dist/adapters/azure/staticHandler.js +81 -81
  38. package/dist/adapters/azure/webpack.d.ts +2 -2
  39. package/dist/adapters/azure/webpack.js +24 -24
  40. package/dist/adapters/azure/webpack.js.map +1 -1
  41. package/dist/adapters/gcs/generateURL.d.ts +8 -8
  42. package/dist/adapters/gcs/generateURL.js +15 -15
  43. package/dist/adapters/gcs/handleDelete.d.ts +8 -8
  44. package/dist/adapters/gcs/handleDelete.js +62 -62
  45. package/dist/adapters/gcs/handleUpload.d.ts +12 -12
  46. package/dist/adapters/gcs/handleUpload.js +72 -72
  47. package/dist/adapters/gcs/index.d.ts +8 -8
  48. package/dist/adapters/gcs/index.js +35 -35
  49. package/dist/adapters/gcs/mock.d.ts +1 -1
  50. package/dist/adapters/gcs/mock.js +4 -4
  51. package/dist/adapters/gcs/staticHandler.d.ts +10 -10
  52. package/dist/adapters/gcs/staticHandler.js +76 -76
  53. package/dist/adapters/gcs/webpack.d.ts +2 -2
  54. package/dist/adapters/gcs/webpack.js +24 -24
  55. package/dist/adapters/gcs/webpack.js.map +1 -1
  56. package/dist/adapters/s3/fileStub.d.ts +2 -2
  57. package/dist/adapters/s3/fileStub.js +3 -3
  58. package/dist/adapters/s3/generateURL.d.ts +8 -8
  59. package/dist/adapters/s3/generateURL.js +15 -15
  60. package/dist/adapters/s3/handleDelete.d.ts +8 -8
  61. package/dist/adapters/s3/handleDelete.js +63 -63
  62. package/dist/adapters/s3/handleUpload.d.ts +12 -12
  63. package/dist/adapters/s3/handleUpload.js +93 -93
  64. package/dist/adapters/s3/index.d.ts +8 -8
  65. package/dist/adapters/s3/index.js +59 -59
  66. package/dist/adapters/s3/mock.d.ts +8 -8
  67. package/dist/adapters/s3/mock.js +9 -9
  68. package/dist/adapters/s3/staticHandler.d.ts +10 -10
  69. package/dist/adapters/s3/staticHandler.js +81 -80
  70. package/dist/adapters/s3/staticHandler.js.map +1 -1
  71. package/dist/adapters/s3/webpack.d.ts +2 -2
  72. package/dist/adapters/s3/webpack.js +24 -24
  73. package/dist/adapters/s3/webpack.js.map +1 -1
  74. package/dist/fields/getFields.d.ts +11 -11
  75. package/dist/fields/getFields.js +118 -118
  76. package/dist/hooks/afterDelete.d.ts +10 -10
  77. package/dist/hooks/afterDelete.js +91 -88
  78. package/dist/hooks/afterDelete.js.map +1 -1
  79. package/dist/hooks/afterRead.d.ts +12 -12
  80. package/dist/hooks/afterRead.js +79 -79
  81. package/dist/hooks/beforeChange.d.ts +10 -10
  82. package/dist/hooks/beforeChange.js +105 -77
  83. package/dist/hooks/beforeChange.js.map +1 -1
  84. package/dist/index.d.ts +1 -1
  85. package/dist/index.js +5 -5
  86. package/dist/plugin.d.ts +3 -3
  87. package/dist/plugin.js +130 -124
  88. package/dist/plugin.js.map +1 -1
  89. package/dist/types.d.ts +68 -62
  90. package/dist/types.js +2 -2
  91. package/dist/utilities/getFilePrefix.d.ts +5 -5
  92. package/dist/utilities/getFilePrefix.js +80 -80
  93. package/dist/utilities/getIncomingFiles.d.ts +7 -7
  94. package/dist/utilities/getIncomingFiles.js +37 -37
  95. package/dist/utilities/getRangeFromHeader.d.ts +6 -6
  96. package/dist/utilities/getRangeFromHeader.js +66 -66
  97. package/dist/webpack.d.ts +9 -9
  98. package/dist/webpack.js +39 -39
  99. package/docs/local-dev.md +47 -47
  100. package/eslint-config/index.js +15 -15
  101. package/eslint-config/rules/import.js +38 -38
  102. package/eslint-config/rules/prettier.js +7 -7
  103. package/eslint-config/rules/style.js +21 -21
  104. package/eslint-config/rules/typescript.js +628 -628
  105. package/gcs.d.ts +1 -1
  106. package/gcs.js +1 -1
  107. package/package.json +67 -67
  108. package/s3.d.ts +1 -1
  109. package/s3.js +1 -1
  110. package/src/adapters/azure/emulator/docker-compose.yml +16 -16
  111. package/src/adapters/azure/fileStub.js +1 -1
  112. package/src/adapters/azure/generateURL.ts +13 -13
  113. package/src/adapters/azure/handleDelete.ts +16 -16
  114. package/src/adapters/azure/handleUpload.ts +41 -41
  115. package/src/adapters/azure/index.ts +48 -47
  116. package/src/adapters/azure/mock.js +13 -13
  117. package/src/adapters/azure/staticHandler.ts +38 -38
  118. package/src/adapters/azure/webpack.ts +24 -20
  119. package/src/adapters/gcs/emulator/docker-compose.yml +15 -15
  120. package/src/adapters/gcs/generateURL.ts +16 -16
  121. package/src/adapters/gcs/handleDelete.ts +16 -16
  122. package/src/adapters/gcs/handleUpload.ts +34 -34
  123. package/src/adapters/gcs/index.ts +37 -37
  124. package/src/adapters/gcs/mock.js +3 -3
  125. package/src/adapters/gcs/staticHandler.ts +34 -34
  126. package/src/adapters/gcs/webpack.ts +21 -17
  127. package/src/adapters/s3/emulator/.localstack/cache/machine.json +1 -0
  128. package/src/adapters/s3/emulator/.localstack/cache/server.test.pem +127 -0
  129. package/src/adapters/s3/emulator/.localstack/cache/server.test.pem.crt +99 -0
  130. package/src/adapters/s3/emulator/.localstack/cache/server.test.pem.key +28 -0
  131. package/src/adapters/s3/emulator/.localstack/cache/service-catalog-2_1_1_dev-1_29_149.pickle +0 -0
  132. package/src/adapters/s3/emulator/docker-compose.yml +15 -15
  133. package/src/adapters/s3/generateURL.ts +14 -14
  134. package/src/adapters/s3/handleDelete.ts +17 -17
  135. package/src/adapters/s3/index.ts +38 -38
  136. package/src/adapters/s3/mock.js +9 -9
  137. package/src/adapters/s3/staticHandler.ts +41 -40
  138. package/src/adapters/s3/webpack.ts +4 -0
  139. package/src/fields/getFields.ts +155 -155
  140. package/src/hooks/afterDelete.ts +35 -35
  141. package/src/hooks/afterRead.ts +38 -38
  142. package/src/hooks/beforeChange.ts +59 -30
  143. package/src/index.ts +1 -1
  144. package/src/plugin.ts +101 -94
  145. package/src/types.ts +79 -73
  146. package/src/utilities/getFilePrefix.ts +26 -26
  147. package/src/utilities/getIncomingFiles.ts +44 -44
  148. package/src/utilities/getRangeFromHeader.ts +27 -27
  149. package/src/webpack.ts +46 -46
  150. package/tsconfig.json +23 -23
  151. package/yarn-error.log +8163 -0
  152. package/yarn.lock +8062 -8155
  153. package/.idea/.gitignore +0 -5
  154. package/.idea/httpRequests/2023-04-07T152957.206.png +0 -0
  155. package/.idea/httpRequests/2023-04-07T153025.403.html +0 -10
  156. package/.idea/httpRequests/2023-04-07T153146.200.png +0 -0
  157. package/.idea/httpRequests/http-client.cookies +0 -1
  158. package/.idea/httpRequests/http-requests-log.http +0 -74
  159. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  160. package/.idea/jsLinters/eslint.xml +0 -6
  161. package/.idea/modules.xml +0 -8
  162. package/.idea/plugin-cloud-storage.iml +0 -12
  163. package/.idea/vcs.xml +0 -6
  164. package/.idea/workspace.xml +0 -269
  165. package/dev/build/127.d2c2ffcfff69fabfdd1b.js +0 -1
  166. package/dev/build/16.17dbe03b1d0a96f3e564.js +0 -2
  167. package/dev/build/16.17dbe03b1d0a96f3e564.js.LICENSE.txt +0 -8
  168. package/dev/build/171.bbcbae3ea90468ad0cad.js +0 -2
  169. package/dev/build/171.bbcbae3ea90468ad0cad.js.LICENSE.txt +0 -8
  170. package/dev/build/18.e50c27edff6716f930d9.js +0 -1
  171. package/dev/build/205.33c7a29683ba98de93e0.js +0 -1
  172. package/dev/build/2211c49456cd07331ea9.woff +0 -0
  173. package/dev/build/234.79395f82c18207c13766.js +0 -1
  174. package/dev/build/266.9d4a240b3e0985bd7dd5.js +0 -1
  175. package/dev/build/296.4c5d646257b42c915834.js +0 -1
  176. package/dev/build/304.40dbe690de322c8f7c0d.js +0 -2
  177. package/dev/build/304.40dbe690de322c8f7c0d.js.LICENSE.txt +0 -37
  178. package/dev/build/349.446c12bffd3905085fdb.js +0 -1
  179. package/dev/build/354.5acd04b85b96a9839125.js +0 -1
  180. package/dev/build/40ad7515b8674bb854a1.woff2 +0 -0
  181. package/dev/build/422.086542466cdc9f6a2437.js +0 -2
  182. package/dev/build/422.086542466cdc9f6a2437.js.LICENSE.txt +0 -6
  183. package/dev/build/491.0bfe1bb0ecfe383179aa.js +0 -1
  184. package/dev/build/4d8845b830f4e8e2affb.png +0 -0
  185. package/dev/build/51922ceb71da289688d3.woff2 +0 -0
  186. package/dev/build/522443364fda49e9e0ed.woff2 +0 -0
  187. package/dev/build/531.1c6f53f3b44a3c45b444.js +0 -2
  188. package/dev/build/531.1c6f53f3b44a3c45b444.js.LICENSE.txt +0 -6
  189. package/dev/build/570.f2d9b99706765fbf0225.js +0 -1
  190. package/dev/build/599.570a04990d5806004f61.js +0 -1
  191. package/dev/build/5b718d9772de251a8c0a.woff2 +0 -0
  192. package/dev/build/778.41ae26bcd617861ad586.js +0 -1
  193. package/dev/build/783.0117995f2ff6036d6746.js +0 -1
  194. package/dev/build/787999a6af6a17efbc7c.woff +0 -0
  195. package/dev/build/78b8935fb481e11c92ce.woff +0 -0
  196. package/dev/build/860.7688681d3269f3f16e9a.js +0 -1
  197. package/dev/build/892.1a4ca5ac67d81038ceec.js +0 -1
  198. package/dev/build/896.d8cb1160388dc29d6364.js +0 -1
  199. package/dev/build/8b4ddd0d08500553efde.woff +0 -0
  200. package/dev/build/8f612153248094525d9d.woff +0 -0
  201. package/dev/build/995.cc11e738ff81a85821b4.js +0 -1
  202. package/dev/build/9c7dfd0036f7bd24b053.woff2 +0 -0
  203. package/dev/build/a1cfdc5b5250b7c4b481.woff2 +0 -0
  204. package/dev/build/d7aeda9e48ce098e7b48.woff +0 -0
  205. package/dev/build/e009f21405b4d7e89367.woff2 +0 -0
  206. package/dev/build/e7caa9e17af6ac87d182.woff +0 -0
  207. package/dev/build/ebcc1430049fddb274f8.svg +0 -15
  208. package/dev/build/efe8f6a3b46446cc9135.woff +0 -0
  209. package/dev/build/f53bb8d4b29adc903703.woff2 +0 -0
  210. package/dev/build/index.html +0 -1
  211. package/dev/build/main.a2003d502fbb9aaa3e8d.js +0 -2
  212. package/dev/build/main.a2003d502fbb9aaa3e8d.js.LICENSE.txt +0 -57
  213. package/dev/build/styles.css +0 -1
  214. package/dev/build/styles.fa29d16b0baf5b98a1cf.js +0 -1
@@ -1,30 +1,59 @@
1
- import type { TypeWithID } from 'payload/dist/collections/config/types'
2
- import type { FileData } from 'payload/dist/uploads/types'
3
- import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload/types'
4
- import type { GeneratedAdapter } from '../types'
5
- import { getIncomingFiles } from '../utilities/getIncomingFiles'
6
-
7
- interface Args {
8
- collection: CollectionConfig
9
- adapter: GeneratedAdapter
10
- }
11
-
12
- export const getBeforeChangeHook =
13
- ({ collection, adapter }: Args): CollectionBeforeChangeHook<FileData & TypeWithID> =>
14
- async ({ req, data }) => {
15
- try {
16
- const files = getIncomingFiles({ req, data })
17
-
18
- const promises = files.map(async file => {
19
- await adapter.handleUpload({ collection, data, req, file })
20
- })
21
-
22
- await Promise.all(promises)
23
- } catch (err: unknown) {
24
- req.payload.logger.error(
25
- `There was an error while uploading files corresponding to the collection ${collection.slug} with filename ${data.filename}:`,
26
- )
27
- req.payload.logger.error(err)
28
- }
29
- return data
30
- }
1
+ import type { TypeWithID } from 'payload/dist/collections/config/types'
2
+ import type { FileData } from 'payload/dist/uploads/types'
3
+ import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload/types'
4
+ import type { GeneratedAdapter } from '../types'
5
+ import { getIncomingFiles } from '../utilities/getIncomingFiles'
6
+
7
+ interface Args {
8
+ collection: CollectionConfig
9
+ adapter: GeneratedAdapter
10
+ }
11
+
12
+ export const getBeforeChangeHook =
13
+ ({ collection, adapter }: Args): CollectionBeforeChangeHook<FileData & TypeWithID> =>
14
+ async ({ req, data, originalDoc }) => {
15
+ try {
16
+ const files = getIncomingFiles({ req, data })
17
+
18
+ if (files.length > 0) {
19
+ // If there is an original doc,
20
+ // And we have new files,
21
+ // We need to delete the old files before uploading new
22
+ if (originalDoc) {
23
+ let filesToDelete: string[] = []
24
+
25
+ if (typeof originalDoc?.filename === 'string') {
26
+ filesToDelete.push(originalDoc.filename)
27
+ }
28
+
29
+ if (typeof originalDoc.sizes === 'object') {
30
+ filesToDelete = filesToDelete.concat(
31
+ Object.values(originalDoc?.sizes || []).map(
32
+ resizedFileData => resizedFileData?.filename,
33
+ ),
34
+ )
35
+ }
36
+
37
+ const deletionPromises = filesToDelete.map(async filename => {
38
+ if (filename) {
39
+ await adapter.handleDelete({ collection, doc: originalDoc, req, filename })
40
+ }
41
+ })
42
+
43
+ await Promise.all(deletionPromises)
44
+ }
45
+
46
+ const promises = files.map(async file => {
47
+ await adapter.handleUpload({ collection, data, req, file })
48
+ })
49
+
50
+ await Promise.all(promises)
51
+ }
52
+ } catch (err: unknown) {
53
+ req.payload.logger.error(
54
+ `There was an error while uploading files corresponding to the collection ${collection.slug} with filename ${data.filename}:`,
55
+ )
56
+ req.payload.logger.error(err)
57
+ }
58
+ return data
59
+ }
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export { cloudStorage } from './plugin'
1
+ export { cloudStorage } from './plugin'
package/src/plugin.ts CHANGED
@@ -1,94 +1,101 @@
1
- import type { Config } from 'payload/config'
2
- import { extendWebpackConfig } from './webpack'
3
- import type { PluginOptions } from './types'
4
- import { getBeforeChangeHook } from './hooks/beforeChange'
5
- import { getAfterDeleteHook } from './hooks/afterDelete'
6
- import { getFields } from './fields/getFields'
7
-
8
- // This plugin extends all targeted collections by offloading uploaded files
9
- // to cloud storage instead of solely storing files locally.
10
-
11
- // It is based on an adapter approach, where adapters can be written for any cloud provider.
12
- // Adapters are responsible for providing four actions that this plugin will use:
13
- // 1. handleUpload, 2. handleDelete, 3. generateURL, 4. staticHandler
14
-
15
- // Optionally, the adapter can specify any Webpack config overrides if they are necessary.
16
-
17
- export const cloudStorage =
18
- (pluginOptions: PluginOptions) =>
19
- (config: Config): Config => {
20
- const { collections: allCollectionOptions } = pluginOptions
21
-
22
- const webpack = extendWebpackConfig({ options: pluginOptions, config })
23
-
24
- const initFunctions: (() => void)[] = []
25
-
26
- return {
27
- ...config,
28
- admin: {
29
- ...(config.admin || {}),
30
- webpack,
31
- },
32
- collections: (config.collections || []).map(existingCollection => {
33
- const options = allCollectionOptions[existingCollection.slug]
34
-
35
- if (options?.adapter) {
36
- const adapter = options.adapter({
37
- collection: existingCollection,
38
- prefix: options.prefix,
39
- })
40
-
41
- if (adapter.onInit) initFunctions.push(adapter.onInit)
42
-
43
- const fields = getFields({
44
- collection: existingCollection,
45
- disablePayloadAccessControl: options.disablePayloadAccessControl,
46
- generateFileURL: options.generateFileURL,
47
- prefix: options.prefix,
48
- adapter,
49
- })
50
-
51
- const handlers = [
52
- ...(typeof existingCollection.upload === 'object' &&
53
- Array.isArray(existingCollection.upload.handlers)
54
- ? existingCollection.upload.handlers
55
- : []),
56
- ]
57
-
58
- if (!options.disablePayloadAccessControl) {
59
- handlers.push(adapter.staticHandler)
60
- }
61
-
62
- return {
63
- ...existingCollection,
64
- upload: {
65
- ...(typeof existingCollection.upload === 'object' ? existingCollection.upload : {}),
66
- handlers,
67
- disableLocalStorage:
68
- typeof options.disableLocalStorage === 'boolean'
69
- ? options.disableLocalStorage
70
- : true,
71
- },
72
- hooks: {
73
- ...(existingCollection.hooks || {}),
74
- beforeChange: [
75
- ...(existingCollection.hooks?.beforeChange || []),
76
- getBeforeChangeHook({ adapter, collection: existingCollection }),
77
- ],
78
- afterDelete: [
79
- ...(existingCollection.hooks?.afterDelete || []),
80
- getAfterDeleteHook({ adapter, collection: existingCollection }),
81
- ],
82
- },
83
- fields,
84
- }
85
- }
86
-
87
- return existingCollection
88
- }),
89
- onInit: async payload => {
90
- initFunctions.forEach(fn => fn())
91
- if (config.onInit) await config.onInit(payload)
92
- },
93
- }
94
- }
1
+ import type { Config } from 'payload/config'
2
+ import { extendWebpackConfig } from './webpack'
3
+ import type { PluginOptions } from './types'
4
+ import { getBeforeChangeHook } from './hooks/beforeChange'
5
+ import { getAfterDeleteHook } from './hooks/afterDelete'
6
+ import { getFields } from './fields/getFields'
7
+
8
+ // This plugin extends all targeted collections by offloading uploaded files
9
+ // to cloud storage instead of solely storing files locally.
10
+
11
+ // It is based on an adapter approach, where adapters can be written for any cloud provider.
12
+ // Adapters are responsible for providing four actions that this plugin will use:
13
+ // 1. handleUpload, 2. handleDelete, 3. generateURL, 4. staticHandler
14
+
15
+ // Optionally, the adapter can specify any Webpack config overrides if they are necessary.
16
+
17
+ export const cloudStorage =
18
+ (pluginOptions: PluginOptions) =>
19
+ (incomingConfig: Config): Config => {
20
+ const { collections: allCollectionOptions, enabled } = pluginOptions
21
+ const config = { ...incomingConfig }
22
+
23
+ const webpack = extendWebpackConfig({ options: pluginOptions, config: incomingConfig })
24
+
25
+ config.admin = {
26
+ ...(config.admin || {}),
27
+ webpack,
28
+ }
29
+
30
+ // Return early if disabled. Only webpack config mods are applied.
31
+ if (enabled === false) {
32
+ return config
33
+ }
34
+
35
+ const initFunctions: Array<() => void> = []
36
+
37
+ return {
38
+ ...config,
39
+ collections: (config.collections || []).map(existingCollection => {
40
+ const options = allCollectionOptions[existingCollection.slug]
41
+
42
+ if (options?.adapter) {
43
+ const adapter = options.adapter({
44
+ collection: existingCollection,
45
+ prefix: options.prefix,
46
+ })
47
+
48
+ if (adapter.onInit) initFunctions.push(adapter.onInit)
49
+
50
+ const fields = getFields({
51
+ collection: existingCollection,
52
+ disablePayloadAccessControl: options.disablePayloadAccessControl,
53
+ generateFileURL: options.generateFileURL,
54
+ prefix: options.prefix,
55
+ adapter,
56
+ })
57
+
58
+ const handlers = [
59
+ ...(typeof existingCollection.upload === 'object' &&
60
+ Array.isArray(existingCollection.upload.handlers)
61
+ ? existingCollection.upload.handlers
62
+ : []),
63
+ ]
64
+
65
+ if (!options.disablePayloadAccessControl) {
66
+ handlers.push(adapter.staticHandler)
67
+ }
68
+
69
+ return {
70
+ ...existingCollection,
71
+ upload: {
72
+ ...(typeof existingCollection.upload === 'object' ? existingCollection.upload : {}),
73
+ handlers,
74
+ disableLocalStorage:
75
+ typeof options.disableLocalStorage === 'boolean'
76
+ ? options.disableLocalStorage
77
+ : true,
78
+ },
79
+ hooks: {
80
+ ...(existingCollection.hooks || {}),
81
+ beforeChange: [
82
+ ...(existingCollection.hooks?.beforeChange || []),
83
+ getBeforeChangeHook({ adapter, collection: existingCollection }),
84
+ ],
85
+ afterDelete: [
86
+ ...(existingCollection.hooks?.afterDelete || []),
87
+ getAfterDeleteHook({ adapter, collection: existingCollection }),
88
+ ],
89
+ },
90
+ fields,
91
+ }
92
+ }
93
+
94
+ return existingCollection
95
+ }),
96
+ onInit: async payload => {
97
+ initFunctions.forEach(fn => fn())
98
+ if (config.onInit) await config.onInit(payload)
99
+ },
100
+ }
101
+ }
package/src/types.ts CHANGED
@@ -1,73 +1,79 @@
1
- import type { NextFunction, Response } from 'express'
2
- import type { TypeWithID } from 'payload/dist/collections/config/types'
3
- import type { FileData, ImageSize } from 'payload/dist/uploads/types'
4
- import type { CollectionConfig, PayloadRequest } from 'payload/types'
5
- import type { Configuration as WebpackConfig } from 'webpack'
6
-
7
- export interface File {
8
- buffer: Buffer
9
- filename: string
10
- filesize: number
11
- mimeType: string
12
- tempFilePath?: string
13
- }
14
-
15
- export type HandleUpload = (args: {
16
- collection: CollectionConfig
17
- req: PayloadRequest
18
- data: any
19
- file: File
20
- }) => Promise<void> | void
21
-
22
- export interface TypeWithPrefix {
23
- prefix?: string
24
- }
25
-
26
- export type HandleDelete = (args: {
27
- collection: CollectionConfig
28
- req: PayloadRequest
29
- doc: TypeWithID & FileData & TypeWithPrefix
30
- filename: string
31
- }) => Promise<void> | void
32
-
33
- export type GenerateURL = (args: {
34
- filename: string
35
- collection: CollectionConfig
36
- prefix?: string
37
- }) => string | Promise<string>
38
-
39
- export type StaticHandler = (
40
- req: PayloadRequest,
41
- res: Response,
42
- next: NextFunction,
43
- ) => Promise<unknown> | unknown
44
-
45
- export interface GeneratedAdapter {
46
- handleUpload: HandleUpload
47
- handleDelete: HandleDelete
48
- generateURL: GenerateURL
49
- staticHandler: StaticHandler
50
- webpack?: (config: WebpackConfig) => WebpackConfig
51
- onInit?: () => void
52
- }
53
-
54
- export type Adapter = (args: { collection: CollectionConfig; prefix?: string }) => GeneratedAdapter
55
-
56
- export type GenerateFileURL = (args: {
57
- collection: CollectionConfig
58
- filename: string
59
- prefix?: string
60
- size?: ImageSize
61
- }) => Promise<string> | string
62
-
63
- export interface CollectionOptions {
64
- disableLocalStorage?: boolean
65
- disablePayloadAccessControl?: true
66
- generateFileURL?: GenerateFileURL
67
- prefix?: string
68
- adapter: Adapter | null
69
- }
70
-
71
- export interface PluginOptions {
72
- collections: Record<string, CollectionOptions>
73
- }
1
+ import type { NextFunction, Response } from 'express'
2
+ import type { TypeWithID } from 'payload/dist/collections/config/types'
3
+ import type { FileData, ImageSize } from 'payload/dist/uploads/types'
4
+ import type { CollectionConfig, PayloadRequest } from 'payload/types'
5
+ import type { Configuration as WebpackConfig } from 'webpack'
6
+
7
+ export interface File {
8
+ buffer: Buffer
9
+ filename: string
10
+ filesize: number
11
+ mimeType: string
12
+ tempFilePath?: string
13
+ }
14
+
15
+ export type HandleUpload = (args: {
16
+ collection: CollectionConfig
17
+ req: PayloadRequest
18
+ data: any
19
+ file: File
20
+ }) => Promise<void> | void
21
+
22
+ export interface TypeWithPrefix {
23
+ prefix?: string
24
+ }
25
+
26
+ export type HandleDelete = (args: {
27
+ collection: CollectionConfig
28
+ req: PayloadRequest
29
+ doc: TypeWithID & FileData & TypeWithPrefix
30
+ filename: string
31
+ }) => Promise<void> | void
32
+
33
+ export type GenerateURL = (args: {
34
+ filename: string
35
+ collection: CollectionConfig
36
+ prefix?: string
37
+ }) => string | Promise<string>
38
+
39
+ export type StaticHandler = (
40
+ req: PayloadRequest,
41
+ res: Response,
42
+ next: NextFunction,
43
+ ) => Promise<unknown> | unknown
44
+
45
+ export interface GeneratedAdapter {
46
+ handleUpload: HandleUpload
47
+ handleDelete: HandleDelete
48
+ generateURL: GenerateURL
49
+ staticHandler: StaticHandler
50
+ webpack?: (config: WebpackConfig) => WebpackConfig
51
+ onInit?: () => void
52
+ }
53
+
54
+ export type Adapter = (args: { collection: CollectionConfig; prefix?: string }) => GeneratedAdapter
55
+
56
+ export type GenerateFileURL = (args: {
57
+ collection: CollectionConfig
58
+ filename: string
59
+ prefix?: string
60
+ size?: ImageSize
61
+ }) => Promise<string> | string
62
+
63
+ export interface CollectionOptions {
64
+ disableLocalStorage?: boolean
65
+ disablePayloadAccessControl?: true
66
+ generateFileURL?: GenerateFileURL
67
+ prefix?: string
68
+ adapter: Adapter | null
69
+ }
70
+
71
+ export interface PluginOptions {
72
+ /**
73
+ * Whether or not to enable the plugin
74
+ *
75
+ * Default: true
76
+ */
77
+ enabled?: boolean
78
+ collections: Record<string, CollectionOptions>
79
+ }
@@ -1,26 +1,26 @@
1
- import type { CollectionConfig, PayloadRequest } from 'payload/types'
2
- import { IncomingUploadType } from 'payload/dist/uploads/types'
3
-
4
- export async function getFilePrefix({
5
- req,
6
- collection,
7
- }: {
8
- req: PayloadRequest
9
- collection: CollectionConfig
10
- }): Promise<string> {
11
- const imageSizes = (collection?.upload as IncomingUploadType)?.imageSizes || []
12
- const files = await req.payload.find({
13
- collection: collection.slug,
14
- where: {
15
- or: [
16
- {
17
- filename: { equals: req.params.filename },
18
- },
19
- ...imageSizes.map(imageSize => ({
20
- [`sizes.${imageSize.name}.filename`]: { equals: req.params.filename },
21
- })),
22
- ],
23
- },
24
- })
25
- return files?.docs?.[0]?.prefix || ''
26
- }
1
+ import type { CollectionConfig, PayloadRequest } from 'payload/types'
2
+ import { IncomingUploadType } from 'payload/dist/uploads/types'
3
+
4
+ export async function getFilePrefix({
5
+ req,
6
+ collection,
7
+ }: {
8
+ req: PayloadRequest
9
+ collection: CollectionConfig
10
+ }): Promise<string> {
11
+ const imageSizes = (collection?.upload as IncomingUploadType)?.imageSizes || []
12
+ const files = await req.payload.find({
13
+ collection: collection.slug,
14
+ where: {
15
+ or: [
16
+ {
17
+ filename: { equals: req.params.filename },
18
+ },
19
+ ...imageSizes.map(imageSize => ({
20
+ [`sizes.${imageSize.name}.filename`]: { equals: req.params.filename },
21
+ })),
22
+ ],
23
+ },
24
+ })
25
+ return files?.docs?.[0]?.prefix || ''
26
+ }
@@ -1,44 +1,44 @@
1
- import type { FileData } from 'payload/dist/uploads/types'
2
- import type { PayloadRequest } from 'payload/types'
3
- import type { File } from '../types'
4
-
5
- export function getIncomingFiles({
6
- req,
7
- data,
8
- }: {
9
- data: Partial<FileData>
10
- req: PayloadRequest
11
- }): File[] {
12
- const file = req.files?.file
13
-
14
- let files: File[] = []
15
-
16
- if (file && data.filename && data.mimeType) {
17
- const mainFile: File = {
18
- filename: data.filename,
19
- mimeType: data.mimeType,
20
- buffer: file.data,
21
- tempFilePath: file.tempFilePath,
22
- filesize: file.size,
23
- }
24
-
25
- files = [mainFile]
26
-
27
- if (data?.sizes) {
28
- Object.entries(data.sizes).forEach(([key, resizedFileData]) => {
29
- if (req.payloadUploadSizes?.[key] && data.mimeType) {
30
- files = files.concat([
31
- {
32
- filename: `${resizedFileData.filename}`,
33
- mimeType: data.mimeType,
34
- buffer: req.payloadUploadSizes[key],
35
- filesize: req.payloadUploadSizes[key].length,
36
- },
37
- ])
38
- }
39
- })
40
- }
41
- }
42
-
43
- return files
44
- }
1
+ import type { FileData } from 'payload/dist/uploads/types'
2
+ import type { PayloadRequest } from 'payload/types'
3
+ import type { File } from '../types'
4
+
5
+ export function getIncomingFiles({
6
+ req,
7
+ data,
8
+ }: {
9
+ data: Partial<FileData>
10
+ req: PayloadRequest
11
+ }): File[] {
12
+ const file = req.files?.file
13
+
14
+ let files: File[] = []
15
+
16
+ if (file && data.filename && data.mimeType) {
17
+ const mainFile: File = {
18
+ filename: data.filename,
19
+ mimeType: data.mimeType,
20
+ buffer: file.data,
21
+ tempFilePath: file.tempFilePath,
22
+ filesize: file.size,
23
+ }
24
+
25
+ files = [mainFile]
26
+
27
+ if (data?.sizes) {
28
+ Object.entries(data.sizes).forEach(([key, resizedFileData]) => {
29
+ if (req.payloadUploadSizes?.[key] && data.mimeType) {
30
+ files = files.concat([
31
+ {
32
+ filename: `${resizedFileData.filename}`,
33
+ mimeType: data.mimeType,
34
+ buffer: req.payloadUploadSizes[key],
35
+ filesize: req.payloadUploadSizes[key].length,
36
+ },
37
+ ])
38
+ }
39
+ })
40
+ }
41
+ }
42
+
43
+ return files
44
+ }
@@ -1,27 +1,27 @@
1
- import type { BlockBlobClient } from '@azure/storage-blob'
2
- import parseRange from 'range-parser'
3
-
4
- const getRangeFromHeader = async (
5
- blockBlobClient: BlockBlobClient,
6
- rangeHeader?: string,
7
- ): Promise<{ start: number; end: number | undefined }> => {
8
- const fullRange = { start: 0, end: undefined }
9
-
10
- if (!rangeHeader) {
11
- return fullRange
12
- }
13
-
14
- const size = await blockBlobClient.getProperties().then(props => props.contentLength)
15
- if (size === undefined) {
16
- return fullRange
17
- }
18
-
19
- const range = parseRange(size, rangeHeader)
20
- if (range === -1 || range === -2 || range.type !== 'bytes' || range.length !== 1) {
21
- return fullRange
22
- }
23
-
24
- return range[0]
25
- }
26
-
27
- export default getRangeFromHeader
1
+ import type { BlockBlobClient } from '@azure/storage-blob'
2
+ import parseRange from 'range-parser'
3
+
4
+ const getRangeFromHeader = async (
5
+ blockBlobClient: BlockBlobClient,
6
+ rangeHeader?: string,
7
+ ): Promise<{ start: number; end: number | undefined }> => {
8
+ const fullRange = { start: 0, end: undefined }
9
+
10
+ if (!rangeHeader) {
11
+ return fullRange
12
+ }
13
+
14
+ const size = await blockBlobClient.getProperties().then(props => props.contentLength)
15
+ if (size === undefined) {
16
+ return fullRange
17
+ }
18
+
19
+ const range = parseRange(size, rangeHeader)
20
+ if (range === -1 || range === -2 || range.type !== 'bytes' || range.length !== 1) {
21
+ return fullRange
22
+ }
23
+
24
+ return range[0]
25
+ }
26
+
27
+ export default getRangeFromHeader