From 4d4ac5dda5706e8ce5121137f16791d2f0ea7df6 Mon Sep 17 00:00:00 2001 From: mei23 Date: Sat, 22 Jan 2022 20:17:29 +0900 Subject: [PATCH 1/2] wip --- .../backend/src/services/drive/add-file.ts | 12 +++++++++--- packages/client/package.json | 1 + packages/client/src/os.ts | 17 +++++++++++++++-- packages/client/yarn.lock | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/services/drive/add-file.ts b/packages/backend/src/services/drive/add-file.ts index 9de4465eb95b..c117fe9100e0 100644 --- a/packages/backend/src/services/drive/add-file.ts +++ b/packages/backend/src/services/drive/add-file.ts @@ -178,6 +178,7 @@ export async function generateAlts(path: string, type: string, generateWeb: bool } let img: sharp.Sharp | null = null; + let webpublicNeeded: boolean; try { img = sharp(path); @@ -191,6 +192,9 @@ export async function generateAlts(path: string, type: string, generateWeb: bool thumbnail: null, }; } + + webpublicNeeded = !!metadata.exif || !!metadata.icc || !!metadata.iptc || !!metadata.xmp || !!metadata.tifftagPhotoshop + || !metadata.width || metadata.width > 2048 || !metadata.height || metadata.height > 2048; } catch (e) { logger.warn(`sharp failed: ${e}`); return { @@ -206,11 +210,13 @@ export async function generateAlts(path: string, type: string, generateWeb: bool logger.info(`creating web image`); try { - if (['image/jpeg'].includes(type)) { + if (['image/jpeg'].includes(type) && webpublicNeeded) { webpublic = await convertSharpToJpeg(img, 2048, 2048); - } else if (['image/webp'].includes(type)) { + } else if (['image/webp'].includes(type) && webpublicNeeded) { webpublic = await convertSharpToWebp(img, 2048, 2048); - } else if (['image/png', 'image/svg+xml'].includes(type)) { + } else if (['image/png'].includes(type) && webpublicNeeded) { + webpublic = await convertSharpToPng(img, 2048, 2048); + } else if (['image/svg+xml'].includes(type)) { webpublic = await convertSharpToPng(img, 2048, 2048); } else { logger.debug(`web image not created (not an required image)`); diff --git a/packages/client/package.json b/packages/client/package.json index 6c4cf764d0c3..19a1fb5a6fb9 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -45,6 +45,7 @@ "autwh": "0.1.0", "blurhash": "1.1.4", "broadcast-channel": "4.9.0", + "browser-image-resizer": "2.2.1", "chart.js": "3.7.0", "chartjs-adapter-date-fns": "2.0.0", "chartjs-plugin-zoom": "1.2.0", diff --git a/packages/client/src/os.ts b/packages/client/src/os.ts index 378523e1bc7f..6e8a93822039 100644 --- a/packages/client/src/os.ts +++ b/packages/client/src/os.ts @@ -9,6 +9,7 @@ import MkPostFormDialog from '@/components/post-form-dialog.vue'; import MkWaitingDialog from '@/components/waiting-dialog.vue'; import { resolve } from '@/router'; import { $i } from '@/account'; +import { readAndCompressImage } from 'browser-image-resizer'; export const pendingApiRequestsCount = ref(0); @@ -548,7 +549,7 @@ export function upload(file: File, folder?: any, name?: string): Promise { + reader.onload = async (e) => { const ctx = reactive({ id: id, name: name || file.name || 'untitled', @@ -557,12 +558,24 @@ export function upload(file: File, folder?: any, name?: string): Promise Date: Sat, 29 Jan 2022 14:53:16 +0900 Subject: [PATCH 2/2] Update packages/client/src/os.ts Co-authored-by: tamaina --- packages/client/src/os.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client/src/os.ts b/packages/client/src/os.ts index 6e8a93822039..5664cded77b6 100644 --- a/packages/client/src/os.ts +++ b/packages/client/src/os.ts @@ -567,7 +567,7 @@ export function upload(file: File, folder?: any, name?: string): Promise