@ozguraltan/strapi-content-input 1.0.8 → 1.0.9
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.
- package/dist/admin/{ContentInputField-CKTOutN-.mjs → ContentInputField-CykwdQ1S.mjs} +1782 -418
- package/dist/admin/{ContentInputField-CLuL3TFr.js → ContentInputField-DTnK3agK.js} +1781 -417
- package/dist/admin/{index-BIwC3VPC.mjs → index-DzJ89qyO.mjs} +1 -1
- package/dist/admin/{index-DV4pLrQl.js → index-ysKdDQY1.js} +1 -1
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.mjs +1 -1
- package/dist/admin/src/components/widgets/StrapiMediaPicker.d.ts +1 -1
- package/dist/server/index.js +486 -3
- package/dist/server/index.mjs +481 -3
- package/dist/server/src/controllers/ci-image.d.ts +32 -0
- package/dist/server/src/controllers/index.d.ts +9 -0
- package/dist/server/src/index.d.ts +30 -0
- package/dist/server/src/routes/index.d.ts +21 -0
- package/package.json +1 -1
package/dist/server/index.mjs
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import { createRequire } from "module";
|
|
4
|
+
import os from "os";
|
|
5
|
+
import path from "path";
|
|
1
6
|
const register = ({ strapi }) => {
|
|
2
7
|
strapi.customFields.register({
|
|
3
8
|
name: "content-input",
|
|
@@ -156,17 +161,435 @@ const ciMenuController = ({ strapi }) => ({
|
|
|
156
161
|
try {
|
|
157
162
|
const files = await strapi.db.query("plugin::upload.file").findMany({
|
|
158
163
|
orderBy: { createdAt: "desc" },
|
|
159
|
-
|
|
164
|
+
populate: { folder: true },
|
|
165
|
+
limit: 500
|
|
160
166
|
});
|
|
161
|
-
|
|
167
|
+
let folders = [];
|
|
168
|
+
try {
|
|
169
|
+
folders = await strapi.db.query("plugin::upload.folder").findMany({
|
|
170
|
+
orderBy: { name: "asc" },
|
|
171
|
+
populate: { parent: true }
|
|
172
|
+
});
|
|
173
|
+
} catch {
|
|
174
|
+
folders = [];
|
|
175
|
+
}
|
|
176
|
+
ctx.body = {
|
|
177
|
+
data: Array.isArray(files) ? files : [],
|
|
178
|
+
folders: Array.isArray(folders) ? folders : []
|
|
179
|
+
};
|
|
162
180
|
} catch (err) {
|
|
163
181
|
ctx.throw(500, err);
|
|
164
182
|
}
|
|
165
183
|
}
|
|
166
184
|
});
|
|
185
|
+
function getSharp() {
|
|
186
|
+
const req = typeof require !== "undefined" ? require : createRequire(path.join(process.cwd(), "package.json"));
|
|
187
|
+
return req("sharp");
|
|
188
|
+
}
|
|
189
|
+
function getCacheDir(strapi) {
|
|
190
|
+
const rootDir = strapi?.dirs?.app?.root || process.cwd();
|
|
191
|
+
const dir = path.join(rootDir, ".cache", "content-input-images");
|
|
192
|
+
if (!fs.existsSync(dir)) {
|
|
193
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
194
|
+
}
|
|
195
|
+
return dir;
|
|
196
|
+
}
|
|
197
|
+
async function loadSourceBuffer(strapi, src) {
|
|
198
|
+
const publicDir = strapi?.dirs?.static?.public || path.join(process.cwd(), "public");
|
|
199
|
+
if (src.startsWith("/")) {
|
|
200
|
+
const cleanRel = decodeURIComponent(src.split("?")[0]).replace(/^\/+/, "");
|
|
201
|
+
const fullPath = path.resolve(publicDir, cleanRel);
|
|
202
|
+
const resolvedPublic = path.resolve(publicDir);
|
|
203
|
+
if (!fullPath.startsWith(resolvedPublic)) {
|
|
204
|
+
throw new Error("Invalid local file path");
|
|
205
|
+
}
|
|
206
|
+
if (!fs.existsSync(fullPath)) {
|
|
207
|
+
throw new Error(`File not found: ${src}`);
|
|
208
|
+
}
|
|
209
|
+
const buffer = await fs.promises.readFile(fullPath);
|
|
210
|
+
const ext = path.extname(fullPath).toLowerCase();
|
|
211
|
+
const mime = ext === ".png" ? "image/png" : ext === ".webp" ? "image/webp" : ext === ".avif" ? "image/avif" : ext === ".gif" ? "image/gif" : "image/jpeg";
|
|
212
|
+
return { buffer, mime };
|
|
213
|
+
}
|
|
214
|
+
if (/^https?:\/\//i.test(src)) {
|
|
215
|
+
const parsed = new URL(src);
|
|
216
|
+
if ((parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1") && parsed.pathname.startsWith("/uploads/")) {
|
|
217
|
+
const cleanRel = decodeURIComponent(parsed.pathname).replace(/^\/+/, "");
|
|
218
|
+
const fullPath = path.resolve(publicDir, cleanRel);
|
|
219
|
+
if (fs.existsSync(fullPath)) {
|
|
220
|
+
const buffer = await fs.promises.readFile(fullPath);
|
|
221
|
+
return { buffer, mime: "image/jpeg" };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const res = await fetch(src);
|
|
225
|
+
if (!res.ok) {
|
|
226
|
+
throw new Error(`Failed to fetch remote image (${res.status})`);
|
|
227
|
+
}
|
|
228
|
+
const contentType = res.headers.get("content-type") || "image/jpeg";
|
|
229
|
+
const arr = await res.arrayBuffer();
|
|
230
|
+
return { buffer: Buffer.from(arr), mime: contentType };
|
|
231
|
+
}
|
|
232
|
+
throw new Error("Unsupported image source URL");
|
|
233
|
+
}
|
|
234
|
+
const ciImageController = ({ strapi }) => ({
|
|
235
|
+
/**
|
|
236
|
+
* GET /api/content-input/image & GET /content-input/image
|
|
237
|
+
* On-the-fly image scaling & cropping powered by sharp with disk + CDN HTTP caching.
|
|
238
|
+
*/
|
|
239
|
+
async processImage(ctx) {
|
|
240
|
+
try {
|
|
241
|
+
const query = ctx.query || {};
|
|
242
|
+
const src = String(query.src || query.url || "").trim();
|
|
243
|
+
if (!src) {
|
|
244
|
+
return ctx.badRequest("Missing src parameter");
|
|
245
|
+
}
|
|
246
|
+
const processMode = String(query.process || "fit").toLowerCase();
|
|
247
|
+
const w = Math.min(3840, Math.max(0, parseInt(String(query.w || query.cWidth || "0"), 10) || 0));
|
|
248
|
+
const h = Math.min(3840, Math.max(0, parseInt(String(query.h || query.cHeight || "0"), 10) || 0));
|
|
249
|
+
const q = Math.min(100, Math.max(20, parseInt(String(query.q || "85"), 10) || 85));
|
|
250
|
+
const rotate = parseInt(String(query.rotate || "0"), 10) || 0;
|
|
251
|
+
const cx = parseInt(String(query.x || "0"), 10) || 0;
|
|
252
|
+
const cy = parseInt(String(query.y || "0"), 10) || 0;
|
|
253
|
+
const cw = parseInt(String(query.cw || "0"), 10) || 0;
|
|
254
|
+
const ch = parseInt(String(query.ch || "0"), 10) || 0;
|
|
255
|
+
const requestedFormat = String(query.format || "").toLowerCase();
|
|
256
|
+
const cacheKeyRaw = JSON.stringify({
|
|
257
|
+
src,
|
|
258
|
+
processMode,
|
|
259
|
+
w,
|
|
260
|
+
h,
|
|
261
|
+
q,
|
|
262
|
+
rotate,
|
|
263
|
+
cx,
|
|
264
|
+
cy,
|
|
265
|
+
cw,
|
|
266
|
+
ch,
|
|
267
|
+
requestedFormat
|
|
268
|
+
});
|
|
269
|
+
const hash = crypto.createHash("sha1").update(cacheKeyRaw).digest("hex");
|
|
270
|
+
const etag = `"ci-${hash}"`;
|
|
271
|
+
if (ctx.req.headers["if-none-match"] === etag) {
|
|
272
|
+
ctx.status = 304;
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const cacheDir = getCacheDir(strapi);
|
|
276
|
+
const metaPath = path.join(cacheDir, `${hash}.json`);
|
|
277
|
+
const binPath = path.join(cacheDir, `${hash}.bin`);
|
|
278
|
+
if (fs.existsSync(metaPath) && fs.existsSync(binPath)) {
|
|
279
|
+
const meta = JSON.parse(await fs.promises.readFile(metaPath, "utf8"));
|
|
280
|
+
const cachedBuffer = await fs.promises.readFile(binPath);
|
|
281
|
+
ctx.set("Content-Type", meta.contentType || "image/jpeg");
|
|
282
|
+
ctx.set("Cache-Control", "public, max-age=31536000, immutable");
|
|
283
|
+
ctx.set("ETag", etag);
|
|
284
|
+
ctx.set("Access-Control-Allow-Origin", "*");
|
|
285
|
+
ctx.body = cachedBuffer;
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const { buffer: sourceBuffer, mime: sourceMime } = await loadSourceBuffer(strapi, src);
|
|
289
|
+
const sharp = getSharp();
|
|
290
|
+
let pipeline = sharp(sourceBuffer, { failOn: "none" }).rotate();
|
|
291
|
+
if (rotate && [90, 180, 270, -90].includes(rotate)) {
|
|
292
|
+
pipeline = pipeline.rotate(rotate);
|
|
293
|
+
}
|
|
294
|
+
if (cw > 0 && ch > 0) {
|
|
295
|
+
pipeline = pipeline.extract({
|
|
296
|
+
left: Math.max(0, cx),
|
|
297
|
+
top: Math.max(0, cy),
|
|
298
|
+
width: cw,
|
|
299
|
+
height: ch
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
if (processMode === "fit" && w > 0 && h > 0) {
|
|
303
|
+
pipeline = pipeline.resize(w, h, {
|
|
304
|
+
fit: "cover",
|
|
305
|
+
position: "centre",
|
|
306
|
+
withoutEnlargement: false
|
|
307
|
+
});
|
|
308
|
+
} else if (processMode === "widen" && w > 0) {
|
|
309
|
+
pipeline = pipeline.resize(w, null, {
|
|
310
|
+
fit: "inside",
|
|
311
|
+
withoutEnlargement: false
|
|
312
|
+
});
|
|
313
|
+
} else if (w > 0 || h > 0) {
|
|
314
|
+
pipeline = pipeline.resize(w || null, h || null, {
|
|
315
|
+
fit: "inside",
|
|
316
|
+
withoutEnlargement: true
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
let outContentType = sourceMime;
|
|
320
|
+
if (requestedFormat === "webp" || sourceMime === "image/webp") {
|
|
321
|
+
pipeline = pipeline.webp({ quality: q });
|
|
322
|
+
outContentType = "image/webp";
|
|
323
|
+
} else if (requestedFormat === "avif" || sourceMime === "image/avif") {
|
|
324
|
+
pipeline = pipeline.avif({ quality: q });
|
|
325
|
+
outContentType = "image/avif";
|
|
326
|
+
} else if (requestedFormat === "png" || sourceMime === "image/png") {
|
|
327
|
+
pipeline = pipeline.png({ quality: q });
|
|
328
|
+
outContentType = "image/png";
|
|
329
|
+
} else {
|
|
330
|
+
pipeline = pipeline.jpeg({ quality: q, mozjpeg: true });
|
|
331
|
+
outContentType = "image/jpeg";
|
|
332
|
+
}
|
|
333
|
+
const outputBuffer = await pipeline.toBuffer();
|
|
334
|
+
let providerUrl;
|
|
335
|
+
try {
|
|
336
|
+
const cleanSrc = src.split("?")[0];
|
|
337
|
+
const existingFile = await strapi.db.query("plugin::upload.file").findOne({
|
|
338
|
+
where: { url: cleanSrc }
|
|
339
|
+
});
|
|
340
|
+
if (existingFile && existingFile.id) {
|
|
341
|
+
const formatKey = `ci_${processMode}_${w}x${h}${rotate ? `_r${rotate}` : ""}`;
|
|
342
|
+
const existingFormat = existingFile.formats?.[formatKey];
|
|
343
|
+
if (existingFormat?.url) {
|
|
344
|
+
providerUrl = existingFormat.url;
|
|
345
|
+
} else {
|
|
346
|
+
const outMeta = await sharp(outputBuffer).metadata();
|
|
347
|
+
const ext = outContentType === "image/png" ? ".png" : outContentType === "image/webp" ? ".webp" : outContentType === "image/avif" ? ".avif" : ".jpg";
|
|
348
|
+
const variantHash = `${formatKey}_${existingFile.hash || hash.slice(0, 10)}`;
|
|
349
|
+
const variantName = `${formatKey}_${existingFile.name || "image"}`;
|
|
350
|
+
const tmpVariantPath = path.join(os.tmpdir(), `${variantHash}${ext}`);
|
|
351
|
+
await fs.promises.writeFile(tmpVariantPath, outputBuffer);
|
|
352
|
+
try {
|
|
353
|
+
const variantFile = {
|
|
354
|
+
name: variantName,
|
|
355
|
+
hash: variantHash,
|
|
356
|
+
ext,
|
|
357
|
+
mime: outContentType,
|
|
358
|
+
filepath: tmpVariantPath,
|
|
359
|
+
path: existingFile.path || null,
|
|
360
|
+
getStream: () => fs.createReadStream(tmpVariantPath),
|
|
361
|
+
width: outMeta.width || w || void 0,
|
|
362
|
+
height: outMeta.height || h || void 0,
|
|
363
|
+
size: Math.round(outputBuffer.length / 1e3 * 100) / 100,
|
|
364
|
+
sizeInBytes: outputBuffer.length
|
|
365
|
+
};
|
|
366
|
+
const providerService = strapi.plugin("upload").service("provider");
|
|
367
|
+
if (providerService && typeof providerService.upload === "function") {
|
|
368
|
+
await providerService.upload(variantFile);
|
|
369
|
+
providerUrl = variantFile.url;
|
|
370
|
+
const { filepath: _fp, getStream: _gs, ...serializableFormat } = variantFile;
|
|
371
|
+
const nextFormats = {
|
|
372
|
+
...existingFile.formats || {},
|
|
373
|
+
[formatKey]: serializableFormat
|
|
374
|
+
};
|
|
375
|
+
await strapi.db.query("plugin::upload.file").update({
|
|
376
|
+
where: { id: existingFile.id },
|
|
377
|
+
data: { formats: nextFormats }
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
} finally {
|
|
381
|
+
await fs.promises.rm(tmpVariantPath, { force: true }).catch(() => void 0);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
} catch {
|
|
386
|
+
}
|
|
387
|
+
await fs.promises.writeFile(binPath, outputBuffer);
|
|
388
|
+
await fs.promises.writeFile(
|
|
389
|
+
metaPath,
|
|
390
|
+
JSON.stringify({
|
|
391
|
+
contentType: outContentType,
|
|
392
|
+
size: outputBuffer.length,
|
|
393
|
+
providerUrl
|
|
394
|
+
})
|
|
395
|
+
);
|
|
396
|
+
ctx.set("Content-Type", outContentType);
|
|
397
|
+
ctx.set("Cache-Control", "public, max-age=31536000, immutable");
|
|
398
|
+
ctx.set("ETag", etag);
|
|
399
|
+
ctx.set("Access-Control-Allow-Origin", "*");
|
|
400
|
+
if (providerUrl) {
|
|
401
|
+
ctx.set("X-Content-Input-CDN-Url", providerUrl);
|
|
402
|
+
}
|
|
403
|
+
ctx.body = outputBuffer;
|
|
404
|
+
} catch (err) {
|
|
405
|
+
ctx.throw(400, err?.message || "Image processing error");
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
/**
|
|
409
|
+
* POST /content-input/media/crop
|
|
410
|
+
* Interactive crop & rotate using sharp on the backend (works seamlessly with local disk AND remote CDNs).
|
|
411
|
+
* Supports mode: 'replace' (crops original asset in Media Library) or 'duplicate' (saves as new asset).
|
|
412
|
+
*/
|
|
413
|
+
async cropMedia(ctx) {
|
|
414
|
+
const tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "ci-crop-"));
|
|
415
|
+
try {
|
|
416
|
+
const body = ctx.request.body || {};
|
|
417
|
+
const id = body.id;
|
|
418
|
+
const mode = body.mode === "replace" ? "replace" : "duplicate";
|
|
419
|
+
const x = Math.max(0, Math.round(Number(body.x) || 0));
|
|
420
|
+
const y = Math.max(0, Math.round(Number(body.y) || 0));
|
|
421
|
+
const width = Math.max(1, Math.round(Number(body.width) || 0));
|
|
422
|
+
const height = Math.max(1, Math.round(Number(body.height) || 0));
|
|
423
|
+
const rotate = Number(body.rotate) || 0;
|
|
424
|
+
const resizeWidth = Math.max(0, Math.round(Number(body.resizeWidth) || 0));
|
|
425
|
+
const resizeHeight = Math.max(0, Math.round(Number(body.resizeHeight) || 0));
|
|
426
|
+
if (!id) {
|
|
427
|
+
return ctx.badRequest("Asset id is required");
|
|
428
|
+
}
|
|
429
|
+
const existingFile = await strapi.db.query("plugin::upload.file").findOne({
|
|
430
|
+
where: { id: Number(id) },
|
|
431
|
+
populate: { folder: true }
|
|
432
|
+
});
|
|
433
|
+
if (!existingFile) {
|
|
434
|
+
return ctx.notFound("Media asset not found");
|
|
435
|
+
}
|
|
436
|
+
const { buffer: sourceBuffer } = await loadSourceBuffer(strapi, existingFile.url);
|
|
437
|
+
const sharp = getSharp();
|
|
438
|
+
let pipeline = sharp(sourceBuffer, { failOn: "none" }).rotate();
|
|
439
|
+
if (rotate && [90, 180, 270, -90].includes(rotate)) {
|
|
440
|
+
pipeline = pipeline.rotate(rotate);
|
|
441
|
+
}
|
|
442
|
+
const meta = await pipeline.metadata();
|
|
443
|
+
const imgW = meta.width || width;
|
|
444
|
+
const imgH = meta.height || height;
|
|
445
|
+
const safeLeft = Math.min(x, Math.max(0, imgW - 1));
|
|
446
|
+
const safeTop = Math.min(y, Math.max(0, imgH - 1));
|
|
447
|
+
const safeWidth = Math.min(width, imgW - safeLeft);
|
|
448
|
+
const safeHeight = Math.min(height, imgH - safeTop);
|
|
449
|
+
if (safeWidth > 0 && safeHeight > 0) {
|
|
450
|
+
pipeline = pipeline.extract({
|
|
451
|
+
left: safeLeft,
|
|
452
|
+
top: safeTop,
|
|
453
|
+
width: safeWidth,
|
|
454
|
+
height: safeHeight
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
if (resizeWidth > 0 || resizeHeight > 0) {
|
|
458
|
+
pipeline = pipeline.resize(resizeWidth || null, resizeHeight || null, {
|
|
459
|
+
fit: resizeWidth > 0 && resizeHeight > 0 ? "fill" : "inside"
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
const ext = (existingFile.ext || ".jpg").toLowerCase();
|
|
463
|
+
const mime = existingFile.mime || "image/jpeg";
|
|
464
|
+
if (ext === ".png" || mime === "image/png") {
|
|
465
|
+
pipeline = pipeline.png({ quality: 92 });
|
|
466
|
+
} else if (ext === ".webp" || mime === "image/webp") {
|
|
467
|
+
pipeline = pipeline.webp({ quality: 90 });
|
|
468
|
+
} else {
|
|
469
|
+
pipeline = pipeline.jpeg({ quality: 90, mozjpeg: true });
|
|
470
|
+
}
|
|
471
|
+
const croppedBuffer = await pipeline.toBuffer();
|
|
472
|
+
const baseName = path.basename(existingFile.name || "image", existingFile.ext || ext);
|
|
473
|
+
const targetFileName = mode === "duplicate" ? `${body.name || `${baseName}-cropped`}${ext.startsWith(".") ? ext : `.${ext}`}` : existingFile.name || `${baseName}${ext}`;
|
|
474
|
+
const tmpFilePath = path.join(tmpDir, targetFileName);
|
|
475
|
+
await fs.promises.writeFile(tmpFilePath, croppedBuffer);
|
|
476
|
+
const uploadService = strapi.plugin("upload").service("upload");
|
|
477
|
+
const fileInfo = {
|
|
478
|
+
name: targetFileName,
|
|
479
|
+
alternativeText: body.alternativeText !== void 0 ? body.alternativeText : existingFile.alternativeText || "",
|
|
480
|
+
caption: body.caption !== void 0 ? body.caption : existingFile.caption || "",
|
|
481
|
+
folder: existingFile.folder?.id ?? existingFile.folder ?? null
|
|
482
|
+
};
|
|
483
|
+
const fileObj = {
|
|
484
|
+
filepath: tmpFilePath,
|
|
485
|
+
originalFilename: targetFileName,
|
|
486
|
+
mimetype: mime,
|
|
487
|
+
detectedMimeType: mime,
|
|
488
|
+
size: croppedBuffer.length
|
|
489
|
+
};
|
|
490
|
+
const user = ctx.state?.user;
|
|
491
|
+
let resultFile;
|
|
492
|
+
if (mode === "replace") {
|
|
493
|
+
resultFile = await uploadService.replace(
|
|
494
|
+
existingFile.id,
|
|
495
|
+
{
|
|
496
|
+
data: { fileInfo },
|
|
497
|
+
file: fileObj
|
|
498
|
+
},
|
|
499
|
+
{ user }
|
|
500
|
+
);
|
|
501
|
+
} else {
|
|
502
|
+
const uploaded = await uploadService.upload(
|
|
503
|
+
{
|
|
504
|
+
data: { fileInfo },
|
|
505
|
+
files: [fileObj]
|
|
506
|
+
},
|
|
507
|
+
{ user }
|
|
508
|
+
);
|
|
509
|
+
resultFile = Array.isArray(uploaded) ? uploaded[0] : uploaded;
|
|
510
|
+
}
|
|
511
|
+
ctx.body = { data: resultFile };
|
|
512
|
+
} catch (err) {
|
|
513
|
+
ctx.throw(500, err?.message || "Failed to crop media asset");
|
|
514
|
+
} finally {
|
|
515
|
+
try {
|
|
516
|
+
await fs.promises.rm(tmpDir, { recursive: true, force: true });
|
|
517
|
+
} catch {
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
},
|
|
521
|
+
/**
|
|
522
|
+
* PUT /content-input/media/:id
|
|
523
|
+
* Update file metadata (name, alternativeText, caption, folder) in Strapi Media Library.
|
|
524
|
+
*/
|
|
525
|
+
async updateMediaInfo(ctx) {
|
|
526
|
+
try {
|
|
527
|
+
const id = Number(ctx.params?.id);
|
|
528
|
+
if (!id) return ctx.badRequest("Asset id is required");
|
|
529
|
+
const { name, alternativeText, caption, folder } = ctx.request.body || {};
|
|
530
|
+
const uploadService = strapi.plugin("upload").service("upload");
|
|
531
|
+
const updated = await uploadService.updateFileInfo(
|
|
532
|
+
id,
|
|
533
|
+
{
|
|
534
|
+
name,
|
|
535
|
+
alternativeText,
|
|
536
|
+
caption,
|
|
537
|
+
folder: folder !== void 0 ? folder : void 0
|
|
538
|
+
},
|
|
539
|
+
{ user: ctx.state?.user }
|
|
540
|
+
);
|
|
541
|
+
ctx.body = { data: updated };
|
|
542
|
+
} catch (err) {
|
|
543
|
+
ctx.throw(500, err?.message || "Failed to update media info");
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
/**
|
|
547
|
+
* DELETE /content-input/media/:id
|
|
548
|
+
* Delete file from Strapi Media Library & configured upload provider (local or CDN).
|
|
549
|
+
*/
|
|
550
|
+
async deleteMedia(ctx) {
|
|
551
|
+
try {
|
|
552
|
+
const id = Number(ctx.params?.id);
|
|
553
|
+
if (!id) return ctx.badRequest("Asset id is required");
|
|
554
|
+
const existingFile = await strapi.db.query("plugin::upload.file").findOne({
|
|
555
|
+
where: { id }
|
|
556
|
+
});
|
|
557
|
+
if (!existingFile) return ctx.notFound("Media asset not found");
|
|
558
|
+
const uploadService = strapi.plugin("upload").service("upload");
|
|
559
|
+
const removed = await uploadService.remove(existingFile);
|
|
560
|
+
ctx.body = { data: removed };
|
|
561
|
+
} catch (err) {
|
|
562
|
+
ctx.throw(500, err?.message || "Failed to delete media asset");
|
|
563
|
+
}
|
|
564
|
+
},
|
|
565
|
+
/**
|
|
566
|
+
* POST /content-input/media/folder
|
|
567
|
+
* Create a new folder in Strapi Media Library.
|
|
568
|
+
*/
|
|
569
|
+
async createFolder(ctx) {
|
|
570
|
+
try {
|
|
571
|
+
const { name, parent } = ctx.request.body || {};
|
|
572
|
+
if (!name || !String(name).trim()) {
|
|
573
|
+
return ctx.badRequest("Folder name is required");
|
|
574
|
+
}
|
|
575
|
+
const folderService = strapi.plugin("upload").service("folder");
|
|
576
|
+
const created = await folderService.create(
|
|
577
|
+
{
|
|
578
|
+
name: String(name).trim(),
|
|
579
|
+
parent: parent ? Number(parent) : null
|
|
580
|
+
},
|
|
581
|
+
{ user: ctx.state?.user }
|
|
582
|
+
);
|
|
583
|
+
ctx.body = { data: created };
|
|
584
|
+
} catch (err) {
|
|
585
|
+
ctx.throw(500, err?.message || "Failed to create folder");
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
});
|
|
167
589
|
const controllers = {
|
|
168
590
|
"ci-template": ciTemplateController,
|
|
169
|
-
"ci-menu": ciMenuController
|
|
591
|
+
"ci-menu": ciMenuController,
|
|
592
|
+
"ci-image": ciImageController
|
|
170
593
|
};
|
|
171
594
|
const routes = {
|
|
172
595
|
admin: {
|
|
@@ -243,6 +666,61 @@ const routes = {
|
|
|
243
666
|
config: {
|
|
244
667
|
policies: []
|
|
245
668
|
}
|
|
669
|
+
},
|
|
670
|
+
{
|
|
671
|
+
method: "POST",
|
|
672
|
+
path: "/media/crop",
|
|
673
|
+
handler: "ci-image.cropMedia",
|
|
674
|
+
config: {
|
|
675
|
+
policies: []
|
|
676
|
+
}
|
|
677
|
+
},
|
|
678
|
+
{
|
|
679
|
+
method: "PUT",
|
|
680
|
+
path: "/media/:id",
|
|
681
|
+
handler: "ci-image.updateMediaInfo",
|
|
682
|
+
config: {
|
|
683
|
+
policies: []
|
|
684
|
+
}
|
|
685
|
+
},
|
|
686
|
+
{
|
|
687
|
+
method: "DELETE",
|
|
688
|
+
path: "/media/:id",
|
|
689
|
+
handler: "ci-image.deleteMedia",
|
|
690
|
+
config: {
|
|
691
|
+
policies: []
|
|
692
|
+
}
|
|
693
|
+
},
|
|
694
|
+
{
|
|
695
|
+
method: "POST",
|
|
696
|
+
path: "/media/folder",
|
|
697
|
+
handler: "ci-image.createFolder",
|
|
698
|
+
config: {
|
|
699
|
+
policies: []
|
|
700
|
+
}
|
|
701
|
+
},
|
|
702
|
+
{
|
|
703
|
+
method: "GET",
|
|
704
|
+
path: "/image",
|
|
705
|
+
handler: "ci-image.processImage",
|
|
706
|
+
config: {
|
|
707
|
+
auth: false,
|
|
708
|
+
policies: []
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
]
|
|
712
|
+
},
|
|
713
|
+
"content-api": {
|
|
714
|
+
type: "content-api",
|
|
715
|
+
routes: [
|
|
716
|
+
{
|
|
717
|
+
method: "GET",
|
|
718
|
+
path: "/image",
|
|
719
|
+
handler: "ci-image.processImage",
|
|
720
|
+
config: {
|
|
721
|
+
auth: false,
|
|
722
|
+
policies: []
|
|
723
|
+
}
|
|
246
724
|
}
|
|
247
725
|
]
|
|
248
726
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Core } from '@strapi/strapi';
|
|
2
|
+
declare const ciImageController: ({ strapi }: {
|
|
3
|
+
strapi: Core.Strapi;
|
|
4
|
+
}) => {
|
|
5
|
+
/**
|
|
6
|
+
* GET /api/content-input/image & GET /content-input/image
|
|
7
|
+
* On-the-fly image scaling & cropping powered by sharp with disk + CDN HTTP caching.
|
|
8
|
+
*/
|
|
9
|
+
processImage(ctx: any): Promise<any>;
|
|
10
|
+
/**
|
|
11
|
+
* POST /content-input/media/crop
|
|
12
|
+
* Interactive crop & rotate using sharp on the backend (works seamlessly with local disk AND remote CDNs).
|
|
13
|
+
* Supports mode: 'replace' (crops original asset in Media Library) or 'duplicate' (saves as new asset).
|
|
14
|
+
*/
|
|
15
|
+
cropMedia(ctx: any): Promise<any>;
|
|
16
|
+
/**
|
|
17
|
+
* PUT /content-input/media/:id
|
|
18
|
+
* Update file metadata (name, alternativeText, caption, folder) in Strapi Media Library.
|
|
19
|
+
*/
|
|
20
|
+
updateMediaInfo(ctx: any): Promise<any>;
|
|
21
|
+
/**
|
|
22
|
+
* DELETE /content-input/media/:id
|
|
23
|
+
* Delete file from Strapi Media Library & configured upload provider (local or CDN).
|
|
24
|
+
*/
|
|
25
|
+
deleteMedia(ctx: any): Promise<any>;
|
|
26
|
+
/**
|
|
27
|
+
* POST /content-input/media/folder
|
|
28
|
+
* Create a new folder in Strapi Media Library.
|
|
29
|
+
*/
|
|
30
|
+
createFolder(ctx: any): Promise<any>;
|
|
31
|
+
};
|
|
32
|
+
export default ciImageController;
|
|
@@ -16,5 +16,14 @@ declare const _default: {
|
|
|
16
16
|
entities(ctx: any): Promise<void>;
|
|
17
17
|
mediaList(ctx: any): Promise<void>;
|
|
18
18
|
};
|
|
19
|
+
'ci-image': ({ strapi }: {
|
|
20
|
+
strapi: import('@strapi/types/dist/core/strapi.js').Strapi;
|
|
21
|
+
}) => {
|
|
22
|
+
processImage(ctx: any): Promise<any>;
|
|
23
|
+
cropMedia(ctx: any): Promise<any>;
|
|
24
|
+
updateMediaInfo(ctx: any): Promise<any>;
|
|
25
|
+
deleteMedia(ctx: any): Promise<any>;
|
|
26
|
+
createFolder(ctx: any): Promise<any>;
|
|
27
|
+
};
|
|
19
28
|
};
|
|
20
29
|
export default _default;
|
|
@@ -32,15 +32,45 @@ declare const _default: {
|
|
|
32
32
|
entities(ctx: any): Promise<void>;
|
|
33
33
|
mediaList(ctx: any): Promise<void>;
|
|
34
34
|
};
|
|
35
|
+
'ci-image': ({ strapi }: {
|
|
36
|
+
strapi: import('@strapi/types/dist/core/strapi.js').Strapi;
|
|
37
|
+
}) => {
|
|
38
|
+
processImage(ctx: any): Promise<any>;
|
|
39
|
+
cropMedia(ctx: any): Promise<any>;
|
|
40
|
+
updateMediaInfo(ctx: any): Promise<any>;
|
|
41
|
+
deleteMedia(ctx: any): Promise<any>;
|
|
42
|
+
createFolder(ctx: any): Promise<any>;
|
|
43
|
+
};
|
|
35
44
|
};
|
|
36
45
|
routes: {
|
|
37
46
|
admin: {
|
|
47
|
+
type: string;
|
|
48
|
+
routes: ({
|
|
49
|
+
method: string;
|
|
50
|
+
path: string;
|
|
51
|
+
handler: string;
|
|
52
|
+
config: {
|
|
53
|
+
policies: any[];
|
|
54
|
+
auth?: undefined;
|
|
55
|
+
};
|
|
56
|
+
} | {
|
|
57
|
+
method: string;
|
|
58
|
+
path: string;
|
|
59
|
+
handler: string;
|
|
60
|
+
config: {
|
|
61
|
+
auth: boolean;
|
|
62
|
+
policies: any[];
|
|
63
|
+
};
|
|
64
|
+
})[];
|
|
65
|
+
};
|
|
66
|
+
'content-api': {
|
|
38
67
|
type: string;
|
|
39
68
|
routes: {
|
|
40
69
|
method: string;
|
|
41
70
|
path: string;
|
|
42
71
|
handler: string;
|
|
43
72
|
config: {
|
|
73
|
+
auth: boolean;
|
|
44
74
|
policies: any[];
|
|
45
75
|
};
|
|
46
76
|
}[];
|
|
@@ -1,11 +1,32 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
2
|
admin: {
|
|
3
|
+
type: string;
|
|
4
|
+
routes: ({
|
|
5
|
+
method: string;
|
|
6
|
+
path: string;
|
|
7
|
+
handler: string;
|
|
8
|
+
config: {
|
|
9
|
+
policies: any[];
|
|
10
|
+
auth?: undefined;
|
|
11
|
+
};
|
|
12
|
+
} | {
|
|
13
|
+
method: string;
|
|
14
|
+
path: string;
|
|
15
|
+
handler: string;
|
|
16
|
+
config: {
|
|
17
|
+
auth: boolean;
|
|
18
|
+
policies: any[];
|
|
19
|
+
};
|
|
20
|
+
})[];
|
|
21
|
+
};
|
|
22
|
+
'content-api': {
|
|
3
23
|
type: string;
|
|
4
24
|
routes: {
|
|
5
25
|
method: string;
|
|
6
26
|
path: string;
|
|
7
27
|
handler: string;
|
|
8
28
|
config: {
|
|
29
|
+
auth: boolean;
|
|
9
30
|
policies: any[];
|
|
10
31
|
};
|
|
11
32
|
}[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ozguraltan/strapi-content-input",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "Locomotive CMS ContentInput visual page/content builder ported to Strapi v5 as a native JSONB Custom Field plugin.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Ozgur Altan <ozguraltan05@gmail.com>",
|