Skip to content

Commit

Permalink
implemented uploading images
Browse files Browse the repository at this point in the history
  • Loading branch information
gibsn committed Dec 1, 2022
1 parent b8f7bd3 commit 6e6bb8a
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions static/js/message/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const initialTextAreaLength = 0;
const fileInputId = 'file-input'
const textInputId = 'text_form'

const imageUploadAPI = '/api/v1/image/add'

// const upload = new FileUploadWithPreview.FileUploadWithPreview('myFirstImage', {
// multiple: true,
// maxFileCount: 5,
Expand Down Expand Up @@ -54,22 +56,56 @@ function previewImages() {

}

async function uploadImages(images, encrypt) {
let ids = []
async function uploadImages(images, ttl, encrypter) {
let ids = [];

for (let i = 0; i < images.length; i++) {
images[i].arrayBuffer().then(function(value) {
if encrypt {
// TODO encrypt
let imageBytes = await images[i].arrayBuffer();

if (encrypter) {
// TODO encrypt
}

let formData = new FormData();
formData.append("image", new Blob([imageBytes], {type: "application/octet-stream"}));
formData.append("ttl", ttl);

try {
let resp = await $.ajax({
type: 'POST',
url: imageUploadAPI,
data: formData,
contentType: false,
processData: false
});

if (resp.code != 200) {
console.error("could not upload image: code is", resp.code, "body is", resp.body);
continue;
}
// TODO upload to backend
// TODO append id to ids
});

ids.append(resp.body.id);
} catch (resp) {
if (resp.status != 200) {
console.error("could not upload image: http status is", resp.status);
continue;
}
}
}

return ids
}

function getCurrentTTL() {
for (const checkmark of document.getElementsByName("ttl")) {
if (checkmark.cheked) {
return checkmark.value;
}
}

return 86400;
}

async function onMessageSubmit(e) {
$('#loading').show();
$('#copy_button').html('Copy link');
Expand All @@ -86,7 +122,9 @@ async function onMessageSubmit(e) {
textFormCopy[0].value = encryptionRes.encrypted;

// encrypt, upload and append images to the form
let imgsIds = uploadImages(document.getElementById(fileInputId).files);
let ttl = getCurrentTTL();
let imgsIds = uploadImages(document.getElementById(fileInputId).files, ttl, null);

textFormCopy.append("imgs", imgsIds);

$.ajax({
Expand Down

0 comments on commit 6e6bb8a

Please sign in to comment.