2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
2023-08-05 03:33:00 +02:00
|
|
|
import { In } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { DriveFilesRepository } from '@/models/_.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiDriveFile } from '@/models/DriveFile.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { appendQuery, query } from '@/misc/prelude/url.js';
|
2022-11-17 01:31:07 +01:00
|
|
|
import { deepClone } from '@/misc/clone.js';
|
2023-08-16 10:51:28 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
import { isMimeImage } from '@/misc/is-mime-image.js';
|
|
|
|
import { isNotNull } from '@/misc/is-not-null.js';
|
2023-10-16 03:45:22 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { UtilityService } from '../UtilityService.js';
|
2023-02-12 10:47:30 +01:00
|
|
|
import { VideoProcessingService } from '../VideoProcessingService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
import { DriveFolderEntityService } from './DriveFolderEntityService.js';
|
2019-04-23 15:35:26 +02:00
|
|
|
|
2021-03-16 04:50:07 +01:00
|
|
|
type PackOptions = {
|
|
|
|
detail?: boolean,
|
|
|
|
self?: boolean,
|
|
|
|
withUser?: boolean,
|
|
|
|
};
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export class DriveFileEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
// 循環参照のため / for circular dependency
|
|
|
|
@Inject(forwardRef(() => UserEntityService))
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private driveFolderEntityService: DriveFolderEntityService,
|
2023-02-12 01:13:47 +01:00
|
|
|
private videoProcessingService: VideoProcessingService,
|
2023-10-16 03:45:22 +02:00
|
|
|
private idService: IdService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
|
|
|
}
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public validateFileName(name: string): boolean {
|
2019-04-07 14:50:36 +02:00
|
|
|
return (
|
|
|
|
(name.trim().length > 0) &&
|
|
|
|
(name.length <= 200) &&
|
|
|
|
(name.indexOf('\\') === -1) &&
|
|
|
|
(name.indexOf('/') === -1) &&
|
|
|
|
(name.indexOf('..') === -1)
|
|
|
|
);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public getPublicProperties(file: MiDriveFile): MiDriveFile['properties'] {
|
2021-12-03 03:19:28 +01:00
|
|
|
if (file.properties.orientation != null) {
|
2022-11-17 01:31:07 +01:00
|
|
|
const properties = deepClone(file.properties);
|
2021-12-03 03:19:28 +01:00
|
|
|
if (file.properties.orientation >= 5) {
|
|
|
|
[properties.width, properties.height] = [properties.height, properties.width];
|
|
|
|
}
|
|
|
|
properties.orientation = undefined;
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.properties;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2021-12-03 03:19:28 +01:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-02-12 10:47:30 +01:00
|
|
|
private getProxiedUrl(url: string, mode?: 'static' | 'avatar'): string {
|
2023-02-12 01:13:47 +01:00
|
|
|
return appendQuery(
|
2023-03-12 09:31:52 +01:00
|
|
|
`${this.config.mediaProxy}/${mode ?? 'image'}.webp`,
|
2023-02-04 05:38:51 +01:00
|
|
|
query({
|
|
|
|
url,
|
|
|
|
...(mode ? { [mode]: '1' } : {}),
|
2023-02-12 10:47:30 +01:00
|
|
|
}),
|
|
|
|
);
|
2023-02-12 01:13:47 +01:00
|
|
|
}
|
2023-02-04 05:38:51 +01:00
|
|
|
|
2023-02-12 01:13:47 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public getThumbnailUrl(file: MiDriveFile): string | null {
|
2023-02-12 01:13:47 +01:00
|
|
|
if (file.type.startsWith('video')) {
|
|
|
|
if (file.thumbnailUrl) return file.thumbnailUrl;
|
|
|
|
|
2023-10-18 02:54:18 +02:00
|
|
|
return this.videoProcessingService.getExternalVideoThumbnailUrl(file.webpublicUrl ?? file.url);
|
2023-02-12 01:13:47 +01:00
|
|
|
} else if (file.uri != null && file.userHost != null && this.config.externalMediaProxyEnabled) {
|
|
|
|
// 動画ではなくリモートかつメディアプロキシ
|
|
|
|
return this.getProxiedUrl(file.uri, 'static');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.uri != null && file.isLink && this.config.proxyRemoteFiles) {
|
|
|
|
// リモートかつ期限切れはローカルプロキシを試みる
|
|
|
|
// 従来は/files/${thumbnailAccessKey}にアクセスしていたが、
|
|
|
|
// /filesはメディアプロキシにリダイレクトするようにしたため直接メディアプロキシを指定する
|
|
|
|
return this.getProxiedUrl(file.uri, 'static');
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = file.webpublicUrl ?? file.url;
|
|
|
|
|
2023-03-11 06:11:40 +01:00
|
|
|
return file.thumbnailUrl ?? (isMimeImage(file.type, 'sharp-convertible-image') ? url : null);
|
2023-02-12 01:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public getPublicUrl(file: MiDriveFile, mode?: 'avatar'): string { // static = thumbnail
|
2019-12-31 09:23:47 +01:00
|
|
|
// リモートかつメディアプロキシ
|
2023-02-04 05:38:51 +01:00
|
|
|
if (file.uri != null && file.userHost != null && this.config.externalMediaProxyEnabled) {
|
2023-02-12 01:13:47 +01:00
|
|
|
return this.getProxiedUrl(file.uri, mode);
|
2019-12-19 17:54:28 +01:00
|
|
|
}
|
2019-12-31 09:23:47 +01:00
|
|
|
|
|
|
|
// リモートかつ期限切れはローカルプロキシを試みる
|
2022-09-17 20:27:08 +02:00
|
|
|
if (file.uri != null && file.isLink && this.config.proxyRemoteFiles) {
|
2023-02-12 01:13:47 +01:00
|
|
|
const key = file.webpublicAccessKey;
|
2019-12-31 09:23:47 +01:00
|
|
|
|
|
|
|
if (key && !key.match('/')) { // 古いものはここにオブジェクトストレージキーが入ってるので除外
|
2023-02-04 05:38:51 +01:00
|
|
|
const url = `${this.config.url}/files/${key}`;
|
2023-02-12 01:13:47 +01:00
|
|
|
if (mode === 'avatar') return this.getProxiedUrl(file.uri, 'avatar');
|
2023-02-04 05:38:51 +01:00
|
|
|
return url;
|
2019-12-31 09:23:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:53:18 +01:00
|
|
|
const url = file.webpublicUrl ?? file.url;
|
2020-01-01 18:45:05 +01:00
|
|
|
|
2023-02-07 14:53:18 +01:00
|
|
|
if (mode === 'avatar') {
|
2023-02-12 01:13:47 +01:00
|
|
|
return this.getProxiedUrl(url, 'avatar');
|
2023-02-04 05:38:51 +01:00
|
|
|
}
|
|
|
|
return url;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
public async calcDriveUsageOf(user: MiUser['id'] | { id: MiUser['id'] }): Promise<number> {
|
2019-04-07 14:50:36 +02:00
|
|
|
const id = typeof user === 'object' ? user.id : user;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const { sum } = await this.driveFilesRepository
|
2019-04-07 14:50:36 +02:00
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userId = :id', { id: id })
|
2021-05-30 06:48:23 +02:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 14:50:36 +02:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
2023-10-18 02:54:18 +02:00
|
|
|
return parseInt(sum, 10) || 0;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async calcDriveUsageOfHost(host: string): Promise<number> {
|
|
|
|
const { sum } = await this.driveFilesRepository
|
2019-04-07 14:50:36 +02:00
|
|
|
.createQueryBuilder('file')
|
2022-09-17 20:27:08 +02:00
|
|
|
.where('file.userHost = :host', { host: this.utilityService.toPuny(host) })
|
2021-05-30 06:48:23 +02:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 14:50:36 +02:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
2023-10-18 02:54:18 +02:00
|
|
|
return parseInt(sum, 10) || 0;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async calcDriveUsageOfLocal(): Promise<number> {
|
|
|
|
const { sum } = await this.driveFilesRepository
|
2019-04-07 14:50:36 +02:00
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userHost IS NULL')
|
2021-05-30 06:48:23 +02:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 14:50:36 +02:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
2023-10-18 02:54:18 +02:00
|
|
|
return parseInt(sum, 10) || 0;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async calcDriveUsageOfRemote(): Promise<number> {
|
|
|
|
const { sum } = await this.driveFilesRepository
|
2019-04-07 14:50:36 +02:00
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userHost IS NOT NULL')
|
2021-05-30 06:48:23 +02:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 14:50:36 +02:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
2023-10-18 02:54:18 +02:00
|
|
|
return parseInt(sum, 10) || 0;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiDriveFile['id'] | MiDriveFile,
|
2022-04-17 05:59:41 +02:00
|
|
|
options?: PackOptions,
|
|
|
|
): Promise<Packed<'DriveFile'>> {
|
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
|
|
|
self: false,
|
|
|
|
}, options);
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const file = typeof src === 'object' ? src : await this.driveFilesRepository.findOneByOrFail({ id: src });
|
2022-04-17 05:59:41 +02:00
|
|
|
|
|
|
|
return await awaitAll<Packed<'DriveFile'>>({
|
|
|
|
id: file.id,
|
2023-10-16 03:45:22 +02:00
|
|
|
createdAt: this.idService.parse(file.id).date.toISOString(),
|
2022-04-17 05:59:41 +02:00
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
md5: file.md5,
|
|
|
|
size: file.size,
|
|
|
|
isSensitive: file.isSensitive,
|
|
|
|
blurhash: file.blurhash,
|
|
|
|
properties: opts.self ? file.properties : this.getPublicProperties(file),
|
2023-02-04 05:38:51 +01:00
|
|
|
url: opts.self ? file.url : this.getPublicUrl(file),
|
2023-02-12 01:13:47 +01:00
|
|
|
thumbnailUrl: this.getThumbnailUrl(file),
|
2022-04-17 05:59:41 +02:00
|
|
|
comment: file.comment,
|
|
|
|
folderId: file.folderId,
|
2022-09-17 20:27:08 +02:00
|
|
|
folder: opts.detail && file.folderId ? this.driveFolderEntityService.pack(file.folderId, {
|
2022-04-17 05:59:41 +02:00
|
|
|
detail: true,
|
|
|
|
}) : null,
|
|
|
|
userId: opts.withUser ? file.userId : null,
|
2022-09-17 20:27:08 +02:00
|
|
|
user: (opts.withUser && file.userId) ? this.userEntityService.pack(file.userId) : null,
|
2022-04-17 05:59:41 +02:00
|
|
|
});
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2022-04-17 05:59:41 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async packNullable(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiDriveFile['id'] | MiDriveFile,
|
2022-04-17 05:59:41 +02:00
|
|
|
options?: PackOptions,
|
2021-09-22 15:35:55 +02:00
|
|
|
): Promise<Packed<'DriveFile'> | null> {
|
2019-04-07 14:50:36 +02:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
2021-12-09 15:58:30 +01:00
|
|
|
self: false,
|
2019-04-07 14:50:36 +02:00
|
|
|
}, options);
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const file = typeof src === 'object' ? src : await this.driveFilesRepository.findOneBy({ id: src });
|
2021-03-16 04:50:07 +01:00
|
|
|
if (file == null) return null;
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
return await awaitAll<Packed<'DriveFile'>>({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: file.id,
|
2023-10-16 03:45:22 +02:00
|
|
|
createdAt: this.idService.parse(file.id).date.toISOString(),
|
2019-04-07 14:50:36 +02:00
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
md5: file.md5,
|
|
|
|
size: file.size,
|
|
|
|
isSensitive: file.isSensitive,
|
2020-07-18 17:24:07 +02:00
|
|
|
blurhash: file.blurhash,
|
2021-12-03 03:19:28 +01:00
|
|
|
properties: opts.self ? file.properties : this.getPublicProperties(file),
|
2023-02-04 05:38:51 +01:00
|
|
|
url: opts.self ? file.url : this.getPublicUrl(file),
|
2023-02-12 01:13:47 +01:00
|
|
|
thumbnailUrl: this.getThumbnailUrl(file),
|
2020-10-17 13:12:00 +02:00
|
|
|
comment: file.comment,
|
2019-04-07 14:50:36 +02:00
|
|
|
folderId: file.folderId,
|
2022-09-17 20:27:08 +02:00
|
|
|
folder: opts.detail && file.folderId ? this.driveFolderEntityService.pack(file.folderId, {
|
2021-12-09 15:58:30 +01:00
|
|
|
detail: true,
|
2019-04-07 14:50:36 +02:00
|
|
|
}) : null,
|
2024-03-05 10:06:57 +01:00
|
|
|
userId: file.userId,
|
2022-09-17 20:27:08 +02:00
|
|
|
user: (opts.withUser && file.userId) ? this.userEntityService.pack(file.userId) : null,
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2019-04-25 06:27:07 +02:00
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async packMany(
|
2023-08-16 10:51:28 +02:00
|
|
|
files: MiDriveFile[],
|
2022-04-17 05:59:41 +02:00
|
|
|
options?: PackOptions,
|
|
|
|
): Promise<Packed<'DriveFile'>[]> {
|
|
|
|
const items = await Promise.all(files.map(f => this.packNullable(f, options)));
|
2024-02-23 06:12:57 +01:00
|
|
|
return items.filter(isNotNull);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
2023-03-04 08:48:50 +01:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async packManyByIdsMap(
|
2023-08-16 10:51:28 +02:00
|
|
|
fileIds: MiDriveFile['id'][],
|
2023-03-04 08:48:50 +01:00
|
|
|
options?: PackOptions,
|
|
|
|
): Promise<Map<Packed<'DriveFile'>['id'], Packed<'DriveFile'> | null>> {
|
2023-04-06 12:45:12 +02:00
|
|
|
if (fileIds.length === 0) return new Map();
|
2023-03-04 08:48:50 +01:00
|
|
|
const files = await this.driveFilesRepository.findBy({ id: In(fileIds) });
|
|
|
|
const packedFiles = await this.packMany(files, options);
|
|
|
|
const map = new Map<Packed<'DriveFile'>['id'], Packed<'DriveFile'> | null>(packedFiles.map(f => [f.id, f]));
|
|
|
|
for (const id of fileIds) {
|
|
|
|
if (!map.has(id)) map.set(id, null);
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async packManyByIds(
|
2023-08-16 10:51:28 +02:00
|
|
|
fileIds: MiDriveFile['id'][],
|
2023-03-04 08:48:50 +01:00
|
|
|
options?: PackOptions,
|
|
|
|
): Promise<Packed<'DriveFile'>[]> {
|
2023-04-06 12:45:12 +02:00
|
|
|
if (fileIds.length === 0) return [];
|
2023-03-04 08:48:50 +01:00
|
|
|
const filesMap = await this.packManyByIdsMap(fileIds, options);
|
|
|
|
return fileIds.map(id => filesMap.get(id)).filter(isNotNull);
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|