Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨fix an issue where images downloaded in safari browser on iphone are text files #1977

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions backend/apps/images/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,13 @@ def save_b64_image(b64_str):

if "," in b64_str:
header, encoded = b64_str.split(",", 1)
mime_type = header.split(";")[0]

mime_type = header.split(";")[0].split(":")[1]
img_data = base64.b64decode(encoded)
image_format = mimetypes.guess_extension(mime_type)

image_format = (
".webp"
if mime_type == "image/webp"
else mimetypes.guess_extension(mime_type) or ".png"
)
image_filename = f"{image_id}{image_format}"
file_path = IMAGE_CACHE_DIR / f"{image_filename}"
with open(file_path, "wb") as f:
Expand Down
35 changes: 32 additions & 3 deletions src/lib/components/common/ImagePreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,40 @@
export let src = '';
export let alt = '';

const downloadImage = (url, filename) => {
const MimeTypes: { [index: string]: string } = {
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
bmp: 'image/bmp',
ico: 'image/x-icon',
tif: 'image/tiff',
tiff: 'image/tiff',
webp: 'image/webp',
svg: 'image/svg+xml',
avif: 'image/avif',
heic: 'image/heic',
heif: 'image/heif',
raw: 'image/x-raw'
};

function getMimeType(extension: string) {
return MimeTypes[extension.toLowerCase()] || 'image/png';
}

const downloadImage = (url) => {
const urlParts = url.split('/');
const fileNameWithExt = urlParts.pop().trim() || '';
const splitted = fileNameWithExt.split('.');
const extension = `${(splitted[splitted.length - 1] || 'png').toLowerCase()}`;
const filename = `Image.${extension}`;

fetch(url)
.then((response) => response.blob())
.then((blob) => {
const objectUrl = window.URL.createObjectURL(blob);
const mimeType = getMimeType(extension);
const newBlob = new Blob([blob], { type: mimeType });
const objectUrl = window.URL.createObjectURL(newBlob);
const link = document.createElement('a');
link.href = objectUrl;
link.download = filename;
Expand Down Expand Up @@ -51,7 +80,7 @@
<button
class=" p-5"
on:click={() => {
downloadImage(src, 'Image.png');
downloadImage(src);
}}
>
<svg
Expand Down