@arkyn/server 1.4.25 → 1.4.26

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.
@@ -1 +1 @@
1
- {"version":3,"file":"sendFileToS3.d.ts","sourceRoot":"","sources":["../../src/services/sendFileToS3.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,kBAAkB,EAExB,MAAM,iBAAiB,CAAC;AAOzB,KAAK,SAAS,GAAG;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,KAAK,MAAM,GAAG;IACZ,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAmDF,iBAAe,YAAY,CACzB,KAAK,EAAE,kBAAkB,EACzB,WAAW,EAAE,SAAS,EACtB,MAAM,GAAE,MAGP;;;;;;GA2CF;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
1
+ {"version":3,"file":"sendFileToS3.d.ts","sourceRoot":"","sources":["../../src/services/sendFileToS3.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,kBAAkB,EAExB,MAAM,iBAAiB,CAAC;AAOzB,KAAK,SAAS,GAAG;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,aAAa,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,KAAK,MAAM,GAAG;IACZ,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAmDF,iBAAe,YAAY,CACzB,KAAK,EAAE,kBAAkB,EACzB,WAAW,EAAE,SAAS,EACtB,MAAM,GAAE,MAGP;;;;;;GAwEF;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -56,6 +56,8 @@ async function sendFileToS3(props, awsS3Config, config = {
56
56
  const filterParams = getScopedParams(request);
57
57
  const width = filterParams.get("w");
58
58
  const height = filterParams.get("h");
59
+ const reduceQuality = filterParams.get("reduceQuality");
60
+ const quality = reduceQuality ? +reduceQuality : 100;
59
61
  const isImage = file.type.startsWith("image");
60
62
  if (isImage && width && height) {
61
63
  const image = sharp(file.getFilePath());
@@ -70,6 +72,28 @@ async function sendFileToS3(props, awsS3Config, config = {
70
72
  }
71
73
  }
72
74
  }
75
+ if (isImage) {
76
+ let image = sharp(file.getFilePath());
77
+ if (file.type === "image/jpeg") {
78
+ image = image.jpeg({ quality });
79
+ }
80
+ else if (file.type === "image/png") {
81
+ image = image.png({ quality });
82
+ }
83
+ else if (file.type === "image/webp") {
84
+ image = image.webp({ quality });
85
+ }
86
+ const compressedFilePath = file.getFilePath() + "_compressed";
87
+ await image.toFile(compressedFilePath);
88
+ file.getFilePath = () => compressedFilePath;
89
+ const streamFile = fs.createReadStream(file.getFilePath());
90
+ const apiResponse = await s3_upload(streamFile, file.type, awsS3Config);
91
+ fs.unlink(compressedFilePath, (err) => {
92
+ if (err)
93
+ console.error(`Delete image error: ${err}`);
94
+ });
95
+ return { url: apiResponse.location };
96
+ }
73
97
  const streamFile = fs.createReadStream(file.getFilePath());
74
98
  const apiResponse = await s3_upload(streamFile, file.type, awsS3Config);
75
99
  return { url: apiResponse.location };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/server",
3
- "version": "1.4.25",
3
+ "version": "1.4.26",
4
4
  "main": "./dist/bundle.js",
5
5
  "module": "./src/index.ts",
6
6
  "type": "module",
@@ -100,6 +100,9 @@ async function sendFileToS3(
100
100
  const width = filterParams.get("w");
101
101
  const height = filterParams.get("h");
102
102
 
103
+ const reduceQuality = filterParams.get("reduceQuality");
104
+ const quality = reduceQuality ? +reduceQuality : 100;
105
+
103
106
  const isImage = file.type.startsWith("image");
104
107
 
105
108
  if (isImage && width && height) {
@@ -118,6 +121,32 @@ async function sendFileToS3(
118
121
  }
119
122
  }
120
123
 
124
+ if (isImage) {
125
+ let image = sharp(file.getFilePath());
126
+
127
+ if (file.type === "image/jpeg") {
128
+ image = image.jpeg({ quality });
129
+ } else if (file.type === "image/png") {
130
+ image = image.png({ quality });
131
+ } else if (file.type === "image/webp") {
132
+ image = image.webp({ quality });
133
+ }
134
+
135
+ const compressedFilePath = file.getFilePath() + "_compressed";
136
+ await image.toFile(compressedFilePath);
137
+
138
+ file.getFilePath = () => compressedFilePath;
139
+
140
+ const streamFile = fs.createReadStream(file.getFilePath());
141
+ const apiResponse = await s3_upload(streamFile, file.type, awsS3Config);
142
+
143
+ fs.unlink(compressedFilePath, (err) => {
144
+ if (err) console.error(`Delete image error: ${err}`);
145
+ });
146
+
147
+ return { url: apiResponse.location };
148
+ }
149
+
121
150
  const streamFile = fs.createReadStream(file.getFilePath());
122
151
  const apiResponse = await s3_upload(streamFile, file.type, awsS3Config);
123
152