refactor, enhance: ドライブ引数のオブジェクト化, 追加時のcomment指定 (#8180)

* refactor: ドライブの引数をオブジェクト化する Resolve #8177

* Resolve #8181

* fix

* archivePath
This commit is contained in:
MeiMei 2022-01-23 22:52:35 +09:00 committed by GitHub
parent d8905a9588
commit 7aecf15f94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 82 additions and 47 deletions

View file

@ -86,7 +86,7 @@ export async function exportBlocking(job: Bull.Job<DbUserJobData>, done: any): P
logger.succ(`Exported to: ${path}`); logger.succ(`Exported to: ${path}`);
const fileName = 'blocking-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv'; 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}`); logger.succ(`Exported to: ${driveFile.id}`);
cleanup(); cleanup();

View file

@ -111,7 +111,7 @@ export async function exportCustomEmojis(job: Bull.Job, done: () => void): Promi
logger.succ(`Exported to: ${archivePath}`); logger.succ(`Exported to: ${archivePath}`);
const fileName = 'custom-emojis-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.zip'; 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}`); logger.succ(`Exported to: ${driveFile.id}`);
cleanup(); cleanup();

View file

@ -87,7 +87,7 @@ export async function exportFollowing(job: Bull.Job<DbUserJobData>, done: () =>
logger.succ(`Exported to: ${path}`); logger.succ(`Exported to: ${path}`);
const fileName = 'following-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv'; 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}`); logger.succ(`Exported to: ${driveFile.id}`);
cleanup(); cleanup();

View file

@ -86,7 +86,7 @@ export async function exportMute(job: Bull.Job<DbUserJobData>, done: any): Promi
logger.succ(`Exported to: ${path}`); logger.succ(`Exported to: ${path}`);
const fileName = 'mute-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv'; 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}`); logger.succ(`Exported to: ${driveFile.id}`);
cleanup(); cleanup();

View file

@ -95,7 +95,7 @@ export async function exportNotes(job: Bull.Job<DbUserJobData>, done: any): Prom
logger.succ(`Exported to: ${path}`); logger.succ(`Exported to: ${path}`);
const fileName = 'notes-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.json'; 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}`); logger.succ(`Exported to: ${driveFile.id}`);
cleanup(); cleanup();

View file

@ -63,7 +63,7 @@ export async function exportUserLists(job: Bull.Job<DbUserJobData>, done: any):
logger.succ(`Exported to: ${path}`); logger.succ(`Exported to: ${path}`);
const fileName = 'user-lists-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv'; 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}`); logger.succ(`Exported to: ${driveFile.id}`);
cleanup(); cleanup();

View file

