From 1a51b9870039da8b328acc673b2e8cb08ae2e663 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 31 Oct 2018 22:35:02 +0900 Subject: [PATCH] Refactor --- src/misc/get-drive-file-url.ts | 20 ++++++++++++++++++++ src/models/drive-file.ts | 13 ++++++++++--- src/models/user.ts | 10 ---------- src/remote/activitypub/models/person.ts | 5 +++-- src/remote/activitypub/renderer/document.ts | 4 ++-- src/remote/activitypub/renderer/image.ts | 4 ++-- src/server/api/endpoints/i/update.ts | 8 ++++---- 7 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 src/misc/get-drive-file-url.ts diff --git a/src/misc/get-drive-file-url.ts b/src/misc/get-drive-file-url.ts new file mode 100644 index 000000000..0fe467261 --- /dev/null +++ b/src/misc/get-drive-file-url.ts @@ -0,0 +1,20 @@ +import { IDriveFile } from '../models/drive-file'; +import config from '../config'; + +export default function(file: IDriveFile, thumbnail = false): string { + if (file == null) return null; + + if (file.metadata.withoutChunks) { + if (thumbnail) { + return file.metadata.thumbnailUrl || file.metadata.url; + } else { + return file.metadata.url; + } + } else { + if (thumbnail) { + return `${config.drive_url}/${file._id}?thumbnail`; + } else { + return `${config.drive_url}/${file._id}`; + } + } +} diff --git a/src/models/drive-file.ts b/src/models/drive-file.ts index 5e2646a30..57f40be6a 100644 --- a/src/models/drive-file.ts +++ b/src/models/drive-file.ts @@ -1,9 +1,9 @@ import * as mongo from 'mongodb'; const deepcopy = require('deepcopy'); import { pack as packFolder } from './drive-folder'; -import config from '../config'; import monkDb, { nativeDbConn } from '../db/mongodb'; import isObjectId from '../misc/is-objectid'; +import getDriveFileUrl from '../misc/get-drive-file-url'; const DriveFile = monkDb.get('driveFiles.files'); DriveFile.createIndex('md5'); @@ -33,7 +33,14 @@ export type IMetadata = { thumbnailUrl?: string; src?: string; deletedAt?: Date; + + /** + * このファイルの中身データがMongoDB内に保存されているのか否か + * オブジェクトストレージを利用している or リモートサーバーへの直リンクである + * な場合は false になります + */ withoutChunks?: boolean; + storage?: string; storageProps?: any; isSensitive?: boolean; @@ -128,8 +135,8 @@ export const pack = ( _target = Object.assign(_target, _file.metadata); - _target.url = _file.metadata.url ? _file.metadata.url : `${config.drive_url}/${_target.id}/${encodeURIComponent(_target.name)}`; - _target.thumbnailUrl = _file.metadata.thumbnailUrl ? _file.metadata.thumbnailUrl : _file.metadata.url ? _file.metadata.url : `${config.drive_url}/${_target.id}/${encodeURIComponent(_target.name)}?thumbnail`; + _target.url = getDriveFileUrl(file); + _target.thumbnailUrl = getDriveFileUrl(file, true); _target.isRemote = _file.metadata.isRemote; if (_target.properties == null) _target.properties = {}; diff --git a/src/models/user.ts b/src/models/user.ts index 57973d6ca..ce877fe27 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -261,16 +261,6 @@ export const pack = ( if (_user.avatarUrl == null) { _user.avatarUrl = `${config.drive_url}/default-avatar.jpg`; - - // 互換性のため - if (_user.avatarId) { - _user.avatarUrl = `${config.drive_url}/${_user.avatarId}`; - } - } - - // 互換性のため - if (_user.bannerId && _user.bannerUrl == null) { - _user.bannerUrl = `${config.drive_url}/${_user.bannerId}`; } if (!meId || !meId.equals(_user.id) || !opts.detail) { diff --git a/src/remote/activitypub/models/person.ts b/src/remote/activitypub/models/person.ts index f115dee87..7842e8788 100644 --- a/src/remote/activitypub/models/person.ts +++ b/src/remote/activitypub/models/person.ts @@ -15,6 +15,7 @@ import { URL } from 'url'; import { resolveNote } from './note'; import registerInstance from '../../../services/register-instance'; import Instance from '../../../models/instance'; +import getDriveFileUrl from '../../../misc/get-drive-file-url'; const log = debug('misskey:activitypub'); @@ -303,8 +304,8 @@ export async function updatePerson(uri: string, resolver?: Resolver, hint?: obje featured: person.featured, avatarId: avatar ? avatar._id : null, bannerId: banner ? banner._id : null, - avatarUrl: (avatar && avatar.metadata.thumbnailUrl) ? avatar.metadata.thumbnailUrl : (avatar && avatar.metadata.url) ? avatar.metadata.url : null, - bannerUrl: banner && banner.metadata.url ? banner.metadata.url : null, + avatarUrl: getDriveFileUrl(avatar, true), + bannerUrl: getDriveFileUrl(banner, true), description: htmlToMFM(person.summary), followersCount, followingCount, diff --git a/src/remote/activitypub/renderer/document.ts b/src/remote/activitypub/renderer/document.ts index 1f4543d7e..17721e941 100644 --- a/src/remote/activitypub/renderer/document.ts +++ b/src/remote/activitypub/renderer/document.ts @@ -1,8 +1,8 @@ -import config from '../../../config'; import { IDriveFile } from '../../../models/drive-file'; +import getDriveFileUrl from '../../../misc/get-drive-file-url'; export default (file: IDriveFile) => ({ type: 'Document', mediaType: file.contentType, - url: file.metadata.url || `${config.drive_url}/${file._id}` + url: getDriveFileUrl(file) }); diff --git a/src/remote/activitypub/renderer/image.ts b/src/remote/activitypub/renderer/image.ts index b2f255500..ec637b952 100644 --- a/src/remote/activitypub/renderer/image.ts +++ b/src/remote/activitypub/renderer/image.ts @@ -1,8 +1,8 @@ -import config from '../../../config'; import { IDriveFile } from '../../../models/drive-file'; +import getDriveFileUrl from '../../../misc/get-drive-file-url'; export default (file: IDriveFile) => ({ type: 'Image', - url: file.metadata.url || `${config.drive_url}/${file._id}`, + url: getDriveFileUrl(file), sensitive: file.metadata.isSensitive }); diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts index 7b8431f0e..357b57129 100644 --- a/src/server/api/endpoints/i/update.ts +++ b/src/server/api/endpoints/i/update.ts @@ -4,9 +4,9 @@ import { publishMainStream } from '../../../../stream'; import DriveFile from '../../../../models/drive-file'; import acceptAllFollowRequests from '../../../../services/following/requests/accept-all'; import { IApp } from '../../../../models/app'; -import config from '../../../../config'; import { publishToFollowers } from '../../../../services/i/update'; import getParams from '../../get-params'; +import getDriveFileUrl from '../../../../misc/get-drive-file-url'; export const meta = { desc: { @@ -129,7 +129,7 @@ export default async (params: any, user: ILocalUser, app: IApp) => new Promise(a if (avatar == null) return rej('avatar not found'); if (!avatar.contentType.startsWith('image/')) return rej('avatar not an image'); - updates.avatarUrl = avatar.metadata.thumbnailUrl || avatar.metadata.url || `${config.drive_url}/${avatar._id}`; + updates.avatarUrl = getDriveFileUrl(avatar, true); if (avatar.metadata.properties.avgColor) { updates.avatarColor = avatar.metadata.properties.avgColor; @@ -144,7 +144,7 @@ export default async (params: any, user: ILocalUser, app: IApp) => new Promise(a if (banner == null) return rej('banner not found'); if (!banner.contentType.startsWith('image/')) return rej('banner not an image'); - updates.bannerUrl = banner.metadata.url || `${config.drive_url}/${banner._id}`; + updates.bannerUrl = getDriveFileUrl(banner, true); if (banner.metadata.properties.avgColor) { updates.bannerColor = banner.metadata.properties.avgColor; @@ -162,7 +162,7 @@ export default async (params: any, user: ILocalUser, app: IApp) => new Promise(a if (wallpaper == null) return rej('wallpaper not found'); - updates.wallpaperUrl = wallpaper.metadata.url || `${config.drive_url}/${wallpaper._id}`; + updates.wallpaperUrl = getDriveFileUrl(wallpaper); if (wallpaper.metadata.properties.avgColor) { updates.wallpaperColor = wallpaper.metadata.properties.avgColor;