working file sharing

This commit is contained in:
2026-04-16 20:38:16 +02:00
parent f0fdaedd8c
commit 84e3f852fe
14 changed files with 190 additions and 81 deletions
+67 -2
View File
@@ -86,6 +86,8 @@
<button onclick="showForm('get-connection-messages')">POST /get/connection/messages</button>
<button onclick="showForm('msg-user')">POST /msg/user</button>
<button onclick="showForm('msg-group')">POST /msg/group</button>
<button onclick="showForm('file-upload')">POST /new/file</button>
<button onclick="showForm('file-download')">POST /get/file</button>
<button onclick="showForm('websocket')">WS /ws</button>
</div>
@@ -260,9 +262,10 @@
fields: [
{ id: 'mu-token', label: 'token', ph: '' },
{ id: 'mu-connectionid', label: 'connectionid', ph: 'UUID' },
{ id: 'mu-msgContent', label: 'msgContent', ph: 'message text' },
{ id: 'mu-msgContent', label: 'msgContent', ph: 'message text (optional if mediaKey set)' },
{ id: 'mu-attachedFile', label: 'attachedFile', ph: 'key returned by /new/file (optional)' },
],
submit: () => httpPost('/msg/user', { token:'mu-token', connectionid:'mu-connectionid', msgContent:'mu-msgContent' })
submit: () => httpPost('/msg/user', { token:'mu-token', connectionid:'mu-connectionid', msgContent:'mu-msgContent', attachedFile:'mu-attachedFile' })
},
'msg-group': {
title: 'POST /msg/group — send message to group',
@@ -273,6 +276,28 @@
],
submit: () => httpPost('/msg/group', { token:'mg-token', groupid:'mg-groupid', content:'mg-content' })
},
'file-upload': {
title: 'POST /new/file — upload file (multipart, token in header)',
renderCustom: () => `
<div class="field"><label>token</label><input id="fu-token" placeholder=""></div>
<div class="field"><label>connectionid</label><input id="fu-connectionid" placeholder="UUID"></div>
<div class="field"><label>file</label><input id="fu-file" type="file"></div>
<div class="form-actions">
<button class="send" onclick="submitFileUpload()">Send</button>
</div>
`
},
'file-download': {
title: 'POST /get/file — get presigned download URL (token in header)',
renderCustom: () => `
<div class="field"><label>token</label><input id="fd-token" placeholder=""></div>
<div class="field"><label>connectionid</label><input id="fd-connectionid" placeholder="UUID"></div>
<div class="field"><label>key</label><input id="fd-key" placeholder="returned by upload"></div>
<div class="form-actions">
<button class="send" onclick="submitFileDownload()">Send</button>
</div>
`
},
'websocket': {
title: 'WS /ws — WebSocket connection',
renderCustom: () => {
@@ -324,6 +349,7 @@
if (def.renderCustom) {
fieldsEl.innerHTML = def.renderCustom();
autofillTokens();
} else {
let html = '';
for (const f of def.fields) {
@@ -420,6 +446,45 @@
log('WS →', msg, 'log-info');
}
async function submitFileUpload() {
const token = document.getElementById('fu-token').value;
const connectionid = document.getElementById('fu-connectionid').value;
const fileInput = document.getElementById('fu-file');
if (!fileInput.files.length) { log('HTTP ERR', 'no file selected', 'log-err'); return; }
const form = new FormData();
form.append('connectionid', connectionid);
form.append('file', fileInput.files[0]);
log('HTTP /new/file', `→ connectionid=${connectionid} file=${fileInput.files[0].name}`, 'log-info');
try {
const resp = await fetch(baseUrl() + '/new/file', { method: 'POST', headers: { token }, body: form });
const text = await resp.text();
log(`HTTP ${resp.status}`, text, resp.ok ? 'log-http' : 'log-err');
if (resp.ok) {
const keyEl = document.getElementById('fd-key');
if (keyEl) keyEl.value = text;
const dmKeyEl = document.getElementById('mu-attachedFile');
if (dmKeyEl) dmKeyEl.value = text;
}
} catch(e) {
log('HTTP ERR', e.message, 'log-err');
}
}
async function submitFileDownload() {
const token = document.getElementById('fd-token').value;
const connectionid = document.getElementById('fd-connectionid').value;
const key = document.getElementById('fd-key').value;
const params = new URLSearchParams({ connectionid, key });
log('HTTP /get/file', `${params.toString()}`, 'log-info');
try {
const resp = await fetch(baseUrl() + '/get/file', { method: 'POST', headers: { token }, body: params });
const text = await resp.text();
log(`HTTP ${resp.status}`, text, resp.ok ? 'log-http' : 'log-err');
} catch(e) {
log('HTTP ERR', e.message, 'log-err');
}
}
function setWsStatus(connected) {
const el = document.getElementById('ws-status');
if (!el) return;