2022-02-27 02:07:39 +00:00
|
|
|
import { uploadFromUrl } from '@/services/drive/upload-from-url.js';
|
2022-07-12 12:41:10 +00:00
|
|
|
import { CacheableRemoteUser } from '@/models/entities/user.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
2022-07-12 12:41:10 +00:00
|
|
|
import { DriveFiles } from '@/models/index.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { truncate } from '@/misc/truncate.js';
|
|
|
|
import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits.js';
|
2022-08-03 11:18:33 +00:00
|
|
|
import Resolver from '../resolver.js';
|
|
|
|
import { apLogger } from '../logger.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を作成します。
|
|
|
|
*/
|
2022-03-25 07:27:41 +00:00
|
|
|
export async function createImage(actor: CacheableRemoteUser, 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,
|
2022-08-10 22:09:29 +00:00
|
|
|
comment: truncate(image.name, DB_MAX_IMAGE_COMMENT_LENGTH),
|
2022-01-23 13:52:35 +00:00
|
|
|
});
|
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
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
file = await DriveFiles.findOneByOrFail({ id: 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-11 04:20:43 +00:00
|
|
|
* Resolve Image.
|
2018-04-08 19:08:56 +00:00
|
|
|
*
|
2022-09-11 04:20:43 +00:00
|
|
|
* If the target Image is registered in FoundKey, return it; otherwise, fetch it from the remote server and return it.
|
|
|
|
* Fetch the image from the remote server, register it in FoundKey and return it.
|
2018-04-08 19:08:56 +00:00
|
|
|
*/
|
2022-03-25 07:27:41 +00:00
|
|
|
export async function resolveImage(actor: CacheableRemoteUser, value: any): Promise<DriveFile> {
|
2018-04-08 19:08:56 +00:00
|
|
|
// TODO
|
|
|
|
|
|
|
|
// リモートサーバーからフェッチしてきて登録
|
|
|
|
return await createImage(actor, value);
|
|
|
|
}
|