summaryrefslogtreecommitdiff
path: root/packages/backend/src
diff options
context:
space:
mode:
authorxianon <xianon@hotmail.co.jp>2021-12-03 11:19:28 +0900
committerGitHub <noreply@github.com>2021-12-03 11:19:28 +0900
commit22464c434eb7faf3af4c8c44389996925c04eca6 (patch)
tree82245fcb5a16b1b832c22df5cf15c7417ac5584e /packages/backend/src
parentfeat: Undo Accept (#7980) (diff)
downloadmisskey-22464c434eb7faf3af4c8c44389996925c04eca6.tar.gz
misskey-22464c434eb7faf3af4c8c44389996925c04eca6.tar.bz2
misskey-22464c434eb7faf3af4c8c44389996925c04eca6.zip
fix: 画像ファイルの縦横サイズの取得で Exif Orientation を考慮する (#8014)
* 画像ファイルの縦横サイズの取得で Exif Orientation を考慮する * test: Add rotate.jpg test * Webpublic 画像を返す時のみ Exif Orientation を考慮して縦横サイズを返す * test: Support orientation
Diffstat (limited to 'packages/backend/src')
-rw-r--r--packages/backend/src/misc/get-file-info.ts5
-rw-r--r--packages/backend/src/models/entities/drive-file.ts2
-rw-r--r--packages/backend/src/models/repositories/drive-file.ts20
-rw-r--r--packages/backend/src/services/drive/add-file.ts4
4 files changed, 29 insertions, 2 deletions
diff --git a/packages/backend/src/misc/get-file-info.ts b/packages/backend/src/misc/get-file-info.ts
index 39ba541395..8d7f6b1bf9 100644
--- a/packages/backend/src/misc/get-file-info.ts
+++ b/packages/backend/src/misc/get-file-info.ts
@@ -19,6 +19,7 @@ export type FileInfo = {
};
width?: number;
height?: number;
+ orientation?: number;
blurhash?: string;
warnings: string[];
};
@@ -47,6 +48,7 @@ export async function getFileInfo(path: string): Promise<FileInfo> {
// image dimensions
let width: number | undefined;
let height: number | undefined;
+ let orientation: number | undefined;
if (['image/jpeg', 'image/gif', 'image/png', 'image/apng', 'image/webp', 'image/bmp', 'image/tiff', 'image/svg+xml', 'image/vnd.adobe.photoshop'].includes(type.mime)) {
const imageSize = await detectImageSize(path).catch(e => {
@@ -61,6 +63,7 @@ export async function getFileInfo(path: string): Promise<FileInfo> {
} else if (imageSize.wUnits === 'px') {
width = imageSize.width;
height = imageSize.height;
+ orientation = imageSize.orientation;
// 制限を超えている画像は octet-stream にする
if (imageSize.width > 16383 || imageSize.height > 16383) {
@@ -87,6 +90,7 @@ export async function getFileInfo(path: string): Promise<FileInfo> {
type,
width,
height,
+ orientation,
blurhash,
warnings,
};
@@ -163,6 +167,7 @@ async function detectImageSize(path: string): Promise<{
height: number;
wUnits: string;
hUnits: string;
+ orientation?: number;
}> {
const readable = fs.createReadStream(path);
const imageSize = await probeImageSize(readable);
diff --git a/packages/backend/src/models/entities/drive-file.ts b/packages/backend/src/models/entities/drive-file.ts
index 698dfac222..4ec7b94ed2 100644
--- a/packages/backend/src/models/entities/drive-file.ts
+++ b/packages/backend/src/models/entities/drive-file.ts
@@ -77,7 +77,7 @@ export class DriveFile {
default: {},
comment: 'The any properties of the DriveFile. For example, it includes image width/height.'
})
- public properties: { width?: number; height?: number; avgColor?: string };
+ public properties: { width?: number; height?: number; orientation?: number; avgColor?: string };
@Index()
@Column('boolean')
diff --git a/packages/backend/src/models/repositories/drive-file.ts b/packages/backend/src/models/repositories/drive-file.ts
index ddf9a46afd..f2f0308dc0 100644
--- a/packages/backend/src/models/repositories/drive-file.ts
+++ b/packages/backend/src/models/repositories/drive-file.ts
@@ -28,6 +28,19 @@ export class DriveFileRepository extends Repository<DriveFile> {
);
}
+ public getPublicProperties(file: DriveFile): DriveFile['properties'] {
+ if (file.properties.orientation != null) {
+ const properties = JSON.parse(JSON.stringify(file.properties));
+ if (file.properties.orientation >= 5) {
+ [properties.width, properties.height] = [properties.height, properties.width];
+ }
+ properties.orientation = undefined;
+ return properties;
+ }
+
+ return file.properties;
+ }
+
public getPublicUrl(file: DriveFile, thumbnail = false, meta?: Meta): string | null {
// リモートかつメディアプロキシ
if (file.uri != null && file.userHost != null && config.mediaProxy != null) {
@@ -122,7 +135,7 @@ export class DriveFileRepository extends Repository<DriveFile> {
size: file.size,
isSensitive: file.isSensitive,
blurhash: file.blurhash,
- properties: file.properties,
+ properties: opts.self ? file.properties : this.getPublicProperties(file),
url: opts.self ? file.url : this.getPublicUrl(file, false, meta),
thumbnailUrl: this.getPublicUrl(file, true, meta),
comment: file.comment,
@@ -202,6 +215,11 @@ export const packedDriveFileSchema = {
optional: true as const, nullable: false as const,
example: 720
},
+ orientation: {
+ type: 'number' as const,
+ optional: true as const, nullable: false as const,
+ example: 8
+ },
avgColor: {
type: 'string' as const,
optional: true as const, nullable: false as const,
diff --git a/packages/backend/src/services/drive/add-file.ts b/packages/backend/src/services/drive/add-file.ts
index 6c5fefd4ad..a57f9cf068 100644
--- a/packages/backend/src/services/drive/add-file.ts
+++ b/packages/backend/src/services/drive/add-file.ts
@@ -372,12 +372,16 @@ export default async function(
const properties: {
width?: number;
height?: number;
+ orientation?: number;
} = {};
if (info.width) {
properties['width'] = info.width;
properties['height'] = info.height;
}
+ if (info.orientation != null) {
+ properties['orientation'] = info.orientation;
+ }
const profile = user ? await UserProfiles.findOne(user.id) : null;