Skip to content

Commit 0992821

Browse files
authored
feat: allow to configure custom paths (#29)
* feat: allow to configure custom paths * fix: missing doc
1 parent c1519a4 commit 0992821

5 files changed

Lines changed: 56 additions & 12 deletions

File tree

docs/guide/index.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,28 @@ module.exports = {
118118
// ...
119119
};
120120
```
121+
122+
### `paths`
123+
124+
Add custom paths to images if you use more than the default path '/uploads'.
125+
126+
> _By default, '/uploads' is set, to cache all default upload routes._
127+
128+
You can set the paths in `config/plugins.js`:
129+
130+
131+
```js [config/plugins.js]
132+
"use strict";
133+
134+
module.exports = {
135+
// ...
136+
137+
"local-image-sharp": {
138+
config: {
139+
paths: ['/uploads','/custom'],
140+
},
141+
},
142+
143+
// ...
144+
};
145+
```

src/config/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
const { pluginConfigSchema } = require('./schema');
44

5-
module.exports = {
5+
const config = {
66
default: ({ env }) => ({
77
cacheDir: env('STRAPI_PLUGIN_LOCAL_IMAGE_SHARP_CACHE_DIR', ''),
88
maxAge: 3600,
9+
paths: ['/uploads']
910
}),
1011
validator(config) {
1112
pluginConfigSchema.validateSync(config);
1213
},
1314
};
15+
16+
module.exports = {
17+
config
18+
};

src/config/schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const yup = require('yup');
55
const pluginConfigSchema = yup.object().shape({
66
cacheDir: yup.string(),
77
maxAge: yup.number().moreThan(0),
8+
paths: yup.array().of(yup.string()),
89
});
910

1011
module.exports = {

src/middleware.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,21 @@ function createMiddleware(ipx) {
1212
const config = strapi.config.get('plugin.local-image-sharp');
1313

1414
return async function ipxMiddleware(ctx, next) {
15-
const [url, query] = ctx.req.url.replace('/uploads', '').split('?');
15+
let path = null;
16+
config.paths.forEach(target => {
17+
if (ctx.req.url.includes(target)) {
18+
path = ctx.req.url.split(target).join('');
19+
}
20+
});
21+
22+
if (!path) {
23+
const statusCode = 500;
24+
const statusMessage = 'No path found';
25+
strapi.log.debug(statusMessage);
26+
return ctx.status = statusCode;
27+
}
28+
29+
const [url, query] = path.split('?');
1630
const [firstSegment = '', ...idSegments] = url
1731
.substr(1 /* leading slash */)
1832
.split('/');
@@ -105,7 +119,6 @@ function createMiddleware(ipx) {
105119

106120
// Create request
107121
const img = ipx(id, modifiers, ctx.req.options);
108-
109122
// Get image meta from source
110123
try {
111124
const src = await img.src();

src/register.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
const Router = require('@koa/router')
44
const { createIPX } = require('ipx')
5-
const { resolve } = require('path')
5+
const { resolve } = require('path')
66
const { existsSync, mkdirSync } = require('fs')
77
const { createMiddleware } = require('./middleware')
88

99
function register({ strapi }) {
1010
const config = strapi.config.get('plugin.local-image-sharp')
11-
config.srcDir = `${strapi.dirs?.static?.public ?? strapi.dirs?.public}/uploads`
11+
config.srcDir = strapi.dirs?.static?.public ?? strapi.dirs?.public
1212

1313
strapi.log.info(
1414
`Using Local Image Sharp plugin`
@@ -36,18 +36,18 @@ function register({ strapi }) {
3636
);
3737
}
3838

39-
40-
const ipx = createIPX({
41-
dir: config.srcDir,
42-
})
4339
const router = new Router()
44-
const middeware = createMiddleware(ipx)
40+
config.paths.forEach(path => {
41+
const ipx = createIPX({
42+
dir: config.srcDir + path,
43+
})
4544

46-
router.get('/uploads/(.*)', middeware)
45+
router.get(path + '/(.*)', createMiddleware(ipx))
46+
})
4747

4848
strapi.server.use(router.routes())
4949
}
5050

5151
module.exports = {
5252
register,
53-
}
53+
}

0 commit comments

Comments
 (0)