@@ -6,8 +6,8 @@ const path = require('path');
6
6
const uniq = require ( 'lodash/uniq' ) ;
7
7
8
8
class Proxy {
9
- constructor ( config , storage ) {
10
- this . storage = storage ;
9
+ constructor ( config ) {
10
+ this . storages = { } ;
11
11
this . provider = Proxy . createProvider ( config ) ;
12
12
this . accessManagers = [ this . provider . accessManager ] ;
13
13
autobind ( this ) ;
@@ -30,12 +30,27 @@ class Proxy {
30
30
return this . isSelfHosted && this . provider . path ;
31
31
}
32
32
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
+
33
39
registerAccessManager ( manager ) {
34
40
this . accessManagers . push ( manager ) ;
35
41
}
36
42
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
+
37
51
createReadStream ( key ) {
38
- return this . storage . createReadStream ( key ) ;
52
+ const storage = this . getStorage ( key ) ;
53
+ return storage . createReadStream ( key ) ;
39
54
}
40
55
41
56
getFileUrl ( key ) {
@@ -65,3 +80,7 @@ function loadProvider(name) {
65
80
throw err ;
66
81
}
67
82
}
83
+
84
+ function compareStringsByLengthDesc ( fst , sec ) {
85
+ return sec . length - fst . length ;
86
+ }
0 commit comments