@strapi/upload 0.0.0 → 5.0.0-beta.7

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 (217) hide show
  1. package/README.md +1 -1
  2. package/dist/_chunks/ca-B2_I-q1t.mjs +121 -0
  3. package/dist/_chunks/ca-B2_I-q1t.mjs.map +1 -0
  4. package/dist/_chunks/ca-BUpuZx8N.js +121 -0
  5. package/dist/_chunks/ca-BUpuZx8N.js.map +1 -0
  6. package/dist/_chunks/de-A7mEKx6c.mjs +107 -0
  7. package/dist/_chunks/de-A7mEKx6c.mjs.map +1 -0
  8. package/dist/_chunks/de-uGb_Pkq7.js +107 -0
  9. package/dist/_chunks/de-uGb_Pkq7.js.map +1 -0
  10. package/dist/_chunks/dk-BPfkJb9q.mjs +103 -0
  11. package/dist/_chunks/dk-BPfkJb9q.mjs.map +1 -0
  12. package/dist/_chunks/dk-Cd8oFO-O.js +103 -0
  13. package/dist/_chunks/dk-Cd8oFO-O.js.map +1 -0
  14. package/dist/_chunks/en-BcOqhiNe.js +144 -0
  15. package/dist/_chunks/en-BcOqhiNe.js.map +1 -0
  16. package/dist/_chunks/en-oDx2Gnre.mjs +144 -0
  17. package/dist/_chunks/en-oDx2Gnre.mjs.map +1 -0
  18. package/dist/_chunks/es-CuWi2pOn.mjs +120 -0
  19. package/dist/_chunks/es-CuWi2pOn.mjs.map +1 -0
  20. package/dist/_chunks/es-DWFtw_h4.js +120 -0
  21. package/dist/_chunks/es-DWFtw_h4.js.map +1 -0
  22. package/dist/_chunks/fr-BN6ndmWf.mjs +144 -0
  23. package/dist/_chunks/fr-BN6ndmWf.mjs.map +1 -0
  24. package/dist/_chunks/fr-D2bop66d.js +144 -0
  25. package/dist/_chunks/fr-D2bop66d.js.map +1 -0
  26. package/dist/_chunks/graphql-BVCZSNa2.mjs +71 -0
  27. package/dist/_chunks/graphql-BVCZSNa2.mjs.map +1 -0
  28. package/dist/_chunks/graphql-Beol7Y3b.js +71 -0
  29. package/dist/_chunks/graphql-Beol7Y3b.js.map +1 -0
  30. package/dist/_chunks/he-BpxHjaZg.js +76 -0
  31. package/dist/_chunks/he-BpxHjaZg.js.map +1 -0
  32. package/dist/_chunks/he-C9ZOXBB-.mjs +76 -0
  33. package/dist/_chunks/he-C9ZOXBB-.mjs.map +1 -0
  34. package/dist/_chunks/index-2C3ZYD4e.mjs +856 -0
  35. package/dist/_chunks/index-2C3ZYD4e.mjs.map +1 -0
  36. package/dist/_chunks/index-ASLx4V_D.mjs +6548 -0
  37. package/dist/_chunks/index-ASLx4V_D.mjs.map +1 -0
  38. package/dist/_chunks/index-BbFs7_Gx.js +859 -0
  39. package/dist/_chunks/index-BbFs7_Gx.js.map +1 -0
  40. package/dist/_chunks/index-BxUjcUMp.js +266 -0
  41. package/dist/_chunks/index-BxUjcUMp.js.map +1 -0
  42. package/dist/_chunks/index-CRhNpXFr.js +2632 -0
  43. package/dist/_chunks/index-CRhNpXFr.js.map +1 -0
  44. package/dist/_chunks/index-DJC1L27M.js +238 -0
  45. package/dist/_chunks/index-DJC1L27M.js.map +1 -0
  46. package/dist/_chunks/index-PKgX3DYk.js +6574 -0
  47. package/dist/_chunks/index-PKgX3DYk.js.map +1 -0
  48. package/dist/_chunks/index-VKvfSVC7.mjs +2622 -0
  49. package/dist/_chunks/index-VKvfSVC7.mjs.map +1 -0
  50. package/dist/_chunks/index-fmt8afwE.mjs +262 -0
  51. package/dist/_chunks/index-fmt8afwE.mjs.map +1 -0
  52. package/dist/_chunks/index-q-dRjRbA.mjs +233 -0
  53. package/dist/_chunks/index-q-dRjRbA.mjs.map +1 -0
  54. package/dist/_chunks/it-B7rmoZNx.mjs +76 -0
  55. package/dist/_chunks/it-B7rmoZNx.mjs.map +1 -0
  56. package/dist/_chunks/it-BKCWXl8t.js +76 -0
  57. package/dist/_chunks/it-BKCWXl8t.js.map +1 -0
  58. package/dist/_chunks/ja-DlaJTi_3.mjs +76 -0
  59. package/dist/_chunks/ja-DlaJTi_3.mjs.map +1 -0
  60. package/dist/_chunks/ja-ajHzIJz6.js +76 -0
  61. package/dist/_chunks/ja-ajHzIJz6.js.map +1 -0
  62. package/dist/_chunks/ko-Pzj-818C.js +106 -0
  63. package/dist/_chunks/ko-Pzj-818C.js.map +1 -0
  64. package/dist/_chunks/ko-vJl9kPpn.mjs +106 -0
  65. package/dist/_chunks/ko-vJl9kPpn.mjs.map +1 -0
  66. package/dist/_chunks/ms-CqwG8v8l.mjs +68 -0
  67. package/dist/_chunks/ms-CqwG8v8l.mjs.map +1 -0
  68. package/dist/_chunks/ms-h3gjldBy.js +68 -0
  69. package/dist/_chunks/ms-h3gjldBy.js.map +1 -0
  70. package/dist/_chunks/pl-Cj8jChOO.mjs +105 -0
  71. package/dist/_chunks/pl-Cj8jChOO.mjs.map +1 -0
  72. package/dist/_chunks/pl-esgZ7ltN.js +105 -0
  73. package/dist/_chunks/pl-esgZ7ltN.js.map +1 -0
  74. package/dist/_chunks/pt-BR-B4LJHJIp.mjs +76 -0
  75. package/dist/_chunks/pt-BR-B4LJHJIp.mjs.map +1 -0
  76. package/dist/_chunks/pt-BR-Cazr7Z5I.js +76 -0
  77. package/dist/_chunks/pt-BR-Cazr7Z5I.js.map +1 -0
  78. package/dist/_chunks/pt-CNOOM_7x.mjs +76 -0
  79. package/dist/_chunks/pt-CNOOM_7x.mjs.map +1 -0
  80. package/dist/_chunks/pt-cbUnkHM5.js +76 -0
  81. package/dist/_chunks/pt-cbUnkHM5.js.map +1 -0
  82. package/dist/_chunks/ru-DqglvSUC.mjs +76 -0
  83. package/dist/_chunks/ru-DqglvSUC.mjs.map +1 -0
  84. package/dist/_chunks/ru-H6MzFUxp.js +76 -0
  85. package/dist/_chunks/ru-H6MzFUxp.js.map +1 -0
  86. package/dist/_chunks/sk-CZxC4dFY.js +122 -0
  87. package/dist/_chunks/sk-CZxC4dFY.js.map +1 -0
  88. package/dist/_chunks/sk-Dgpb3lnz.mjs +122 -0
  89. package/dist/_chunks/sk-Dgpb3lnz.mjs.map +1 -0
  90. package/dist/_chunks/th-C6unJZ8j.js +76 -0
  91. package/dist/_chunks/th-C6unJZ8j.js.map +1 -0
  92. package/dist/_chunks/th-DRfzuiFf.mjs +76 -0
  93. package/dist/_chunks/th-DRfzuiFf.mjs.map +1 -0
  94. package/dist/_chunks/tr--GzWXE_A.mjs +122 -0
  95. package/dist/_chunks/tr--GzWXE_A.mjs.map +1 -0
  96. package/dist/_chunks/tr-CY6AwX50.js +122 -0
  97. package/dist/_chunks/tr-CY6AwX50.js.map +1 -0
  98. package/dist/_chunks/uk-BniyNsD4.js +73 -0
  99. package/dist/_chunks/uk-BniyNsD4.js.map +1 -0
  100. package/dist/_chunks/uk-DVMT2Piq.mjs +73 -0
  101. package/dist/_chunks/uk-DVMT2Piq.mjs.map +1 -0
  102. package/dist/_chunks/zh-CsZw0IpM.js +129 -0
  103. package/dist/_chunks/zh-CsZw0IpM.js.map +1 -0
  104. package/dist/_chunks/zh-HOnih0is.mjs +129 -0
  105. package/dist/_chunks/zh-HOnih0is.mjs.map +1 -0
  106. package/dist/_chunks/zh-Hans-Cpmhg8uH.mjs +152 -0
  107. package/dist/_chunks/zh-Hans-Cpmhg8uH.mjs.map +1 -0
  108. package/dist/_chunks/zh-Hans-k_xAc6nm.js +152 -0
  109. package/dist/_chunks/zh-Hans-k_xAc6nm.js.map +1 -0
  110. package/dist/admin/index.js +5 -0
  111. package/dist/admin/index.js.map +1 -0
  112. package/dist/admin/index.mjs +6 -0
  113. package/dist/admin/index.mjs.map +1 -0
  114. package/dist/server/index.js +4 -0
  115. package/dist/server/index.js.map +1 -0
  116. package/dist/server/index.mjs +5 -0
  117. package/dist/server/index.mjs.map +1 -0
  118. package/dist/server/src/bootstrap.d.ts +5 -0
  119. package/dist/server/src/bootstrap.d.ts.map +1 -0
  120. package/dist/server/src/config.d.ts +10 -0
  121. package/dist/server/src/config.d.ts.map +1 -0
  122. package/dist/server/src/constants.d.ts +20 -0
  123. package/dist/server/src/constants.d.ts.map +1 -0
  124. package/dist/server/src/content-types/file.d.ts +110 -0
  125. package/dist/server/src/content-types/file.d.ts.map +1 -0
  126. package/dist/server/src/content-types/folder.d.ts +61 -0
  127. package/dist/server/src/content-types/folder.d.ts.map +1 -0
  128. package/dist/server/src/content-types/index.d.ts +170 -0
  129. package/dist/server/src/content-types/index.d.ts.map +1 -0
  130. package/dist/server/src/controllers/admin-file.d.ts +16 -0
  131. package/dist/server/src/controllers/admin-file.d.ts.map +1 -0
  132. package/dist/server/src/controllers/admin-folder-file.d.ts +7 -0
  133. package/dist/server/src/controllers/admin-folder-file.d.ts.map +1 -0
  134. package/dist/server/src/controllers/admin-folder.d.ts +10 -0
  135. package/dist/server/src/controllers/admin-folder.d.ts.map +1 -0
  136. package/dist/server/src/controllers/admin-settings.d.ts +7 -0
  137. package/dist/server/src/controllers/admin-settings.d.ts.map +1 -0
  138. package/dist/server/src/controllers/admin-upload.d.ts +9 -0
  139. package/dist/server/src/controllers/admin-upload.d.ts.map +1 -0
  140. package/dist/server/src/controllers/content-api.d.ts +15 -0
  141. package/dist/server/src/controllers/content-api.d.ts.map +1 -0
  142. package/dist/server/src/controllers/index.d.ts +53 -0
  143. package/dist/server/src/controllers/index.d.ts.map +1 -0
  144. package/dist/server/src/controllers/utils/find-entity-and-check-permissions.d.ts +6 -0
  145. package/dist/server/src/controllers/utils/find-entity-and-check-permissions.d.ts.map +1 -0
  146. package/dist/server/src/controllers/utils/folders.d.ts +4 -0
  147. package/dist/server/src/controllers/utils/folders.d.ts.map +1 -0
  148. package/dist/server/src/controllers/validation/admin/configureView.d.ts +15 -0
  149. package/dist/server/src/controllers/validation/admin/configureView.d.ts.map +1 -0
  150. package/dist/server/src/controllers/validation/admin/folder-file.d.ts +3 -0
  151. package/dist/server/src/controllers/validation/admin/folder-file.d.ts.map +1 -0
  152. package/dist/server/src/controllers/validation/admin/folder.d.ts +3 -0
  153. package/dist/server/src/controllers/validation/admin/folder.d.ts.map +1 -0
  154. package/dist/server/src/controllers/validation/admin/settings.d.ts +18 -0
  155. package/dist/server/src/controllers/validation/admin/settings.d.ts.map +1 -0
  156. package/dist/server/src/controllers/validation/admin/upload.d.ts +109 -0
  157. package/dist/server/src/controllers/validation/admin/upload.d.ts.map +1 -0
  158. package/dist/server/src/controllers/validation/admin/utils.d.ts +3 -0
  159. package/dist/server/src/controllers/validation/admin/utils.d.ts.map +1 -0
  160. package/dist/server/src/controllers/validation/content-api/upload.d.ts +91 -0
  161. package/dist/server/src/controllers/validation/content-api/upload.d.ts.map +1 -0
  162. package/dist/server/src/controllers/view-configuration.d.ts +7 -0
  163. package/dist/server/src/controllers/view-configuration.d.ts.map +1 -0
  164. package/dist/server/src/graphql.d.ts +5 -0
  165. package/dist/server/src/graphql.d.ts.map +1 -0
  166. package/dist/server/src/index.d.ts +475 -0
  167. package/dist/server/src/index.d.ts.map +1 -0
  168. package/dist/server/src/middlewares/upload.d.ts +9 -0
  169. package/dist/server/src/middlewares/upload.d.ts.map +1 -0
  170. package/dist/server/src/register.d.ts +8 -0
  171. package/dist/server/src/register.d.ts.map +1 -0
  172. package/dist/server/src/routes/admin.d.ts +17 -0
  173. package/dist/server/src/routes/admin.d.ts.map +1 -0
  174. package/dist/server/src/routes/content-api.d.ts +9 -0
  175. package/dist/server/src/routes/content-api.d.ts.map +1 -0
  176. package/dist/server/src/routes/index.d.ts +43 -0
  177. package/dist/server/src/routes/index.d.ts.map +1 -0
  178. package/dist/server/src/routes/view-configuration.d.ts +17 -0
  179. package/dist/server/src/routes/view-configuration.d.ts.map +1 -0
  180. package/dist/server/src/services/api-upload-folder.d.ts +5 -0
  181. package/dist/server/src/services/api-upload-folder.d.ts.map +1 -0
  182. package/dist/server/src/services/extensions/content-manager/entity-manager.d.ts +15 -0
  183. package/dist/server/src/services/extensions/content-manager/entity-manager.d.ts.map +1 -0
  184. package/dist/server/src/services/extensions/content-manager/index.d.ts +4 -0
  185. package/dist/server/src/services/extensions/content-manager/index.d.ts.map +1 -0
  186. package/dist/server/src/services/extensions/core/entity-service.d.ts +4 -0
  187. package/dist/server/src/services/extensions/core/entity-service.d.ts.map +1 -0
  188. package/dist/server/src/services/extensions/core/index.d.ts +4 -0
  189. package/dist/server/src/services/extensions/core/index.d.ts.map +1 -0
  190. package/dist/server/src/services/extensions/index.d.ts +8 -0
  191. package/dist/server/src/services/extensions/index.d.ts.map +1 -0
  192. package/dist/server/src/services/extensions/utils.d.ts +14 -0
  193. package/dist/server/src/services/extensions/utils.d.ts.map +1 -0
  194. package/dist/server/src/services/file.d.ts +8 -0
  195. package/dist/server/src/services/file.d.ts.map +1 -0
  196. package/dist/server/src/services/folder.d.ts +32 -0
  197. package/dist/server/src/services/folder.d.ts.map +1 -0
  198. package/dist/server/src/services/image-manipulation.d.ts +63 -0
  199. package/dist/server/src/services/image-manipulation.d.ts.map +1 -0
  200. package/dist/server/src/services/index.d.ts +196 -0
  201. package/dist/server/src/services/index.d.ts.map +1 -0
  202. package/dist/server/src/services/metrics.d.ts +8 -0
  203. package/dist/server/src/services/metrics.d.ts.map +1 -0
  204. package/dist/server/src/services/provider.d.ts +10 -0
  205. package/dist/server/src/services/provider.d.ts.map +1 -0
  206. package/dist/server/src/services/upload.d.ts +67 -0
  207. package/dist/server/src/services/upload.d.ts.map +1 -0
  208. package/dist/server/src/services/weekly-metrics.d.ts +17 -0
  209. package/dist/server/src/services/weekly-metrics.d.ts.map +1 -0
  210. package/dist/server/src/types.d.ts +74 -0
  211. package/dist/server/src/types.d.ts.map +1 -0
  212. package/dist/server/src/utils/cron.d.ts +3 -0
  213. package/dist/server/src/utils/cron.d.ts.map +1 -0
  214. package/dist/server/src/utils/index.d.ts +23 -0
  215. package/dist/server/src/utils/index.d.ts.map +1 -0
  216. package/package.json +99 -7
  217. package/strapi-server.js +3 -0
