-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesystemManager.js
88 lines (71 loc) · 2.76 KB
/
filesystemManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require('@ostro/support/helpers')
const Manager = require('@ostro/support/manager')
const InvalidArgumentException = require('./invalidArgumentException')
const FilesystemAdapter = require('./filesystemAdapter')
const callbackHandler = require('./callbackHandler')
const kHandler = Symbol('handler')
class FileSystemManager extends Manager {
$type = 'filesystem';
constructor($app) {
super($app)
this[kHandler] = this.getConfig('handler')
if (typeof this[kHandler] != 'function' || typeof this[kHandler] != 'object') {
this[kHandler] = callbackHandler
} else if (typeof this[kHandler] != 'object') {
this[kHandler] = this[kHandler].constructor
util.inherits(this[kHandler], callbackHandler)
} else {
util.inherits(this[kHandler], callbackHandler)
}
this[kHandler] = new this[kHandler]
}
drive($name = null) {
return this.driver($name);
}
disk(name = null) {
return this.driver(name)
}
cloud() {
var name = this.getDefaultCloudDriver();
return this.disk(name)
}
resolve($name) {
var $config = this.getConfig($name);
if (!$config) {
throw new InvalidArgumentException("Disk [{" + $name + "}] was not available.");
}
return super.resolve($name, $config)
}
createLocalDriver($config) {
return this.adapt(new(require('./adapter/local'))($config['root'], $config), $config);
}
createAzureDriver($config) {
return this.adapt(new(require('./adapter/azure'))(new(require('./clients/azure'))($config), $config), $config);
}
createS3Driver($config) {
return this.adapt(new(require('./adapter/s3'))(new(require('./clients/s3'))($config), $config), $config);
}
adapt(adapter, config) {
return new FilesystemAdapter(adapter, this[kHandler]);
}
getConfig(name) {
return super.getConfig(`disks.${name}`);
}
getDefaultCloudDriver() {
return super.getConfig('cloud');
}
registerToRequest(FileRequest) {
let self = this
FileRequest.prototype.store = function(dir = '', disk = '', options = {}) {
options = typeof disk == 'object' ? disk : options
disk = typeof disk == 'object' ? '' : disk
return self.disk((disk || self.getDefaultDriver())).putFile(dir, this, options)
}
FileRequest.prototype.storeAs = function(dir = '', filename, disk = '', options = {}) {
options = typeof disk == 'object' ? disk : options
disk = typeof disk == 'object' ? '' : disk
return self.disk((disk || self.getDefaultDriver())).putFileAs(dir, this, filename, options)
}
}
}
module.exports = FileSystemManager