Upload file_ids if set

FossilOrigin-Name: 9c3717500e89386b8795e8dfc22345fd7da79c0ca0a9087b3f4f162e72b7c1d5
This commit is contained in:
nekobit 2022-07-14 05:23:39 +00:00
parent b20d906292
commit 7e1b555925
4 changed files with 79 additions and 55 deletions

90
dist/js/main.js vendored
View file

@ -1,11 +1,11 @@
(function(){
// Global state variable
let state = {
file: {
files: {},
file_counter: 0,
}
};
// let state = {
// file: {
// files: {},
// file_counter: 0,
// }
// };
Element.prototype.insertAfter = function(element) {
element.parentNode.insertBefore(this, element.nextSibling);
@ -277,13 +277,12 @@
return en.innerHTML;
}
function construct_file_upload(id, file, file_content)
function construct_file_upload(file, file_content)
{
let container = document.createElement("div");
let content = document.createElement("img");
let info = document.createElement("span");
container.className = "file-upload";
container.id = "file-id-" + id;
info.className = "upload-info";
info.innerHTML = `<span class="filesize">${filesize_to_str(file.size)}</span> &bull; <span class="filename">${html_encode(file.name)}</span>`;
@ -299,80 +298,72 @@
return container;
}
// Created if not exist
function filepicker_create(id)
function update_uploads_json(dom)
{
if (!state.file[id])
let root = dom.parentNode;
let items = root.getElementsByClassName("file-upload");
let ids = [];
for (let i of items)
{
state.file[id] = { files: [], count: 0 }
return true;
ids.push(i.dataset.id);
}
return false;
}
function filepicker_add(id, file)
{
filepicker_create(id);
// Goto statusbox
root = root.parentNode;
let file_ids = root.querySelector(".file-ids-json");
if (!file_ids)
{
// Create if doesn't exist
file_ids = document.createElement("input");
file_ids.type = "hidden";
file_ids.className = "file-ids-json";
file_ids.name = "fileids";
root.appendChild(file_ids);
}
state.file[id].files.push(file);
return state.file[id].count++;
file_ids.value = JSON.stringify(ids);
}
function evt_file_upload(e)
{
let target = e.target;
// is New?
let id = state.file.file_counter;
// TODO do something with this
if (!target.classList.contains("used"))
{
target.id = `file-picker-id-${state.file.file_counter++}`;
target.classList.add("used");
}
else {
// "id-476" -> 476
id = Number(target.id.substr("file-picker-id-".length));
}
let file_upload_dom = this.closest("form").querySelector(".file-uploads-container");
file_upload_dom.className = "file-uploads-container";
const files = [...this.files];
let reader;
// Clear file input
this.value = '';
// Create file upload
for (let file of files)
{
reader = new FileReader();
reader.onload = (() => {
return (e) => {
let file_id = filepicker_add(id, file);
let file_dom = construct_file_upload(file, e.target.result);
file_upload_dom.appendChild(construct_file_upload(file_id, file, e.target.result));
file_upload_dom.appendChild(file_dom);
let xhr = upload_file("/treebird_api/v1/attachment",
"file",
file,
(xhr, args) => {
if (xhr.status !== 200)
{
// Undo action if failure
// interact_action(status, type);
}
// TODO errors
file_dom.dataset.id = JSON.parse(xhr.response).id;
update_uploads_json(file_dom);
}, null,
(e) => {
let upload_file_container = document.getElementById("file-id-"+file_id);
let upload_file_progress = upload_file_container.querySelector(".file-progress");
let upload_file_progress = file_dom
.querySelector(".file-progress");
// Add offset of 3
upload_file_progress.style.width = 3+((e.loaded/e.total)*97);
},
(e) => {
let upload_file_container = document.getElementById("file-id-"+file_id);
upload_file_container.querySelector(".upload-content").style.opacity = "1.0";
upload_file_container.querySelector(".file-progress").remove();
file_dom.querySelector(".upload-content").style.opacity = "1.0";
file_dom.querySelector(".file-progress").remove();
});
}
})(file);
@ -409,5 +400,8 @@
{
file_input.addEventListener('change', evt_file_upload);
}
// let submit = document.querySelector("form");
// submit.onsubmit
});
})();

View file

@ -102,6 +102,7 @@ char* read_post_data(struct post_values* post)
{ "replyid", &(post->replyid), key_string },
{ "min_id", &(post->min_id), key_string },
{ "max_id", &(post->max_id), key_string },
{ "fileids", &(post->file_ids), key_string },
{ "start_id", &(post->start_id), key_string },
{ "instance", &(post->instance), key_string },
{ "visibility", &(post->visibility), key_string },

View file

@ -54,6 +54,7 @@ struct post_values
struct key replies_only; // Int
struct key replies_policy; // Int
struct key file_ids; // String
struct key content; // String
struct key itype; // String
struct key id; // String

View file

@ -84,13 +84,34 @@ int try_post_status(struct session* ssn, mastodont_t* api)
struct mstdnt_storage storage = { 0 }, *att_storage = NULL;
char** files;
size_t files_len;
char** files = NULL;
size_t files_len = 0;
struct mstdnt_attachment* attachments = NULL;
char** media_ids = NULL;
cJSON* json_ids = NULL;
size_t json_ids_len = 0;
// Upload images
try_upload_media(&att_storage, ssn, api, &attachments, &media_ids);
if (!ssn->post.file_ids.is_set)
try_upload_media(&att_storage, ssn, api, &attachments, &media_ids);
else
{
// Parse json file ids
json_ids = cJSON_Parse(keystr(ssn->post.file_ids));
json_ids_len = cJSON_GetArraySize(json_ids);
if (json_ids_len)
{
media_ids = malloc(json_ids_len * sizeof(char*));
// TODO error
cJSON* id;
int i = 0;
cJSON_ArrayForEach(id, json_ids)
{
media_ids[i] = id->valuestring;
++i;
}
}
}
// Cookie copy and read
struct mstdnt_status_args args = {
@ -100,7 +121,8 @@ int try_post_status(struct session* ssn, mastodont_t* api)
.in_reply_to_id = keystr(ssn->post.replyid),
.language = NULL,
.media_ids = media_ids,
.media_ids_len = media_ids ? keyfile(ssn->post.files).array_size : 0,
.media_ids_len = (media_ids ? keyfile(ssn->post.files).array_size :
(json_ids ? json_ids_len : 0)),
.poll = NULL,
.preview = 0,
.scheduled_at = NULL,
@ -110,15 +132,21 @@ int try_post_status(struct session* ssn, mastodont_t* api)
.visibility = keystr(ssn->post.visibility),
};
// Finally, create (no error checking)
mastodont_create_status(api, &m_args, &args, &storage);
mastodont_storage_cleanup(&storage);
if (att_storage)
cleanup_media_storages(ssn, att_storage);
cleanup_media_ids(ssn, media_ids);
if (attachments) free(attachments);
if (json_ids)
free(media_ids);
else
cleanup_media_ids(ssn, media_ids);
free(attachments);
if (json_ids) cJSON_Delete(json_ids);
return 0;
}