2022-02-27 02:07:39 +00:00
|
|
|
import { uploadFromUrl } from '@/services/drive/upload-from-url.js';
|
|
|
|
import { IRemoteUser } from '@/models/entities/user.js';
|
|
|
|
import Resolver from '../resolver.js';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
import { apLogger } from '../logger.js';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
|
|
|
import { DriveFiles } from '@/models/index.js';
|
|
|
|
import { truncate } from '@/misc/truncate.js';
|
|
|
|
import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits.js';
|
2018-04-08 19:08:56 +00:00
|
|
|
|
2019-02-03 07:45:13 +00:00
|
|
|
const logger = apLogger;
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Imageを作成します。
|
|
|
|
*/
|
2019-04-07 12:50:36 +00:00
|
|
|
export async function createImage(actor: IRemoteUser, value: any): Promise<DriveFile> {
|
2018-04-19 09:54:34 +00:00
|
|
|
// 投稿者が凍結されていたらスキップ
|
|
|
|
if (actor.isSuspended) {
|
2019-04-12 16:43:22 +00:00
|
|
|
throw new Error('actor has been suspended');
|
2018-04-19 09:54:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 17:40:37 +00:00
|
|
|
const image = await new Resolver().resolve(value) as any;
|
2018-04-08 20:14:47 +00:00
|
|
|
|
|
|
|
if (image.url == null) {
|
|
|
|
throw new Error('invalid image: url not privided');
|
|
|
|
}
|
|
|
|
|
2019-02-03 07:45:13 +00:00
|
|
|
logger.info(`Creating the Image: ${image.url}`);
|
2018-04-08 19:08:56 +00:00
|
|
|
|
2018-11-07 10:43:21 +00:00
|
|
|
const instance = await fetchMeta();
|
|
|
|
|
2022-01-23 13:52:35 +00:00
|
|
|
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)
|
|
|
|
});
|
2018-10-13 08:57:40 +00:00
|
|
|
|
2019-04-09 14:07:08 +00:00
|
|
|
if (file.isLink) {
|
2018-10-16 13:21:08 +00:00
|
|
|
// URLが異なっている場合、同じ画像が以前に異なるURLで登録されていたということなので、
|
|
|
|
// URLを更新する
|
2019-04-07 12:50:36 +00:00
|
|
|
if (file.url !== image.url) {
|
|
|
|
await DriveFiles.update({ id: file.id }, {
|
|
|
|
url: image.url,
|
2021-12-09 14:58:30 +00:00
|
|
|
uri: image.url,
|
2018-10-16 13:21:08 +00:00
|
|
|
});
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2021-02-13 06:33:38 +00:00
|
|
|
file = await DriveFiles.findOneOrFail(file.id);
|
2018-10-16 13:21:08 +00:00
|
|
|
}
|
2018-10-13 08:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
2018-04-08 19:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imageを解決します。
|
|
|
|
*
|
|
|
|
* Misskeyに対象のImageが登録されていればそれを返し、そうでなければ
|
|
|
|
* リモートサーバーからフェッチしてMisskeyに登録しそれを返します。
|
|
|
|
*/
|
2019-04-07 12:50:36 +00:00
|
|
|
export async function resolveImage(actor: IRemoteUser, value: any): Promise<DriveFile> {
|
2018-04-08 19:08:56 +00:00
|
|
|
// TODO
|
|
|
|
|
|
|
|
// リモートサーバーからフェッチしてきて登録
|
|
|
|
return await createImage(actor, value);
|
|
|
|
}
|