@uxf/resizer 2.2.0 → 2.3.0
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/.resizer-config.json +2 -2
- package/README.md +18 -3
- package/index.js +27 -6
- package/package.json +1 -1
- package/example-assets/assets/logo.png +0 -0
package/.resizer-config.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"source": "/Users/vejvis/UXF/projects/resizer/example-upload/:namespace/:p1/:p2/:filename.:extension"
|
|
9
9
|
},
|
|
10
10
|
{
|
|
11
|
-
"route": "/generated/
|
|
12
|
-
"source": "https://
|
|
11
|
+
"route": "/generated/static/:width(\\d+)_:height(\\d+)/:filename(*).:extension.:toFormat",
|
|
12
|
+
"source": "https://www.uxf.cz/:filename+.:extension"
|
|
13
13
|
}
|
|
14
14
|
]
|
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ You can use configuration file `.resizer-config.json` or environment variable `U
|
|
|
21
21
|
```json
|
|
22
22
|
[
|
|
23
23
|
{
|
|
24
|
-
"route": "/generated/:namespace/:p1/:p2/:filename([a-f0-9\\-]+)_:width(\\d+)_:height(\\d+)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)_:extension.:toFormat",
|
|
24
|
+
"route": "/generated/:namespace/:p1/:p2/:filename([a-f0-9\\-]+)_:width(\\d+)_:height(\\d+)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)_:quality(\\d+)_:extension.:toFormat",
|
|
25
25
|
"source": "/var/www/UXF/projects/resizer/example-upload/:namespace/:p1/:p2/:filename.:extension"
|
|
26
26
|
},
|
|
27
27
|
{
|
|
@@ -29,8 +29,22 @@ You can use configuration file `.resizer-config.json` or environment variable `U
|
|
|
29
29
|
"source": "/var/www/UXF/projects/resizer/example-upload/:namespace/:p1/:p2/:filename.:extension"
|
|
30
30
|
},
|
|
31
31
|
{
|
|
32
|
-
"route": "/generated/
|
|
33
|
-
"source": "https://
|
|
32
|
+
"route": "/generated/static/:width(\\d+)_:height(\\d+)/:filename(*).:extension.:toExtension",
|
|
33
|
+
"source": "https://www.uxf.cz/:filename+"
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### UXF Basic configuration
|
|
39
|
+
```json
|
|
40
|
+
[
|
|
41
|
+
{
|
|
42
|
+
"route": "/generated/static/:width(\\d+|x)_:height(\\d+|x)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)/:version/:filename(*).:extension.:toFormat",
|
|
43
|
+
"source": "https://uxf-base.uxf.dev/:filename+.:extension"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"route": "/generated/:namespace/:p1/:p2/:filename([a-f0-9\\-]+)_:width(\\d+|x)_:height(\\d+|x)_:fit([a-z]+)_:position([a-z]+)_:background([a-z]+)_:trim([a-z]+)_:quality(\\d+)_:extension.:toFormat",
|
|
47
|
+
"source": "https://s3.uxf.dev/${APP_NAME}-${APP_ENV}/:namespace/:p1/:p2/:filename.:extension"
|
|
34
48
|
}
|
|
35
49
|
]
|
|
36
50
|
```
|
|
@@ -45,6 +59,7 @@ You can use configuration file `.resizer-config.json` or environment variable `U
|
|
|
45
59
|
| background | `FFFFFF` or `t` (transparent) | |
|
|
46
60
|
| toFormat | `webp`, `png`, `avif`, `jpg` | `jpg` |
|
|
47
61
|
| trim | `number` or `nt` (not trim) | |
|
|
62
|
+
| quality | `number` (min: 1, max: 100) | 60 |
|
|
48
63
|
|
|
49
64
|
#### Fit
|
|
50
65
|
- `cv`: cover,
|
package/index.js
CHANGED
|
@@ -66,6 +66,10 @@ var POSITION_OPTIONS = {
|
|
|
66
66
|
rt: "right top",
|
|
67
67
|
t: "top",
|
|
68
68
|
};
|
|
69
|
+
var getQuality = function (_a) {
|
|
70
|
+
var quality = _a.quality;
|
|
71
|
+
return (quality ? Number(quality) : 60);
|
|
72
|
+
};
|
|
69
73
|
var getWidth = function (_a) {
|
|
70
74
|
var width = _a.width;
|
|
71
75
|
return (width && width !== "x" ? Number(width) : undefined);
|
|
@@ -100,6 +104,21 @@ function log(message) {
|
|
|
100
104
|
// eslint-disable-next-line no-console
|
|
101
105
|
console.log(message);
|
|
102
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* TODO @vejvis HACK
|
|
109
|
+
*
|
|
110
|
+
* pro odchycení celé cesty i s lomítkama je potřeba udělat hack: /generated/:restPath(*) => restPath bude string
|
|
111
|
+
* pro zpětné složení pomocí pathToRegexp::compile() se používá: /generated/:restPath+ => restPath musí být string[]
|
|
112
|
+
* Tato funkce projde celý objekt a pokud v hodnotě atributu najde "/" tak tento atribut převede na pole
|
|
113
|
+
*/
|
|
114
|
+
var repairParams = function (params) {
|
|
115
|
+
var obj = {};
|
|
116
|
+
Object.keys(params).forEach(function (key) {
|
|
117
|
+
var value = params[key];
|
|
118
|
+
obj[key] = value.includes("/") ? value.split("/") : value;
|
|
119
|
+
});
|
|
120
|
+
return obj;
|
|
121
|
+
};
|
|
103
122
|
var resizerMiddleware = function (config) {
|
|
104
123
|
var middleware = (0, express_1.Router)();
|
|
105
124
|
config.forEach(function (c, i) {
|
|
@@ -107,20 +126,21 @@ var resizerMiddleware = function (config) {
|
|
|
107
126
|
log(" Route: ".concat(c.route));
|
|
108
127
|
log(" Source: ".concat(c.source));
|
|
109
128
|
middleware.get(c.route, function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
110
|
-
var url, domain, sourceFilename, generatedFilename, generatedDirectory, params, sourceFile, _a, pipeline, trim;
|
|
129
|
+
var url, domain, repairedParams, sourceFilename, generatedFilename, generatedDirectory, params, sourceFile, _a, pipeline, trim, quality;
|
|
111
130
|
var _b, _c;
|
|
112
131
|
return __generator(this, function (_d) {
|
|
113
132
|
switch (_d.label) {
|
|
114
133
|
case 0:
|
|
115
134
|
url = c.source.startsWith("http") ? new URL(c.source) : null;
|
|
116
135
|
domain = url === null || url === void 0 ? void 0 : url.origin;
|
|
117
|
-
|
|
136
|
+
repairedParams = repairParams(req.params);
|
|
137
|
+
sourceFilename = (0, path_to_regexp_1.compile)((_b = url === null || url === void 0 ? void 0 : url.pathname) !== null && _b !== void 0 ? _b : c.source, {})(repairedParams);
|
|
118
138
|
generatedFilename = ((_c = process.env.UXF_RESIZER_GENERATE_PATH) !== null && _c !== void 0 ? _c : process.cwd()) + req.path;
|
|
119
139
|
if ((0, fs_1.existsSync)(generatedFilename)) {
|
|
120
140
|
return [2 /*return*/, res.sendFile(generatedFilename)];
|
|
121
141
|
}
|
|
122
142
|
log("----------------------------------------");
|
|
123
|
-
log(
|
|
143
|
+
log(repairedParams);
|
|
124
144
|
log("Source filename: ".concat(sourceFilename));
|
|
125
145
|
log("Generated filename: ".concat(generatedFilename));
|
|
126
146
|
generatedDirectory = (0, path_1.dirname)(generatedFilename);
|
|
@@ -156,13 +176,14 @@ var resizerMiddleware = function (config) {
|
|
|
156
176
|
if (trim !== undefined) {
|
|
157
177
|
pipeline.trim(trim);
|
|
158
178
|
}
|
|
179
|
+
quality = getQuality(params);
|
|
159
180
|
switch (params.toFormat) {
|
|
160
181
|
case "webp":
|
|
161
|
-
pipeline.webp({ quality:
|
|
182
|
+
pipeline.webp({ quality: quality });
|
|
162
183
|
res.setHeader("Content-Type", "image/webp");
|
|
163
184
|
break;
|
|
164
185
|
case "png":
|
|
165
|
-
pipeline.png({ progressive: true, quality:
|
|
186
|
+
pipeline.png({ progressive: true, quality: quality });
|
|
166
187
|
res.setHeader("Content-Type", "image/png");
|
|
167
188
|
break;
|
|
168
189
|
case "avif":
|
|
@@ -170,7 +191,7 @@ var resizerMiddleware = function (config) {
|
|
|
170
191
|
res.setHeader("Content-Type", "image/avif");
|
|
171
192
|
break;
|
|
172
193
|
default:
|
|
173
|
-
pipeline.jpeg({ progressive: true, quality:
|
|
194
|
+
pipeline.jpeg({ progressive: true, quality: quality });
|
|
174
195
|
res.setHeader("Content-Type", "image/jpeg");
|
|
175
196
|
break;
|
|
176
197
|
}
|
package/package.json
CHANGED
|
Binary file
|