@@ -6,8 +6,8 @@ const path = require('path');
66const uniq = require ( 'lodash/uniq' ) ;
77
88class Proxy {
9- constructor ( config , storage ) {
10- this . storage = storage ;
9+ constructor ( config ) {
10+ this . storages = { } ;
1111 this . provider = Proxy . createProvider ( config ) ;
1212 this . accessManagers = [ this . provider . accessManager ] ;
1313 autobind ( this ) ;
@@ -30,12 +30,27 @@ class Proxy {
3030 return this . isSelfHosted && this . provider . path ;
3131 }
3232
33+ addStorage ( path , storage ) {
34+ const existing = this . storages [ path ] ;
35+ if ( existing ) throw new Error ( `Storage is already mounted on ${ path } path.` ) ;
36+ this . storages [ path ] = storage ;
37+ }
38+
3339 registerAccessManager ( manager ) {
3440 this . accessManagers . push ( manager ) ;
3541 }
3642
43+ getStorage ( key ) {
44+ const path = Object . keys ( this . storages )
45+ . sort ( compareStringsByLengthDesc )
46+ . find ( path => key . startsWith ( path ) ) ;
47+
48+ return path && this . storages [ path ] ;
49+ }
50+
3751 createReadStream ( key ) {
38- return this . storage . createReadStream ( key ) ;
52+ const storage = this . getStorage ( key ) ;
53+ return storage . createReadStream ( key ) ;
3954 }
4055
4156 getFileUrl ( key ) {
@@ -65,3 +80,7 @@ function loadProvider(name) {
6580 throw err ;
6681 }
6782}
83+
84+ function compareStringsByLengthDesc ( fst , sec ) {
85+ return sec . length - fst . length ;
86+ }
0 commit comments