File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+ ```
Original file line number Diff line number Diff line change 22
33const { 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+ } ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ const yup = require('yup');
55const pluginConfigSchema = yup . object ( ) . shape ( {
66 cacheDir : yup . string ( ) ,
77 maxAge : yup . number ( ) . moreThan ( 0 ) ,
8+ paths : yup . array ( ) . of ( yup . string ( ) ) ,
89} ) ;
910
1011module . exports = {
Original file line number Diff line number Diff 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 ( ) ;
Original file line number Diff line number Diff line change 22
33const Router = require ( '@koa/router' )
44const { createIPX } = require ( 'ipx' )
5- const { resolve } = require ( 'path' )
5+ const { resolve } = require ( 'path' )
66const { existsSync, mkdirSync } = require ( 'fs' )
77const { createMiddleware } = require ( './middleware' )
88
99function 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
5151module . exports = {
5252 register,
53- }
53+ }
You can’t perform that action at this time.
0 commit comments