@ostro/filesystem 0.0.0-alpha.3 → 0.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,98 +1,98 @@
1
- const path = require('path')
2
- const FileNotFoundException = require('./fileNotFoundException')
3
- const FileDeleteException = require('./fileDeleteException')
4
- const FileUploadException = require('./fileUploadException')
5
- const DirectoryCreateException = require('./directoryCreateException')
6
- const kAdapter = Symbol('adapter')
7
- const kHandler = Symbol('handler')
8
- class FilesystemAdapter {
9
- constructor($adapter, $handler) {
10
- this[kAdapter] = $adapter;
11
- this[kHandler] = $handler;
12
- }
13
-
14
- exists($path, options) {
15
-
16
- return new Promise((resolve, reject) => {
17
- this[kAdapter].has($path, options, (err, data, basePath) => {
18
- this[kHandler].exists(null, (err ? false : true), resolve, $path, options, basePath)
19
- })
20
- })
21
- }
22
-
23
- path($path) {
24
- return this[kAdapter].getAdapter().getPathPrefix().$path;
25
- }
26
-
27
- get($path, options) {
28
-
29
- return new Promise((resolve, reject) => {
30
- this[kAdapter].read($path, options, (err, data, basePath) => {
31
- if (err) {
32
- this[kHandler].get(new FileNotFoundException(err), null, reject, $path, options, basePath)
33
- } else {
34
- this[kHandler].get(null, (typeof data == 'function' ? data() : data), resolve, $path, options, basePath)
35
- }
36
- })
37
- })
38
- }
39
-
40
- delete($path, ...rest) {
41
- let callback = Array.from(rest).find(arg => typeof arg == 'function')
42
- let options = Array.from(rest).find(arg => typeof arg == 'object') || {}
43
- if (callback)
44
- return this[kAdapter].delete($path, options, callback)
45
- else {
46
- return new Promise((resolve, reject) => {
47
- this[kAdapter].delete($path, options, (err, data, basePath) => {
48
- if (err) {
49
- this[kHandler].delete(new FileDeleteException(err), null, reject, $path, options, basePath)
50
- } else {
51
- this[kHandler].delete(null, (typeof data == 'function' ? data() : data), resolve, $path, options, basePath)
52
- }
53
- })
54
- })
55
- }
56
- }
57
-
58
- makeDirectory($path, options) {
59
- return new Promise((resolve, reject) => {
60
- this[kAdapter].createDir($path, options, (err, data, basePath) => {
61
- if (err) {
62
- this[kHandler].createDir(new DirectoryCreateException(err), null, reject, $path, options, basePath)
63
- } else {
64
- this[kHandler].createDir(null, (typeof data == 'function' ? data() : data), resolve, $path, options, basePath)
65
- }
66
- })
67
- })
68
- }
69
-
70
- put($path, content, options = {}) {
71
-
72
- return new Promise((resolve, reject) => {
73
- this[kAdapter].write($path, content, options, (err, data, basePath) => {
74
- if (err) {
75
- this[kHandler].delete(new FileUploadException(err), null, reject, $path, options, basePath, content)
76
- } else {
77
- this[kHandler].put(null, (typeof data == 'function' ? data() : data), resolve, $path, options, basePath)
78
- }
79
- })
80
- })
81
- }
82
-
83
- putFile($path, $file, $options = {}) {
84
- $options.filename = $file.getHashname()
85
- return this.putFileAs($path, $file, $options);
86
- }
87
-
88
- putFileAs($path, $file, $name, $options = {}) {
89
- $path = path.join($path,$name)
90
- return this.put($path, $file.getBufferData(), $options);
91
- }
92
-
93
- getAdapter() {
94
- return this[kAdapter];
95
- }
96
-
97
- }
1
+ const path = require('path')
2
+ const FileNotFoundException = require('./fileNotFoundException')
3
+ const FileDeleteException = require('./fileDeleteException')
4
+ const FileUploadException = require('./fileUploadException')
5
+ const DirectoryCreateException = require('./directoryCreateException')
6
+ const kAdapter = Symbol('adapter')
7
+ const kHandler = Symbol('handler')
8
+ class FilesystemAdapter {
9
+ constructor($adapter, $handler) {
10
+ this[kAdapter] = $adapter;
11
+ this[kHandler] = $handler;
12
+ }
13
+
14
+ exists($path, options) {
15
+
16
+ return new Promise((resolve, reject) => {
17
+ this[kAdapter].has($path, options, (err, data, basePath) => {
18
+ this[kHandler].exists(null, (err ? false : true), resolve, $path, options, basePath)
19
+ })
20
+ })
21
+ }
22
+
23
+ path($path) {
24
+ return this[kAdapter].getAdapter().getPathPrefix().$path;
25
+ }
26
+
27
+ get($path, options) {
28
+
29
+ return new Promise((resolve, reject) => {
30
+ this[kAdapter].read($path, options, (err, data, basePath) => {
31
+ if (err) {
32
+ this[kHandler].get(new FileNotFoundException(err), null, reject, $path, options, basePath)
33
+ } else {
34
+ this[kHandler].get(null, (typeof data == 'function' ? data() : data), resolve, $path, options, basePath)
35
+ }
36
+ })
37
+ })
38
+ }
39
+
40
+ delete($path, ...rest) {
41
+ let callback = Array.from(rest).find(arg => typeof arg == 'function')
42
+ let options = Array.from(rest).find(arg => typeof arg == 'object') || {}
43
+ if (callback)
44
+ return this[kAdapter].delete($path, options, callback)
45
+ else {
46
+ return new Promise((resolve, reject) => {
47
+ this[kAdapter].delete($path, options, (err, data, basePath) => {
48
+ if (err) {
49
+ this[kHandler].delete(new FileDeleteException(err), null, reject, $path, options, basePath)
50
+ } else {
51
+ this[kHandler].delete(null, (typeof data == 'function' ? data() : data), resolve, $path, options, basePath)
52
+ }
53
+ })
54
+ })
55
+ }
56
+ }
57
+
58
+ makeDirectory($path, options) {
59
+ return new Promise((resolve, reject) => {
60
+ this[kAdapter].createDir($path, options, (err, data, basePath) => {
61
+ if (err) {
62
+ this[kHandler].createDir(new DirectoryCreateException(err), null, reject, $path, options, basePath)
63
+ } else {
64
+ this[kHandler].createDir(null, (typeof data == 'function' ? data() : data), resolve, $path, options, basePath)
65
+ }
66
+ })
67
+ })
68
+ }
69
+
70
+ put($path, content, options = {}) {
71
+
72
+ return new Promise((resolve, reject) => {
73
+ this[kAdapter].write($path, content, options, (err, data, basePath) => {
74
+ if (err) {
75
+ this[kHandler].delete(new FileUploadException(err), null, reject, $path, options, basePath, content)
76
+ } else {
77
+ this[kHandler].put(null, (typeof data == 'function' ? data() : data), resolve, $path, options, basePath)
78
+ }
79
+ })
80
+ })
81
+ }
82
+
83
+ putFile($path, $file, $options = {}) {
84
+ $options.filename = $file.getHashname()
85
+ return this.putFileAs($path, $file, $options);
86
+ }
87
+
88
+ putFileAs($path, $file, $name, $options = {}) {
89
+ $path = path.join($path,$name)
90
+ return this.put($path, $file.getBufferData(), $options);
91
+ }
92
+
93
+ getAdapter() {
94
+ return this[kAdapter];
95
+ }
96
+
97
+ }
98
98
  module.exports = FilesystemAdapter
