Skip to content

Commit c1d4726

Browse files
committed
refactor: (Files) Files Service
- refactor Files Service
1 parent a1129f8 commit c1d4726

File tree

13 files changed

+130
-111
lines changed

13 files changed

+130
-111
lines changed

src/files/action.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/files/copy.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import FilesUtils from './utils'
1+
import Urls from '../urls'
2+
import Request from '../request'
23

3-
import { doAction } from './action'
4+
import FilesUtils from './utils'
45

56
export function copyFile(sourcePath, targetPath, asyncHandler) {
6-
77
const parameters = {
88
sourcePath: FilesUtils.ensureSlashInPath(sourcePath),
99
targetPath: FilesUtils.ensureSlashInPath(targetPath)
1010
}
1111

12-
return doAction('copy', parameters, asyncHandler)
12+
return Request.put({
13+
url : Urls.fileCopy(),
14+
data : parameters,
15+
isAsync : !!asyncHandler,
16+
asyncHandler: asyncHandler
17+
})
1318
}
19+

src/files/count.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
11
import Utils from '../utils'
22
import Urls from '../urls'
33
import Request from '../request'
4+
import Async from '../request/async'
45

5-
export function getFileCount(path, /** pattern, recursive, countDirectories, async */) {
6-
const responder = Utils.extractResponder(arguments)
7-
const isAsync = !!responder
8-
const query = buildCountQueryObject(arguments, isAsync)
6+
export function getFileCount(path, pattern, recursive, countDirectories, asyncHandler) {
97

10-
if (!query.path || !Utils.isString(query.path)) {
11-
throw new Error('Missing value for the "path" argument. The argument must contain a string value')
12-
}
8+
if (countDirectories instanceof Async) {
9+
asyncHandler = countDirectories
10+
countDirectories = undefined
1311

14-
if (!query.pattern || !Utils.isString(query.pattern)) {
15-
throw new Error('Missing value for the "pattern" argument. The argument must contain a string value')
16-
}
12+
} else if (recursive instanceof Async) {
13+
asyncHandler = recursive
14+
recursive = undefined
15+
countDirectories = undefined
1716

18-
if (!Utils.isBoolean(query.recursive)) {
19-
throw new Error('Missing value for the "recursive" argument. The argument must contain a boolean value')
17+
} else if (pattern instanceof Async) {
18+
asyncHandler = pattern
19+
pattern = undefined
20+
recursive = undefined
21+
countDirectories = undefined
2022
}
2123

22-
if (!Utils.isBoolean(query.countDirectories)) {
23-
throw new Error('Missing value for the "countDirectories" argument. The argument must contain a boolean value')
24+
const query = {
25+
action : 'count',
26+
pattern : pattern !== undefined ? pattern : '*',
27+
recursive : !!recursive,
28+
countDirectories: !!countDirectories
2429
}
2530

26-
delete query.path
31+
if (!path || !Utils.isString(path)) {
32+
throw new Error('Files "path" must not be empty and must be String')
33+
}
2734

28-
const url = Urls.filePath(path) + '?' + Utils.toQueryParams(query)
35+
if (!query.pattern || !Utils.isString(query.pattern)) {
36+
throw new Error('Files "path" must not be empty and must be String')
37+
}
2938

3039
return Request.get({
31-
url : url,
32-
isAsync : isAsync,
33-
asyncHandler: responder
40+
url : Urls.filePath(path),
41+
query : query,
42+
isAsync : !!asyncHandler,
43+
asyncHandler: asyncHandler
3444
})
3545
}
36-
37-
export function buildCountQueryObject(args, isAsync) {
38-
args = isAsync ? Array.prototype.slice.call(args, 0, -1) : args
39-
40-
return {
41-
action : 'count',
42-
path : args[0],
43-
pattern : args[1] !== undefined ? args[1] : '*',
44-
recursive : args[2] !== undefined ? args[2] : false,
45-
countDirectories: args[3] !== undefined ? args[3] : false
46-
}
47-
}

src/files/exists.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ import Utils from '../utils'
22
import Urls from '../urls'
33
import Request from '../request'
44

5-
export function exists(path/**, async */) {
5+
export function exists(path, asyncHandler) {
66
if (!path || !Utils.isString(path)) {
7-
throw new Error('Missing value for the "path" argument. The argument must contain a string value')
7+
throw new Error('Files "path" must not be empty and must be String')
88
}
99

10-
const responder = Utils.extractResponder(arguments)
11-
const isAsync = !!responder
12-
const url = Urls.filePath(path) + '?action=exists'
13-
1410
return Request.get({
15-
url : url,
16-
isAsync : isAsync,
17-
asyncHandler: responder
11+
url : Urls.filePath(path),
12+
query : { action: 'exists' },
13+
isAsync : !!asyncHandler,
14+
asyncHandler: asyncHandler
1815
})
1916
}

src/files/listinig.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,52 @@ import Urls from '../urls'
33
import Async from '../request/async'
44
import Request from '../request'
55

6-
export function listing(path, pattern, recursively, pagesize, offset/**, async */) {
7-
const responder = Utils.extractResponder(arguments)
8-
const isAsync = !!responder
9-
let url = Urls.filePath(path)
6+
export function listing(path, pattern, recursively, pagesize, offset, asyncHandler) {
7+
if (offset instanceof Async) {
8+
asyncHandler = offset
9+
offset = undefined
1010

11-
if ((arguments.length > 1) && !(arguments[1] instanceof Async)) {
12-
url += '?'
11+
} else if (pagesize instanceof Async) {
12+
asyncHandler = pagesize
13+
pagesize = undefined
14+
offset = undefined
15+
16+
} else if (recursively instanceof Async) {
17+
asyncHandler = recursively
18+
recursively = undefined
19+
pagesize = undefined
20+
offset = undefined
21+
22+
} else if (pattern instanceof Async) {
23+
asyncHandler = pattern
24+
pattern = undefined
25+
recursively = undefined
26+
pagesize = undefined
27+
offset = undefined
1328
}
1429

30+
const query = {}
31+
1532
if (Utils.isString(pattern)) {
16-
url += ('pattern=' + pattern)
33+
query.pattern = pattern
1734
}
1835

1936
if (Utils.isBoolean(recursively)) {
20-
url += ('&sub=' + recursively)
37+
query.sub = recursively
2138
}
2239

2340
if (Utils.isNumber(pagesize)) {
24-
url += '&pagesize=' + pagesize
41+
query.pagesize = pagesize
2542
}
2643

2744
if (Utils.isNumber(offset)) {
28-
url += '&offset=' + offset
45+
query.offset = offset
2946
}
3047

3148
return Request.get({
32-
url : url,
33-
isAsync : isAsync,
34-
asyncHandler: responder
49+
url : Urls.filePath(path),
50+
query : query,
51+
isAsync : !!asyncHandler,
52+
asyncHandler: asyncHandler
3553
})
3654
}

src/files/move.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import FilesUtils from './utils'
1+
import Urls from '../urls'
2+
import Request from '../request'
23

3-
import { doAction } from './action'
4+
import FilesUtils from './utils'
45

56
export function moveFile(sourcePath, targetPath, asyncHandler) {
6-
77
const parameters = {
88
sourcePath: FilesUtils.ensureSlashInPath(sourcePath),
99
targetPath: FilesUtils.ensureSlashInPath(targetPath)
1010
}
1111

12-
return doAction('move', parameters, asyncHandler)
13-
}
12+
return Request.put({
13+
url : Urls.fileMove(),
14+
data : parameters,
15+
isAsync : !!asyncHandler,
16+
asyncHandler: asyncHandler
17+
})
18+
}

src/files/remove-directory.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import Utils from '../utils'
22
import Urls from '../urls'
33
import Request from '../request'
44

5-
//TODO: looks like is's the same as removeFile method
6-
export function removeDirectory(path /**, async */) {
7-
const responder = Utils.extractResponder(arguments)
8-
const isAsync = !!responder
5+
export function removeDirectory(path, asyncHandler) {
6+
if (!path || !Utils.isString(path)) {
7+
throw new Error('Directory "path" must not be empty and must be String')
8+
}
99

10-
return Request.delete({
10+
Request.delete({
1111
url : Urls.filePath(path),
12-
isAsync : isAsync,
13-
asyncHandler: responder
12+
isAsync : !!asyncHandler,
13+
asyncHandler: asyncHandler
1414
})
1515
}

src/files/remove.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import Request from '../request'
44

55
const isRemoteUrl = url => url.startsWith('http://') || url.startsWith('https://')
66

7-
export function remove(path/**, async */) {
8-
const responder = Utils.extractResponder(arguments)
9-
const isAsync = !!responder
10-
11-
const url = isRemoteUrl(path) ? path : Urls.filePath(path)
7+
export function remove(path, asyncHandler) {
8+
if (!path || !Utils.isString(path)) {
9+
throw new Error('File "path" must not be empty and must be String')
10+
}
1211

1312
Request.delete({
14-
url : url,
15-
isAsync : isAsync,
16-
asyncHandler: responder
13+
url : isRemoteUrl(path) ? path : Urls.filePath(path),
14+
isAsync : !!asyncHandler,
15+
asyncHandler: asyncHandler
1716
})
1817
}

src/files/rename.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
import FilesUtils from './utils'
1+
import Utils from '../utils'
2+
import Urls from '../urls'
3+
import Request from '../request'
24

3-
import { doAction } from './action'
5+
import FilesUtils from './utils'
46

57
export function renameFile(oldPathName, newName, asyncHandler) {
8+
if (!oldPathName || !Utils.isString(oldPathName)) {
9+
throw new Error('Old File "path" must not be empty and must be String')
10+
}
11+
12+
if (!newName || !Utils.isString(newName)) {
13+
throw new Error('New File "path" must not be empty and must be String')
14+
}
15+
616
const parameters = {
717
oldPathName: FilesUtils.ensureSlashInPath(oldPathName),
818
newName : newName
919
}
1020

11-
return doAction('rename', parameters, asyncHandler)
12-
}
21+
return Request.put({
22+
url : Urls.fileRename(),
23+
data : parameters,
24+
isAsync : !!asyncHandler,
25+
asyncHandler: asyncHandler
26+
})
27+
}

src/files/save.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const getContentSize = content => {
4343
* @param {String} fileName
4444
* @param {String|Uint8Array} fileContent
4545
* @param {Boolean} overwrite
46+
* @param {Async} asyncHandler
4647
* @returns {Promise.<String>}
4748
*/
4849
export function saveFile(path, fileName, fileContent, overwrite, asyncHandler) {
@@ -56,7 +57,7 @@ export function saveFile(path, fileName, fileContent, overwrite, asyncHandler) {
5657

5758
if (overwrite instanceof Async) {
5859
asyncHandler = overwrite
59-
overwrite = null
60+
overwrite = undefined
6061
}
6162

6263
fileContent = toByteArray(fileContent)

0 commit comments

Comments
 (0)