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

enhance: 画像の圧縮(webpublic化)をクライアントサイドで行う #8176

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/backend/src/services/drive/add-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Comment on lines +196 to +197
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

判定を逆にしたほうがわかりやすいような気もする (変数名はsatisfyWebpublicとか?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここ切り出してユニットテスト入れたい気もしてたから、そのうちどうにかしておくのだわ

} catch (e) {
logger.warn(`sharp failed: ${e}`);
return {
Expand All @@ -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)`);
Expand Down
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 15 additions & 2 deletions packages/client/src/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -548,7 +549,7 @@ export function upload(file: File, folder?: any, name?: string): Promise<Misskey
const id = Math.random().toString();

const reader = new FileReader();
reader.onload = (e) => {
reader.onload = async (e) => {
const ctx = reactive({
id: id,
name: name || file.name || 'untitled',
Expand All @@ -557,12 +558,24 @@ export function upload(file: File, folder?: any, name?: string): Promise<Misskey
img: window.URL.createObjectURL(file)
});

let resizedImage: any;
if (file.type === 'image/jpeg') {
const config = {
quality: 0.85,
maxWidth: 2048,
maxHeight: 2048,
autoRotate: true,
debug: true
};
resizedImage = await readAndCompressImage(file, config);
}

uploads.value.push(ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これはif (file.type === 'image/jpeg')の前に移した方がよさそう


const data = new FormData();
data.append('i', $i.token);
data.append('force', 'true');
data.append('file', file);
data.append('file', resizedImage || file);

if (folder) data.append('folderId', folder);
if (name) data.append('name', name);
Expand Down
19 changes: 19 additions & 0 deletions packages/client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,13 @@ [email protected]:
rimraf "3.0.2"
unload "2.3.1"

[email protected]:
version "2.2.1"
resolved "https://registry.yarnpkg.com/browser-image-resizer/-/browser-image-resizer-2.2.1.tgz#61ba6d71edbdcc2caf2017c5687f5a8cf6301689"
integrity sha512-NwtjlgzKzX9zjI8bh77bnVeOrn6ZuOhD1u7NFjNClQbjCEwI4VH0GYqCh5Bcb0WIWZCDEVJFH9PH49Blk53nlw==
dependencies:
exifreader "^3.1.0"

[email protected]:
version "1.3.1"
resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60"
Expand Down Expand Up @@ -2748,6 +2755,13 @@ executable@^4.1.1:
dependencies:
pify "^2.2.0"

exifreader@^3.1.0:
version "3.16.0"
resolved "https://registry.yarnpkg.com/exifreader/-/exifreader-3.16.0.tgz#3c106eccd134e8f4786f9e8e600f8269c5296b80"
integrity sha512-RfdE1LrU3KJm8NFnU3jJG4/1quEancwTz1VgwzItP11fBoVnb3Vp7JL8bQKU8rSgZm+Roa3+BJ7zg4OtoFhTFA==
optionalDependencies:
xmldom "^0.1.31"

exit-on-epipe@~1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/exit-on-epipe/-/exit-on-epipe-1.0.1.tgz#0bdd92e87d5285d267daa8171d0eb06159689692"
Expand Down Expand Up @@ -6417,6 +6431,11 @@ xml-js@^1.6.11:
dependencies:
sax "^1.2.4"

xmldom@^0.1.31:
version "0.1.31"
resolved "https://registry.yarnpkg.com/xmldom/-/xmldom-0.1.31.tgz#b76c9a1bd9f0a9737e5a72dc37231cf38375e2ff"
integrity sha512-yS2uJflVQs6n+CyjHoaBmVSqIDevTAWrzMmjG1Gc7h1qQ7uVozNhEPJAwZXWyGQ/Gafo3fCwrcaokezLPupVyQ==

y18n@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.1.tgz#8db2b83c31c5d75099bb890b23f3094891e247d4"
Expand Down