Skip to content

Commit

Permalink
Merge pull request #59 from anatawa12/fix-original-image-is-not-origi…
Browse files Browse the repository at this point in the history
…nal-image

fix(backend): original image is not original image on web page
  • Loading branch information
anatawa12 authored Sep 26, 2023
2 parents cf8a75b + 707fce9 commit f6e194e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
### General
- Enhance: モデレーションログ機能の強化

---

- 4K/8K画像が2K以下で表示されてしまう問題を修正しました
- 既存のファイルは更新されず、新規アップロード分にのみ適用されます

### Client
- Fix: ノートのメニューにある「詳細」ボタンの表示がログイン/ログアウト状態で統一されていない問題を修正

Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type Source = {
nirila?: {
abuseDiscordHook?: string;
disableAbuseRepository?: boolean;
maxWebImageSize?: number;
}
};

Expand Down Expand Up @@ -172,6 +173,7 @@ export type Config = {
nirila: {
abuseDiscordHook?: string;
disableAbuseRepository?: boolean;
maxWebImageSize?: number;
}
};

Expand Down
15 changes: 11 additions & 4 deletions packages/backend/src/core/DriveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,26 @@ export class DriveService {
let img: sharp.Sharp | null = null;
let satisfyWebpublic: boolean;
let isAnimated: boolean;
let compressedWidth: number;
let compressedHeight: number;

try {
img = await sharpBmp(path, type);
const metadata = await img.metadata();
isAnimated = !!(metadata.pages && metadata.pages > 1);

const maxSize = this.config.nirila?.maxWebImageSize ?? 8192;
// nirila Extension: We want to keep original size as possible
// noinspection PointlessBooleanExpressionJS
satisfyWebpublic = !!(
type !== 'image/svg+xml' && // security reason
type !== 'image/avif' && // not supported by Mastodon and MS Edge
!(metadata.exif ?? metadata.iptc ?? metadata.xmp ?? metadata.tifftagPhotoshop) &&
metadata.width && metadata.width <= 2048 &&
metadata.height && metadata.height <= 2048
metadata.width && metadata.width <= maxSize &&
metadata.height && metadata.height <= maxSize
);
compressedWidth = metadata.width && metadata.width <= maxSize ? metadata.width : maxSize;
compressedHeight = metadata.height && metadata.height <= maxSize ? metadata.height : maxSize;
} catch (err) {
this.registerLogger.warn(`sharp failed: ${err}`);
return {
Expand All @@ -330,9 +337,9 @@ export class DriveService {

try {
if (['image/jpeg', 'image/webp', 'image/avif'].includes(type)) {
webpublic = await this.imageProcessingService.convertSharpToWebp(img, 2048, 2048);
webpublic = await this.imageProcessingService.convertSharpToWebp(img, compressedWidth, compressedHeight);
} else if (['image/png', 'image/bmp', 'image/svg+xml'].includes(type)) {
webpublic = await this.imageProcessingService.convertSharpToPng(img, 2048, 2048);
webpublic = await this.imageProcessingService.convertSharpToPng(img, compressedWidth, compressedHeight);
} else {
this.registerLogger.debug('web image not created (not an required image)');
}
Expand Down

0 comments on commit f6e194e

Please sign in to comment.