@strapi/upload 5.9.0 → 5.10.1

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