UNPKG

podchat-browser

Version:

Javascript SDK to use POD's Chat Service - Browser Only

135 lines (105 loc) 4.67 kB
import * as tus from "tus-js-client"; let upload = {}; function UploadMethods(app) { function sendResumableFileMessage(params, callbacks) { init(params, callbacks) start(params.uniqueId) } function init(params, callbacks) { const {file, userGroupHash, threadId, uniqueId, fileObject, originalFileName} = params upload[uniqueId] = new tus.Upload(file, { // endpoint: props.data.upload_url, // endpoint: "https://podspace.pod.ir/api/usergroups/" + userGroupHash + "/files", endpoint: app.sdkParams.SERVICE_ADDRESSES.PODSPACE_FILESERVER_ADDRESS + "/api/files/resumable_upload", retryDelays: [0, 3000, 5000, 10000, 20000, 30000, 40000, 50000, 60000], // crossDomain: false, chunkSize: 1024 * 1024 * 5, // chunkSize: 102400, // overridePatchMethod:true, // mode: 'cors', // cache: 'no-cache', // credentials: 'same-origin', headers: { 'Authorization': 'Bearer ' + app.sdkParams.token, // "Content-Type": "application/offset+octet-stream", // "Access-Control-Allow-Origin": "*", // "Access-Control-Allow-Methods": "GET,POST,HEAD,PUT,PATCH,DELETE,OPTIONS,DELETE", }, // redirect: 'follow', // referrerPolicy: 'no-referrer', metadata: { userGroupHash: userGroupHash, name: file.name, }, onError: function (error) { }, onProgress: function (bytesUploaded, bytesTotal) { var percentage = (bytesUploaded / bytesTotal * 100).toFixed(0) if (percentage > upload[uniqueId].last_percentage) { app.chatEvents.fireEvent('fileUploadEvents', { threadId: threadId, uniqueId: uniqueId, state: 'UPLOADING', progress: percentage, fileInfo: { fileName: originalFileName, fileSize: fileObject.size }, fileObject: fileObject }); } upload[uniqueId].last_percentage = percentage }, onSuccess: function () { fetch(app.sdkParams.SERVICE_ADDRESSES.PODSPACE_FILESERVER_ADDRESS + "/api/files/uploaded_file_info/" + upload[uniqueId].url.split("/").pop(), { headers: { 'Authorization': 'Bearer ' + app.sdkParams.token, } }) .then(function (serverPromise) { serverPromise.json() .then(function (result) { callbacks && callbacks(result); }) }) delete upload[uniqueId] }, onShouldRetry: function (err, retryAttempt, options) { var status = err.originalResponse ? err.originalResponse.getStatus() : 0 // Do not retry if the status is a 403. if (status === 400 || status === 401) { return false } // For any other status code, we retry. return true } }) } function resume(uniqueId) { // Check if there are any previous uploads to continue. upload[uniqueId]?.findPreviousUploads().then(function (previousUploads) { // Found previous uploads so we select the first one. if (previousUploads.length) { upload[uniqueId].resumeFromPreviousUpload(previousUploads[0]) } }); upload[uniqueId]?.start() } function start(uniqueId) { upload[uniqueId].findPreviousUploads().then(function (previousUploads) { // Found previous uploads so we select the first one. if (previousUploads.length) { upload[uniqueId].resumeFromPreviousUpload(previousUploads[0]) } }); upload[uniqueId]?.start() } function pause(uniqueId) { upload[uniqueId]?.abort(); } function abort(uniqueId) { upload[uniqueId]?.abort(); } return {sendResumableFileMessage, resume, start, pause, abort} } export default UploadMethods