@dnax/core 0.9.5 → 0.9.6

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 (2) hide show
  1. package/app/hono.ts +68 -12
  2. package/package.json +5 -1
package/app/hono.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import type { Q, Tenant, sessionCtx } from "../types/";
2
-
2
+ import fs from "fs-extra";
3
+ import sharp from "sharp";
4
+ import path from "path";
5
+ import mime from "mime-types";
3
6
  import { Hono } from "hono";
4
7
  import { getCookie, setCookie } from "hono/cookie";
5
8
  import colors from "@colors/colors/safe";
@@ -551,15 +554,68 @@ function HonoInstance(): typeof app {
551
554
  }
552
555
  });
553
556
 
557
+ // serve static files
558
+ app.get("/files/:folder/public/:filename", async (c, next) => {
559
+ let { folder, filename } = c.req.param();
560
+ const filePath = path.join(
561
+ cleanPath("uploads/" + folder + "/public/"),
562
+ filename
563
+ );
564
+ const width = parseInt(c.req.query("w")) || null;
565
+ const height = parseInt(c.req.query("h")) || null;
566
+ const quality = parseInt(c.req.query("q")) || 80;
567
+ // Vérifier si le fichier existe
568
+ if (!fs.existsSync(filePath)) {
569
+ return c.text("file not found", 404);
570
+ }
571
+
572
+ // Obtenir le type MIME du fichier
573
+ const mimeType = mime.lookup(filePath);
574
+
575
+ // Si ce n'est pas une image, retourner le fichier original
576
+ if (!mimeType || !mimeType.startsWith("image/")) {
577
+ const originalFile = fs.readFileSync(filePath);
578
+ return c.body(originalFile, 200, {
579
+ "Content-Type": mimeType || "application/octet-stream",
580
+ });
581
+ }
582
+
583
+ try {
584
+ // Redimensionner l'image avec Sharp
585
+ const buffer = await sharp(filePath)
586
+ .resize(width, height)
587
+ .jpeg({ quality })
588
+ .toBuffer();
589
+
590
+ // Retourner l'image redimensionnée
591
+ return c.body(buffer, 200, {
592
+ "Content-Type": "image/jpeg",
593
+ });
594
+ } catch (error) {
595
+ // Si une erreur se produit avec Sharp, retourner le fichier original
596
+ const file = fs.readFileSync(filePath);
597
+ return c.body(file, 200, {
598
+ "Content-Type": mimeType || "application/octet-stream",
599
+ });
600
+ }
601
+ });
602
+
554
603
  // media endpoints
555
- Cfg.collections?.map((c) => {
556
- if (c?.type == "media" && c.media?.enabled) {
557
- let mediaPath = cleanPath(
558
- "/files/" + (c?.media?.overwriteFolderName || c?.slug) + "/public/*"
559
- );
560
-
561
- //console.log("Media Path :", mediaPath);
562
- app.get(
604
+ //Cfg.collections?.map((c) => {
605
+ // if (c?.type == "media" && c.media?.enabled) {
606
+ //let mediaPath = cleanPath(
607
+ // "/files/" + (c?.media?.overwriteFolderName || c?.slug) + "/public/*"
608
+ // );
609
+
610
+ /* app.get(optimizeImage, async (c, next) => {
611
+ let width = parseInt(c.req.query("w")) || null;
612
+ let height = parseInt(c.req.query("h")) || null;
613
+ let quality = parseInt(c.req.query("q")) || 80;
614
+
615
+ }); */
616
+
617
+ //console.log("Media Path :", mediaPath);
618
+ /* app.get(
563
619
  mediaPath,
564
620
  serveStatic({
565
621
  root: cleanPath("uploads"),
@@ -568,9 +624,9 @@ function HonoInstance(): typeof app {
568
624
  console.log(`${path} is not found, you access ${c.req.path}`);
569
625
  },
570
626
  })
571
- );
572
- }
573
- });
627
+ ); */
628
+ //}
629
+ //});
574
630
 
575
631
  // Endpoint
576
632
  Cfg?.endpoints?.map((e) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.9.5",
3
+ "version": "0.9.6",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
@@ -8,6 +8,8 @@
8
8
  },
9
9
  "devDependencies": {
10
10
  "@types/bun": "latest",
11
+ "@types/fs-extra": "^11.0.4",
12
+ "@types/mime-types": "^2.1.4",
11
13
  "@types/nodemailer": "^6.4.15",
12
14
  "@types/uuid": "^10.0.0"
13
15
  },
@@ -35,11 +37,13 @@
35
37
  "joi": "^17.13.3",
36
38
  "json-joy": "^16.8.0",
37
39
  "jsonwebtoken": "^9.0.2",
40
+ "mime-types": "^2.1.35",
38
41
  "mingo": "^6.4.15",
39
42
  "moment": "^2.30.1",
40
43
  "mongodb": "^6.8.0",
41
44
  "nodemailer": "^6.9.14",
42
45
  "radash": "^12.1.0",
46
+ "sharp": "^0.33.5",
43
47
  "signaldb": "^0.18.0",
44
48
  "socket.io": "^4.7.5",
45
49
  "ufo": "^1.5.3",