-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfileProvider.js
More file actions
251 lines (222 loc) · 7.9 KB
/
Copy pathfileProvider.js
File metadata and controls
251 lines (222 loc) · 7.9 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
'use strict'
const CompilerImport = require('../compiler/compiler-imports')
const EventManager = require('../../lib/events')
const modalDialogCustom = require('../ui/modal-dialog-custom')
const tooltip = require('../ui/tooltip')
const remixLib = require('@remix-project/remix-lib')
const Storage = remixLib.Storage
class FileProvider {
constructor (name) {
this.event = new EventManager()
this.type = name
this.providerExternalsStorage = new Storage('providerExternals:')
this.externalFolders = [this.type + '/swarm', this.type + '/ipfs', this.type + '/github', this.type + '/gists', this.type + '/https']
}
addNormalizedName (path, url) {
this.providerExternalsStorage.set(this.type + '/' + path, url)
this.providerExternalsStorage.set('reverse-' + url, this.type + '/' + path)
}
removeNormalizedName (path) {
const value = this.providerExternalsStorage.get(path)
this.providerExternalsStorage.remove(path)
this.providerExternalsStorage.remove('reverse-' + value)
}
normalizedNameExists (path) {
return this.providerExternalsStorage.exists(path)
}
getNormalizedName (path) {
return this.providerExternalsStorage.get(path)
}
getPathFromUrl (url) {
return this.providerExternalsStorage.get('reverse-' + url)
}
isExternalFolder (path) {
return this.externalFolders.includes(path)
}
discardChanges (path) {
this.remove(path)
const compilerImport = new CompilerImport()
this.providerExternalsStorage.keys().map(value => {
if (value.indexOf(path) === 0) {
compilerImport.import(
this.getNormalizedName(value),
true,
(loadingMsg) => { tooltip(loadingMsg) },
(error, content, cleanUrl, type, url) => {
if (error) {
modalDialogCustom.alert(error)
} else {
this.addExternal(type + '/' + cleanUrl, content, url)
}
}
)
}
})
}
exists (path, cb) {
// todo check the type (directory/file) as well #2386
// currently it is not possible to have a file and folder with same path
return cb(null, this._exists(path))
}
_exists (path) {
path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here
var unprefixedpath = this.removePrefix(path)
return path === this.type ? true : window.remixFileSystem.existsSync(unprefixedpath)
}
init (cb) {
cb()
}
get (path, cb) {
cb = cb || function () {}
path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here
var unprefixedpath = this.removePrefix(path)
var exists = window.remixFileSystem.existsSync(unprefixedpath)
if (!exists) return cb(null, null)
window.remixFileSystem.readFile(unprefixedpath, 'utf8', (err, content) => {
cb(err, content)
})
}
set (path, content, cb) {
cb = cb || function () {}
var unprefixedpath = this.removePrefix(path)
var exists = window.remixFileSystem.existsSync(unprefixedpath)
if (exists && window.remixFileSystem.readFileSync(unprefixedpath, 'utf8') === content) {
cb()
return true
}
if (!exists && unprefixedpath.indexOf('/') !== -1) {
// the last element is the filename and we should remove it
this.createDir(path.substr(0, path.lastIndexOf('/')))
}
try {
window.remixFileSystem.writeFileSync(unprefixedpath, content)
} catch (e) {
cb(e)
return false
}
if (!exists) {
this.event.trigger('fileAdded', [this._normalizePath(unprefixedpath), false])
} else {
this.event.trigger('fileChanged', [this._normalizePath(unprefixedpath)])
}
cb()
return true
}
createDir (path, cb) {
const unprefixedpath = this.removePrefix(path)
const paths = unprefixedpath.split('/')
if (paths.length && paths[0] === '') paths.shift()
let currentCheck = ''
paths.forEach((value) => {
currentCheck = currentCheck + '/' + value
if (!window.remixFileSystem.existsSync(currentCheck)) {
window.remixFileSystem.mkdirSync(currentCheck)
this.event.trigger('folderAdded', [this._normalizePath(currentCheck)])
}
})
if (cb) cb()
}
// this will not add a folder as readonly but keep the original url to be able to restore it later
addExternal (path, content, url) {
if (url) this.addNormalizedName(path, url)
return this.set(path, content)
}
isReadOnly (path) {
return false
}
isDirectory (path) {
const unprefixedpath = this.removePrefix(path)
return path === this.type ? true : window.remixFileSystem.statSync(unprefixedpath).isDirectory()
}
isFile (path) {
path = this.getPathFromUrl(path) || path // ensure we actually use the normalized path from here
path = this.removePrefix(path)
return window.remixFileSystem.statSync(path).isFile()
}
/**
* Removes the folder recursively
* @param {*} path is the folder to be removed
*/
remove (path) {
return new Promise((resolve, reject) => {
path = this.removePrefix(path)
if (window.remixFileSystem.existsSync(path)) {
const stat = window.remixFileSystem.statSync(path)
try {
if (!stat.isDirectory()) {
resolve(this.removeFile(path))
} else {
const items = window.remixFileSystem.readdirSync(path)
if (items.length !== 0) {
items.forEach((item, index) => {
const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}`
if (window.remixFileSystem.statSync(curPath).isDirectory()) { // delete folder
this.remove(curPath)
} else { // delete file
this.removeFile(curPath)
}
})
if (window.remixFileSystem.readdirSync(path).length === 0) window.remixFileSystem.rmdirSync(path, console.log)
} else {
// folder is empty
window.remixFileSystem.rmdirSync(path, console.log)
}
this.event.trigger('fileRemoved', [this._normalizePath(path)])
}
} catch (e) {
console.log(e)
return resolve(false)
}
}
return resolve(true)
})
}
removeFile (path) {
path = this.removePrefix(path)
if (window.remixFileSystem.existsSync(path) && !window.remixFileSystem.statSync(path).isDirectory()) {
window.remixFileSystem.unlinkSync(path, console.log)
this.event.trigger('fileRemoved', [this._normalizePath(path)])
return true
} else return false
}
rename (oldPath, newPath, isFolder) {
var unprefixedoldPath = this.removePrefix(oldPath)
var unprefixednewPath = this.removePrefix(newPath)
if (this._exists(unprefixedoldPath)) {
window.remixFileSystem.renameSync(unprefixedoldPath, unprefixednewPath)
this.event.trigger('fileRenamed', [
this._normalizePath(unprefixedoldPath),
this._normalizePath(unprefixednewPath),
isFolder
])
return true
}
return false
}
resolveDirectory (path, callback) {
if (!path) return callback(null, { [this.type]: {} })
path = this.removePrefix(path)
if (path.indexOf('/') !== 0) path = '/' + path
window.remixFileSystem.readdir(path, (error, files) => {
var ret = {}
if (files) {
files.forEach(element => {
const absPath = (path === '/' ? '' : path) + '/' + element
ret[absPath.indexOf('/') === 0 ? absPath.substr(1, absPath.length) : absPath] = { isDirectory: window.remixFileSystem.statSync(absPath).isDirectory() }
// ^ ret does not accept path starting with '/'
})
}
callback(error, ret)
})
}
removePrefix (path) {
path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path
if (path === '') return '/'
return path
}
_normalizePath (path) {
if (path.indexOf('/') !== 0) path = '/' + path
return this.type + path
}
}
module.exports = FileProvider