|
|
|
|
@ -16,6 +16,75 @@ const readAsDataURL = (file, callback) => { |
|
|
|
|
return reader.readAsDataURL(file); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const uploadFileWithMessage = async (rid, tmid, { description, fileName, msg, file }) => { |
|
|
|
|
const data = new FormData(); |
|
|
|
|
description && data.append('description', description); |
|
|
|
|
msg && data.append('msg', msg); |
|
|
|
|
tmid && data.append('tmid', tmid); |
|
|
|
|
data.append('file', file.file, fileName); |
|
|
|
|
|
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
|
|
|
|
|
const upload = { |
|
|
|
|
id: Random.id(), |
|
|
|
|
name: fileName, |
|
|
|
|
percentage: 0, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
uploads.push(upload); |
|
|
|
|
Session.set('uploading', uploads); |
|
|
|
|
|
|
|
|
|
const { xhr, promise } = APIClient.upload(`v1/rooms.upload/${ rid }`, {}, data, { |
|
|
|
|
progress(progress) { |
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
|
|
|
|
|
if (progress === 100) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
uploads.filter((u) => u.id === upload.id).forEach((u) => { |
|
|
|
|
u.percentage = Math.round(progress) || 0; |
|
|
|
|
}); |
|
|
|
|
Session.set('uploading', uploads); |
|
|
|
|
}, |
|
|
|
|
error(error) { |
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
uploads.filter((u) => u.id === upload.id).forEach((u) => { |
|
|
|
|
u.error = error.message; |
|
|
|
|
u.percentage = 0; |
|
|
|
|
}); |
|
|
|
|
Session.set('uploading', uploads); |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
Tracker.autorun((computation) => { |
|
|
|
|
const isCanceling = Session.get(`uploading-cancel-${ upload.id }`); |
|
|
|
|
if (!isCanceling) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
computation.stop(); |
|
|
|
|
Session.delete(`uploading-cancel-${ upload.id }`); |
|
|
|
|
|
|
|
|
|
xhr.abort(); |
|
|
|
|
|
|
|
|
|
const uploads = Session.get('uploading') || {}; |
|
|
|
|
Session.set('uploading', uploads.filter((u) => u.id !== upload.id)); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
await promise; |
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
return Session.set('uploading', uploads.filter((u) => u.id !== upload.id)); |
|
|
|
|
} catch (error) { |
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
uploads.filter((u) => u.id === upload.id).forEach((u) => { |
|
|
|
|
u.error = (error.xhr && error.xhr.responseJSON && error.xhr.responseJSON.error) || error.message; |
|
|
|
|
u.percentage = 0; |
|
|
|
|
}); |
|
|
|
|
Session.set('uploading', uploads); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const showUploadPreview = (file, callback) => { |
|
|
|
|
// If greater then 10MB don't try and show a preview
|
|
|
|
|
if (file.file.size > (10 * 1000000)) { |
|
|
|
|
@ -197,84 +266,16 @@ export const fileUpload = async (files, input, { rid, tmid }) => { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const record = { |
|
|
|
|
name: document.getElementById('file-name').value || file.name || file.file.name, |
|
|
|
|
size: file.file.size, |
|
|
|
|
type: file.file.type, |
|
|
|
|
rid, |
|
|
|
|
description: document.getElementById('file-description').value, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const fileName = document.getElementById('file-name').value || file.name || file.file.name; |
|
|
|
|
|
|
|
|
|
const data = new FormData(); |
|
|
|
|
record.description && data.append('description', record.description); |
|
|
|
|
msg && data.append('msg', msg); |
|
|
|
|
tmid && data.append('tmid', tmid); |
|
|
|
|
data.append('file', file.file, fileName); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
|
|
|
|
|
const upload = { |
|
|
|
|
id: Random.id(), |
|
|
|
|
name: fileName, |
|
|
|
|
percentage: 0, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
uploads.push(upload); |
|
|
|
|
Session.set('uploading', uploads); |
|
|
|
|
|
|
|
|
|
uploadNextFile(); |
|
|
|
|
|
|
|
|
|
const { xhr, promise } = APIClient.upload(`v1/rooms.upload/${ rid }`, {}, data, { |
|
|
|
|
progress(progress) { |
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
|
|
|
|
|
if (progress === 100) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
uploads.filter((u) => u.id === upload.id).forEach((u) => { |
|
|
|
|
u.percentage = Math.round(progress) || 0; |
|
|
|
|
}); |
|
|
|
|
Session.set('uploading', uploads); |
|
|
|
|
}, |
|
|
|
|
error(error) { |
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
uploads.filter((u) => u.id === upload.id).forEach((u) => { |
|
|
|
|
u.error = error.message; |
|
|
|
|
u.percentage = 0; |
|
|
|
|
}); |
|
|
|
|
Session.set('uploading', uploads); |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
Tracker.autorun((computation) => { |
|
|
|
|
const isCanceling = Session.get(`uploading-cancel-${ upload.id }`); |
|
|
|
|
if (!isCanceling) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
computation.stop(); |
|
|
|
|
Session.delete(`uploading-cancel-${ upload.id }`); |
|
|
|
|
|
|
|
|
|
xhr.abort(); |
|
|
|
|
|
|
|
|
|
const uploads = Session.get('uploading') || {}; |
|
|
|
|
Session.set('uploading', uploads.filter((u) => u.id !== upload.id)); |
|
|
|
|
uploadFileWithMessage(rid, tmid, { |
|
|
|
|
description: document.getElementById('file-description').value || undefined, |
|
|
|
|
fileName, |
|
|
|
|
msg: msg || undefined, |
|
|
|
|
file, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
await promise; |
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
return Session.set('uploading', uploads.filter((u) => u.id !== upload.id)); |
|
|
|
|
} catch (error) { |
|
|
|
|
const uploads = Session.get('uploading') || []; |
|
|
|
|
uploads.filter((u) => u.id === upload.id).forEach((u) => { |
|
|
|
|
u.error = (error.xhr && error.xhr.responseJSON && error.xhr.responseJSON.error) || error.message; |
|
|
|
|
u.percentage = 0; |
|
|
|
|
}); |
|
|
|
|
Session.set('uploading', uploads); |
|
|
|
|
} |
|
|
|
|
uploadNextFile(); |
|
|
|
|
})); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|