add function to upload files to seqta's storage

This commit is contained in:
SethBurkart123
2023-11-27 17:21:45 +11:00
parent eb4d834c98
commit 9c1ee08c2e
2 changed files with 46 additions and 3 deletions
+3 -3
View File
@@ -284,7 +284,7 @@ async function finishLoad() {
}
chrome.storage.local.get(['justupdated'], function (result) {
if (result.justupdated) {
if (result.justupdated && !document.getElementById('whatsnewbk')) {
OpenWhatsNewPopup();
}
});
@@ -530,10 +530,10 @@ async function LoadPageElements() {
if (ranOnce) return;
ranOnce = true;
animate(
'.dashboard *:not(.dashlet-timetable), .dashboard .message *',
'.dashboard > *',
{ opacity: [0, 1], y: [10, 0] },
{
delay: stagger(0.01),
delay: stagger(0.1),
duration: 0.5,
easing: [.22, .03, .26, 1]
}
+43
View File
@@ -0,0 +1,43 @@
/**
* Uploads an image file to a specified endpoint using a POST request.
*
* @param {File} file - The image file to be uploaded.
* @returns {Promise} A promise that resolves to the response from the server.
* @throws {Error} If no file is provided or if there is an error during upload.
*/
export async function UploadImage(file) {
// Ensuring that file is provided
if (!file) {
throw new Error("No file provided");
}
// Extracting the filename
const fileName = file.name;
// Assuming 'document.cookie' contains all the cookies in the format you provided
const cookies = document.cookie;
// Setting up the request options
const requestOptions = {
method: 'POST',
headers: {
'Cookie': cookies,
'X-File-Name': fileName
},
body: file // Binary file data
};
// Making the fetch request and returning the promise
return fetch('/seqta/student/file/upload/xhr2', requestOptions)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const json = response.json();
return `/seqta/student/load/file?type=message&file=${json.uuid}`;
})
.catch(error => {
console.error('Error during file upload:', error);
throw error;
});
}