-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileList.js
More file actions
293 lines (282 loc) · 7.43 KB
/
FileList.js
File metadata and controls
293 lines (282 loc) · 7.43 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"use strict";
(function(){
const iconTypes = new Map([
[/\/f\/pdf-24$/, 'pdf'],
[/\/f\/document-24$/, 'doc'],
[/\/f\/powerpoint-24$/, 'slides'],
[/\/f\/spreadsheet-24$/, 'spreadsheet'],
[/\/f\/calc-24$/, 'calc'],
]);
const extensionTypes = new Map([
['pdf', 'pdf'],
['doc', 'doc'],
['ppt', 'slides'],
['pptx', 'slides'],
['xls', 'spreadsheet'],
['xlsx', 'spreadsheet'],
['zip', 'archive'],
['7z', 'archive'],
['rar', 'archive'],
['gz', 'archive'],
]);
function getFiletypeByIcon(icon) {
for (let [iconPattern, filetype] of iconTypes) {
if (iconPattern.test(icon)) {
return filetype;
}
}
return 'other';
}
function getFiletypeByExtension(extension) {
if (extensionTypes.has(extension)) {
return extensionTypes.get(extension);
} else {
return 'other';
}
}
function safeFilename(filename) {
return filename
.trim()
.replace(/\s+/g, ' ')
.replace(/[\\\/\0<>:"\|\?\*]+/g, '_');
}
function chromeDownload(url, path, name) {
return new Promise((resolve, reject) => {
let filename = url.slice(url.lastIndexOf('/') + 1);
if (filename.indexOf('?') !== -1) {
filename = filename.slice(0, filename.indexOf('?'));
}
filename = decodeURIComponent(filename).trim();
if (!filename) {
filename = name;
}
chrome.downloads.download({
url,
filename: path + filename,
conflictAction: 'overwrite',
}, (downloadId) => {
if (downloadId) {
resolve(downloadId);
} else {
reject();
}
});
});
}
function parseFolder(node) {
const filenameIcon = node.querySelector('.fp-filename-icon');
const icon = filenameIcon.querySelector('.fp-icon>img').src;
const name = filenameIcon.querySelector('.fp-filename').textContent;
let type, url, children, fetched, filetype;
if (filenameIcon.tagName === 'SPAN') {
type = 'file';
url = filenameIcon.querySelector('a[href]').href;
filetype = getFiletypeByIcon(icon) || 'other';
} else {
type = 'dir';
fetched = true;
children = [];
const childrenNodes = node.querySelector('.fp-filename-icon~ul');
if (childrenNodes) {
for (const child of childrenNodes.childNodes) {
if (child.nodeType === 1 && child.tagName === 'LI') {
children.push(parseFolder(child))
}
}
}
}
return JSON.parse(JSON.stringify({
type,
name,
icon,
url,
children,
fetched,
filetype,
}));
}
Vue.nextTickPromise = function() {
return new Promise((resolve, reject) => {
Vue.nextTick(resolve);
});
}
Vue.component('file-list', {
template: '#file-list',
props: {
node: Object,
},
data() {
return {
checked: false,
expanded: this.node.type === 'root',
selectedFiles: 0,
shownFiles: 0,
unfetchedDirs: 0,
};
},
methods: {
addSelectedFiles(n) {
this.selectedFiles += n;
if (this.$parent !== this.$root) {
return this.$parent.addSelectedFiles(n);
}
},
addShownFiles(n) {
this.shownFiles += n;
if (this.$parent !== this.$root) {
return this.$parent.addShownFiles(n);
}
},
addUnfetchedDirs(n) {
this.unfetchedDirs += n;
if (this.$parent !== this.$root) {
return this.$parent.addUnfetchedDirs(n);
}
},
updateShownFiles() {
const shownFiles = this.$root.pattern.test(this.node.name) ? 1 : 0;
if (this.checked) {
this.$root.addFiles(this.node.filetype, shownFiles - this.shownFiles);
this.addSelectedFiles(shownFiles - this.shownFiles);
}
this.addShownFiles(shownFiles - this.shownFiles);
},
updateChecked() {
this.checked = this.selectedFiles === this.shownFiles;
if (this.$parent.node.type !== 'root') {
return this.$parent.updateChecked();
}
},
onCheck: co.wrap(function*() {
yield this.setCheck(this.checked);
if (this.$parent.node.type !== 'root') {
this.$parent.updateChecked();
}
}),
setCheck: co.wrap(function*(checked) {
if (this.node.type === 'file') {
this.addSelectedFiles(checked ? 1 : -1);
this.$root.addFiles(this.node.filetype, checked ? 1 : -1);
} else {
if (!this.node.fetched) {
yield this.fetch();
}
yield Vue.nextTickPromise();
for (const child of this.$children) {
if (child.unfetchedDirs || child.shownFiles && child.checked !== checked) {
child.checked = checked;
yield child.setCheck(checked, false);
}
}
}
}),
fetch: co.wrap(function*() {
this.$root.startLoading();
const children = [];
const resp = yield fetch(`https://moodle.nottingham.ac.uk/mod/folder/view.php?id=${this.node.id.split('-')[1]}`, { credentials: 'include' });
const html = yield resp.text();
const main = html.match(/<div id="folder_tree0" class="filemanager"><ul><li>([\s\S]*?)<\/div><div class="box generalbox folderbuttons">/)[1];
const node = document.createElement('div');
node.innerHTML = main;
const folder = parseFolder(node);
const name = html.match('<h2>(.*?)</h2>')[1];
if (!this.node.name) {
this.node.name = name;
}
this.node.fetched = true;
this.node.children = folder.children;
this.addUnfetchedDirs(-1);
this.$root.stopLoading();
}),
download: co.wrap(function*(path) {
if (!this.selectedFiles) {
return;
}
if (this.node.type === 'file') {
if (this.$root.filetypes.some(x => x.id === this.node.filetype && x.checked)) {
if (!this.node.url) {
yield this.getUrl();
}
yield chromeDownload(this.node.url, path, safeFilename(this.node.name));
this.$root.addDownloaded(1);
}
} else {
for (const child of this.$children) {
yield child.download(`${path || ''}${safeFilename(this.node.name)}/`);
}
}
}),
getUrl: co.wrap(function*() {
const [type, id] = this.node.id.split('-');
let url, resp;
switch (type) {
case 'resource':
url = `https://moodle.nottingham.ac.uk/mod/resource/view.php?id=${id}`;
resp = yield fetch(url, {
method: 'HEAD',
credentials: 'include',
});
if (resp.url.startsWith('https://moodle.nottingham.ac.uk/pluginfile.php')) {
this.node.url = resp.url;
} else if (resp.url === url) {
const resp = yield fetch(url, { credentials: 'include' });
const text = yield resp.text();
const match = text.match(/Click <a href="(https:\/\/moodle.nottingham.ac.uk\/pluginfile.php.*?)"[\s\S]*?<\/a> link to view the file/);
if (match) {
this.node.url = match[1];
} else {
throw Error('Unknown page content');
}
} else {
throw Error('Unknown redirection');
}
break;
case 'equella':
url = `https://moodle.nottingham.ac.uk/mod/equella/view.php?id=${id}`;
resp = yield fetch(url, {
method: 'HEAD',
credentials: 'include',
});
if (resp.url.startsWith('https://equella.nottingham.')) {
this.node.url = resp.url;
} else if (resp.url === url) {
const resp = yield fetch(url, { credentials: 'include' });
const text = yield resp.text();
const match = text.match(/Click <a href="(https:\/\/equella.nottingham.*?)"[\s\S]*?<\/a> link to open resource/);
if (match) {
this.node.url = match[1];
} else {
throw Error('Unknown page content');
}
} else {
throw Error('Unknown redirection');
}
break;
default:
throw Error('Unknown file');
}
}),
},
created() {
if (this.node.type === 'file') {
this.updateShownFiles();
this.$root.$watch('pattern', () => {
this.updateShownFiles();
});
} else if (this.node.type === 'dir') {
if (!this.node.fetched) {
this.addUnfetchedDirs(1);
}
}
},
watch: {
expanded() {
if (!this.node.fetched) {
return this.fetch();
} else {
return Promise.resolve();
}
},
},
});
})();