()
| 1175 | |
| 1176 | // Make init async-safe; it resolves when Resumable is constructed |
| 1177 | async function initResumableUpload() { |
| 1178 | if (resumableInstance) return; |
| 1179 | // Load the library if needed |
| 1180 | const ResumableCtor = await lazyLoadResumable().catch(err => { |
| 1181 | console.error('Failed to load Resumable.js:', err); |
| 1182 | return null; |
| 1183 | }); |
| 1184 | if (!ResumableCtor) return; |
| 1185 | |
| 1186 | // Construct the instance once |
| 1187 | if (!resumableInstance) { |
| 1188 | resumableInstance = new ResumableCtor({ |
| 1189 | target: RESUMABLE_TARGET, |
| 1190 | chunkSize: getResumableChunkSizeBytes(), |
| 1191 | simultaneousUploads: 3, |
| 1192 | forceChunkSize: true, |
| 1193 | testChunks: true, |
| 1194 | withCredentials: true, |
| 1195 | headers: { 'X-CSRF-Token': window.csrfToken }, |
| 1196 | query: () => { |
| 1197 | const q = { |
| 1198 | folder: window.currentFolder || "root", |
| 1199 | upload_token: window.csrfToken |
| 1200 | }; |
| 1201 | const sourceId = getActiveUploadSourceId(); |
| 1202 | if (sourceId) q.sourceId = sourceId; |
| 1203 | return q; |
| 1204 | } |
| 1205 | }); |
| 1206 | } |
| 1207 | |
| 1208 | // keep query fresh when folder changes (call this from your folder nav code) |
| 1209 | function updateResumableQuery() { |
| 1210 | if (!resumableInstance) return; |
| 1211 | resumableInstance.opts.headers['X-CSRF-Token'] = window.csrfToken; |
| 1212 | if (typeof resumableInstance.opts.query === 'function') return; |
| 1213 | resumableInstance.opts.query.folder = window.currentFolder || 'root'; |
| 1214 | resumableInstance.opts.query.upload_token = window.csrfToken; |
| 1215 | const sourceId = getActiveUploadSourceId(); |
| 1216 | if (sourceId) { |
| 1217 | resumableInstance.opts.query.sourceId = sourceId; |
| 1218 | } else { |
| 1219 | delete resumableInstance.opts.query.sourceId; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | resumableInstance.on("fileAdded", function (file) { |
| 1226 | // Build a stable per-file key |
| 1227 | const id = |
| 1228 | file.uniqueIdentifier || |
| 1229 | ((file.fileName || file.name || '') + ':' + (file.size || 0)); |
| 1230 | |
| 1231 | // If we've already seen this id in the current batch, skip wiring it again |
| 1232 | if (_currentResumableIds.has(id)) { |
| 1233 | return; |
| 1234 | } |
no test coverage detected