Skip to content

Commit c689472

Browse files
author
nickyang
committed
Support pasted image chat attachments
1 parent f8de49e commit c689472

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

gui/src/apps/chat/chat.vue

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
style="color:#2a1f13"
9999
@input="autoResize"
100100
@keydown.enter.exact="onEnter"
101+
@paste="onPaste"
101102
@compositionstart="composing = true"
102103
@compositionend="composing = false"
103104
/>
@@ -494,6 +495,34 @@ const onPickFiles = async (e) => {
494495
if (fileInput.value) fileInput.value.value = '';
495496
};
496497
498+
const fileExtFromMime = (type = '') => {
499+
if (type === 'image/jpeg') return 'jpg';
500+
if (type === 'image/png') return 'png';
501+
if (type === 'image/webp') return 'webp';
502+
if (type === 'image/gif') return 'gif';
503+
return 'png';
504+
};
505+
506+
const onPaste = async (event) => {
507+
if (busy.value) return;
508+
const items = Array.from(event.clipboardData?.items || []);
509+
const files = items
510+
.filter((item) => item.kind === 'file' && String(item.type || '').startsWith('image/'))
511+
.map((item, idx) => {
512+
const file = item.getAsFile();
513+
if (!file) return null;
514+
const ext = fileExtFromMime(file.type);
515+
const name = file.name && file.name !== 'image.png'
516+
? file.name
517+
: `pasted-image-${Date.now()}-${idx + 1}.${ext}`;
518+
return new File([file], name, { type: file.type || `image/${ext}` });
519+
})
520+
.filter(Boolean);
521+
if (!files.length) return;
522+
event.preventDefault();
523+
await appendFiles(files);
524+
};
525+
497526
const hasDraggedFiles = (event) => {
498527
const types = event?.dataTransfer?.types;
499528
return !!types && Array.from(types).includes('Files');

server/main/api/chat.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const CHAT_UPLOAD_DIR = join(FILES_DIR, "uploads", "chat");
1313
const MAX_UPLOAD_BYTES = 10 * 1024 * 1024;
1414
const UPLOADABLE_EXT = new Set([
1515
".txt", ".md", ".pdf", ".doc", ".docx", ".json", ".csv",
16-
".png", ".jpg", ".jpeg", ".webp", ".log", ".pptx", ".xlsx"
16+
".png", ".jpg", ".jpeg", ".webp", ".gif", ".log", ".pptx", ".xlsx",
17+
".html", ".htm"
1718
]);
1819
const safeName = (name = "file") => {
1920
const normalized = String(name || "file")

server/main/api/fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const MAX_UPLOAD_BYTES = 10 * 1024 * 1024;
1515
const MAX_TEXT_BYTES = 1024 * 1024;
1616
const TEXT_EXT = new Set([".txt", ".md", ".json", ".csv", ".log", ".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx", ".vue", ".css", ".html", ".xml", ".yml", ".yaml", ".toml"]);
1717
const WRITABLE_EXT = new Set([".txt", ".md", ".json", ".csv", ".log", ".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx", ".vue", ".css", ".html", ".xml", ".yml", ".yaml", ".toml"]);
18-
const UPLOADABLE_EXT = new Set([".txt", ".md", ".pdf", ".doc", ".docx", ".json", ".csv", ".png", ".jpg", ".jpeg", ".webp", ".log", ".pptx", ".xlsx"]);
18+
const UPLOADABLE_EXT = new Set([".txt", ".md", ".pdf", ".doc", ".docx", ".json", ".csv", ".png", ".jpg", ".jpeg", ".webp", ".gif", ".log", ".pptx", ".xlsx", ".html", ".htm"]);
1919
const MIME_MAP = {
2020
".txt": "text/plain",
2121
".md": "text/markdown",

0 commit comments

Comments
 (0)