@@ -1,135 +1,88 @@
1
- require('@ostro/support/helpers')
2
- const { Macroable } = require('@ostro/support/macro')
3
- const ObjectGet = require('lodash.get')
4
- const InvalidArgumentException = require('./invalidArgumentException')
5
- const FilesystemAdapter = require('./filesystemAdapter')
6
- const callbackHandler = require('./callbackHandler')
7
- const util = require('util')
8
- const kApp = Symbol('app')
9
- const kDisks = Symbol('disks')
10
- const kHandler = Symbol('handler')
11
- const kCustomCreators = Symbol('customCreators')
12
- class FileSystemManager extends Macroable {
13
- constructor($app) {
14
- super()
15
- Object.defineProperties(this, {
16
- [kApp]: {
17
- value: $app,
18
- },
19
- [kDisks]: {
20
- value: Object.create(null),
21
- writable: true,
22
- },
23
- [kCustomCreators]: {
24
- value: Object.create(null),
25
- writable: true,
26
- },
27
-
28
- })
29
- this[kHandler] = this.getConfig('handler')
30
- if (typeof this[kHandler] != 'function' || typeof this[kHandler] != 'object') {
31
- this[kHandler] = callbackHandler
32
- } else if (typeof this[kHandler] != 'object') {
33
- this[kHandler] = this[kHandler].constructor
34
- util.inherits(this[kHandler], callbackHandler)
35
- } else {
36
- util.inherits(this[kHandler], callbackHandler)
37
- }
38
- this[kHandler] = new this[kHandler]
39
- }
40
-
41
- drive($name = null) {
42
- return this.disk($name);
43
- }
44
-
45
- driver($name = null) {
46
- return this.disk($name);
47
- }
48
-
49
- disk(name = null) {
50
- name = name ? name : this.getDefaultDriver();
51
- return this[kDisks][name] = this.getDriver(name);
52
- }
53
- cloud() {
54
- var name = this.getDefaultCloudDriver();
55
- return this[kDisks][name] = this.getDriver(name);
56
- }
57
-
58
- getDriver(name) {
59
- return this[kDisks][name] || this.resolve(name);
60
- }
61
-
62
- resolve($name) {
63
- var config = this.getConfig($name);
64
- if (!config) {
65
- throw new InvalidArgumentException("Disk [{" + $name + "}] was not available.");
66
- }
67
- var driverMethod = 'create' + (config['driver']).ucfirst() + 'Driver';
68
- if ((this[kCustomCreators][config['driver']])) {
69
- return this.callCustomCreator(config);
70
- } else if (this[driverMethod]) {
71
- return this[driverMethod](config);
72
- } else {
73
- throw new InvalidArgumentException("Driver [{" + config['driver'] + "}] is not supported.");
74
- }
75
- }
76
-
77
- callCustomCreator($config) {
78
- return this[kCustomCreators][$config['driver']];
79
- }
80
-
81
- createLocalDriver($config) {
82
- return this.adapt(new(require('./adapter/local'))($config['root'], $config), $config);
83
- }
84
-
85
- createAzureDriver($config) {
86
- return this.adapt(new(require('./adapter/azure'))(new(require('./clients/azure'))($config), $config), $config);
87
- }
88
-
89
- createS3Driver($config) {
90
- return this.adapt(new(require('./adapter/s3'))(new(require('./clients/s3'))($config), $config), $config);
91
- }
92
-
93
- adapt(adapter, config) {
94
- return new FilesystemAdapter(adapter, this[kHandler]);
95
- }
96
-
97
- getConfig(name) {
98
- return ObjectGet(this[kApp]['config']['filesystem']['disks'], name);
99
- }
100
-
101
- getDefaultDriver() {
102
- return this[kApp]['config']['filesystem']['default'];
103
- }
104
-
105
- getDefaultCloudDriver() {
106
- return this[kApp]['config']['filesystem']['cloud'];
107
- }
108
- registerToRequest(FileRequest) {
109
- let self = this
110
- FileRequest.prototype.store = function(dir = '', disk = '', options = {}) {
111
- options = typeof disk == 'object' ? disk : options
112
- disk = typeof disk == 'object' ? '' : disk
113
- return self.disk((disk || self.getDefaultDriver())).putFile(dir, this, options)
114
- }
115
- FileRequest.prototype.storeAs = function(dir = '', filename, disk = '', options = {}) {
116
- options = typeof disk == 'object' ? disk : options
117
- disk = typeof disk == 'object' ? '' : disk
118
- return self.disk((disk || self.getDefaultDriver())).putFileAs(dir, this, filename, options)
119
- }
120
-
121
- }
122
- extends($driver, $callback) {
123
- const config = this.getConfig($driver)
124
- if (!config) {
125
- throw new InvalidArgumentException(`Config not found for [{${$driver}}] driver.`);
126
- }
127
- this[kCustomCreators][config['driver']] = $callback.call(this, this);
128
- return this;
129
- }
130
- __get(target, method) {
131
- return this.make(target.disk(), method)
132
- }
133
- }
134
-
1
+ require('@ostro/support/helpers')
2
+ const Manager = require('@ostro/support/manager')
3
+ const InvalidArgumentException = require('./invalidArgumentException')
4
+ const FilesystemAdapter = require('./filesystemAdapter')
5
+ const callbackHandler = require('./callbackHandler')
6
+ const kHandler = Symbol('handler')
7
+ class FileSystemManager extends Manager {
8
+
9
+ $type = 'filesystem';
10
+
11
+ constructor($app){
12
+ super($app)
13
+ this[kHandler] = this.getConfig('handler')
14
+ if (typeof this[kHandler] != 'function' || typeof this[kHandler] != 'object') {
15
+ this[kHandler] = callbackHandler
16
+ } else if (typeof this[kHandler] != 'object') {
17
+ this[kHandler] = this[kHandler].constructor
18
+ util.inherits(this[kHandler], callbackHandler)
19
+ } else {
20
+ util.inherits(this[kHandler], callbackHandler)
21
+ }
22
+ this[kHandler] = new this[kHandler]
23
+ }
24
+
25
+ drive($name = null) {
26
+ return this.driver($name);
27
+ }
28
+
29
+ disk(name = null) {
30
+ return this.driver(name)
31
+ }
32
+
33
+ cloud() {
34
+ var name = this.getDefaultCloudDriver();
35
+ return this.disk(name)
36
+ }
37
+
38
+ resolve($name) {
39
+ var $config = this.getConfig($name);
40
+ if (!$config) {
41
+ throw new InvalidArgumentException("Disk [{" + $name + "}] was not available.");
42
+ }
43
+ return super.resolve($name, $config)
44
+ }
45
+
46
+
47
+ createLocalDriver($config) {
48
+ return this.adapt(new(require('./adapter/local'))($config['root'], $config), $config);
49
+ }
50
+
51
+ createAzureDriver($config) {
52
+ return this.adapt(new(require('./adapter/azure'))(new(require('./clients/azure'))($config), $config), $config);
53
+ }
54
+
55
+ createS3Driver($config) {
56
+ return this.adapt(new(require('./adapter/s3'))(new(require('./clients/s3'))($config), $config), $config);
57
+ }
58
+
59
+ adapt(adapter, config) {
60
+ return new FilesystemAdapter(adapter,this[kHandler]);
61
+ }
62
+
63
+ getConfig(name) {
64
+ return super.getConfig(`disks.${name}`);
65
+ }
66
+
67
+ getDefaultCloudDriver() {
68
+ return this.getConfig('cloud');
69
+ }
70
+
71
+ registerToRequest(FileRequest) {
72
+ let self = this
73
+ FileRequest.prototype.store = function(dir = '', disk = '', options = {}) {
74
+ options = typeof disk == 'object' ? disk : options
75
+ disk = typeof disk == 'object' ? '' : disk
76
+ return self.disk((disk || self.getDefaultDriver())).putFile(dir, this, options)
77
+ }
78
+ FileRequest.prototype.storeAs = function(dir = '', filename, disk = '', options = {}) {
79
+ options = typeof disk == 'object' ? disk : options
80
+ disk = typeof disk == 'object' ? '' : disk
81
+ return self.disk((disk || self.getDefaultDriver())).putFileAs(dir, this, filename, options)
82
+ }
83
+
84
+ }
85
+
86
+ }
87
+
135
88
  module.exports = FileSystemManager
