From e5bac649c85c8a295d573b630bfb5c71098d265e Mon Sep 17 00:00:00 2001 From: naskya Date: Sat, 27 Apr 2024 10:05:48 +0900 Subject: [PATCH] refactor (backend): flatten a type --- packages/backend/src/misc/get-file-info.ts | 11 +++++------ packages/backend/src/services/drive/add-file.ts | 17 +++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/misc/get-file-info.ts b/packages/backend/src/misc/get-file-info.ts index 532bbeec56..de2117eea7 100644 --- a/packages/backend/src/misc/get-file-info.ts +++ b/packages/backend/src/misc/get-file-info.ts @@ -9,13 +9,11 @@ import sharp from "sharp"; import { encode } from "blurhash"; import { inspect } from "node:util"; -export type FileInfo = { +type FileInfo = { size: number; md5: string; - type: { - mime: string; - ext: string | null; - }; + mime: string; + fileExtension: string | null; width?: number; height?: number; orientation?: number; @@ -109,7 +107,8 @@ export async function getFileInfo(path: string): Promise { return { size, md5, - type, + mime: type.mime, + fileExtension: type.ext, width, height, orientation, diff --git a/packages/backend/src/services/drive/add-file.ts b/packages/backend/src/services/drive/add-file.ts index 9d28112c1d..6615340cd1 100644 --- a/packages/backend/src/services/drive/add-file.ts +++ b/packages/backend/src/services/drive/add-file.ts @@ -482,7 +482,8 @@ export async function addFile({ // detect name const detectedName = - name || (info.type.ext ? `untitled.${info.type.ext}` : "untitled"); + name || + (info.fileExtension ? `untitled.${info.fileExtension}` : "untitled"); if (user && !force) { // Check if there is a file with the same hash @@ -561,12 +562,12 @@ export async function addFile({ orientation?: number; } = {}; - if (info.width) { - properties["width"] = info.width; - properties["height"] = info.height; + if (info.width != null && info.height != null) { + properties.width = info.width; + properties.height = info.height; } if (info.orientation != null) { - properties["orientation"] = info.orientation; + properties.orientation = info.orientation; } const profile = user @@ -584,7 +585,7 @@ export async function addFile({ file.folderId = folder != null ? folder.id : null; file.comment = comment; file.properties = properties; - file.blurhash = info.blurhash || null; + file.blurhash = info.blurhash ?? null; file.isLink = isLink; file.requestIp = requestIp; file.requestHeaders = requestHeaders; @@ -619,7 +620,7 @@ export async function addFile({ file.size = 0; file.md5 = info.md5; file.name = detectedName; - file.type = info.type.mime; + file.type = info.mime; file.storedInternal = false; file = await DriveFiles.insert(file).then((x) => @@ -644,7 +645,7 @@ export async function addFile({ file, path, detectedName, - info.type.mime, + info.mime, info.md5, info.size, usageHint,