@ -59,7 +59,7 @@ export async function importCustomEmojis(job: Bull.Job<DbUserImportJobData>, don
await Emojis.delete({ await Emojis.delete({
name: emojiInfo.name, 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({ const emoji = await Emojis.insert({
id: genId(), id: genId(),
updatedAt: new Date(), updatedAt: new Date(),

View file

@ -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 { IRemoteUser } from '@/models/entities/user';
import Resolver from '../resolver'; import Resolver from '../resolver';
import { fetchMeta } from '@/misc/fetch-meta'; import { fetchMeta } from '@/misc/fetch-meta';
@ -28,9 +28,15 @@ export async function createImage(actor: IRemoteUser, value: any): Promise<Drive
logger.info(`Creating the Image: ${image.url}`); logger.info(`Creating the Image: ${image.url}`);
const instance = await fetchMeta(); const instance = await fetchMeta();
const cache = instance.cacheRemoteFiles;
let file = await uploadFromUrl(image.url, actor, null, image.url, image.sensitive, false, !cache, truncate(image.name, DB_MAX_IMAGE_COMMENT_LENGTH)); let file = await uploadFromUrl({
url: image.url,
user: actor,
uri: image.url,
sensitive: image.sensitive,
isLink: !instance.cacheRemoteFiles,
comment: truncate(image.name, DB_MAX_IMAGE_COMMENT_LENGTH)
});
if (file.isLink) { if (file.isLink) {
// URLが異なっている場合、同じ画像が以前に異なるURLで登録されていたということなので、 // URLが異なっている場合、同じ画像が以前に異なるURLで登録されていたということなので、

View file

@ -6,7 +6,7 @@ import { getConnection } from 'typeorm';
import { ApiError } from '../../../error'; import { ApiError } from '../../../error';
import { DriveFile } from '@/models/entities/drive-file'; import { DriveFile } from '@/models/entities/drive-file';
import { ID } from '@/misc/cafy-id'; import { ID } from '@/misc/cafy-id';
import uploadFromUrl from '@/services/drive/upload-from-url'; import { uploadFromUrl } from '@/services/drive/upload-from-url';
import { publishBroadcastStream } from '@/services/stream'; import { publishBroadcastStream } from '@/services/stream';
export const meta = { export const meta = {
@ -54,7 +54,7 @@ export default define(meta, async (ps, me) => {
try { try {
// Create file // Create file
driveFile = await uploadFromUrl(emoji.originalUrl, null, null, null, false, true); driveFile = await uploadFromUrl({ url: emoji.originalUrl, user: null, force: true });
} catch (e) { } catch (e) {
throw new ApiError(); throw new ApiError();
} }

View file

@ -6,6 +6,7 @@ import define from '../../../define';
import { apiLogger } from '../../../logger'; import { apiLogger } from '../../../logger';
import { ApiError } from '../../../error'; import { ApiError } from '../../../error';
import { DriveFiles } from '@/models/index'; import { DriveFiles } from '@/models/index';
import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits';
export const meta = { export const meta = {
tags: ['drive'], tags: ['drive'],
@ -32,6 +33,11 @@ export const meta = {
default: null, default: null,
}, },
comment: {
validator: $.optional.nullable.str.max(DB_MAX_IMAGE_COMMENT_LENGTH),
default: null,
},
isSensitive: { isSensitive: {
validator: $.optional.either($.bool, $.str), validator: $.optional.either($.bool, $.str),
default: false, default: false,
@ -79,7 +85,7 @@ export default define(meta, async (ps, user, _, file, cleanup) => {
try { try {
// Create file // 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 }); return await DriveFiles.pack(driveFile, { self: true });
} catch (e) { } catch (e) {
apiLogger.error(e); apiLogger.error(e);

View file

@ -1,7 +1,7 @@
import $ from 'cafy'; import $ from 'cafy';
import { ID } from '@/misc/cafy-id'; import { ID } from '@/misc/cafy-id';
import ms from 'ms'; 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 define from '../../../define';
import { DriveFiles } from '@/models/index'; import { DriveFiles } from '@/models/index';
import { publishMainStream } from '@/services/stream'; import { publishMainStream } from '@/services/stream';
@ -54,7 +54,7 @@ export const meta = {
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default define(meta, async (ps, user) => { 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 => { DriveFiles.pack(file, { self: true }).then(packedFile => {
publishMainStream(user.id, 'urlUploadFinished', { publishMainStream(user.id, 'urlUploadFinished', {
marker: ps.marker, marker: ps.marker,

View file

@ -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 * 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( export async function addFile({
user: { id: User['id']; host: User['host'] } | null, user,
path: string, path,
name: string | null = null, name = null,
comment: string | null = null, comment = null,
folderId: any = null, folderId = null,
force: boolean = false, force = false,
isLink: boolean = false, isLink = false,
url: string | null = null, url = null,
uri: string | null = null, uri = null,
sensitive: boolean | null = null sensitive = null
): Promise<DriveFile> { }: AddFileArgs): Promise<DriveFile> {
const info = await getFileInfo(path); const info = await getFileInfo(path);
logger.info(`${JSON.stringify(info)}`); logger.info(`${JSON.stringify(info)}`);

View file

@ -10,16 +10,27 @@ import { DriveFiles } from '@/models/index';
const logger = driveLogger.createSubLogger('downloader'); const logger = driveLogger.createSubLogger('downloader');
export default async ( type Args = {
url: string, url: string;
user: { id: User['id']; host: User['host'] } | null, user: { id: User['id']; host: User['host'] } | null;
folderId: DriveFolder['id'] | null = null, folderId?: DriveFolder['id'] | null;
uri: string | null = 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, sensitive = false,
force = false, force = false,
link = false, isLink = false,
comment = null comment = null
): Promise<DriveFile> => { }: Args): Promise<DriveFile> {
let name = new URL(url).pathname.split('/').pop() || null; let name = new URL(url).pathname.split('/').pop() || null;
if (name == null || !DriveFiles.validateFileName(name)) { if (name == null || !DriveFiles.validateFileName(name)) {
name = null; name = null;
@ -41,7 +52,7 @@ export default async (
let error; let error;
try { 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}`); logger.succ(`Got: ${driveFile.id}`);
} catch (e) { } catch (e) {
error = e; error = e;
@ -59,4 +70,4 @@ export default async (
} else { } else {
return driveFile!; return driveFile!;
} }
}; }