-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathutils.js
168 lines (144 loc) · 3.95 KB
/
utils.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { sortByName, sortBySize } from '../../lib/sort'
import { ACTIONS, IS_MAC, SORTING } from './consts'
/**
* @param {string} basename
* @param {Function} action
* @param {Object} [options]
* @param {boolean} [options.mfsOnly]
* @returns {(...args:any[]) => (...args:any[]) => Promise<any>}
*/
export const make = (basename, action, options = {}) => (...args) => async (args2) => {
const id = Symbol(basename)
const { dispatch, getIpfs, store } = args2
dispatch({ type: `FILES_${basename}_STARTED`, payload: { id } })
let data
if (options.mfsOnly) {
const info = store.selectFilesPathInfo()
if (!info || !info.isMfs) {
// musn't happen
return
}
}
try {
data = await action(getIpfs(), ...args, id, args2)
// TODO: Add a comment explaining what is going on here.
const paths = Array.isArray(args[0]) ? args[0].flat() : []
dispatch({
type: `FILES_${basename}_FINISHED`,
payload: {
id,
...data,
paths
}
})
// Rename specific logic
if (basename === ACTIONS.MOVE) {
const src = args[0]
const dst = args[1]
if (src === store.selectFiles().path) {
await store.doUpdateHash(dst)
}
}
// Delete specific logic
if (basename === ACTIONS.DELETE) {
const src = args[0][0]
let path = src.split('/')
path.pop()
path = path.join('/')
await store.doUpdateHash(path)
}
} catch (error) {
if (!error.code) {
error.code = `ERR_${basename}`
}
console.error(error)
dispatch({ type: `FILES_${basename}_FAILED`, payload: { id, error } })
} finally {
if (basename !== ACTIONS.FETCH) {
await store.doFilesFetch()
}
if (basename === ACTIONS.PIN_ADD || basename === ACTIONS.PIN_REMOVE) {
await store.doPinsFetch()
}
}
return data
}
/**
* @template {{name:string, type:string, cumulativeSize?:number, size:number}} T
* @param {T[]} files
* @param {Object} sorting
* @param {boolean} [sorting.asc]
* @param {import('./consts').SORTING} [sorting.by]
* @returns {T[]}
*/
export const sortFiles = (files, sorting) => {
const sortDir = sorting.asc ? 1 : -1
const nameSort = sortByName(sortDir)
const sizeSort = sortBySize(sortDir)
return files.sort((a, b) => {
if (a.type === b.type || IS_MAC) {
if (sorting.by === SORTING.BY_NAME) {
return nameSort(a.name, b.name)
} else {
return sizeSort(a.cumulativeSize || a.size, b.cumulativeSize || b.size)
}
}
if (a.type === 'directory') {
return -1
} else {
return 1
}
})
}
/**
* @typedef {Object} Info
* @property {string} path
* @property {string} realPath
* @property {boolean} isMfs
* @property {boolean} isPins
* @property {boolean} isRoot
*
* @param {string} path
* @param {boolean} uriDecode
* @returns {Info|void}
*/
export const infoFromPath = (path, uriDecode = true) => {
const info = {
path: path,
realPath: '',
isMfs: false,
isPins: false,
isRoot: false
}
/**
* @param {string} prefix
*/
const check = (prefix) => {
info.realPath = info.path.substr(prefix.length).trim() || '/'
info.isRoot = info.realPath === '/'
}
if (info.path.startsWith('/ipns') || info.path.startsWith('/ipfs')) {
info.realPath = info.path
info.isRoot = info.path === '/ipns' || info.path === '/ipfs'
} else if (info.path.startsWith('/files')) {
check('/files')
info.isMfs = true
} else if (info.path.startsWith('/pins')) {
check('/pins')
info.isPins = true
if (info.realPath !== '/') {
info.realPath = `/ipfs${info.realPath}`
}
} else {
return
}
if (info.path.endsWith('/') && info.realPath !== '/') {
info.path = info.path.substring(0, info.path.length - 1)
info.realPath = info.realPath.substring(0, info.realPath.length - 1)
}
if (uriDecode) {
info.realPath = decodeURIComponent(info.realPath)
info.path = decodeURIComponent(info.path)
}
return info
}