refactor, enhance: ドライブ引数のオブジェクト化, 追加時のcomment指定 (#8180)
* refactor: ドライブの引数をオブジェクト化する Resolve #8177 * Resolve #8181 * fix * archivePath
This commit is contained in:
		
							parent
							
								
									d8905a9588
								
							
						
					
					
						commit
						7aecf15f94
					
				
					 13 changed files with 82 additions and 47 deletions
				
			
		|  | @ -86,7 +86,7 @@ export async function exportBlocking(job: Bull.Job<DbUserJobData>, 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(); | ||||
|  |  | |||
|  | @ -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(); | ||||
|  |  | |||
|  | @ -87,7 +87,7 @@ export async function exportFollowing(job: Bull.Job<DbUserJobData>, 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(); | ||||
|  |  | |||
|  | @ -86,7 +86,7 @@ export async function exportMute(job: Bull.Job<DbUserJobData>, 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(); | ||||
|  |  | |||
|  | @ -95,7 +95,7 @@ export async function exportNotes(job: Bull.Job<DbUserJobData>, 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(); | ||||
|  |  | |||
|  | @ -63,7 +63,7 @@ export async function exportUserLists(job: Bull.Job<DbUserJobData>, 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(); | ||||
|  |  | |||
|  | @ -59,7 +59,7 @@ export async function importCustomEmojis(job: Bull.Job<DbUserImportJobData>, 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(), | ||||
|  |  | |||
|  | @ -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<Drive | |||
| 	logger.info(`Creating the Image: ${image.url}`); | ||||
| 
 | ||||
| 	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) { | ||||
| 		// URLが異なっている場合、同じ画像が以前に異なるURLで登録されていたということなので、
 | ||||
|  |  | |||
|  | @ -6,7 +6,7 @@ import { getConnection } from 'typeorm'; | |||
| import { ApiError } from '../../../error'; | ||||
| import { DriveFile } from '@/models/entities/drive-file'; | ||||
| 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'; | ||||
| 
 | ||||
| export const meta = { | ||||
|  | @ -54,7 +54,7 @@ export default define(meta, async (ps, me) => { | |||
| 
 | ||||
| 	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(); | ||||
| 	} | ||||
|  |  | |||
|  | @ -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); | ||||
|  |  | |||
|  | @ -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, | ||||
|  |  | |||
|  | @ -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<DriveFile> { | ||||
| export async function addFile({ | ||||
| 	user, | ||||
| 	path, | ||||
| 	name = null, | ||||
| 	comment = null, | ||||
| 	folderId = null, | ||||
| 	force = false, | ||||
| 	isLink = false, | ||||
| 	url = null, | ||||
| 	uri = null, | ||||
| 	sensitive = null | ||||
| }: AddFileArgs): Promise<DriveFile> { | ||||
| 	const info = await getFileInfo(path); | ||||
| 	logger.info(`${JSON.stringify(info)}`); | ||||
| 
 | ||||
|  |  | |||
|  | @ -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<DriveFile> => { | ||||
| }: Args): Promise<DriveFile> { | ||||
| 	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!; | ||||
| 	} | ||||
| }; | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue