Skip to content

Commit f60a50b

Browse files
committed
Enable adding multiple storages to proxy ✨
1 parent a89967c commit f60a50b

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

server/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ const config = require('../config/server');
1717
const logger = require('./shared/logger')();
1818
const storage = require('./shared/storage')(config.storage);
1919
serviceProvider.set('storage', storage);
20-
const storageProxy = require('./shared/storage/proxy')(config.storage.proxy, storage);
20+
const storageProxy = require('./shared/storage/proxy')(config.storage.proxy);
2121
serviceProvider.set('storageProxy', storageProxy);
2222
const router = require('./router');
2323
/* eslint-enable */
2424

25+
storageProxy.addStorage('repository', storage);
2526
const { STORAGE_PATH } = process.env;
2627

2728
const app = express();

server/shared/storage/proxy/index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const path = require('path');
66
const uniq = require('lodash/uniq');
77

88
class 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

Comments
 (0)