@@ -1,48 +1,48 @@
1
- const ServiceProvider = require('@ostro/support/serviceProvider');
2
- const FilesystemManager = require('./filesystemManager');
3
- const Filesystem = require('./filesystem');
4
- class filesystemServiceProvider extends ServiceProvider {
5
-
6
- register() {
7
- this.registerNativeFilesystem();
8
-
9
- this.registerFlysystem();
10
- }
11
-
12
- registerNativeFilesystem() {
13
- this.$app.singleton('files', function() {
14
- return new Filesystem;
15
- });
16
- }
17
-
18
- registerFlysystem() {
19
- this.registerManager();
20
-
21
- this.$app.singleton('filesystem.disk', ($app) => {
22
- return $app['filesystem'].disk(this.getDefaultDriver());
23
- });
24
-
25
- this.$app.singleton('filesystem.cloud', ($app) => {
26
- return $app['filesystem'].disk(this.getCloudDriver());
27
- });
28
- }
29
-
30
- registerManager() {
31
- this.$app.singleton('filesystem', function($app) {
32
- return new FilesystemManager($app);
33
- });
34
- }
35
-
36
- getDefaultDriver() {
37
- return this.$app['config']['filesystems.default'];
38
- }
39
-
40
- getCloudDriver() {
41
- return this.$app['config']['filesystems.cloud'];
42
- }
43
- boot() {
44
-
45
- }
46
-
47
- }
1
+ const ServiceProvider = require('@ostro/support/serviceProvider');
2
+ const FilesystemManager = require('./filesystemManager');
3
+ const Filesystem = require('./filesystem');
4
+ class filesystemServiceProvider extends ServiceProvider {
5
+
6
+ register() {
7
+ this.registerNativeFilesystem();
8
+
9
+ this.registerFlysystem();
10
+ }
11
+
12
+ registerNativeFilesystem() {
13
+ this.$app.singleton('files', function() {
14
+ return new Filesystem;
15
+ });
16
+ }
17
+
18
+ registerFlysystem() {
19
+ this.registerManager();
20
+
21
+ this.$app.singleton('filesystem.disk', ($app) => {
22
+ return $app['filesystem'].disk(this.getDefaultDriver());
23
+ });
24
+
25
+ this.$app.singleton('filesystem.cloud', ($app) => {
26
+ return $app['filesystem'].disk(this.getCloudDriver());
27
+ });
28
+ }
29
+
30
+ registerManager() {
31
+ this.$app.singleton('filesystem', function($app) {
32
+ return new FilesystemManager($app);
33
+ });
34
+ }
35
+
36
+ getDefaultDriver() {
37
+ return this.$app['config']['filesystems.default'];
38
+ }
39
+
40
+ getCloudDriver() {
41
+ return this.$app['config']['filesystems.cloud'];
42
+ }
43
+ boot() {
44
+
45
+ }
46
+
47
+ }
48
48
  module.exports = filesystemServiceProvider
@@ -1,12 +1,12 @@
1
- const InvalidArgumentExceptionContract = require('@ostro/contracts/exception/invalidArgumentException')
2
- class InvalidArgumentException extends InvalidArgumentExceptionContract {
3
- constructor(error) {
4
- super();
5
- this.name = this.constructor.name;
6
- this.message = error;
7
- this.statusCode = 500;
8
- Error.captureStackTrace(this, this.constructor);
9
-
10
- }
11
- }
1
+ const InvalidArgumentExceptionContract = require('@ostro/contracts/exception/invalidArgumentException')
2
+ class InvalidArgumentException extends InvalidArgumentExceptionContract {
3
+ constructor(error) {
4
+ super();
5
+ this.name = this.constructor.name;
6
+ this.message = error;
7
+ this.statusCode = 500;
8
+ Error.captureStackTrace(this, this.constructor);
9
+
10
+ }
11
+ }
12
12
  module.exports = InvalidArgumentException
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ostro/filesystem",
3
- "version": "0.0.0-alpha.3",
3
+ "version": "0.0.0-alpha.4",
4
4
  "description": "filesystem module for OstroJS",
5
5
  "main": "filesystemManager.js",
6
6
  "scripts": {