Skip to content

Commit

Permalink
Create media-uploader.js
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkPool-SP authored Dec 22, 2024
1 parent 4910ee5 commit 11d86b8
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions scripts/media-uploader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Special Thanks to MubiLop with YeetYourFiles.lol for powering
the Community Spotlight File Uploader!
https://yeetyourfiles.lol/
*/

async function uploadFiles(mediaObj, id) {
let mediaPaths = [];
const allMedia = Object.values(mediaObj);
for (let i = 0; i < allMedia.length; i++) {
const json = await uploadFile(allMedia[i].d, id, i);
if (json) mediaPaths.push({ t: allMedia[i].t, id: json.fileId, url: json.fileUrl });
}
return mediaPaths;
}

async function uploadFile(base64, id, i) {
const fileName = `CSpotlight-Media-${id}${i}}.txt`;

const byteChars = atob(base64);
const byteNums = new Array(byteChars.length);
for (let i = 0; i < byteChars.length; i++) { byteNums[i] = byteChars.charCodeAt(i) }
const byteArray = new Uint8Array(byteNums);
const blob = new Blob([byteArray], { type: "text/plain" });

const formData = new FormData();
formData.append("file", blob, fileName);
return fetch("https://yeetyourfiles.lol/api/upload", { method: "POST", body: formData })
.then(response => response.json())
.then(data => {
return JSON.stringify(data);
})
.catch(e => {
console.warn("POST ERROR:", e);
return undefined;
});
}

0 comments on commit 11d86b8

Please sign in to comment.