Share webp images from web, desktop, obsidian app to chat with

This commit is contained in:
Debanjum Singh Solanky 2024-10-18 01:25:16 -07:00
parent dbd9a945b0
commit a4e6e1d5e8
6 changed files with 13 additions and 4 deletions

View file

@ -19,7 +19,7 @@ const textFileTypes = [
'org', 'md', 'markdown', 'txt', 'html', 'xml', 'org', 'md', 'markdown', 'txt', 'html', 'xml',
// Other valid text file extensions from https://google.github.io/magika/model/config.json // Other valid text file extensions from https://google.github.io/magika/model/config.json
'appleplist', 'asm', 'asp', 'batch', 'c', 'cs', 'css', 'csv', 'eml', 'go', 'html', 'ini', 'internetshortcut', 'java', 'javascript', 'json', 'latex', 'lisp', 'makefile', 'markdown', 'mht', 'mum', 'pem', 'perl', 'php', 'powershell', 'python', 'rdf', 'rst', 'rtf', 'ruby', 'rust', 'scala', 'shell', 'smali', 'sql', 'svg', 'symlinktext', 'txt', 'vba', 'winregistry', 'xml', 'yaml'] 'appleplist', 'asm', 'asp', 'batch', 'c', 'cs', 'css', 'csv', 'eml', 'go', 'html', 'ini', 'internetshortcut', 'java', 'javascript', 'json', 'latex', 'lisp', 'makefile', 'markdown', 'mht', 'mum', 'pem', 'perl', 'php', 'powershell', 'python', 'rdf', 'rst', 'rtf', 'ruby', 'rust', 'scala', 'shell', 'smali', 'sql', 'svg', 'symlinktext', 'txt', 'vba', 'winregistry', 'xml', 'yaml']
const binaryFileTypes = ['pdf', 'jpg', 'jpeg', 'png'] const binaryFileTypes = ['pdf', 'jpg', 'jpeg', 'png', 'webp']
const validFileTypes = textFileTypes.concat(binaryFileTypes); const validFileTypes = textFileTypes.concat(binaryFileTypes);
const schema = { const schema = {
@ -104,6 +104,8 @@ function filenameToMimeType (filename) {
case 'jpg': case 'jpg':
case 'jpeg': case 'jpeg':
return 'image/jpeg'; return 'image/jpeg';
case 'webp':
return 'image/webp';
case 'md': case 'md':
case 'markdown': case 'markdown':
return 'text/markdown'; return 'text/markdown';

View file

@ -37,6 +37,8 @@ function filenameToMimeType (filename: TFile): string {
case 'jpg': case 'jpg':
case 'jpeg': case 'jpeg':
return 'image/jpeg'; return 'image/jpeg';
case 'webp':
return 'image/webp';
case 'md': case 'md':
case 'markdown': case 'markdown':
return 'text/markdown'; return 'text/markdown';
@ -50,7 +52,7 @@ function filenameToMimeType (filename: TFile): string {
export const fileTypeToExtension = { export const fileTypeToExtension = {
'pdf': ['pdf'], 'pdf': ['pdf'],
'image': ['png', 'jpg', 'jpeg'], 'image': ['png', 'jpg', 'jpeg', 'webp'],
'markdown': ['md', 'markdown'], 'markdown': ['md', 'markdown'],
}; };
export const supportedImageFilesTypes = fileTypeToExtension.image; export const supportedImageFilesTypes = fileTypeToExtension.image;

View file

@ -241,6 +241,7 @@ function getIconFromFilename(
case "jpg": case "jpg":
case "jpeg": case "jpeg":
case "png": case "png":
case "webp":
return <Image className={className} weight="fill" />; return <Image className={className} weight="fill" />;
default: default:
return <File className={className} weight="fill" />; return <File className={className} weight="fill" />;

View file

@ -168,12 +168,12 @@ export default function ChatInputArea(props: ChatInputProps) {
function uploadFiles(files: FileList) { function uploadFiles(files: FileList) {
if (!props.isLoggedIn) { if (!props.isLoggedIn) {
setLoginRedirectMessage("Whoa! You need to login to upload files"); setLoginRedirectMessage("Please login to chat with your files");
setShowLoginPrompt(true); setShowLoginPrompt(true);
return; return;
} }
// check for image file // check for image file
const image_endings = ["jpg", "jpeg", "png"]; const image_endings = ["jpg", "jpeg", "png", "webp"];
for (let i = 0; i < files.length; i++) { for (let i = 0; i < files.length; i++) {
const file = files[i]; const file = files[i];
const file_extension = file.name.split(".").pop(); const file_extension = file.name.split(".").pop();

View file

@ -64,6 +64,8 @@ class ImageToEntries(TextToEntries):
tmp_file = f"tmp_image_file_{timestamp_now}.png" tmp_file = f"tmp_image_file_{timestamp_now}.png"
elif image_file.endswith(".jpg") or image_file.endswith(".jpeg"): elif image_file.endswith(".jpg") or image_file.endswith(".jpeg"):
tmp_file = f"tmp_image_file_{timestamp_now}.jpg" tmp_file = f"tmp_image_file_{timestamp_now}.jpg"
elif image_file.endswith(".webp"):
tmp_file = f"tmp_image_file_{timestamp_now}.webp"
with open(tmp_file, "wb") as f: with open(tmp_file, "wb") as f:
bytes = image_files[image_file] bytes = image_files[image_file]
f.write(bytes) f.write(bytes)

View file

@ -127,6 +127,8 @@ def get_file_type(file_type: str, file_content: bytes) -> tuple[str, str]:
return "image", encoding return "image", encoding
elif file_type in ["image/png"]: elif file_type in ["image/png"]:
return "image", encoding return "image", encoding
elif file_type in ["image/webp"]:
return "image", encoding
elif content_group in ["code", "text"]: elif content_group in ["code", "text"]:
return "plaintext", encoding return "plaintext", encoding
else: else: