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

@@ -38,7 +38,7 @@ Load the extension in Chrome: **Extensions → Load unpacked → select `dist/`*
- Memos API v1: `/api/v1/memos`, `/api/v1/attachments` - Memos API v1: `/api/v1/memos`, `/api/v1/attachments`
- Requires Memos v0.22+ - Requires Memos v0.22+
- Bearer token auth via `chrome.storage.sync` - Bearer token auth via `chrome.storage.sync`
- Attachment flow: upload via `POST /api/v1/attachments` (JSON + base64 `content`), create memo, then link each attachment to the memo via `PATCH /api/v1/attachments/{id}` with `{ memo: "memos/{id}" }` - Attachment flow: create memo first (`POST /api/v1/memos`), then upload each attachment via `POST /api/v1/attachments` (JSON + base64 `content` + `memo: "memos/{id}"`), then patch the memo content to replace original image URLs with attachment URLs (`PATCH /api/v1/memos/{id}`)
### Content Extraction ### Content Extraction
- Removes boilerplate: nav, ads, sidebars, cookie banners (45+ selectors) - Removes boilerplate: nav, ads, sidebars, cookie banners (45+ selectors)
@@ -49,8 +49,9 @@ Load the extension in Chrome: **Extensions → Load unpacked → select `dist/`*
- Filters images smaller than 32px (icons/tracking pixels) - Filters images smaller than 32px (icons/tracking pixels)
- Deduplicates images - Deduplicates images
- Supports data URIs - Supports data URIs
- Uploads images as attachments (`POST /api/v1/attachments`) with base64-encoded content - Uploads images as attachments (`POST /api/v1/attachments`) with base64-encoded content and `memo` reference
- After memo creation, links each attachment to the memo via `PATCH /api/v1/attachments/{id}` - Memo is created first; attachment uploads include `memo: "memos/{id}"` to associate them immediately
- After all uploads, memo content is patched to replace original image URLs with attachment file URLs
- Attachment file URL pattern: `{memosUrl}/file/attachments/{id}` - Attachment file URL pattern: `{memosUrl}/file/attachments/{id}`
### Storage ### Storage

View File

@@ -295,7 +295,28 @@ sendBtn.addEventListener("click", async () => {
sendBtn.disabled = true; sendBtn.disabled = true;
try { try {
// 1. Upload images as attachments if requested (without memo link yet) // 1. Create the memo first (with original image URLs)
sendBtn.textContent = "Creating memo…";
const res = await fetch(`${baseUrl}/api/v1/memos`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ content: finalContent, visibility }),
});
if (!res.ok) {
const txt = await res.text();
throw new Error(`API error ${res.status}: ${txt.slice(0, 200)}`);
}
const memo = await res.json();
// memo.name is "memos/{id}"
const memoName = memo.name;
// 2. Upload images as attachments referencing the created memo
const imageMap = new Map(); // originalUrl -> attachment name ("attachments/{id}") const imageMap = new Map(); // originalUrl -> attachment name ("attachments/{id}")
if (attachCheck.checked) { if (attachCheck.checked) {
const toUpload = state.images.filter((img) => img.keep); const toUpload = state.images.filter((img) => img.keep);
@@ -303,7 +324,7 @@ sendBtn.addEventListener("click", async () => {
for (const img of toUpload) { for (const img of toUpload) {
sendBtn.textContent = `Uploading image ${uploaded + 1}/${toUpload.length}`; sendBtn.textContent = `Uploading image ${uploaded + 1}/${toUpload.length}`;
try { try {
const attachment = await uploadAttachment(baseUrl, token, img); const attachment = await uploadAttachment(baseUrl, token, img, memoName);
imageMap.set(img.src, attachment.name); imageMap.set(img.src, attachment.name);
uploaded++; uploaded++;
} catch (e) { } catch (e) {
@@ -312,9 +333,10 @@ sendBtn.addEventListener("click", async () => {
} }
} }
sendBtn.textContent = "Creating memo…"; // 3. If images were uploaded, update the memo content to reference attachment URLs
if (imageMap.size > 0) {
sendBtn.textContent = "Updating memo…";
// 2. Replace original image URLs in markdown with attachment external links
let contentWithImages = finalContent; let contentWithImages = finalContent;
for (const [origUrl, attachName] of imageMap.entries()) { for (const [origUrl, attachName] of imageMap.entries()) {
const attachmentUrl = `${baseUrl}/file/${attachName}`; const attachmentUrl = `${baseUrl}/file/${attachName}`;
@@ -331,44 +353,14 @@ sendBtn.addEventListener("click", async () => {
} }
} }
// 3. Create the memo await fetch(`${baseUrl}/api/v1/${memoName}`, {
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),
});
if (!res.ok) {
const txt = await res.text();
throw new Error(`API error ${res.status}: ${txt.slice(0, 200)}`);
}
const memo = await res.json();
// 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", method: "PATCH",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
}, },
body: JSON.stringify({ memo: memoName }), body: JSON.stringify({ content: contentWithImages }),
}); });
} catch (e) {
console.warn("Failed to link attachment to memo:", attachName, e.message);
}
} }
let memoId = ""; let memoId = "";
@@ -392,7 +384,7 @@ sendBtn.addEventListener("click", async () => {
}); });
// ── Upload a single image as an attachment ──────────────────────────────────── // ── 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 // Validate URL scheme to prevent SSRF via crafted page image URLs
if (!img.src.startsWith("data:")) { if (!img.src.startsWith("data:")) {
let parsedUrl; let parsedUrl;
@@ -444,6 +436,7 @@ async function uploadAttachment(baseUrl, token, img) {
filename, filename,
type: blob.type || "application/octet-stream", type: blob.type || "application/octet-stream",
content: base64, content: base64,
memo: memoName,
}), }),
}); });