From 7aecf15f9473ceafc625c07f69241a37bd61ecbf Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Sun, 23 Jan 2022 22:52:35 +0900 Subject: [PATCH] =?UTF-8?q?refactor,=20enhance:=20=E3=83=89=E3=83=A9?= =?UTF-8?q?=E3=82=A4=E3=83=96=E5=BC=95=E6=95=B0=E3=81=AE=E3=82=AA=E3=83=96?= =?UTF-8?q?=E3=82=B8=E3=82=A7=E3=82=AF=E3=83=88=E5=8C=96,=20=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E6=99=82=E3=81=AEcomment=E6=8C=87=E5=AE=9A=20(#8180)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: ドライブの引数をオブジェクト化する Resolve #8177 * Resolve #8181 * fix * archivePath --- .../queue/processors/db/export-blocking.ts | 2 +- .../processors/db/export-custom-emojis.ts | 2 +- .../queue/processors/db/export-following.ts | 2 +- .../src/queue/processors/db/export-mute.ts | 2 +- .../src/queue/processors/db/export-notes.ts | 2 +- .../queue/processors/db/export-user-lists.ts | 2 +- .../processors/db/import-custom-emojis.ts | 2 +- .../src/remote/activitypub/models/image.ts | 12 +++- .../server/api/endpoints/admin/emoji/copy.ts | 4 +- .../api/endpoints/drive/files/create.ts | 8 ++- .../endpoints/drive/files/upload-from-url.ts | 4 +- .../backend/src/services/drive/add-file.ts | 58 +++++++++++-------- .../src/services/drive/upload-from-url.ts | 29 +++++++--- 13 files changed, 82 insertions(+), 47 deletions(-) diff --git a/packages/backend/src/queue/processors/db/export-blocking.ts b/packages/backend/src/queue/processors/db/export-blocking.ts index af5c7eba1..01edaaeb6 100644 --- a/packages/backend/src/queue/processors/db/export-blocking.ts +++ b/packages/backend/src/queue/processors/db/export-blocking.ts @@ -86,7 +86,7 @@ export async function exportBlocking(job: Bull.Job, done: any): P logger.succ(`Exported to: ${path}`); const fileName = 'blocking-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv'; - const driveFile = await addFile(user, path, fileName, null, null, true); + const driveFile = await addFile({ user, path, name: fileName, force: true }); logger.succ(`Exported to: ${driveFile.id}`); cleanup(); diff --git a/packages/backend/src/queue/processors/db/export-custom-emojis.ts b/packages/backend/src/queue/processors/db/export-custom-emojis.ts index 0c06b12c9..240a542fe 100644 --- a/packages/backend/src/queue/processors/db/export-custom-emojis.ts +++ b/packages/backend/src/queue/processors/db/export-custom-emojis.ts @@ -111,7 +111,7 @@ export async function exportCustomEmojis(job: Bull.Job, done: () => void): Promi logger.succ(`Exported to: ${archivePath}`); const fileName = 'custom-emojis-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.zip'; - const driveFile = await addFile(user, archivePath, fileName, null, null, true); + const driveFile = await addFile({ user, path: archivePath, name: fileName, force: true }); logger.succ(`Exported to: ${driveFile.id}`); cleanup(); diff --git a/packages/backend/src/queue/processors/db/export-following.ts b/packages/backend/src/queue/processors/db/export-following.ts index 0c088dc37..06572acec 100644 --- a/packages/backend/src/queue/processors/db/export-following.ts +++ b/packages/backend/src/queue/processors/db/export-following.ts @@ -87,7 +87,7 @@ export async function exportFollowing(job: Bull.Job, done: () => logger.succ(`Exported to: ${path}`); const fileName = 'following-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv'; - const driveFile = await addFile(user, path, fileName, null, null, true); + const driveFile = await addFile({ user, path, name: fileName, force: true }); logger.succ(`Exported to: ${driveFile.id}`); cleanup(); diff --git a/packages/backend/src/queue/processors/db/export-mute.ts b/packages/backend/src/queue/processors/db/export-mute.ts index f5928b875..4a856f8ef 100644 --- a/packages/backend/src/queue/processors/db/export-mute.ts +++ b/packages/backend/src/queue/processors/db/export-mute.ts @@ -86,7 +86,7 @@ export async function exportMute(job: Bull.Job, done: any): Promi logger.succ(`Exported to: ${path}`); const fileName = 'mute-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv'; - const driveFile = await addFile(user, path, fileName, null, null, true); + const driveFile = await addFile({ user, path, name: fileName, force: true }); logger.succ(`Exported to: ${driveFile.id}`); cleanup(); diff --git a/packages/backend/src/queue/processors/db/export-notes.ts b/packages/backend/src/queue/processors/db/export-notes.ts index df7675dec..305abf44c 100644 --- a/packages/backend/src/queue/processors/db/export-notes.ts +++ b/packages/backend/src/queue/processors/db/export-notes.ts @@ -95,7 +95,7 @@ export async function exportNotes(job: Bull.Job, done: any): Prom logger.succ(`Exported to: ${path}`); const fileName = 'notes-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.json'; - const driveFile = await addFile(user, path, fileName, null, null, true); + const driveFile = await addFile({ user, path, name: fileName, force: true }); logger.succ(`Exported to: ${driveFile.id}`); cleanup(); diff --git a/packages/backend/src/queue/processors/db/export-user-lists.ts b/packages/backend/src/queue/processors/db/export-user-lists.ts index b9b6cb0de..f907cf952 100644 --- a/packages/backend/src/queue/processors/db/export-user-lists.ts +++ b/packages/backend/src/queue/processors/db/export-user-lists.ts @@ -63,7 +63,7 @@ export async function exportUserLists(job: Bull.Job, done: any): logger.succ(`Exported to: ${path}`); const fileName = 'user-lists-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv'; - const driveFile = await addFile(user, path, fileName, null, null, true); + const driveFile = await addFile({ user, path, name: fileName, force: true }); logger.succ(`Exported to: ${driveFile.id}`); cleanup(); diff --git a/packages/backend/src/queue/processors/db/import-custom-emojis.ts b/packages/backend/src/queue/processors/db/import-custom-emojis.ts index d2b0eb269..04e93671e 100644 --- a/packages/backend/src/queue/processors/db/import-custom-emojis.ts +++ b/packages/backend/src/queue/processors/db/import-custom-emojis.ts @@ -59,7 +59,7 @@ export async function importCustomEmojis(job: Bull.Job, don await Emojis.delete({ name: emojiInfo.name, }); - const driveFile = await addFile(null, emojiPath, record.fileName, null, null, true); + const driveFile = await addFile({ user: null, path: emojiPath, name: record.fileName, force: true }); const emoji = await Emojis.insert({ id: genId(), updatedAt: new Date(), diff --git a/packages/backend/src/remote/activitypub/models/image.ts b/packages/backend/src/remote/activitypub/models/image.ts index 902eb36a1..6f60b7827 100644 --- a/packages/backend/src/remote/activitypub/models/image.ts +++ b/packages/backend/src/remote/activitypub/models/image.ts @@ -1,4 +1,4 @@ -import uploadFromUrl from '@/services/drive/upload-from-url'; +import { uploadFromUrl } from '@/services/drive/upload-from-url'; import { IRemoteUser } from '@/models/entities/user'; import Resolver from '../resolver'; import { fetchMeta } from '@/misc/fetch-meta'; @@ -28,9 +28,15 @@ export async function createImage(actor: IRemoteUser, value: any): Promise { try { // Create file - driveFile = await uploadFromUrl(emoji.originalUrl, null, null, null, false, true); + driveFile = await uploadFromUrl({ url: emoji.originalUrl, user: null, force: true }); } catch (e) { throw new ApiError(); } diff --git a/packages/backend/src/server/api/endpoints/drive/files/create.ts b/packages/backend/src/server/api/endpoints/drive/files/create.ts index 379918154..dd65ab061 100644 --- a/packages/backend/src/server/api/endpoints/drive/files/create.ts +++ b/packages/backend/src/server/api/endpoints/drive/files/create.ts @@ -6,6 +6,7 @@ import define from '../../../define'; import { apiLogger } from '../../../logger'; import { ApiError } from '../../../error'; import { DriveFiles } from '@/models/index'; +import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits'; export const meta = { tags: ['drive'], @@ -32,6 +33,11 @@ export const meta = { default: null, }, + comment: { + validator: $.optional.nullable.str.max(DB_MAX_IMAGE_COMMENT_LENGTH), + default: null, + }, + isSensitive: { validator: $.optional.either($.bool, $.str), default: false, @@ -79,7 +85,7 @@ export default define(meta, async (ps, user, _, file, cleanup) => { try { // Create file - const driveFile = await addFile(user, file.path, name, null, ps.folderId, ps.force, false, null, null, ps.isSensitive); + const driveFile = await addFile({ user, path: file.path, name, comment: ps.comment, folderId: ps.folderId, force: ps.force, sensitive: ps.isSensitive }); return await DriveFiles.pack(driveFile, { self: true }); } catch (e) { apiLogger.error(e); diff --git a/packages/backend/src/server/api/endpoints/drive/files/upload-from-url.ts b/packages/backend/src/server/api/endpoints/drive/files/upload-from-url.ts index 6ab1ca137..40da1a4fb 100644 --- a/packages/backend/src/server/api/endpoints/drive/files/upload-from-url.ts +++ b/packages/backend/src/server/api/endpoints/drive/files/upload-from-url.ts @@ -1,7 +1,7 @@ import $ from 'cafy'; import { ID } from '@/misc/cafy-id'; import ms from 'ms'; -import uploadFromUrl from '@/services/drive/upload-from-url'; +import { uploadFromUrl } from '@/services/drive/upload-from-url'; import define from '../../../define'; import { DriveFiles } from '@/models/index'; import { publishMainStream } from '@/services/stream'; @@ -54,7 +54,7 @@ export const meta = { // eslint-disable-next-line import/no-default-export export default define(meta, async (ps, user) => { - uploadFromUrl(ps.url, user, ps.folderId, null, ps.isSensitive, ps.force, false, ps.comment).then(file => { + uploadFromUrl({ url: ps.url, user, folderId: ps.folderId, sensitive: ps.isSensitive, force: ps.force, comment: ps.comment }).then(file => { DriveFiles.pack(file, { self: true }).then(packedFile => { publishMainStream(user.id, 'urlUploadFinished', { marker: ps.marker, diff --git a/packages/backend/src/services/drive/add-file.ts b/packages/backend/src/services/drive/add-file.ts index 9de4465eb..a89e068f4 100644 --- a/packages/backend/src/services/drive/add-file.ts +++ b/packages/backend/src/services/drive/add-file.ts @@ -297,33 +297,45 @@ async function deleteOldFile(user: IRemoteUser) { } } +type AddFileArgs = { + /** User who wish to add file */ + user: { id: User['id']; host: User['host'] } | null; + /** File path */ + path: string; + /** Name */ + name?: string | null; + /** Comment */ + comment?: string | null; + /** Folder ID */ + folderId?: any; + /** If set to true, forcibly upload the file even if there is a file with the same hash. */ + force?: boolean; + /** Do not save file to local */ + isLink?: boolean; + /** URL of source (URLからアップロードされた場合(ローカル/リモート)の元URL) */ + url?: string | null; + /** URL of source (リモートインスタンスのURLからアップロードされた場合の元URL) */ + uri?: string | null; + /** Mark file as sensitive */ + sensitive?: boolean | null; +}; + /** * Add file to drive * - * @param user User who wish to add file - * @param path File path - * @param name Name - * @param comment Comment - * @param folderId Folder ID - * @param force If set to true, forcibly upload the file even if there is a file with the same hash. - * @param isLink Do not save file to local - * @param url URL of source (URLからアップロードされた場合(ローカル/リモート)の元URL) - * @param uri URL of source (リモートインスタンスのURLからアップロードされた場合の元URL) - * @param sensitive Mark file as sensitive - * @return Created drive file */ -export async function addFile( - user: { id: User['id']; host: User['host'] } | null, - path: string, - name: string | null = null, - comment: string | null = null, - folderId: any = null, - force: boolean = false, - isLink: boolean = false, - url: string | null = null, - uri: string | null = null, - sensitive: boolean | null = null -): Promise { +export async function addFile({ + user, + path, + name = null, + comment = null, + folderId = null, + force = false, + isLink = false, + url = null, + uri = null, + sensitive = null +}: AddFileArgs): Promise { const info = await getFileInfo(path); logger.info(`${JSON.stringify(info)}`); diff --git a/packages/backend/src/services/drive/upload-from-url.ts b/packages/backend/src/services/drive/upload-from-url.ts index a723c3e9a..7c5fa5ce3 100644 --- a/packages/backend/src/services/drive/upload-from-url.ts +++ b/packages/backend/src/services/drive/upload-from-url.ts @@ -10,16 +10,27 @@ import { DriveFiles } from '@/models/index'; const logger = driveLogger.createSubLogger('downloader'); -export default async ( - url: string, - user: { id: User['id']; host: User['host'] } | null, - folderId: DriveFolder['id'] | null = null, - uri: string | null = null, +type Args = { + url: string; + user: { id: User['id']; host: User['host'] } | null; + folderId?: DriveFolder['id'] | null; + uri?: string | null; + sensitive?: boolean; + force?: boolean; + isLink?: boolean; + comment?: string | null; +}; + +export async function uploadFromUrl({ + url, + user, + folderId = null, + uri = null, sensitive = false, force = false, - link = false, + isLink = false, comment = null -): Promise => { +}: Args): Promise { let name = new URL(url).pathname.split('/').pop() || null; if (name == null || !DriveFiles.validateFileName(name)) { name = null; @@ -41,7 +52,7 @@ export default async ( let error; try { - driveFile = await addFile(user, path, name, comment, folderId, force, link, url, uri, sensitive); + driveFile = await addFile({ user, path, name, comment, folderId, force, isLink, url, uri, sensitive }); logger.succ(`Got: ${driveFile.id}`); } catch (e) { error = e; @@ -59,4 +70,4 @@ export default async ( } else { return driveFile!; } -}; +}