@@ -0,0 +1,2622 @@
1
+ import _ from "lodash";
2
+ import utils, { errors, file as file$2, contentTypes as contentTypes$1, strings, sanitize, setCreatorFields, async, yup, validateYupSchema } from "@strapi/utils";
3
+ import range from "koa-range";
4
+ import koaStatic from "koa-static";
5
+ import { isFunction, map, isUndefined, sortBy, cloneDeep, defaultTo, get, isNil, merge, defaultsDeep, isEmpty, intersection } from "lodash/fp";
6
+ import os from "os";
7
+ import path, { join } from "path";
8
+ import crypto from "crypto";
9
+ import fs from "fs";
10
+ import fse from "fs-extra";
11
+ import { extension } from "mime-types";
12
+ import sharp from "sharp";
13
+ import { add } from "date-fns";
14
+ const registerUploadMiddleware = ({ strapi: strapi2 }) => {
15
+ strapi2.server.app.on("error", (err) => {
16
+ if (err.code === "EPIPE") {
17
+ return;
18
+ }
19
+ strapi2.server.app.onerror(err);
20
+ });
21
+ const localServerConfig = strapi2.config.get("plugin::upload.providerOptions.localServer", {});
22
+ strapi2.server.routes([
23
+ {
24
+ method: "GET",
25
+ path: "/uploads/(.*)",
26
+ handler: [range, koaStatic(strapi2.dirs.static.public, { defer: true, ...localServerConfig })],
27
+ config: { auth: false }
28
+ }
29
+ ]);
30
+ };
31
+ const paths = {
32
+ "/upload": {
33
+ post: {
34
+ description: "Upload files",
35
+ responses: {
36
+ "200": {
37
+ description: "response",
38
+ content: {
39
+ "application/json": {
40
+ schema: {
41
+ type: "array",
42
+ items: {
43
+ $ref: "#/components/schemas/UploadFile"
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+ },
50
+ summary: "",
51
+ tags: [
52
+ "Upload - File"
53
+ ],
54
+ requestBody: {
55
+ description: "Upload files",
56
+ required: true,
57
+ content: {
58
+ "multipart/form-data": {
59
+ schema: {
60
+ required: [
61
+ "files"
62
+ ],
63
+ type: "object",
64
+ properties: {
65
+ path: {
66
+ type: "string",
67
+ description: "The folder where the file(s) will be uploaded to (only supported on strapi-provider-upload-aws-s3)."
68
+ },
69
+ refId: {
70
+ type: "string",
71
+ description: "The ID of the entry which the file(s) will be linked to"
72
+ },
73
+ ref: {
74
+ type: "string",
75
+ description: "The unique ID (uid) of the model which the file(s) will be linked to (api::restaurant.restaurant)."
76
+ },
77
+ field: {
78
+ type: "string",
79
+ description: "The field of the entry which the file(s) will be precisely linked to."
80
+ },
81
+ files: {
82
+ type: "array",
83
+ items: {
84
+ type: "string",
85
+ format: "binary"
86
+ }
87
+ }
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+ }
94
+ },
95
+ "/upload?id={id}": {
96
+ post: {
97
+ parameters: [
98
+ {
99
+ name: "id",
100
+ "in": "query",
101
+ description: "File id",
102
+ required: true,
103
+ schema: {
104
+ type: "string"
105
+ }
106
+ }
107
+ ],
108
+ description: "Upload file information",
109
+ responses: {
110
+ "200": {
111
+ description: "response",
112
+ content: {
113
+ "application/json": {
114
+ schema: {
115
+ type: "array",
116
+ items: {
117
+ $ref: "#/components/schemas/UploadFile"
118
+ }
119
+ }
120
+ }
121
+ }
122
+ }
123
+ },
124
+ summary: "",
125
+ tags: [
126
+ "Upload - File"
127
+ ],
128
+ requestBody: {
129
+ description: "Upload files",
130
+ required: true,
131
+ content: {
132
+ "multipart/form-data": {
133
+ schema: {
134
+ type: "object",
135
+ properties: {
136
+ fileInfo: {
137
+ type: "object",
138
+ properties: {
139
+ name: {
140
+ type: "string"
141
+ },
142
+ alternativeText: {
143
+ type: "string"
144
+ },
145
+ caption: {
146
+ type: "string"
147
+ }
148
+ }
149
+ },
150
+ files: {
151
+ type: "string",
152
+ format: "binary"
153
+ }
154
+ }
155
+ }
156
+ }
157
+ }
158
+ }
159
+ }
160
+ },
161
+ "/upload/files": {
162
+ get: {
163
+ tags: [
164
+ "Upload - File"
165
+ ],
166
+ responses: {
167
+ "200": {
168
+ description: "Get a list of files",
169
+ content: {
170
+ "application/json": {
171
+ schema: {
172
+ type: "array",
173
+ items: {
174
+ $ref: "#/components/schemas/UploadFile"
175
+ }
176
+ }
177
+ }
178
+ }
179
+ }
180
+ }
181
+ }
182
+ },
183
+ "/upload/files/{id}": {
184
+ get: {
185
+ parameters: [
186
+ {
187
+ name: "id",
188
+ "in": "path",
189
+ description: "",
190
+ deprecated: false,
191
+ required: true,
192
+ schema: {
193
+ type: "string"
194
+ }
195
+ }
196
+ ],
197
+ tags: [
198
+ "Upload - File"
199
+ ],
200
+ responses: {
201
+ "200": {
202
+ description: "Get a specific file",
203
+ content: {
204
+ "application/json": {
205
+ schema: {
206
+ $ref: "#/components/schemas/UploadFile"
207
+ }
208
+ }
209
+ }
210
+ }
211
+ }
212
+ },
213
+ "delete": {
214
+ parameters: [
215
+ {
216
+ name: "id",
217
+ "in": "path",
218
+ description: "",
219
+ deprecated: false,
220
+ required: true,
221
+ schema: {
222
+ type: "string"
223
+ }
224
+ }
225
+ ],
226
+ tags: [
227
+ "Upload - File"
228
+ ],
229
+ responses: {
230
+ "200": {
231
+ description: "Delete a file",
232
+ content: {
233
+ "application/json": {
234
+ schema: {
235
+ $ref: "#/components/schemas/UploadFile"
236
+ }
237
+ }
238
+ }
239
+ }
240
+ }
241
+ }
242
+ }
243
+ };
244
+ const components = {
245
+ schemas: {
246
+ UploadFile: {
247
+ properties: {
248
+ id: {
249
+ type: "number"
250
+ },
251
+ name: {
252
+ type: "string"
253
+ },
254
+ alternativeText: {
255
+ type: "string"
256
+ },
257
+ caption: {
258
+ type: "string"
259
+ },
260
+ width: {
261
+ type: "number",
262
+ format: "integer"
263
+ },
264
+ height: {
265
+ type: "number",
266
+ format: "integer"
267
+ },
268
+ formats: {
269
+ type: "number"
270
+ },
271
+ hash: {
272
+ type: "string"
273
+ },
274
+ ext: {
275
+ type: "string"
276
+ },
277
+ mime: {
278
+ type: "string"
279
+ },
280
+ size: {
281
+ type: "number",
282
+ format: "double"
283
+ },
284
+ url: {
285
+ type: "string"
286
+ },
287
+ previewUrl: {
288
+ type: "string"
289
+ },
290
+ provider: {
291
+ type: "string"
292
+ },
293
+ provider_metadata: {
294
+ type: "object"
295
+ },
296
+ createdAt: {
297
+ type: "string",
298
+ format: "date-time"
299
+ },
300
+ updatedAt: {
301
+ type: "string",
302
+ format: "date-time"
303
+ }
304
+ }
305
+ }
306
+ }
307
+ };
308
+ const spec = {
309
+ paths,
310
+ components
311
+ };
312
+ const { PayloadTooLargeError } = errors;
313
+ const { bytesToHumanReadable, kbytesToBytes } = file$2;
314
+ async function register({ strapi: strapi2 }) {
315
+ strapi2.plugin("upload").provider = createProvider(strapi2.config.get("plugin::upload"));
316
+ await registerUploadMiddleware({ strapi: strapi2 });
317
+ if (strapi2.plugin("graphql")) {
318
+ const { installGraphqlExtension } = await import("./graphql-BVCZSNa2.mjs");
319
+ installGraphqlExtension({ strapi: strapi2 });
320
+ }
321
+ if (strapi2.plugin("documentation")) {
322
+ strapi2.plugin("documentation").service("override").registerOverride(spec, {
323
+ pluginOrigin: "upload",
324
+ excludeFromGeneration: ["upload"]
325
+ });
326
+ }
327
+ }
328
+ const createProvider = (config2) => {
329
+ const { providerOptions, actionOptions = {} } = config2;
330
+ const providerName = _.toLower(config2.provider);
331
+ let provider2;
332
+ let modulePath;
333
+ try {
334
+ modulePath = require.resolve(`@strapi/provider-upload-${providerName}`);
335
+ } catch (error) {
336
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "MODULE_NOT_FOUND") {
337
+ modulePath = providerName;
338
+ } else {
339
+ throw error;
340
+ }
341
+ }
342
+ try {
343
+ provider2 = require(modulePath);
344
+ } catch (err) {
345
+ const newError = new Error(`Could not load upload provider "${providerName}".`);
346
+ if (err instanceof Error) {
347
+ newError.stack = err.stack;
348
+ }
349
+ throw newError;
350
+ }
351
+ const providerInstance = provider2.init(providerOptions);
352
+ if (!providerInstance.delete) {
353
+ throw new Error(`The upload provider "${providerName}" doesn't implement the delete method.`);
354
+ }
355
+ if (!providerInstance.upload && !providerInstance.uploadStream) {
356
+ throw new Error(
357
+ `The upload provider "${providerName}" doesn't implement the uploadStream nor the upload method.`
358
+ );
359
+ }
360
+ if (!providerInstance.uploadStream) {
361
+ process.emitWarning(
362
+ `The upload provider "${providerName}" doesn't implement the uploadStream function. Strapi will fallback on the upload method. Some performance issues may occur.`
363
+ );
364
+ }
365
+ const wrappedProvider = _.mapValues(providerInstance, (method, methodName) => {
366
+ return async (file2, options = actionOptions[methodName]) => providerInstance[methodName](file2, options);
367
+ });
368
+ return Object.assign(Object.create(baseProvider), wrappedProvider);
369
+ };
370
+ const baseProvider = {
371
+ extend(obj) {
372
+ Object.assign(this, obj);
373
+ },
374
+ checkFileSize(file2, { sizeLimit }) {
375
+ if (sizeLimit && kbytesToBytes(file2.size) > sizeLimit) {
376
+ throw new PayloadTooLargeError(
377
+ `${file2.originalFilename} exceeds size limit of ${bytesToHumanReadable(sizeLimit)}.`
378
+ );
379
+ }
380
+ },
381
+ getSignedUrl(file2) {
382
+ return file2;
383
+ },
384
+ isPrivate() {
385
+ return false;
386
+ }
387
+ };
388
+ const getService = (name) => {
389
+ return strapi.plugin("upload").service(name);
390
+ };
391
+ const ACTIONS = {
392
+ read: "plugin::upload.read",
393
+ readSettings: "plugin::upload.settings.read",
394
+ create: "plugin::upload.assets.create",
395
+ update: "plugin::upload.assets.update",
396
+ download: "plugin::upload.assets.download",
397
+ copyLink: "plugin::upload.assets.copy-link",
398
+ configureView: "plugin::upload.configure-view"
399
+ };
400
+ const ALLOWED_SORT_STRINGS = [
401
+ "createdAt:DESC",
402
+ "createdAt:ASC",
403
+ "name:ASC",
404
+ "name:DESC",
405
+ "updatedAt:DESC",
406
+ "updatedAt:ASC"
407
+ ];
408
+ const ALLOWED_WEBHOOK_EVENTS = {
409
+ MEDIA_CREATE: "media.create",
410
+ MEDIA_UPDATE: "media.update",
411
+ MEDIA_DELETE: "media.delete"
412
+ };
413
+ const FOLDER_MODEL_UID = "plugin::upload.folder";
414
+ const FILE_MODEL_UID = "plugin::upload.file";
415
+ const API_UPLOAD_FOLDER_BASE_NAME = "API Uploads";
416
+ async function bootstrap({ strapi: strapi2 }) {
417
+ const defaultConfig = {
418
+ settings: {
419
+ sizeOptimization: true,
420
+ responsiveDimensions: true,
421
+ autoOrientation: false
422
+ },
423
+ view_configuration: {
424
+ pageSize: 10,
425
+ sort: ALLOWED_SORT_STRINGS[0]
426
+ }
427
+ };
428
+ for (const [key, defaultValue] of Object.entries(defaultConfig)) {
429
+ const configurator = strapi2.store({ type: "plugin", name: "upload", key });
430
+ const config2 = await configurator.get({});
431
+ if (config2 && Object.keys(defaultValue).every((key2) => Object.prototype.hasOwnProperty.call(config2, key2))) {
432
+ continue;
433
+ }
434
+ await configurator.set({
435
+ value: Object.assign(defaultValue, config2 || {})
436
+ });
437
+ }
438
+ await registerPermissionActions();
439
+ await registerWebhookEvents();
440
+ await getService("weeklyMetrics").registerCron();
441
+ getService("metrics").sendUploadPluginMetrics();
442
+ if (strapi2.config.get("plugin::upload.signAdminURLsOnly", false)) {
443
+ getService("extensions").contentManager.entityManager.addSignedFileUrlsToAdmin();
444
+ } else {
445
+ getService("extensions").core.entityService.addSignedFileUrlsToEntityService();
446
+ }
447
+ }
448
+ const registerWebhookEvents = async () => Object.entries(ALLOWED_WEBHOOK_EVENTS).forEach(([key, value]) => {
449
+ strapi.get("webhookStore").addAllowedEvent(key, value);
450
+ });
451
+ const registerPermissionActions = async () => {
452
+ const actions = [
453
+ {
454
+ section: "plugins",
455
+ displayName: "Access the Media Library",
456
+ uid: "read",
457
+ pluginName: "upload"
458
+ },
459
+ {
460
+ section: "plugins",
461
+ displayName: "Create (upload)",
462
+ uid: "assets.create",
463
+ subCategory: "assets",
464
+ pluginName: "upload"
465
+ },
466
+ {
467
+ section: "plugins",
468
+ displayName: "Update (crop, details, replace) + delete",
469
+ uid: "assets.update",
470
+ subCategory: "assets",
471
+ pluginName: "upload"
472
+ },
473
+ {
474
+ section: "plugins",
475
+ displayName: "Download",
476
+ uid: "assets.download",
477
+ subCategory: "assets",
478
+ pluginName: "upload"
479
+ },
480
+ {
481
+ section: "plugins",
482
+ displayName: "Copy link",
483
+ uid: "assets.copy-link",
484
+ subCategory: "assets",
485
+ pluginName: "upload"
486
+ },
487
+ {
488
+ section: "plugins",
489
+ displayName: "Configure view",
490
+ uid: "configure-view",
491
+ pluginName: "upload"
492
+ },
493
+ {
494
+ section: "settings",
495
+ displayName: "Access the Media Library settings page",
496
+ uid: "settings.read",
497
+ category: "media library",
498
+ pluginName: "upload"
499
+ }
500
+ ];
501
+ await strapi.service("admin::permission").actionProvider.registerMany(actions);
502
+ };
503
+ const file$1 = {
504
+ schema: {
505
+ collectionName: "files",
506
+ info: {
507
+ singularName: "file",
508
+ pluralName: "files",
509
+ displayName: "File",
510
+ description: ""
511
+ },
512
+ options: {},
513
+ pluginOptions: {
514
+ "content-manager": {
515
+ visible: false
516
+ },
517
+ "content-type-builder": {
518
+ visible: false
519
+ }
520
+ },
521
+ attributes: {
522
+ name: {
523
+ type: "string",
524
+ configurable: false,
525
+ required: true
526
+ },
527
+ alternativeText: {
528
+ type: "string",
529
+ configurable: false
530
+ },
531
+ caption: {
532
+ type: "string",
533
+ configurable: false
534
+ },
535
+ width: {
536
+ type: "integer",
537
+ configurable: false
538
+ },
539
+ height: {
540
+ type: "integer",
541
+ configurable: false
542
+ },
543
+ formats: {
544
+ type: "json",
545
+ configurable: false
546
+ },
547
+ hash: {
548
+ type: "string",
549
+ configurable: false,
550
+ required: true
551
+ },
552
+ ext: {
553
+ type: "string",
554
+ configurable: false
555
+ },
556
+ mime: {
557
+ type: "string",
558
+ configurable: false,
559
+ required: true
560
+ },
561
+ size: {
562
+ type: "decimal",
563
+ configurable: false,
564
+ required: true
565
+ },
566
+ url: {
567
+ type: "string",
568
+ configurable: false,
569
+ required: true
570
+ },
571
+ previewUrl: {
572
+ type: "string",
573
+ configurable: false
574
+ },
575
+ provider: {
576
+ type: "string",
577
+ configurable: false,
578
+ required: true
579
+ },
580
+ provider_metadata: {
581
+ type: "json",
582
+ configurable: false
583
+ },
584
+ related: {
585
+ type: "relation",
586
+ relation: "morphToMany",
587
+ configurable: false
588
+ },
589
+ folder: {
590
+ type: "relation",
591
+ relation: "manyToOne",
592
+ target: FOLDER_MODEL_UID,
593
+ inversedBy: "files",
594
+ private: true
595
+ },
596
+ folderPath: {
597
+ type: "string",
598
+ minLength: 1,
599
+ required: true,
600
+ private: true,
601
+ searchable: false
602
+ }
603
+ },
604
+ // experimental feature:
605
+ indexes: [
606
+ {
607
+ name: "upload_files_folder_path_index",
608
+ columns: ["folder_path"],
609
+ type: null
610
+ },
611
+ {
612
+ name: `upload_files_created_at_index`,
613
+ columns: ["created_at"],
614
+ type: null
615
+ },
616
+ {
617
+ name: `upload_files_updated_at_index`,
618
+ columns: ["updated_at"],
619
+ type: null
620
+ },
621
+ {
622
+ name: `upload_files_name_index`,
623
+ columns: ["name"],
624
+ type: null
625
+ },
626
+ {
627
+ name: `upload_files_size_index`,
628
+ columns: ["size"],
629
+ type: null
630
+ },
631
+ {
632
+ name: `upload_files_ext_index`,
633
+ columns: ["ext"],
634
+ type: null
635
+ }
636
+ ]
637
+ }
638
+ };
639
+ const folder$1 = {
640
+ schema: {
641
+ collectionName: "upload_folders",
642
+ info: {
643
+ singularName: "folder",
644
+ pluralName: "folders",
645
+ displayName: "Folder"
646
+ },
647
+ options: {},
648
+ pluginOptions: {
649
+ "content-manager": {
650
+ visible: false
651
+ },
652
+ "content-type-builder": {
653
+ visible: false
654
+ }
655
+ },
656
+ attributes: {
657
+ name: {
658
+ type: "string",
659
+ minLength: 1,
660
+ required: true
661
+ },
662
+ pathId: {
663
+ type: "integer",
664
+ unique: true,
665
+ required: true
666
+ },
667
+ parent: {
668
+ type: "relation",
669
+ relation: "manyToOne",
670
+ target: FOLDER_MODEL_UID,
671
+ inversedBy: "children"
672
+ },
673
+ children: {
674
+ type: "relation",
675
+ relation: "oneToMany",
676
+ target: FOLDER_MODEL_UID,
677
+ mappedBy: "parent"
678
+ },
679
+ files: {
680
+ type: "relation",
681
+ relation: "oneToMany",
682
+ target: FILE_MODEL_UID,
683
+ mappedBy: "folder"
684
+ },
685
+ path: {
686
+ type: "string",
687
+ minLength: 1,
688
+ required: true
689
+ }
690
+ },
691
+ // experimental feature:
692
+ indexes: [
693
+ {
694
+ name: "upload_folders_path_id_index",
695
+ columns: ["path_id"],
696
+ type: "unique"
697
+ },
698
+ {
699
+ name: "upload_folders_path_index",
700
+ columns: ["path"],
701
+ type: "unique"
702
+ }
703
+ ]
704
+ }
705
+ };
706
+ const contentTypes = {
707
+ file: file$1,
708
+ folder: folder$1
709
+ };
710
+ const provider = ({ strapi: strapi2 }) => ({
711
+ async checkFileSize(file2) {
712
+ const { sizeLimit } = strapi2.config.get("plugin::upload");
713
+ await strapi2.plugin("upload").provider.checkFileSize(file2, { sizeLimit });
714
+ },
715
+ async upload(file2) {
716
+ if (isFunction(strapi2.plugin("upload").provider.uploadStream)) {
717
+ file2.stream = file2.getStream();
718
+ await strapi2.plugin("upload").provider.uploadStream(file2);
719
+ delete file2.stream;
720
+ } else {
721
+ file2.buffer = await file$2.streamToBuffer(file2.getStream());
722
+ await strapi2.plugin("upload").provider.upload(file2);
723
+ delete file2.buffer;
724
+ }
725
+ }
726
+ });
727
+ const { UPDATED_BY_ATTRIBUTE, CREATED_BY_ATTRIBUTE } = contentTypes$1.constants;
728
+ const { MEDIA_CREATE, MEDIA_UPDATE, MEDIA_DELETE } = ALLOWED_WEBHOOK_EVENTS;
729
+ const { ApplicationError, NotFoundError } = errors;
730
+ const { bytesToKbytes: bytesToKbytes$1 } = file$2;
731
+ const upload = ({ strapi: strapi2 }) => {
732
+ const randomSuffix = () => crypto.randomBytes(5).toString("hex");
733
+ const generateFileName = (name) => {
734
+ const baseName = strings.nameToSlug(name, { separator: "_", lowercase: false });
735
+ return `${baseName}_${randomSuffix()}`;
736
+ };
737
+ const sendMediaMetrics = (data) => {
738
+ if (_.has(data, "caption") && !_.isEmpty(data.caption)) {
739
+ strapi2.telemetry.send("didSaveMediaWithCaption");
740
+ }
741
+ if (_.has(data, "alternativeText") && !_.isEmpty(data.alternativeText)) {
742
+ strapi2.telemetry.send("didSaveMediaWithAlternativeText");
743
+ }
744
+ };
745
+ const createAndAssignTmpWorkingDirectoryToFiles = async (files) => {
746
+ const tmpWorkingDirectory = await fse.mkdtemp(path.join(os.tmpdir(), "strapi-upload-"));
747
+ if (Array.isArray(files)) {
748
+ files.forEach((file2) => {
749
+ file2.tmpWorkingDirectory = tmpWorkingDirectory;
750
+ });
751
+ } else {
752
+ files.tmpWorkingDirectory = tmpWorkingDirectory;
753
+ }
754
+ return tmpWorkingDirectory;
755
+ };
756
+ function filenameReservedRegex() {
757
+ return /[<>:"/\\|?*\u0000-\u001F]/g;
758
+ }
759
+ function windowsReservedNameRegex() {
760
+ return /^(con|prn|aux|nul|com\d|lpt\d)$/i;
761
+ }
762
+ function isValidFilename(string) {
763
+ if (!string || string.length > 255) {
764
+ return false;
765
+ }
766
+ if (filenameReservedRegex().test(string) || windowsReservedNameRegex().test(string)) {
767
+ return false;
768
+ }
769
+ if (string === "." || string === "..") {
770
+ return false;
771
+ }
772
+ return true;
773
+ }
774
+ async function emitEvent(event, data) {
775
+ const modelDef = strapi2.getModel(FILE_MODEL_UID);
776
+ const sanitizedData = await sanitize.sanitizers.defaultSanitizeOutput(
777
+ {
778
+ schema: modelDef,
779
+ getModel(uid) {
780
+ return strapi2.getModel(uid);
781
+ }
782
+ },
783
+ data
784
+ );
785
+ strapi2.eventHub.emit(event, { media: sanitizedData });
786
+ }
787
+ async function formatFileInfo({ filename, type, size }, fileInfo = {}, metas = {}) {
788
+ const fileService = getService("file");
789
+ if (!isValidFilename(filename)) {
790
+ throw new ApplicationError("File name contains invalid characters");
791
+ }
792
+ let ext = path.extname(filename);
793
+ if (!ext) {
794
+ ext = `.${extension(type)}`;
795
+ }
796
+ const usedName = (fileInfo.name || filename).normalize();
797
+ const basename = path.basename(usedName, ext);
798
+ if (!isValidFilename(filename)) {
799
+ throw new ApplicationError("File name contains invalid characters");
800
+ }
801
+ const entity = {
802
+ name: usedName,
803
+ alternativeText: fileInfo.alternativeText,
804
+ caption: fileInfo.caption,
805
+ folder: fileInfo.folder,
806
+ folderPath: await fileService.getFolderPath(fileInfo.folder),
807
+ hash: generateFileName(basename),
808
+ ext,
809
+ mime: type,
810
+ size: bytesToKbytes$1(size),
811
+ sizeInBytes: size
812
+ };
813
+ const { refId, ref, field } = metas;
814
+ if (refId && ref && field) {
815
+ entity.related = [
816
+ {
817
+ id: refId,
818
+ __type: ref,
819
+ __pivot: { field }
820
+ }
821
+ ];
822
+ }
823
+ if (metas.path) {
824
+ entity.path = metas.path;
825
+ }
826
+ if (metas.tmpWorkingDirectory) {
827
+ entity.tmpWorkingDirectory = metas.tmpWorkingDirectory;
828
+ }
829
+ return entity;
830
+ }
831
+ async function enhanceAndValidateFile(file2, fileInfo, metas) {
832
+ const currentFile = await formatFileInfo(
833
+ {
834
+ filename: file2.originalFilename ?? "unamed",
835
+ type: file2.mimetype ?? "application/octet-stream",
836
+ size: file2.size
837
+ },
838
+ fileInfo,
839
+ {
840
+ ...metas,
841
+ tmpWorkingDirectory: file2.tmpWorkingDirectory
842
+ }
843
+ );
844
+ currentFile.getStream = () => fs.createReadStream(file2.filepath);
845
+ const { optimize: optimize2, isImage: isImage2, isFaultyImage: isFaultyImage2, isOptimizableImage: isOptimizableImage2 } = strapi2.plugin("upload").service("image-manipulation");
846
+ if (await isImage2(currentFile)) {
847
+ if (await isFaultyImage2(currentFile)) {
848
+ throw new ApplicationError("File is not a valid image");
849
+ }
850
+ if (await isOptimizableImage2(currentFile)) {
851
+ return optimize2(currentFile);
852
+ }
853
+ }
854
+ return currentFile;
855
+ }
856
+ async function upload2({
857
+ data,
858
+ files
859
+ }, opts) {
860
+ const { user } = opts ?? {};
861
+ const tmpWorkingDirectory = await createAndAssignTmpWorkingDirectoryToFiles(files);
862
+ let uploadedFiles = [];
863
+ try {
864
+ const { fileInfo, ...metas } = data;
865
+ const fileArray = Array.isArray(files) ? files : [files];
866
+ const fileInfoArray = Array.isArray(fileInfo) ? fileInfo : [fileInfo];
867
+ const doUpload = async (file2, fileInfo2) => {
868
+ const fileData = await enhanceAndValidateFile(file2, fileInfo2, metas);
869
+ return uploadFileAndPersist(fileData, { user });
870
+ };
871
+ uploadedFiles = await Promise.all(
872
+ fileArray.map((file2, idx) => doUpload(file2, fileInfoArray[idx] || {}))
873
+ );
874
+ } finally {
875
+ await fse.remove(tmpWorkingDirectory);
876
+ }
877
+ return uploadedFiles;
878
+ }
879
+ async function uploadImage(fileData) {
880
+ const { getDimensions: getDimensions2, generateThumbnail: generateThumbnail2, generateResponsiveFormats: generateResponsiveFormats2, isResizableImage: isResizableImage2 } = getService("image-manipulation");
881
+ const { width, height } = await getDimensions2(fileData);
882
+ _.assign(fileData, {
883
+ width,
884
+ height
885
+ });
886
+ const uploadThumbnail = async (thumbnailFile) => {
887
+ await getService("provider").upload(thumbnailFile);
888
+ _.set(fileData, "formats.thumbnail", thumbnailFile);
889
+ };
890
+ const uploadResponsiveFormat = async (format) => {
891
+ const { key, file: file2 } = format;
892
+ await getService("provider").upload(file2);
893
+ _.set(fileData, ["formats", key], file2);
894
+ };
895
+ const uploadPromises = [];
896
+ uploadPromises.push(getService("provider").upload(fileData));
897
+ if (await isResizableImage2(fileData)) {
898
+ const thumbnailFile = await generateThumbnail2(fileData);
899
+ if (thumbnailFile) {
900
+ uploadPromises.push(uploadThumbnail(thumbnailFile));
901
+ }
902
+ const formats = await generateResponsiveFormats2(fileData);
903
+ if (Array.isArray(formats) && formats.length > 0) {
904
+ for (const format of formats) {
905
+ if (!format)
906
+ continue;
907
+ uploadPromises.push(uploadResponsiveFormat(format));
908
+ }
909
+ }
910
+ }
911
+ await Promise.all(uploadPromises);
912
+ }
913
+ async function uploadFileAndPersist(fileData, opts) {
914
+ const { user } = opts ?? {};
915
+ const config2 = strapi2.config.get("plugin::upload");
916
+ const { isImage: isImage2 } = getService("image-manipulation");
917
+ await getService("provider").checkFileSize(fileData);
918
+ if (await isImage2(fileData)) {
919
+ await uploadImage(fileData);
920
+ } else {
921
+ await getService("provider").upload(fileData);
922
+ }
923
+ _.set(fileData, "provider", config2.provider);
924
+ return add2(fileData, { user });
925
+ }
926
+ async function updateFileInfo(id, { name, alternativeText, caption, folder: folder2 }, opts) {
927
+ const { user } = opts ?? {};
928
+ const dbFile = await findOne(id);
929
+ if (!dbFile) {
930
+ throw new NotFoundError();
931
+ }
932
+ const fileService = getService("file");
933
+ const newName = _.isNil(name) ? dbFile.name : name;
934
+ const newInfos = {
935
+ name: newName,
936
+ alternativeText: _.isNil(alternativeText) ? dbFile.alternativeText : alternativeText,
937
+ caption: _.isNil(caption) ? dbFile.caption : caption,
938
+ folder: _.isUndefined(folder2) ? dbFile.folder : folder2,
939
+ folderPath: _.isUndefined(folder2) ? dbFile.path : await fileService.getFolderPath(folder2)
940
+ };
941
+ return update2(id, newInfos, { user });
942
+ }
943
+ async function replace(id, { data, file: file2 }, opts) {
944
+ const { user } = opts ?? {};
945
+ const config2 = strapi2.config.get("plugin::upload");
946
+ const { isImage: isImage2 } = getService("image-manipulation");
947
+ const dbFile = await findOne(id);
948
+ if (!dbFile) {
949
+ throw new NotFoundError();
950
+ }
951
+ const tmpWorkingDirectory = await createAndAssignTmpWorkingDirectoryToFiles(file2);
952
+ let fileData;
953
+ try {
954
+ const { fileInfo } = data;
955
+ fileData = await enhanceAndValidateFile(file2, fileInfo);
956
+ _.assign(fileData, {
957
+ hash: dbFile.hash,
958
+ ext: dbFile.ext
959
+ });
960
+ if (dbFile.provider === config2.provider) {
961
+ await strapi2.plugin("upload").provider.delete(dbFile);
962
+ if (dbFile.formats) {
963
+ await Promise.all(
964
+ Object.keys(dbFile.formats).map((key) => {
965
+ return strapi2.plugin("upload").provider.delete(dbFile.formats[key]);
966
+ })
967
+ );
968
+ }
969
+ }
970
+ _.set(fileData, "formats", {});
971
+ if (await isImage2(fileData)) {
972
+ await uploadImage(fileData);
973
+ } else {
974
+ await getService("provider").upload(fileData);
975
+ }
976
+ _.set(fileData, "provider", config2.provider);
977
+ } finally {
978
+ await fse.remove(tmpWorkingDirectory);
979
+ }
980
+ return update2(id, fileData, { user });
981
+ }
982
+ async function update2(id, values, opts) {
983
+ const { user } = opts ?? {};
984
+ const fileValues = { ...values };
985
+ if (user) {
986
+ Object.assign(fileValues, {
987
+ [UPDATED_BY_ATTRIBUTE]: user.id
988
+ });
989
+ }
990
+ sendMediaMetrics(fileValues);
991
+ const res = await strapi2.db.query(FILE_MODEL_UID).update({ where: { id }, data: fileValues });
992
+ await emitEvent(MEDIA_UPDATE, res);
993
+ return res;
994
+ }
995
+ async function add2(values, opts) {
996
+ const { user } = opts ?? {};
997
+ const fileValues = { ...values };
998
+ if (user) {
999
+ Object.assign(fileValues, {
1000
+ [UPDATED_BY_ATTRIBUTE]: user.id,
1001
+ [CREATED_BY_ATTRIBUTE]: user.id
1002
+ });
1003
+ }
1004
+ sendMediaMetrics(fileValues);
1005
+ const res = await strapi2.db.query(FILE_MODEL_UID).create({ data: fileValues });
1006
+ await emitEvent(MEDIA_CREATE, res);
1007
+ return res;
1008
+ }
1009
+ function findOne(id, populate = {}) {
1010
+ const query = strapi2.get("query-params").transform(FILE_MODEL_UID, {
1011
+ populate
1012
+ });
1013
+ return strapi2.db.query(FILE_MODEL_UID).findOne({
1014
+ where: { id },
1015
+ ...query
1016
+ });
1017
+ }
1018
+ function findMany(query = {}) {
1019
+ return strapi2.db.query(FILE_MODEL_UID).findMany(strapi2.get("query-params").transform(FILE_MODEL_UID, query));
1020
+ }
1021
+ function findPage(query = {}) {
1022
+ return strapi2.db.query(FILE_MODEL_UID).findPage(strapi2.get("query-params").transform(FILE_MODEL_UID, query));
1023
+ }
1024
+ async function remove(file2) {
1025
+ const config2 = strapi2.config.get("plugin::upload");
1026
+ if (file2.provider === config2.provider) {
1027
+ await strapi2.plugin("upload").provider.delete(file2);
1028
+ if (file2.formats) {
1029
+ const keys = Object.keys(file2.formats);
1030
+ await Promise.all(
1031
+ keys.map((key) => {
1032
+ return strapi2.plugin("upload").provider.delete(file2.formats[key]);
1033
+ })
1034
+ );
1035
+ }
1036
+ }
1037
+ const media = await strapi2.db.query(FILE_MODEL_UID).findOne({
1038
+ where: { id: file2.id }
1039
+ });
1040
+ await emitEvent(MEDIA_DELETE, media);
1041
+ return strapi2.db.query(FILE_MODEL_UID).delete({ where: { id: file2.id } });
1042
+ }
1043
+ async function getSettings() {
1044
+ const res = await strapi2.store({ type: "plugin", name: "upload", key: "settings" }).get({});
1045
+ return res;
1046
+ }
1047
+ function setSettings(value) {
1048
+ if (value.responsiveDimensions === true) {
1049
+ strapi2.telemetry.send("didEnableResponsiveDimensions");
1050
+ } else {
1051
+ strapi2.telemetry.send("didDisableResponsiveDimensions");
1052
+ }
1053
+ return strapi2.store({ type: "plugin", name: "upload", key: "settings" }).set({ value });
1054
+ }
1055
+ async function getConfiguration() {
1056
+ const res = await strapi2.store({
1057
+ type: "plugin",
1058
+ name: "upload",
1059
+ key: "view_configuration"
1060
+ }).get({});
1061
+ return res;
1062
+ }
1063
+ function setConfiguration(value) {
1064
+ return strapi2.store({ type: "plugin", name: "upload", key: "view_configuration" }).set({
1065
+ value
1066
+ });
1067
+ }
1068
+ return {
1069
+ formatFileInfo,
1070
+ upload: upload2,
1071
+ updateFileInfo,
1072
+ replace,
1073
+ findOne,
1074
+ findMany,
1075
+ findPage,
1076
+ remove,
1077
+ getSettings,
1078
+ setSettings,
1079
+ getConfiguration,
1080
+ setConfiguration,
1081
+ /**
1082
+ * exposed for testing only
1083
+ * @internal
1084
+ */
1085
+ _uploadImage: uploadImage
1086
+ };
1087
+ };
1088
+ const { bytesToKbytes, writableDiscardStream } = file$2;
1089
+ const FORMATS_TO_RESIZE = ["jpeg", "png", "webp", "tiff", "gif"];
1090
+ const FORMATS_TO_PROCESS = ["jpeg", "png", "webp", "tiff", "svg", "gif", "avif"];
1091
+ const FORMATS_TO_OPTIMIZE = ["jpeg", "png", "webp", "tiff", "avif"];
1092
+ const isOptimizableFormat = (format) => format !== void 0 && FORMATS_TO_OPTIMIZE.includes(format);
1093
+ const writeStreamToFile = (stream, path2) => new Promise((resolve, reject) => {
1094
+ const writeStream = fs.createWriteStream(path2);
1095
+ stream.on("error", reject);
1096
+ stream.pipe(writeStream);
1097
+ writeStream.on("close", resolve);
1098
+ writeStream.on("error", reject);
1099
+ });
1100
+ const getMetadata = (file2) => new Promise((resolve, reject) => {
1101
+ const pipeline = sharp();
1102
+ pipeline.metadata().then(resolve).catch(reject);
1103
+ file2.getStream().pipe(pipeline);
1104
+ });
1105
+ const getDimensions = async (file2) => {
1106
+ const { width = null, height = null } = await getMetadata(file2);
1107
+ return { width, height };
1108
+ };
1109
+ const THUMBNAIL_RESIZE_OPTIONS = {
1110
+ width: 245,
1111
+ height: 156,
1112
+ fit: "inside"
1113
+ };
1114
+ const resizeFileTo = async (file2, options, {
1115
+ name,
1116
+ hash
1117
+ }) => {
1118
+ const filePath = file2.tmpWorkingDirectory ? join(file2.tmpWorkingDirectory, hash) : hash;
1119
+ await writeStreamToFile(file2.getStream().pipe(sharp().resize(options)), filePath);
1120
+ const newFile = {
1121
+ name,
1122
+ hash,
1123
+ ext: file2.ext,
1124
+ mime: file2.mime,
1125
+ path: file2.path || null,
1126
+ getStream: () => fs.createReadStream(filePath)
1127
+ };
1128
+ const { width, height, size } = await getMetadata(newFile);
1129
+ Object.assign(newFile, {
1130
+ width,
1131
+ height,
1132
+ size: size ? bytesToKbytes(size) : 0,
1133
+ sizeInBytes: size
1134
+ });
1135
+ return newFile;
1136
+ };
1137
+ const generateThumbnail = async (file2) => {
1138
+ if (file2.width && file2.height && (file2.width > THUMBNAIL_RESIZE_OPTIONS.width || file2.height > THUMBNAIL_RESIZE_OPTIONS.height)) {
1139
+ const newFile = await resizeFileTo(file2, THUMBNAIL_RESIZE_OPTIONS, {
1140
+ name: `thumbnail_${file2.name}`,
1141
+ hash: `thumbnail_${file2.hash}`
1142
+ });
1143
+ return newFile;
1144
+ }
1145
+ return null;
1146
+ };
1147
+ const optimize = async (file2) => {
1148
+ const { sizeOptimization = false, autoOrientation = false } = await getService("upload").getSettings() ?? {};
1149
+ const newFile = { ...file2 };
1150
+ const { width, height, size, format } = await getMetadata(newFile);
1151
+ if ((sizeOptimization || autoOrientation) && isOptimizableFormat(format)) {
1152
+ const transformer = sharp();
1153
+ transformer[format]({ quality: sizeOptimization ? 80 : 100 });
1154
+ if (autoOrientation) {
1155
+ transformer.rotate();
1156
+ }
1157
+ const filePath = file2.tmpWorkingDirectory ? join(file2.tmpWorkingDirectory, `optimized-${file2.hash}`) : `optimized-${file2.hash}`;
1158
+ await writeStreamToFile(file2.getStream().pipe(transformer), filePath);
1159
+ newFile.getStream = () => fs.createReadStream(filePath);
1160
+ }
1161
+ const { width: newWidth, height: newHeight, size: newSize } = await getMetadata(newFile);
1162
+ if (newSize && size && newSize > size) {
1163
+ return { ...file2, width, height, size: bytesToKbytes(size), sizeInBytes: size };
1164
+ }
1165
+ return Object.assign(newFile, {
1166
+ width: newWidth,
1167
+ height: newHeight,
1168
+ size: newSize ? bytesToKbytes(newSize) : 0,
1169
+ sizeInBytes: newSize
1170
+ });
1171
+ };
1172
+ const DEFAULT_BREAKPOINTS = {
1173
+ large: 1e3,
1174
+ medium: 750,
1175
+ small: 500
1176
+ };
1177
+ const getBreakpoints = () => strapi.config.get("plugin::upload.breakpoints", DEFAULT_BREAKPOINTS);
1178
+ const generateResponsiveFormats = async (file2) => {
1179
+ const { responsiveDimensions = false } = await getService("upload").getSettings() ?? {};
1180
+ if (!responsiveDimensions)
1181
+ return [];
1182
+ const originalDimensions = await getDimensions(file2);
1183
+ const breakpoints = getBreakpoints();
1184
+ return Promise.all(
1185
+ Object.keys(breakpoints).map((key) => {
1186
+ const breakpoint = breakpoints[key];
1187
+ if (breakpointSmallerThan(breakpoint, originalDimensions)) {
1188
+ return generateBreakpoint(key, { file: file2, breakpoint });
1189
+ }
1190
+ return void 0;
1191
+ })
1192
+ );
1193
+ };
1194
+ const generateBreakpoint = async (key, { file: file2, breakpoint }) => {
1195
+ const newFile = await resizeFileTo(
1196
+ file2,
1197
+ {
1198
+ width: breakpoint,
1199
+ height: breakpoint,
1200
+ fit: "inside"
1201
+ },
1202
+ {
1203
+ name: `${key}_${file2.name}`,
1204
+ hash: `${key}_${file2.hash}`
1205
+ }
1206
+ );
1207
+ return {
1208
+ key,
1209
+ file: newFile
1210
+ };
1211
+ };
1212
+ const breakpointSmallerThan = (breakpoint, { width, height }) => {
1213
+ return breakpoint < (width ?? 0) || breakpoint < (height ?? 0);
1214
+ };
1215
+ const isFaultyImage = (file2) => new Promise((resolve) => {
1216
+ file2.getStream().pipe(sharp().rotate()).on("error", () => resolve(true)).pipe(writableDiscardStream()).on("error", () => resolve(true)).on("close", () => resolve(false));
1217
+ });
1218
+ const isOptimizableImage = async (file2) => {
1219
+ let format;
1220
+ try {
1221
+ const metadata = await getMetadata(file2);
1222
+ format = metadata.format;
1223
+ } catch (e) {
1224
+ return false;
1225
+ }
1226
+ return format && FORMATS_TO_OPTIMIZE.includes(format);
1227
+ };
1228
+ const isResizableImage = async (file2) => {
1229
+ let format;
1230
+ try {
1231
+ const metadata = await getMetadata(file2);
1232
+ format = metadata.format;
1233
+ } catch (e) {
1234
+ return false;
1235
+ }
1236
+ return format && FORMATS_TO_RESIZE.includes(format);
1237
+ };
1238
+ const isImage = async (file2) => {
1239
+ let format;
1240
+ try {
1241
+ const metadata = await getMetadata(file2);
1242
+ format = metadata.format;
1243
+ } catch (e) {
1244
+ return false;
1245
+ }
1246
+ return format && FORMATS_TO_PROCESS.includes(format);
1247
+ };
1248
+ const imageManipulation = {
1249
+ isFaultyImage,
1250
+ isOptimizableImage,
1251
+ isResizableImage,
1252
+ isImage,
1253
+ getDimensions,
1254
+ generateResponsiveFormats,
1255
+ generateThumbnail,
1256
+ optimize
1257
+ };
1258
+ const setPathIdAndPath = async (folder2) => {
1259
+ const { max } = await strapi.db.queryBuilder(FOLDER_MODEL_UID).max("pathId").first().execute();
1260
+ const pathId = max + 1;
1261
+ let parentPath = "/";
1262
+ if (folder2.parent) {
1263
+ const parentFolder = await strapi.db.query(FOLDER_MODEL_UID).findOne({ where: { id: folder2.parent } });
1264
+ parentPath = parentFolder.path;
1265
+ }
1266
+ return Object.assign(folder2, {
1267
+ pathId,
1268
+ path: strings.joinBy("/", parentPath, `${pathId}`)
1269
+ });
1270
+ };
1271
+ const create = async (folderData, opts) => {
1272
+ const folderService = getService("folder");
1273
+ const { user } = opts || {};
1274
+ let enrichedFolder = await folderService.setPathIdAndPath(folderData);
1275
+ if (user) {
1276
+ enrichedFolder = await setCreatorFields({ user })(enrichedFolder);
1277
+ }
1278
+ const folder2 = await strapi.db.query(FOLDER_MODEL_UID).create({ data: enrichedFolder });
1279
+ strapi.eventHub.emit("media-folder.create", { folder: folder2 });
1280
+ return folder2;
1281
+ };
1282
+ const deleteByIds$1 = async (ids = []) => {
1283
+ const folders = await strapi.db.query(FOLDER_MODEL_UID).findMany({ where: { id: { $in: ids } } });
1284
+ if (folders.length === 0) {
1285
+ return {
1286
+ folders: [],
1287
+ totalFolderNumber: 0,
1288
+ totalFileNumber: 0
1289
+ };
1290
+ }
1291
+ const pathsToDelete = map("path", folders);
1292
+ const filesToDelete = await strapi.db.query(FILE_MODEL_UID).findMany({
1293
+ where: {
1294
+ $or: pathsToDelete.flatMap((path2) => [
1295
+ { folderPath: { $eq: path2 } },
1296
+ { folderPath: { $startsWith: `${path2}/` } }
1297
+ ])
1298
+ }
1299
+ });
1300
+ await Promise.all(filesToDelete.map((file2) => getService("upload").remove(file2)));
1301
+ const { count: totalFolderNumber } = await strapi.db.query(FOLDER_MODEL_UID).deleteMany({
1302
+ where: {
1303
+ $or: pathsToDelete.flatMap((path2) => [
1304
+ { path: { $eq: path2 } },
1305
+ { path: { $startsWith: `${path2}/` } }
1306
+ ])
1307
+ }
1308
+ });
1309
+ strapi.eventHub.emit("media-folder.delete", { folders });
1310
+ return {
1311
+ folders,
1312
+ totalFolderNumber,
1313
+ totalFileNumber: filesToDelete.length
1314
+ };
1315
+ };
1316
+ const update = async (id, {
1317
+ name,
1318
+ parent
1319
+ }, { user }) => {
1320
+ if (isUndefined(parent)) {
1321
+ const existingFolder = await strapi.db.query(FOLDER_MODEL_UID).findOne({ where: { id } });
1322
+ if (!existingFolder) {
1323
+ return void 0;
1324
+ }
1325
+ const newFolder = setCreatorFields({ user, isEdition: true })({ name, parent });
1326
+ if (isUndefined(parent)) {
1327
+ const folder2 = await strapi.db.query(FOLDER_MODEL_UID).update({ where: { id }, data: newFolder });
1328
+ return folder2;
1329
+ }
1330
+ } else {
1331
+ const trx = await strapi.db.transaction();
1332
+ try {
1333
+ const existingFolder = await strapi.db.queryBuilder(FOLDER_MODEL_UID).select(["pathId", "path"]).where({ id }).transacting(trx.get()).forUpdate().first().execute();
1334
+ const { joinTable } = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.parent;
1335
+ await strapi.db.queryBuilder(joinTable.name).transacting(trx.get()).delete().where({ [joinTable.joinColumn.name]: id }).execute();
1336
+ if (parent !== null) {
1337
+ await strapi.db.queryBuilder(joinTable.name).transacting(trx.get()).insert({ [joinTable.inverseJoinColumn.name]: parent, [joinTable.joinColumn.name]: id }).where({ [joinTable.joinColumn.name]: id }).execute();
1338
+ }
1339
+ let destinationFolderPath = "/";
1340
+ if (parent !== null) {
1341
+ const destinationFolder = await strapi.db.queryBuilder(FOLDER_MODEL_UID).select("path").where({ id: parent }).transacting(trx.get()).first().execute();
1342
+ destinationFolderPath = destinationFolder.path;
1343
+ }
1344
+ const folderTable = strapi.getModel(FOLDER_MODEL_UID).collectionName;
1345
+ const fileTable = strapi.getModel(FILE_MODEL_UID).collectionName;
1346
+ const folderPathColumnName = (
1347
+ // @ts-expect-error - no dynamic types
1348
+ strapi.db.metadata.get(FILE_MODEL_UID).attributes.folderPath.columnName
1349
+ );
1350
+ const pathColumnName = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.path.columnName;
1351
+ await strapi.db.getConnection(folderTable).transacting(trx.get()).where(pathColumnName, existingFolder.path).orWhere(pathColumnName, "like", `${existingFolder.path}/%`).update(
1352
+ pathColumnName,
1353
+ strapi.db.connection.raw("REPLACE(??, ?, ?)", [
1354
+ pathColumnName,
1355
+ existingFolder.path,
1356
+ strings.joinBy("/", destinationFolderPath, `${existingFolder.pathId}`)
1357
+ ])
1358
+ );
1359
+ await strapi.db.getConnection(fileTable).transacting(trx.get()).where(folderPathColumnName, existingFolder.path).orWhere(folderPathColumnName, "like", `${existingFolder.path}/%`).update(
1360
+ folderPathColumnName,
1361
+ strapi.db.connection.raw("REPLACE(??, ?, ?)", [
1362
+ folderPathColumnName,
1363
+ existingFolder.path,
1364
+ strings.joinBy("/", destinationFolderPath, `${existingFolder.pathId}`)
1365
+ ])
1366
+ );
1367
+ await trx.commit();
1368
+ } catch (e) {
1369
+ await trx.rollback();
1370
+ throw e;
1371
+ }
1372
+ const newFolder = setCreatorFields({ user, isEdition: true })({ name });
1373
+ const folder2 = await strapi.db.query(FOLDER_MODEL_UID).update({ where: { id }, data: newFolder });
1374
+ strapi.eventHub.emit("media-folder.update", { folder: folder2 });
1375
+ return folder2;
1376
+ }
1377
+ };
1378
+ const exists = async (params = {}) => {
1379
+ const count = await strapi.db.query(FOLDER_MODEL_UID).count({ where: params });
1380
+ return count > 0;
1381
+ };
1382
+ const getStructure = async () => {
1383
+ const { joinTable } = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.parent;
1384
+ const qb = strapi.db.queryBuilder(FOLDER_MODEL_UID);
1385
+ const alias = qb.getAlias();
1386
+ const folders = await qb.select(["id", "name", `${alias}.${joinTable.inverseJoinColumn.name} as parent`]).join({
1387
+ alias,
1388
+ referencedTable: joinTable.name,
1389
+ referencedColumn: joinTable.joinColumn.name,
1390
+ rootColumn: joinTable.joinColumn.referencedColumn,
1391
+ rootTable: qb.alias
1392
+ }).execute({ mapResults: false });
1393
+ const folderMap = {
1394
+ null: { children: [] }
1395
+ };
1396
+ folders.forEach((f) => {
1397
+ folderMap[f.id] = { ...f, children: [] };
1398
+ });
1399
+ folders.forEach((f) => {
1400
+ const parentId = f.parent || "null";
1401
+ if (!folderMap[parentId]) {
1402
+ folderMap[parentId] = { children: [] };
1403
+ }
1404
+ folderMap[parentId].children.push(folderMap[f.id]);
1405
+ folderMap[parentId].children = sortBy("name", folderMap[parentId].children);
1406
+ delete folderMap[f.id].parent;
1407
+ });
1408
+ return folderMap.null.children;
1409
+ };
1410
+ const folder = {
1411
+ create,
1412
+ exists,
1413
+ deleteByIds: deleteByIds$1,
1414
+ update,
1415
+ setPathIdAndPath,
1416
+ getStructure
1417
+ };
1418
+ const getFolderPath = async (folderId) => {
1419
+ if (!folderId)
1420
+ return "/";
1421
+ const parentFolder = await strapi.db.query(FOLDER_MODEL_UID).findOne({ where: { id: folderId } });
1422
+ return parentFolder.path;
1423
+ };
1424
+ const deleteByIds = async (ids = []) => {
1425
+ const filesToDelete = await strapi.db.query(FILE_MODEL_UID).findMany({ where: { id: { $in: ids } } });
1426
+ await Promise.all(filesToDelete.map((file2) => getService("upload").remove(file2)));
1427
+ return filesToDelete;
1428
+ };
1429
+ const signFileUrls = async (file2) => {
1430
+ const { provider: provider2 } = strapi.plugins.upload;
1431
+ const { provider: providerConfig } = strapi.config.get("plugin::upload");
1432
+ const isPrivate = await provider2.isPrivate();
1433
+ file2.isUrlSigned = false;
1434
+ if (file2.provider !== providerConfig || !isPrivate) {
1435
+ return file2;
1436
+ }
1437
+ const signUrl = async (file22) => {
1438
+ const signedUrl = await provider2.getSignedUrl(file22);
1439
+ file22.url = signedUrl.url;
1440
+ file22.isUrlSigned = true;
1441
+ };
1442
+ const signedFile = cloneDeep(file2);
1443
+ await signUrl(signedFile);
1444
+ if (file2.formats) {
1445
+ await async.map(Object.values(signedFile.formats ?? {}), signUrl);
1446
+ }
1447
+ return signedFile;
1448
+ };
1449
+ const file = { getFolderPath, deleteByIds, signFileUrls };
1450
+ const getWeeklyCronScheduleAt = (date) => `${date.getSeconds()} ${date.getMinutes()} ${date.getHours()} * * ${date.getDay()}`;
1451
+ const ONE_WEEK = 7 * 24 * 60 * 60 * 1e3;
1452
+ const getMetricsStoreValue = async () => {
1453
+ const value = await strapi.store.get({ type: "plugin", name: "upload", key: "metrics" });
1454
+ return defaultTo({}, value);
1455
+ };
1456
+ const setMetricsStoreValue = (value) => strapi.store.set({ type: "plugin", name: "upload", key: "metrics", value });
1457
+ const weeklyMetrics = ({ strapi: strapi2 }) => ({
1458
+ async computeMetrics() {
1459
+ const pathColName = strapi2.db.metadata.get(FOLDER_MODEL_UID).attributes.path.columnName;
1460
+ const folderTable = strapi2.getModel(FOLDER_MODEL_UID).collectionName;
1461
+ let keepOnlySlashesSQLString = "??";
1462
+ const queryParams = [pathColName];
1463
+ for (let i = 0; i < 10; i += 1) {
1464
+ keepOnlySlashesSQLString = `REPLACE(${keepOnlySlashesSQLString}, ?, ?)`;
1465
+ queryParams.push(String(i), "");
1466
+ }
1467
+ const res = await strapi2.db.getConnection(folderTable).select(
1468
+ strapi2.db.connection.raw(
1469
+ `LENGTH(${keepOnlySlashesSQLString}) AS depth, COUNT(*) AS occurence`,
1470
+ queryParams
1471
+ )
1472
+ ).groupBy("depth");
1473
+ const folderLevelsArray = res.map((map2) => ({
1474
+ depth: Number(map2.depth),
1475
+ occurence: Number(map2.occurence)
1476
+ }));
1477
+ let product = 0;
1478
+ let folderNumber = 0;
1479
+ let maxDepth = 0;
1480
+ for (const folderLevel of folderLevelsArray) {
1481
+ product += folderLevel.depth * folderLevel.occurence;
1482
+ folderNumber += folderLevel.occurence;
1483
+ if (folderLevel.depth > maxDepth) {
1484
+ maxDepth = folderLevel.depth;
1485
+ }
1486
+ }
1487
+ const averageDepth = folderNumber !== 0 ? product / folderNumber : 0;
1488
+ let sumOfDeviation = 0;
1489
+ for (const folderLevel of folderLevelsArray) {
1490
+ sumOfDeviation += Math.abs(folderLevel.depth - averageDepth) * folderLevel.occurence;
1491
+ }
1492
+ const averageDeviationDepth = folderNumber !== 0 ? sumOfDeviation / folderNumber : 0;
1493
+ const assetNumber = await strapi2.db.query(FILE_MODEL_UID).count();
1494
+ return {
1495
+ assetNumber,
1496
+ folderNumber,
1497
+ averageDepth,
1498
+ maxDepth,
1499
+ averageDeviationDepth
1500
+ };
1501
+ },
1502
+ async sendMetrics() {
1503
+ const metrics2 = await this.computeMetrics();
1504
+ strapi2.telemetry.send("didSendUploadPropertiesOnceAWeek", {
1505
+ groupProperties: { metrics: metrics2 }
1506
+ });
1507
+ const metricsInfoStored = await getMetricsStoreValue();
1508
+ await setMetricsStoreValue({ ...metricsInfoStored, lastWeeklyUpdate: (/* @__PURE__ */ new Date()).getTime() });
1509
+ },
1510
+ async ensureWeeklyStoredCronSchedule() {
1511
+ const metricsInfoStored = await getMetricsStoreValue();
1512
+ const { weeklySchedule: currentSchedule, lastWeeklyUpdate } = metricsInfoStored;
1513
+ const now = /* @__PURE__ */ new Date();
1514
+ let weeklySchedule = currentSchedule;
1515
+ if (!weeklySchedule || !lastWeeklyUpdate || lastWeeklyUpdate + ONE_WEEK < now.getTime()) {
1516
+ weeklySchedule = getWeeklyCronScheduleAt(add(now, { minutes: 5 }));
1517
+ await setMetricsStoreValue({ ...metricsInfoStored, weeklySchedule });
1518
+ return weeklySchedule;
1519
+ }
1520
+ return weeklySchedule;
1521
+ },
1522
+ async registerCron() {
1523
+ const weeklySchedule = await this.ensureWeeklyStoredCronSchedule();
1524
+ strapi2.cron.add({ [weeklySchedule]: this.sendMetrics.bind(this) });
1525
+ }
1526
+ });
1527
+ const getProviderName = () => strapi.config.get("plugin::upload.provider", "local");
1528
+ const isProviderPrivate = async () => strapi.plugin("upload").provider.isPrivate();
1529
+ const metrics = ({ strapi: strapi2 }) => ({
1530
+ async sendUploadPluginMetrics() {
1531
+ const uploadProvider = getProviderName();
1532
+ const privateProvider = await isProviderPrivate();
1533
+ strapi2.telemetry.send("didInitializePluginUpload", {
1534
+ groupProperties: {
1535
+ uploadProvider,
1536
+ privateProvider
1537
+ }
1538
+ });
1539
+ }
1540
+ });
1541
+ const getStore = () => strapi.store({ type: "plugin", name: "upload", key: "api-folder" });
1542
+ const createApiUploadFolder = async () => {
1543
+ let name = API_UPLOAD_FOLDER_BASE_NAME;
1544
+ const folderService = getService("folder");
1545
+ let exists2 = true;
1546
+ let index2 = 1;
1547
+ while (exists2) {
1548
+ exists2 = await folderService.exists({ name, parent: null });
1549
+ if (exists2) {
1550
+ name = `${API_UPLOAD_FOLDER_BASE_NAME} (${index2})`;
1551
+ index2 += 1;
1552
+ }
1553
+ }
1554
+ const folder2 = await folderService.create({ name });
1555
+ await getStore().set({ value: { id: folder2.id } });
1556
+ return folder2;
1557
+ };
1558
+ const getAPIUploadFolder = async () => {
1559
+ const storeValue = await getStore().get({});
1560
+ const folderId = get("id", storeValue);
1561
+ const folder2 = folderId ? await strapi.db.query(FOLDER_MODEL_UID).findOne({ where: { id: folderId } }) : null;
1562
+ return isNil(folder2) ? createApiUploadFolder() : folder2;
1563
+ };
1564
+ const apiUploadFolder = {
1565
+ getAPIUploadFolder
1566
+ };
1567
+ const addSignedFileUrlsToAdmin = async () => {
1568
+ };
1569
+ const entityManager = { addSignedFileUrlsToAdmin };
1570
+ const contentManager = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1571
+ __proto__: null,
1572
+ entityManager
1573
+ }, Symbol.toStringTag, { value: "Module" }));
1574
+ const addSignedFileUrlsToEntityService = async () => {
1575
+ };
1576
+ const entityService = { addSignedFileUrlsToEntityService };
1577
+ const core = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
1578
+ __proto__: null,
1579
+ entityService
1580
+ }, Symbol.toStringTag, { value: "Module" }));
1581
+ const extensions = {
1582
+ contentManager,
1583
+ core
1584
+ };
1585
+ const services = {
1586
+ provider,
1587
+ upload,
1588
+ folder,
1589
+ file,
1590
+ weeklyMetrics,
1591
+ metrics,
1592
+ "image-manipulation": imageManipulation,
1593
+ "api-upload-folder": apiUploadFolder,
1594
+ extensions
1595
+ };
1596
+ const routes$3 = {
1597
+ type: "admin",
1598
+ routes: [
1599
+ {
1600
+ method: "GET",
1601
+ path: "/settings",
1602
+ handler: "admin-settings.getSettings",
1603
+ config: {
1604
+ policies: [
1605
+ "admin::isAuthenticatedAdmin",
1606
+ {
1607
+ name: "admin::hasPermissions",
1608
+ config: {
1609
+ actions: ["plugin::upload.settings.read"]
1610
+ }
1611
+ }
1612
+ ]
1613
+ }
1614
+ },
1615
+ {
1616
+ method: "PUT",
1617
+ path: "/settings",
1618
+ handler: "admin-settings.updateSettings",
1619
+ config: {
1620
+ policies: [
1621
+ "admin::isAuthenticatedAdmin",
1622
+ {
1623
+ name: "admin::hasPermissions",
1624
+ config: {
1625
+ actions: ["plugin::upload.settings.read"]
1626
+ }
1627
+ }
1628
+ ]
1629
+ }
1630
+ },
1631
+ {
1632
+ method: "POST",
1633
+ path: "/",
1634
+ handler: "admin-upload.upload",
1635
+ config: {
1636
+ policies: ["admin::isAuthenticatedAdmin"]
1637
+ }
1638
+ },
1639
+ {
1640
+ method: "GET",
1641
+ path: "/files",
1642
+ handler: "admin-file.find",
1643
+ config: {
1644
+ policies: [
1645
+ "admin::isAuthenticatedAdmin",
1646
+ {
1647
+ name: "admin::hasPermissions",
1648
+ config: {
1649
+ actions: ["plugin::upload.read"]
1650
+ }
1651
+ }
1652
+ ]
1653
+ }
1654
+ },
1655
+ {
1656
+ method: "GET",
1657
+ path: "/files/:id",
1658
+ handler: "admin-file.findOne",
1659
+ config: {
1660
+ policies: [
1661
+ "admin::isAuthenticatedAdmin",
1662
+ {
1663
+ name: "admin::hasPermissions",
1664
+ config: {
1665
+ actions: ["plugin::upload.read"]
1666
+ }
1667
+ }
1668
+ ]
1669
+ }
1670
+ },
1671
+ {
1672
+ method: "DELETE",
1673
+ path: "/files/:id",
1674
+ handler: "admin-file.destroy",
1675
+ config: {
1676
+ policies: [
1677
+ "admin::isAuthenticatedAdmin",
1678
+ {
1679
+ name: "admin::hasPermissions",
1680
+ config: {
1681
+ actions: ["plugin::upload.assets.update"]
1682
+ }
1683
+ }
1684
+ ]
1685
+ }
1686
+ },
1687
+ {
1688
+ method: "GET",
1689
+ path: "/folders/:id",
1690
+ handler: "admin-folder.findOne",
1691
+ config: {
1692
+ policies: [
1693
+ "admin::isAuthenticatedAdmin",
1694
+ {
1695
+ name: "admin::hasPermissions",
1696
+ config: {
1697
+ actions: ["plugin::upload.read"]
1698
+ }
1699
+ }
1700
+ ]
1701
+ }
1702
+ },
1703
+ {
1704
+ method: "GET",
1705
+ path: "/folders",
1706
+ handler: "admin-folder.find",
1707
+ config: {
1708
+ policies: [
1709
+ "admin::isAuthenticatedAdmin",
1710
+ {
1711
+ name: "admin::hasPermissions",
1712
+ config: {
1713
+ actions: ["plugin::upload.read"]
1714
+ }
1715
+ }
1716
+ ]
1717
+ }
1718
+ },
1719
+ {
1720
+ method: "POST",
1721
+ path: "/folders",
1722
+ handler: "admin-folder.create",
1723
+ config: {
1724
+ policies: [
1725
+ "admin::isAuthenticatedAdmin",
1726
+ {
1727
+ name: "admin::hasPermissions",
1728
+ config: {
1729
+ actions: ["plugin::upload.assets.create"]
1730
+ }
1731
+ }
1732
+ ]
1733
+ }
1734
+ },
1735
+ {
1736
+ method: "PUT",
1737
+ path: "/folders/:id",
1738
+ handler: "admin-folder.update",
1739
+ config: {
1740
+ policies: [
1741
+ "admin::isAuthenticatedAdmin",
1742
+ {
1743
+ name: "admin::hasPermissions",
1744
+ config: {
1745
+ actions: ["plugin::upload.assets.update"]
1746
+ }
1747
+ }
1748
+ ]
1749
+ }
1750
+ },
1751
+ {
1752
+ method: "GET",
1753
+ path: "/folder-structure",
1754
+ handler: "admin-folder.getStructure",
1755
+ config: {
1756
+ policies: [
1757
+ "admin::isAuthenticatedAdmin",
1758
+ {
1759
+ name: "admin::hasPermissions",
1760
+ config: {
1761
+ actions: ["plugin::upload.read"]
1762
+ }
1763
+ }
1764
+ ]
1765
+ }
1766
+ },
1767
+ {
1768
+ method: "POST",
1769
+ path: "/actions/bulk-delete",
1770
+ handler: "admin-folder-file.deleteMany",
1771
+ config: {
1772
+ policies: [
1773
+ "admin::isAuthenticatedAdmin",
1774
+ {
1775
+ name: "admin::hasPermissions",
1776
+ config: {
1777
+ actions: ["plugin::upload.assets.update"]
1778
+ }
1779
+ }
1780
+ ]
1781
+ }
1782
+ },
1783
+ {
1784
+ method: "POST",
1785
+ path: "/actions/bulk-move",
1786
+ handler: "admin-folder-file.moveMany",
1787
+ config: {
1788
+ policies: [
1789
+ "admin::isAuthenticatedAdmin",
1790
+ {
1791
+ name: "admin::hasPermissions",
1792
+ config: {
1793
+ actions: ["plugin::upload.assets.update"]
1794
+ }
1795
+ }
1796
+ ]
1797
+ }
1798
+ }
1799
+ ]
1800
+ };
1801
+ const routes$2 = {
1802
+ type: "content-api",
1803
+ routes: [
1804
+ {
1805
+ method: "POST",
1806
+ path: "/",
1807
+ handler: "content-api.upload"
1808
+ },
1809
+ {
1810
+ method: "GET",
1811
+ path: "/files",
1812
+ handler: "content-api.find"
1813
+ },
1814
+ {
1815
+ method: "GET",
1816
+ path: "/files/:id",
1817
+ handler: "content-api.findOne"
1818
+ },
1819
+ {
1820
+ method: "DELETE",
1821
+ path: "/files/:id",
1822
+ handler: "content-api.destroy"
1823
+ }
1824
+ ]
1825
+ };
1826
+ const routes$1 = {
1827
+ type: "admin",
1828
+ routes: [
1829
+ {
1830
+ method: "GET",
1831
+ path: "/configuration",
1832
+ handler: "view-configuration.findViewConfiguration",
1833
+ config: {
1834
+ policies: ["admin::isAuthenticatedAdmin"]
1835
+ }
1836
+ },
1837
+ {
1838
+ method: "PUT",
1839
+ path: "/configuration",
1840
+ handler: "view-configuration.updateViewConfiguration",
1841
+ config: {
1842
+ policies: [
1843
+ "admin::isAuthenticatedAdmin",
1844
+ {
1845
+ name: "admin::hasPermissions",
1846
+ config: {
1847
+ actions: [ACTIONS.configureView]
1848
+ }
1849
+ }
1850
+ ]
1851
+ }
1852
+ }
1853
+ ]
1854
+ };
1855
+ const routes = {
1856
+ admin: routes$3,
1857
+ "content-api": routes$2,
1858
+ viewConfiguration: routes$1
1859
+ };
1860
+ const config = {
1861
+ default: {
1862
+ enabled: true,
1863
+ provider: "local",
1864
+ sizeLimit: 1e9,
1865
+ // 1GB
1866
+ actionOptions: {}
1867
+ },
1868
+ validator() {
1869
+ }
1870
+ };
1871
+ const findEntityAndCheckPermissions = async (ability, action, model, id) => {
1872
+ const file2 = await getService("upload").findOne(id, [
1873
+ contentTypes$1.constants.CREATED_BY_ATTRIBUTE,
1874
+ "folder"
1875
+ ]);
1876
+ if (_.isNil(file2)) {
1877
+ throw new errors.NotFoundError();
1878
+ }
1879
+ const pm = strapi.service("admin::permission").createPermissionsManager({ ability, action, model });
1880
+ const creatorId = _.get(file2, [contentTypes$1.constants.CREATED_BY_ATTRIBUTE, "id"]);
1881
+ const author = creatorId ? await strapi.service("admin::user").findOne(creatorId, ["roles"]) : null;
1882
+ const fileWithRoles = _.set(_.cloneDeep(file2), "createdBy", author);
1883
+ if (pm.ability.cannot(pm.action, pm.toSubject(fileWithRoles))) {
1884
+ throw new errors.ForbiddenError();
1885
+ }
1886
+ return { pm, file: file2 };
1887
+ };
1888
+ const adminFile = {
1889
+ async find(ctx) {
1890
+ const {
1891
+ state: { userAbility }
1892
+ } = ctx;
1893
+ const defaultQuery = { populate: { folder: true } };
1894
+ const pm = strapi.service("admin::permission").createPermissionsManager({
1895
+ ability: userAbility,
1896
+ action: ACTIONS.read,
1897
+ model: FILE_MODEL_UID
1898
+ });
1899
+ if (!pm.isAllowed) {
1900
+ return ctx.forbidden();
1901
+ }
1902
+ const pmQuery = pm.addPermissionsQueryTo(merge(defaultQuery, ctx.query));
1903
+ await pm.validateQuery(pmQuery);
1904
+ const query = await pm.sanitizeQuery(pmQuery);
1905
+ const { results: files, pagination } = await getService("upload").findPage(query);
1906
+ const signedFiles = await async.map(files, getService("file").signFileUrls);
1907
+ const sanitizedFiles = await pm.sanitizeOutput(signedFiles);
1908
+ return { results: sanitizedFiles, pagination };
1909
+ },
1910
+ async findOne(ctx) {
1911
+ const {
1912
+ state: { userAbility },
1913
+ params: { id }
1914
+ } = ctx;
1915
+ const { pm, file: file2 } = await findEntityAndCheckPermissions(
1916
+ userAbility,
1917
+ ACTIONS.read,
1918
+ FILE_MODEL_UID,
1919
+ id
1920
+ );
1921
+ const signedFile = await getService("file").signFileUrls(file2);
1922
+ ctx.body = await pm.sanitizeOutput(signedFile);
1923
+ },
1924
+ async destroy(ctx) {
1925
+ const { id } = ctx.params;
1926
+ const { userAbility } = ctx.state;
1927
+ const { pm, file: file2 } = await findEntityAndCheckPermissions(
1928
+ userAbility,
1929
+ ACTIONS.update,
1930
+ FILE_MODEL_UID,
1931
+ id
1932
+ );
1933
+ const [body] = await Promise.all([
1934
+ pm.sanitizeOutput(file2, { action: ACTIONS.read }),
1935
+ getService("upload").remove(file2)
1936
+ ]);
1937
+ ctx.body = body;
1938
+ }
1939
+ };
1940
+ const folderExists = async (folderId) => {
1941
+ if (isNil(folderId)) {
1942
+ return true;
1943
+ }
1944
+ const exists2 = await getService("folder").exists({ id: folderId });
1945
+ return exists2;
1946
+ };
1947
+ const isFolderOrChild = (folderOrChild, folder2) => folderOrChild.path === folder2.path || folderOrChild.path.startsWith(`${folder2.path}/`);
1948
+ const NO_SLASH_REGEX = /^[^/]+$/;
1949
+ const NO_SPACES_AROUND = /^(?! ).+(?<! )$/;
1950
+ const isNameUniqueInFolder = (id) => {
1951
+ return async function test(name) {
1952
+ const { exists: exists2 } = getService("folder");
1953
+ const filters = { name, parent: this.parent.parent || null };
1954
+ if (id) {
1955
+ filters.id = { $ne: id };
1956
+ if (isUndefined(name)) {
1957
+ const existingFolder = await strapi.db.query(FOLDER_MODEL_UID).findOne({ where: { id } });
1958
+ filters.name = get("name", existingFolder);
1959
+ }
1960
+ }
1961
+ const doesExist = await exists2(filters);
1962
+ return !doesExist;
1963
+ };
1964
+ };
1965
+ const validateCreateFolderSchema = yup.object().shape({
1966
+ name: yup.string().min(1).matches(NO_SLASH_REGEX, "name cannot contain slashes").matches(NO_SPACES_AROUND, "name cannot start or end with a whitespace").required().test("is-folder-unique", "A folder with this name already exists", isNameUniqueInFolder()),
1967
+ parent: yup.strapiID().nullable().test("folder-exists", "parent folder does not exist", folderExists)
1968
+ }).noUnknown().required();
1969
+ const validateUpdateFolderSchema = (id) => yup.object().shape({
1970
+ name: yup.string().min(1).matches(NO_SLASH_REGEX, "name cannot contain slashes").matches(NO_SPACES_AROUND, "name cannot start or end with a whitespace").test(
1971
+ "is-folder-unique",
1972
+ "A folder with this name already exists",
1973
+ isNameUniqueInFolder(id)
1974
+ ),
1975
+ parent: yup.strapiID().nullable().test("folder-exists", "parent folder does not exist", folderExists).test(
1976
+ "dont-move-inside-self",
1977
+ "folder cannot be moved inside itself",
1978
+ async function test(parent) {
1979
+ if (isNil(parent))
1980
+ return true;
1981
+ const destinationFolder = await strapi.db.query(FOLDER_MODEL_UID).findOne({
1982
+ select: ["path"],
1983
+ where: { id: parent }
1984
+ });
1985
+ const currentFolder = await strapi.db.query(FOLDER_MODEL_UID).findOne({
1986
+ select: ["path"],
1987
+ where: { id }
1988
+ });
1989
+ if (!destinationFolder || !currentFolder)
1990
+ return true;
1991
+ return !isFolderOrChild(destinationFolder, currentFolder);
1992
+ }
1993
+ )
1994
+ }).noUnknown().required();
1995
+ const validateCreateFolder = validateYupSchema(validateCreateFolderSchema);
1996
+ const validateUpdateFolder = (id) => validateYupSchema(validateUpdateFolderSchema(id));
1997
+ const adminFolder = {
1998
+ async findOne(ctx) {
1999
+ const { id } = ctx.params;
2000
+ const permissionsManager = strapi.service("admin::permission").createPermissionsManager({
2001
+ ability: ctx.state.userAbility,
2002
+ model: FOLDER_MODEL_UID
2003
+ });
2004
+ await permissionsManager.validateQuery(ctx.query);
2005
+ const query = await permissionsManager.sanitizeQuery(ctx.query);
2006
+ const { results } = await strapi.db.query(FOLDER_MODEL_UID).findPage(
2007
+ strapi.get("query-params").transform(
2008
+ FOLDER_MODEL_UID,
2009
+ defaultsDeep(
2010
+ {
2011
+ filters: { id },
2012
+ populate: {
2013
+ children: {
2014
+ count: true
2015
+ },
2016
+ files: {
2017
+ count: true
2018
+ }
2019
+ }
2020
+ },
2021
+ query
2022
+ )
2023
+ )
2024
+ );
2025
+ if (results.length === 0) {
2026
+ return ctx.notFound("folder not found");
2027
+ }
2028
+ ctx.body = {
2029
+ data: await permissionsManager.sanitizeOutput(results[0])
2030
+ };
2031
+ },
2032
+ async find(ctx) {
2033
+ const permissionsManager = strapi.service("admin::permission").createPermissionsManager({
2034
+ ability: ctx.state.userAbility,
2035
+ model: FOLDER_MODEL_UID
2036
+ });
2037
+ await permissionsManager.validateQuery(ctx.query);
2038
+ const query = await permissionsManager.sanitizeQuery(ctx.query);
2039
+ const results = await strapi.db.query(FOLDER_MODEL_UID).findMany(
2040
+ strapi.get("query-params").transform(
2041
+ FOLDER_MODEL_UID,
2042
+ defaultsDeep(
2043
+ {
2044
+ populate: {
2045
+ children: {
2046
+ count: true
2047
+ },
2048
+ files: {
2049
+ count: true
2050
+ }
2051
+ }
2052
+ },
2053
+ query
2054
+ )
2055
+ )
2056
+ );
2057
+ ctx.body = {
2058
+ data: await permissionsManager.sanitizeOutput(results)
2059
+ };
2060
+ },
2061
+ async create(ctx) {
2062
+ const { user } = ctx.state;
2063
+ const { body } = ctx.request;
2064
+ await validateCreateFolder(body);
2065
+ const folderService = getService("folder");
2066
+ const folder2 = await folderService.create(body, { user });
2067
+ const permissionsManager = strapi.service("admin::permission").createPermissionsManager({
2068
+ ability: ctx.state.userAbility,
2069
+ model: FOLDER_MODEL_UID
2070
+ });
2071
+ ctx.created({
2072
+ data: await permissionsManager.sanitizeOutput(folder2)
2073
+ });
2074
+ },
2075
+ async update(ctx) {
2076
+ const { id } = ctx.params;
2077
+ const { user } = ctx.state;
2078
+ const { body } = ctx.request;
2079
+ const permissionsManager = strapi.service("admin::permission").createPermissionsManager({
2080
+ ability: ctx.state.userAbility,
2081
+ model: FOLDER_MODEL_UID
2082
+ });
2083
+ await validateUpdateFolder(id)(body);
2084
+ const folderService = getService("folder");
2085
+ const updatedFolder = await folderService.update(id, body, { user });
2086
+ if (!updatedFolder) {
2087
+ return ctx.notFound("folder not found");
2088
+ }
2089
+ ctx.body = {
2090
+ data: await permissionsManager.sanitizeOutput(updatedFolder)
2091
+ };
2092
+ },
2093
+ async getStructure(ctx) {
2094
+ const { getStructure: getStructure2 } = getService("folder");
2095
+ const structure = await getStructure2();
2096
+ ctx.body = {
2097
+ data: structure
2098
+ };
2099
+ }
2100
+ };
2101
+ const validateDeleteManyFoldersFilesSchema = yup.object().shape({
2102
+ fileIds: yup.array().of(yup.strapiID().required()),
2103
+ folderIds: yup.array().of(yup.strapiID().required())
2104
+ }).noUnknown().required();
2105
+ const validateStructureMoveManyFoldersFilesSchema = yup.object().shape({
2106
+ destinationFolderId: yup.strapiID().nullable().defined().test("folder-exists", "destination folder does not exist", folderExists),
2107
+ fileIds: yup.array().of(yup.strapiID().required()),
2108
+ folderIds: yup.array().of(yup.strapiID().required())
2109
+ }).noUnknown().required();
2110
+ const validateDuplicatesMoveManyFoldersFilesSchema = yup.object().test("are-folders-unique", "some folders already exist", async function(value) {
2111
+ const { folderIds, destinationFolderId } = value;
2112
+ if (isEmpty(folderIds))
2113
+ return true;
2114
+ const folders = await strapi.db.query(FOLDER_MODEL_UID).findMany({
2115
+ select: ["name"],
2116
+ where: { id: { $in: folderIds } }
2117
+ });
2118
+ const existingFolders = await strapi.db.query(FOLDER_MODEL_UID).findMany({
2119
+ select: ["name"],
2120
+ where: { parent: { id: destinationFolderId } }
2121
+ });
2122
+ const duplicatedNames = intersection(map("name", folders), map("name", existingFolders));
2123
+ if (duplicatedNames.length > 0) {
2124
+ return this.createError({
2125
+ message: `some folders already exists: ${duplicatedNames.join(", ")}`
2126
+ });
2127
+ }
2128
+ return true;
2129
+ });
2130
+ const validateMoveFoldersNotInsideThemselvesSchema = yup.object().test(
2131
+ "dont-move-inside-self",
2132
+ "folders cannot be moved inside themselves or one of its children",
2133
+ async function(value) {
2134
+ const { folderIds, destinationFolderId } = value;
2135
+ if (destinationFolderId === null || isEmpty(folderIds))
2136
+ return true;
2137
+ const destinationFolder = await strapi.db.query(FOLDER_MODEL_UID).findOne({
2138
+ select: ["path"],
2139
+ where: { id: destinationFolderId }
2140
+ });
2141
+ const folders = await strapi.db.query(FOLDER_MODEL_UID).findMany({
2142
+ select: ["name", "path"],
2143
+ where: { id: { $in: folderIds } }
2144
+ });
2145
+ const unmovableFoldersNames = folders.filter((folder2) => isFolderOrChild(destinationFolder, folder2)).map((f) => f.name);
2146
+ if (unmovableFoldersNames.length > 0) {
2147
+ return this.createError({
2148
+ message: `folders cannot be moved inside themselves or one of its children: ${unmovableFoldersNames.join(
2149
+ ", "
2150
+ )}`
2151
+ });
2152
+ }
2153
+ return true;
2154
+ }
2155
+ );
2156
+ const validateDeleteManyFoldersFiles = validateYupSchema(
2157
+ validateDeleteManyFoldersFilesSchema
2158
+ );
2159
+ async function validateMoveManyFoldersFiles(body) {
2160
+ await validateYupSchema(validateStructureMoveManyFoldersFilesSchema)(body);
2161
+ await validateYupSchema(validateDuplicatesMoveManyFoldersFilesSchema)(body);
2162
+ await validateYupSchema(validateMoveFoldersNotInsideThemselvesSchema)(body);
2163
+ }
2164
+ const adminFolderFile = {
2165
+ async deleteMany(ctx) {
2166
+ const { body } = ctx.request;
2167
+ const {
2168
+ state: { userAbility }
2169
+ } = ctx;
2170
+ const pmFolder = strapi.service("admin::permission").createPermissionsManager({
2171
+ ability: ctx.state.userAbility,
2172
+ model: FOLDER_MODEL_UID
2173
+ });
2174
+ const pmFile = strapi.service("admin::permission").createPermissionsManager({
2175
+ ability: userAbility,
2176
+ action: ACTIONS.read,
2177
+ model: FILE_MODEL_UID
2178
+ });
2179
+ await validateDeleteManyFoldersFiles(body);
2180
+ const fileService = getService("file");
2181
+ const folderService = getService("folder");
2182
+ const deletedFiles = await fileService.deleteByIds(body.fileIds);
2183
+ const {
2184
+ folders: deletedFolders,
2185
+ totalFolderNumber,
2186
+ totalFileNumber
2187
+ } = await folderService.deleteByIds(body.folderIds);
2188
+ if (deletedFiles.length + deletedFolders.length > 1) {
2189
+ strapi.telemetry.send("didBulkDeleteMediaLibraryElements", {
2190
+ eventProperties: {
2191
+ rootFolderNumber: deletedFolders.length,
2192
+ rootAssetNumber: deletedFiles.length,
2193
+ totalFolderNumber,
2194
+ totalAssetNumber: totalFileNumber + deletedFiles.length
2195
+ }
2196
+ });
2197
+ }
2198
+ ctx.body = {
2199
+ data: {
2200
+ files: await pmFile.sanitizeOutput(deletedFiles),
2201
+ folders: await pmFolder.sanitizeOutput(deletedFolders)
2202
+ }
2203
+ };
2204
+ },
2205
+ async moveMany(ctx) {
2206
+ const { body } = ctx.request;
2207
+ const {
2208
+ state: { userAbility }
2209
+ } = ctx;
2210
+ const pmFolder = strapi.service("admin::permission").createPermissionsManager({
2211
+ ability: ctx.state.userAbility,
2212
+ model: FOLDER_MODEL_UID
2213
+ });
2214
+ const pmFile = strapi.service("admin::permission").createPermissionsManager({
2215
+ ability: userAbility,
2216
+ action: ACTIONS.read,
2217
+ model: FILE_MODEL_UID
2218
+ });
2219
+ await validateMoveManyFoldersFiles(body);
2220
+ const { folderIds = [], fileIds = [], destinationFolderId } = body;
2221
+ let totalFolderNumber = 0;
2222
+ let totalFileNumber = 0;
2223
+ const trx = await strapi.db.transaction();
2224
+ try {
2225
+ const existingFolders = await strapi.db.queryBuilder(FOLDER_MODEL_UID).select(["id", "pathId", "path"]).where({ id: { $in: folderIds } }).transacting(trx.get()).forUpdate().execute();
2226
+ const existingFiles = await strapi.db.queryBuilder(FILE_MODEL_UID).select(["id"]).where({ id: { $in: fileIds } }).transacting(trx.get()).forUpdate().execute();
2227
+ let destinationFolderPath = "/";
2228
+ if (destinationFolderId !== null) {
2229
+ const destinationFolder = await strapi.db.queryBuilder(FOLDER_MODEL_UID).select("path").where({ id: destinationFolderId }).transacting(trx.get()).first().execute();
2230
+ destinationFolderPath = destinationFolder.path;
2231
+ }
2232
+ const fileTable = strapi.getModel(FILE_MODEL_UID).collectionName;
2233
+ const folderTable = strapi.getModel(FOLDER_MODEL_UID).collectionName;
2234
+ const folderPathColName = (
2235
+ // @ts-expect-error - no dynamic typings for the models
2236
+ strapi.db.metadata.get(FILE_MODEL_UID).attributes.folderPath.columnName
2237
+ );
2238
+ const pathColName = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.path.columnName;
2239
+ if (existingFolders.length > 0) {
2240
+ const { joinTable } = strapi.db.metadata.get(FOLDER_MODEL_UID).attributes.parent;
2241
+ await strapi.db.queryBuilder(joinTable.name).transacting(trx.get()).delete().where({ [joinTable.joinColumn.name]: { $in: folderIds } }).execute();
2242
+ if (destinationFolderId !== null) {
2243
+ await strapi.db.queryBuilder(joinTable.name).transacting(trx.get()).insert(
2244
+ existingFolders.map((folder2) => ({
2245
+ [joinTable.inverseJoinColumn.name]: destinationFolderId,
2246
+ [joinTable.joinColumn.name]: folder2.id
2247
+ }))
2248
+ ).execute();
2249
+ }
2250
+ for (const existingFolder of existingFolders) {
2251
+ let replaceQuery;
2252
+ switch (strapi.db.dialect.client) {
2253
+ case "sqlite":
2254
+ replaceQuery = "? || SUBSTRING(??, ?)";
2255
+ break;
2256
+ case "postgres":
2257
+ replaceQuery = "CONCAT(?::TEXT, SUBSTRING(??, ?::INTEGER))";
2258
+ break;
2259
+ default:
2260
+ replaceQuery = "CONCAT(?, SUBSTRING(??, ?))";
2261
+ }
2262
+ totalFolderNumber = await strapi.db.getConnection(folderTable).transacting(trx.get()).where(pathColName, existingFolder.path).orWhere(pathColName, "like", `${existingFolder.path}/%`).update(
2263
+ pathColName,
2264
+ strapi.db.connection.raw(replaceQuery, [
2265
+ strings.joinBy("/", destinationFolderPath, `${existingFolder.pathId}`),
2266
+ pathColName,
2267
+ existingFolder.path.length + 1
2268
+ ])
2269
+ );
2270
+ totalFileNumber = await strapi.db.getConnection(fileTable).transacting(trx.get()).where(folderPathColName, existingFolder.path).orWhere(folderPathColName, "like", `${existingFolder.path}/%`).update(
2271
+ folderPathColName,
2272
+ strapi.db.connection.raw(replaceQuery, [
2273
+ strings.joinBy("/", destinationFolderPath, `${existingFolder.pathId}`),
2274
+ folderPathColName,
2275
+ existingFolder.path.length + 1
2276
+ ])
2277
+ );
2278
+ }
2279
+ }
2280
+ if (existingFiles.length > 0) {
2281
+ const fileJoinTable = strapi.db.metadata.get(FILE_MODEL_UID).attributes.folder.joinTable;
2282
+ await strapi.db.queryBuilder(fileJoinTable.name).transacting(trx.get()).delete().where({ [fileJoinTable.joinColumn.name]: { $in: fileIds } }).execute();
2283
+ if (destinationFolderId !== null) {
2284
+ await strapi.db.queryBuilder(fileJoinTable.name).transacting(trx.get()).insert(
2285
+ existingFiles.map((file2) => ({
2286
+ [fileJoinTable.inverseJoinColumn.name]: destinationFolderId,
2287
+ [fileJoinTable.joinColumn.name]: file2.id
2288
+ }))
2289
+ ).execute();
2290
+ }
2291
+ await strapi.db.getConnection(fileTable).transacting(trx.get()).whereIn("id", fileIds).update(folderPathColName, destinationFolderPath);
2292
+ }
2293
+ await trx.commit();
2294
+ } catch (e) {
2295
+ await trx.rollback();
2296
+ throw e;
2297
+ }
2298
+ const updatedFolders = await strapi.db.query(FOLDER_MODEL_UID).findMany({
2299
+ where: { id: { $in: folderIds } }
2300
+ });
2301
+ const updatedFiles = await strapi.db.query(FILE_MODEL_UID).findMany({
2302
+ where: { id: { $in: fileIds } }
2303
+ });
2304
+ strapi.telemetry.send("didBulkMoveMediaLibraryElements", {
2305
+ eventProperties: {
2306
+ rootFolderNumber: updatedFolders.length,
2307
+ rootAssetNumber: updatedFiles.length,
2308
+ totalFolderNumber,
2309
+ totalAssetNumber: totalFileNumber + updatedFiles.length
2310
+ }
2311
+ });
2312
+ ctx.body = {
2313
+ data: {
2314
+ files: await pmFile.sanitizeOutput(updatedFiles),
2315
+ folders: await pmFolder.sanitizeOutput(updatedFolders)
2316
+ }
2317
+ };
2318
+ }
2319
+ };
2320
+ const settingsSchema = yup.object({
2321
+ sizeOptimization: yup.boolean().required(),
2322
+ responsiveDimensions: yup.boolean().required(),
2323
+ autoOrientation: yup.boolean()
2324
+ });
2325
+ const validateSettings = validateYupSchema(settingsSchema);
2326
+ const adminSettings = {
2327
+ async updateSettings(ctx) {
2328
+ const {
2329
+ request: { body },
2330
+ state: { userAbility }
2331
+ } = ctx;
2332
+ if (userAbility.cannot(ACTIONS.readSettings, FILE_MODEL_UID)) {
2333
+ return ctx.forbidden();
2334
+ }
2335
+ const data = await validateSettings(body);
2336
+ await getService("upload").setSettings(data);
2337
+ ctx.body = { data };
2338
+ },
2339
+ async getSettings(ctx) {
2340
+ const {
2341
+ state: { userAbility }
2342
+ } = ctx;
2343
+ if (userAbility.cannot(ACTIONS.readSettings, FILE_MODEL_UID)) {
2344
+ return ctx.forbidden();
2345
+ }
2346
+ const data = await getService("upload").getSettings();
2347
+ ctx.body = { data };
2348
+ }
2349
+ };
2350
+ const fileInfoSchema$1 = yup.object({
2351
+ name: yup.string().nullable(),
2352
+ alternativeText: yup.string().nullable(),
2353
+ caption: yup.string().nullable(),
2354
+ folder: yup.strapiID().nullable().test("folder-exists", "the folder does not exist", async (folderId) => {
2355
+ if (isNil(folderId)) {
2356
+ return true;
2357
+ }
2358
+ const exists2 = await getService("folder").exists({ id: folderId });
2359
+ return exists2;
2360
+ })
2361
+ });
2362
+ const uploadSchema$1 = yup.object({
2363
+ fileInfo: fileInfoSchema$1
2364
+ });
2365
+ const multiUploadSchema$1 = yup.object({
2366
+ fileInfo: yup.array().of(fileInfoSchema$1)
2367
+ });
2368
+ const validateUploadBody$1 = (data = {}, isMulti = false) => {
2369
+ const schema = isMulti ? multiUploadSchema$1 : uploadSchema$1;
2370
+ return validateYupSchema(schema, { strict: false })(data);
2371
+ };
2372
+ const adminUpload = {
2373
+ async updateFileInfo(ctx) {
2374
+ const {
2375
+ state: { userAbility, user },
2376
+ query: { id },
2377
+ request: { body }
2378
+ } = ctx;
2379
+ if (typeof id !== "string") {
2380
+ throw new errors.ValidationError("File id is required");
2381
+ }
2382
+ const uploadService = getService("upload");
2383
+ const { pm } = await findEntityAndCheckPermissions(
2384
+ userAbility,
2385
+ ACTIONS.update,
2386
+ FILE_MODEL_UID,
2387
+ id
2388
+ );
2389
+ const data = await validateUploadBody$1(body);
2390
+ const file2 = await uploadService.updateFileInfo(id, data.fileInfo, { user });
2391
+ ctx.body = await pm.sanitizeOutput(file2, { action: ACTIONS.read });
2392
+ },
2393
+ async replaceFile(ctx) {
2394
+ const {
2395
+ state: { userAbility, user },
2396
+ query: { id },
2397
+ request: { body, files: { files } = {} }
2398
+ } = ctx;
2399
+ if (typeof id !== "string") {
2400
+ throw new errors.ValidationError("File id is required");
2401
+ }
2402
+ const uploadService = getService("upload");
2403
+ const { pm } = await findEntityAndCheckPermissions(
2404
+ userAbility,
2405
+ ACTIONS.update,
2406
+ FILE_MODEL_UID,
2407
+ id
2408
+ );
2409
+ if (Array.isArray(files)) {
2410
+ throw new errors.ApplicationError("Cannot replace a file with multiple ones");
2411
+ }
2412
+ const data = await validateUploadBody$1(body);
2413
+ const replacedFile = await uploadService.replace(id, { data, file: files }, { user });
2414
+ const signedFile = await getService("file").signFileUrls(replacedFile);
2415
+ ctx.body = await pm.sanitizeOutput(signedFile, { action: ACTIONS.read });
2416
+ },
2417
+ async uploadFiles(ctx) {
2418
+ const {
2419
+ state: { userAbility, user },
2420
+ request: { body, files: { files } = {} }
2421
+ } = ctx;
2422
+ const uploadService = getService("upload");
2423
+ const pm = strapi.service("admin::permission").createPermissionsManager({
2424
+ ability: userAbility,
2425
+ action: ACTIONS.create,
2426
+ model: FILE_MODEL_UID
2427
+ });
2428
+ if (!pm.isAllowed) {
2429
+ return ctx.forbidden();
2430
+ }
2431
+ const data = await validateUploadBody$1(body);
2432
+ const uploadedFiles = await uploadService.upload({ data, files }, { user });
2433
+ const signedFiles = await async.map(uploadedFiles, getService("file").signFileUrls);
2434
+ ctx.body = await pm.sanitizeOutput(signedFiles, { action: ACTIONS.read });
2435
+ ctx.status = 201;
2436
+ },
2437
+ // TODO: split into multiple endpoints
2438
+ async upload(ctx) {
2439
+ const {
2440
+ query: { id },
2441
+ request: { files: { files } = {} }
2442
+ } = ctx;
2443
+ if (_.isEmpty(files) || !Array.isArray(files) && files.size === 0) {
2444
+ if (id) {
2445
+ return this.updateFileInfo(ctx);
2446
+ }
2447
+ throw new errors.ApplicationError("Files are empty");
2448
+ }
2449
+ await (id ? this.replaceFile : this.uploadFiles)(ctx);
2450
+ }
2451
+ };
2452
+ const fileInfoSchema = yup.object({
2453
+ name: yup.string().nullable(),
2454
+ alternativeText: yup.string().nullable(),
2455
+ caption: yup.string().nullable()
2456
+ }).noUnknown();
2457
+ const uploadSchema = yup.object({
2458
+ fileInfo: fileInfoSchema
2459
+ });
2460
+ const multiUploadSchema = yup.object({
2461
+ fileInfo: yup.array().of(fileInfoSchema)
2462
+ });
2463
+ const validateUploadBody = (data = {}, isMulti = false) => {
2464
+ const schema = isMulti ? multiUploadSchema : uploadSchema;
2465
+ return validateYupSchema(schema, { strict: false })(data);
2466
+ };
2467
+ const { ValidationError } = utils.errors;
2468
+ const contentApi = ({ strapi: strapi2 }) => {
2469
+ const sanitizeOutput = async (data, ctx) => {
2470
+ const schema = strapi2.getModel(FILE_MODEL_UID);
2471
+ const { auth } = ctx.state;
2472
+ return strapi2.contentAPI.sanitize.output(data, schema, { auth });
2473
+ };
2474
+ const validateQuery = async (data, ctx) => {
2475
+ const schema = strapi2.getModel(FILE_MODEL_UID);
2476
+ const { auth } = ctx.state;
2477
+ return strapi2.contentAPI.validate.query(data, schema, { auth });
2478
+ };
2479
+ const sanitizeQuery = async (data, ctx) => {
2480
+ const schema = strapi2.getModel(FILE_MODEL_UID);
2481
+ const { auth } = ctx.state;
2482
+ return strapi2.contentAPI.sanitize.query(data, schema, { auth });
2483
+ };
2484
+ return {
2485
+ async find(ctx) {
2486
+ await validateQuery(ctx.query, ctx);
2487
+ const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
2488
+ const files = await getService("upload").findMany(sanitizedQuery);
2489
+ ctx.body = await sanitizeOutput(files, ctx);
2490
+ },
2491
+ async findOne(ctx) {
2492
+ const {
2493
+ params: { id }
2494
+ } = ctx;
2495
+ await validateQuery(ctx.query, ctx);
2496
+ const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
2497
+ const file2 = await getService("upload").findOne(id, sanitizedQuery.populate);
2498
+ if (!file2) {
2499
+ return ctx.notFound("file.notFound");
2500
+ }
2501
+ ctx.body = await sanitizeOutput(file2, ctx);
2502
+ },
2503
+ async destroy(ctx) {
2504
+ const {
2505
+ params: { id }
2506
+ } = ctx;
2507
+ const file2 = await getService("upload").findOne(id);
2508
+ if (!file2) {
2509
+ return ctx.notFound("file.notFound");
2510
+ }
2511
+ await getService("upload").remove(file2);
2512
+ ctx.body = await sanitizeOutput(file2, ctx);
2513
+ },
2514
+ async updateFileInfo(ctx) {
2515
+ const {
2516
+ query: { id },
2517
+ request: { body }
2518
+ } = ctx;
2519
+ const data = await validateUploadBody(body);
2520
+ if (!id || typeof id !== "string" && typeof id !== "number") {
2521
+ throw new ValidationError("File id is required and must be a single value");
2522
+ }
2523
+ const result = await getService("upload").updateFileInfo(id, data.fileInfo);
2524
+ ctx.body = await sanitizeOutput(result, ctx);
2525
+ },
2526
+ async replaceFile(ctx) {
2527
+ const {
2528
+ query: { id },
2529
+ request: { body, files: { files } = {} }
2530
+ } = ctx;
2531
+ if (Array.isArray(files)) {
2532
+ throw new ValidationError("Cannot replace a file with multiple ones");
2533
+ }
2534
+ if (!id || typeof id !== "string" && typeof id !== "number") {
2535
+ throw new ValidationError("File id is required and must be a single value");
2536
+ }
2537
+ const data = await validateUploadBody(body);
2538
+ const replacedFiles = await getService("upload").replace(id, { data, file: files });
2539
+ ctx.body = await sanitizeOutput(replacedFiles, ctx);
2540
+ },
2541
+ async uploadFiles(ctx) {
2542
+ const {
2543
+ request: { body, files: { files } = {} }
2544
+ } = ctx;
2545
+ const data = await validateUploadBody(body, Array.isArray(files));
2546
+ const apiUploadFolderService = getService("api-upload-folder");
2547
+ const apiUploadFolder2 = await apiUploadFolderService.getAPIUploadFolder();
2548
+ if (Array.isArray(files)) {
2549
+ data.fileInfo = data.fileInfo || [];
2550
+ data.fileInfo = files.map((_f, i) => ({ ...data.fileInfo[i], folder: apiUploadFolder2.id }));
2551
+ } else {
2552
+ data.fileInfo = { ...data.fileInfo, folder: apiUploadFolder2.id };
2553
+ }
2554
+ const uploadedFiles = await getService("upload").upload({
2555
+ data,
2556
+ files
2557
+ });
2558
+ ctx.body = await sanitizeOutput(uploadedFiles, ctx);
2559
+ ctx.status = 201;
2560
+ },
2561
+ // TODO: split into multiple endpoints
2562
+ async upload(ctx) {
2563
+ const {
2564
+ query: { id },
2565
+ request: { files: { files } = {} }
2566
+ } = ctx;
2567
+ if (_.isEmpty(files) || !Array.isArray(files) && files.size === 0) {
2568
+ if (id) {
2569
+ return this.updateFileInfo(ctx);
2570
+ }
2571
+ throw new ValidationError("Files are empty");
2572
+ }
2573
+ await (id ? this.replaceFile : this.uploadFiles)(ctx);
2574
+ }
2575
+ };
2576
+ };
2577
+ const configSchema = yup.object({
2578
+ pageSize: yup.number().required(),
2579
+ sort: yup.mixed().oneOf(ALLOWED_SORT_STRINGS)
2580
+ });
2581
+ const validateViewConfiguration = validateYupSchema(configSchema);
2582
+ const viewConfiguration = {
2583
+ async updateViewConfiguration(ctx) {
2584
+ const {
2585
+ request: { body },
2586
+ state: { userAbility }
2587
+ } = ctx;
2588
+ if (userAbility.cannot(ACTIONS.configureView)) {
2589
+ return ctx.forbidden();
2590
+ }
2591
+ const data = await validateViewConfiguration(body);
2592
+ await getService("upload").setConfiguration(data);
2593
+ ctx.body = { data };
2594
+ },
2595
+ async findViewConfiguration(ctx) {
2596
+ const data = await getService("upload").getConfiguration();
2597
+ ctx.body = { data };
2598
+ }
2599
+ };
2600
+ const controllers = {
2601
+ "admin-file": adminFile,
2602
+ "admin-folder": adminFolder,
2603
+ "admin-folder-file": adminFolderFile,
2604
+ "admin-settings": adminSettings,
2605
+ "admin-upload": adminUpload,
2606
+ "content-api": contentApi,
2607
+ "view-configuration": viewConfiguration
2608
+ };
2609
+ const index = () => ({
2610
+ register,
2611
+ bootstrap,
2612
+ config,
2613
+ routes,
2614
+ controllers,
2615
+ contentTypes,
2616
+ services
2617
+ });
2618
+ export {
2619
+ FILE_MODEL_UID as F,
2620
+ index as i
2621
+ };
2622
+ //# sourceMappingURL=index-VKvfSVC7.mjs.map