-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfcdownloader.js
175 lines (163 loc) · 5.51 KB
/
fcdownloader.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
169
170
171
172
173
174
175
class Downloader {
constructor(collectionUrl, progressCallback) {
this.collectionUrl = collectionUrl;
this.documentCount = null
this.documentsDownloaded = 0
this.progressCallback = progressCallback
this.finished = false
}
async start() {
let dirHandle
try {
dirHandle = await this.getDirectoryHandle()
} catch (err) {
console.error('Error getting save handle:', err);
return false
}
console.log("Got dirHandle", dirHandle)
await this.downloadDirectory(dirHandle, null);
return true
}
async downloadDirectory(dirHandle, directory) {
const collection = await this.getCollectionData(directory)
if (directory === null) {
this.documentCount = collection.document_count
}
const documentGenerator = this.getDocumentsData(collection.documents_uri, directory)
for await (const document of documentGenerator) {
console.log("Downloading file", document)
await this.downloadAndWriteFile(document, dirHandle)
this.documentsDownloaded += 1
this.progressCallback(this.documentsDownloaded / this.documentCount * 100)
}
for (const directory of collection.directories) {
const subDirHandle = await this.getSubDirectoryHandle(dirHandle, directory)
await this.downloadDirectory(subDirHandle, directory)
}
}
addDirectoryParamToUrl(url, directory) {
url = new URL(url, window.location.origin)
const params = new URLSearchParams(url.search)
if (directory) {
params.append('directory', directory.id)
} else {
params.append('directory', "-")
}
url.search = params
return url
}
async getCollectionData(directory = null) {
const url = this.addDirectoryParamToUrl(this.collectionUrl, directory)
const response = await fetch(url);
return await response.json();
}
async *getDocumentsData(documentUrl, directory = null) {
let url = this.addDirectoryParamToUrl(documentUrl, directory)
while (url) {
const response = await fetch(url);
const data = await response.json();
for (const obj of data.objects) {
yield {
id: obj.id,
url: obj.file_url
}
}
url = data.meta.next;
}
}
async getDirectoryHandle () {
const pickerOpts = {
mode: 'readwrite',
startIn: 'downloads'
};
return await window.showDirectoryPicker(pickerOpts);
}
async getSubDirectoryHandle(dirHandle, directory) {
const dirName = directory.name.replace(/[\\/:*?"<>|]/gi, '_')
try {
// If directory exists, return handle
return await dirHandle.getDirectoryHandle(dirName, { create: false });
} catch (e) {
if (e.name !== "NotFoundError") {
console.warn("Error getting subdirectory handle", directory)
throw e
}
}
// If directory does not exist, try creating it
return await dirHandle.getDirectoryHandle(dirName, { create: true });
}
async downloadAndWriteFile(document, dirHandle) {
const filename = document.url.split('/').pop().replace(/\.pdf$/, `-${document.id}.pdf`);
try {
// if file exists, skip
await dirHandle.getFileHandle(filename, { create: false });
return
} catch (e) {
if (e.name !== "NotFoundError") {
throw e
}
}
const fileHandle = await dirHandle.getFileHandle(filename, { create: true });
const response = await fetch(document.url);
if (response.status !== 200) {
throw new Error(`Failed to download ${document.url}: ${response.status}`);
}
const writable = await fileHandle.createWritable();
const reader = response.body.getReader();
let bytesWritten = 0
while (true) {
const {value, done} = await reader.read();
if (done) break;
await writable.write(value);
bytesWritten += value.byteLength
}
await writable.close();
if (bytesWritten === 0) {
console.warn("File is empty", document)
}
}
}
const startDownload = async (collectionUrl, downloadError, downloadButton, downloadProgress) => {
const progressCallback = (progress) => {
downloadProgress.setAttribute('aria-valuenow', progress)
const bar = downloadProgress.querySelector('.progress-bar')
bar.style.width = `${progress}%`
bar.classList.remove('progress-bar-animated')
}
const downloader = new Downloader(collectionUrl, progressCallback)
downloadButton.disabled = true;
downloadProgress.hidden = false
downloadButton.textContent = downloadButton.dataset.downloading;
let result
try {
result = await downloader.start();
} catch (err) {
downloadError.textContent = err.message;
downloadError.hidden = false;
return
}
if (result) {
progressCallback(100)
downloadButton.textContent = downloadButton.dataset.downloaded;
} else {
downloadButton.disabled = false;
}
}
document.addEventListener('DOMContentLoaded', () => {
const downloadSection = document.querySelector('[data-fcdownload]')
const downloadButton = downloadSection?.querySelector('button');
const downloadProgress = downloadSection?.querySelector('.progress');
const downloadError = downloadSection.querySelector('.alert')
if (downloadButton === null) {
return;
}
if ('showDirectoryPicker' in window) {
downloadButton.addEventListener('click', () => {
const collectionUrl = downloadSection.dataset.fcdownload
startDownload(collectionUrl, downloadError, downloadButton, downloadProgress);
});
} else {
downloadButton.disabled = true;
downloadError.hidden = false;
}
});