update attachent upload

This commit is contained in:
2026-03-18 19:33:09 +01:00
parent 84b3dd69f1
commit 4355fa78fa
2 changed files with 52 additions and 58 deletions

View File

@@ -295,55 +295,16 @@ sendBtn.addEventListener("click", async () => {
sendBtn.disabled = true;
try {
// 1. Upload images as attachments if requested (without memo link yet)
const imageMap = new Map(); // originalUrl -> attachment name ("attachments/{id}")
if (attachCheck.checked) {
const toUpload = state.images.filter((img) => img.keep);
let uploaded = 0;
for (const img of toUpload) {
sendBtn.textContent = `Uploading image ${uploaded + 1}/${toUpload.length}`;
try {
const attachment = await uploadAttachment(baseUrl, token, img);
imageMap.set(img.src, attachment.name);
uploaded++;
} catch (e) {
console.warn("Failed to upload image:", img.src, e.message);
}
}
}
// 1. Create the memo first (with original image URLs)
sendBtn.textContent = "Creating memo…";
// 2. Replace original image URLs in markdown with attachment external links
let contentWithImages = finalContent;
for (const [origUrl, attachName] of imageMap.entries()) {
const attachmentUrl = `${baseUrl}/file/${attachName}`;
const escapedUrl = origUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp(`!\\[(.*?)\\]\\(${escapedUrl}\\)`, 'g');
contentWithImages = contentWithImages.replace(re, `![$1](${attachmentUrl})`);
}
// Append any uploaded attachments not already referenced in the markdown
for (const attachName of imageMap.values()) {
const attachmentUrl = `${baseUrl}/file/${attachName}`;
if (!contentWithImages.includes(attachmentUrl)) {
contentWithImages += `\n\n![attachment](${attachmentUrl})`;
}
}
// 3. Create the memo
const body = {
content: contentWithImages,
visibility,
};
const res = await fetch(`${baseUrl}/api/v1/memos`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(body),
body: JSON.stringify({ content: finalContent, visibility }),
});
if (!res.ok) {
@@ -355,22 +316,53 @@ sendBtn.addEventListener("click", async () => {
// memo.name is "memos/{id}"
const memoName = memo.name;
// 4. Link each attachment to the created memo via PATCH
for (const attachName of imageMap.values()) {
try {
await fetch(`${baseUrl}/api/v1/${attachName}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ memo: memoName }),
});
} catch (e) {
console.warn("Failed to link attachment to memo:", attachName, e.message);
// 2. Upload images as attachments referencing the created memo
const imageMap = new Map(); // originalUrl -> attachment name ("attachments/{id}")
if (attachCheck.checked) {
const toUpload = state.images.filter((img) => img.keep);
let uploaded = 0;
for (const img of toUpload) {
sendBtn.textContent = `Uploading image ${uploaded + 1}/${toUpload.length}`;
try {
const attachment = await uploadAttachment(baseUrl, token, img, memoName);
imageMap.set(img.src, attachment.name);
uploaded++;
} catch (e) {
console.warn("Failed to upload image:", img.src, e.message);
}
}
}
// 3. If images were uploaded, update the memo content to reference attachment URLs
if (imageMap.size > 0) {
sendBtn.textContent = "Updating memo…";
let contentWithImages = finalContent;
for (const [origUrl, attachName] of imageMap.entries()) {
const attachmentUrl = `${baseUrl}/file/${attachName}`;
const escapedUrl = origUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp(`!\\[(.*?)\\]\\(${escapedUrl}\\)`, 'g');
contentWithImages = contentWithImages.replace(re, `![$1](${attachmentUrl})`);
}
// Append any uploaded attachments not already referenced in the markdown
for (const attachName of imageMap.values()) {
const attachmentUrl = `${baseUrl}/file/${attachName}`;
if (!contentWithImages.includes(attachmentUrl)) {
contentWithImages += `\n\n![attachment](${attachmentUrl})`;
}
}
await fetch(`${baseUrl}/api/v1/${memoName}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ content: contentWithImages }),
});
}
let memoId = "";
if (memoName) {
memoId = memoName.replace(/^memos\//, "");
@@ -392,7 +384,7 @@ sendBtn.addEventListener("click", async () => {
});
// ── Upload a single image as an attachment ────────────────────────────────────
async function uploadAttachment(baseUrl, token, img) {
async function uploadAttachment(baseUrl, token, img, memoName) {
// Validate URL scheme to prevent SSRF via crafted page image URLs
if (!img.src.startsWith("data:")) {
let parsedUrl;
@@ -444,6 +436,7 @@ async function uploadAttachment(baseUrl, token, img) {
filename,
type: blob.type || "application/octet-stream",
content: base64,
memo: memoName,
}),
});