2022-09-17 21:27:08 +03:00
|
|
|
import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DataSource, In } from 'typeorm';
|
|
|
|
import * as mfm from 'mfm-js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { NotesRepository, DriveFilesRepository } from '@/models/index.js';
|
|
|
|
import { Config } from '@/config.js';
|
|
|
|
import type { Packed } from '@/misc/schema.js';
|
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { DriveFile } from '@/models/entities/DriveFile.js';
|
|
|
|
import { appendQuery, query } from '@/misc/prelude/url.js';
|
|
|
|
import { UtilityService } from '../UtilityService.js';
|
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
import { DriveFolderEntityService } from './DriveFolderEntityService.js';
|
2019-04-23 16:35:26 +03:00
|
|
|
|
2021-03-16 05:50:07 +02:00
|
|
|
type PackOptions = {
|
|
|
|
detail?: boolean,
|
|
|
|
self?: boolean,
|
|
|
|
withUser?: boolean,
|
|
|
|
};
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
@Injectable()
|
|
|
|
export class DriveFileEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.db)
|
|
|
|
private db: DataSource,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
// 循環参照のため / for circular dependency
|
|
|
|
@Inject(forwardRef(() => UserEntityService))
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private driveFolderEntityService: DriveFolderEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public validateFileName(name: string): boolean {
|
2019-04-07 15:50:36 +03:00
|
|
|
return (
|
|
|
|
(name.trim().length > 0) &&
|
|
|
|
(name.length <= 200) &&
|
|
|
|
(name.indexOf('\\') === -1) &&
|
|
|
|
(name.indexOf('/') === -1) &&
|
|
|
|
(name.indexOf('..') === -1)
|
|
|
|
);
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
public getPublicProperties(file: DriveFile): DriveFile['properties'] {
|
2021-12-03 04:19:28 +02:00
|
|
|
if (file.properties.orientation != null) {
|
2022-06-04 11:26:56 +03:00
|
|
|
// TODO
|
|
|
|
//const properties = structuredClone(file.properties);
|
|
|
|
const properties = JSON.parse(JSON.stringify(file.properties));
|
2021-12-03 04:19:28 +02: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 21:27:08 +03:00
|
|
|
}
|
2021-12-03 04:19:28 +02:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
public getPublicUrl(file: DriveFile, thumbnail = false): string | null {
|
2019-12-31 10:23:47 +02:00
|
|
|
// リモートかつメディアプロキシ
|
2022-09-17 21:27:08 +03:00
|
|
|
if (file.uri != null && file.userHost != null && this.config.mediaProxy != null) {
|
|
|
|
return appendQuery(this.config.mediaProxy, query({
|
2019-12-31 10:23:47 +02:00
|
|
|
url: file.uri,
|
2021-12-09 16:58:30 +02:00
|
|
|
thumbnail: thumbnail ? '1' : undefined,
|
2019-12-31 10:23:47 +02:00
|
|
|
}));
|
2019-12-19 18:54:28 +02:00
|
|
|
}
|
2019-12-31 10:23:47 +02:00
|
|
|
|
|
|
|
// リモートかつ期限切れはローカルプロキシを試みる
|
2022-09-17 21:27:08 +03:00
|
|
|
if (file.uri != null && file.isLink && this.config.proxyRemoteFiles) {
|
2019-12-31 10:23:47 +02:00
|
|
|
const key = thumbnail ? file.thumbnailAccessKey : file.webpublicAccessKey;
|
|
|
|
|
|
|
|
if (key && !key.match('/')) { // 古いものはここにオブジェクトストレージキーが入ってるので除外
|
2022-09-17 21:27:08 +03:00
|
|
|
return `${this.config.url}/files/${key}`;
|
2019-12-31 10:23:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:45:05 +02:00
|
|
|
const isImage = file.type && ['image/png', 'image/apng', 'image/gif', 'image/jpeg', 'image/webp', 'image/svg+xml'].includes(file.type);
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
return thumbnail ? (file.thumbnailUrl ?? (isImage ? (file.webpublicUrl ?? file.url) : null)) : (file.webpublicUrl ?? file.url);
|
|
|
|
}
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
public async calcDriveUsageOf(user: User['id'] | { id: User['id'] }): Promise<number> {
|
2019-04-07 15:50:36 +03:00
|
|
|
const id = typeof user === 'object' ? user.id : user;
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const { sum } = await this.driveFilesRepository
|
2019-04-07 15:50:36 +03:00
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userId = :id', { id: id })
|
2021-05-30 07:48:23 +03:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 15:50:36 +03:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
return parseInt(sum, 10) ?? 0;
|
|
|
|
}
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
public async calcDriveUsageOfHost(host: string): Promise<number> {
|
|
|
|
const { sum } = await this.driveFilesRepository
|
2019-04-07 15:50:36 +03:00
|
|
|
.createQueryBuilder('file')
|
2022-09-17 21:27:08 +03:00
|
|
|
.where('file.userHost = :host', { host: this.utilityService.toPuny(host) })
|
2021-05-30 07:48:23 +03:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 15:50:36 +03:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
return parseInt(sum, 10) ?? 0;
|
|
|
|
}
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
public async calcDriveUsageOfLocal(): Promise<number> {
|
|
|
|
const { sum } = await this.driveFilesRepository
|
2019-04-07 15:50:36 +03:00
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userHost IS NULL')
|
2021-05-30 07:48:23 +03:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 15:50:36 +03:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
return parseInt(sum, 10) ?? 0;
|
|
|
|
}
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
public async calcDriveUsageOfRemote(): Promise<number> {
|
|
|
|
const { sum } = await this.driveFilesRepository
|
2019-04-07 15:50:36 +03:00
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userHost IS NOT NULL')
|
2021-05-30 07:48:23 +03:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 15:50:36 +03:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
return parseInt(sum, 10) ?? 0;
|
|
|
|
}
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
public async pack(
|
2019-04-07 15:50:36 +03:00
|
|
|
src: DriveFile['id'] | DriveFile,
|
2022-04-17 06:59:41 +03:00
|
|
|
options?: PackOptions,
|
|
|
|
): Promise<Packed<'DriveFile'>> {
|
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
|
|
|
self: false,
|
|
|
|
}, options);
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const file = typeof src === 'object' ? src : await this.driveFilesRepository.findOneByOrFail({ id: src });
|
2022-04-17 06:59:41 +03:00
|
|
|
|
|
|
|
return await awaitAll<Packed<'DriveFile'>>({
|
|
|
|
id: file.id,
|
|
|
|
createdAt: file.createdAt.toISOString(),
|
|
|
|
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),
|
|
|
|
url: opts.self ? file.url : this.getPublicUrl(file, false),
|
|
|
|
thumbnailUrl: this.getPublicUrl(file, true),
|
|
|
|
comment: file.comment,
|
|
|
|
folderId: file.folderId,
|
2022-09-17 21:27:08 +03:00
|
|
|
folder: opts.detail && file.folderId ? this.driveFolderEntityService.pack(file.folderId, {
|
2022-04-17 06:59:41 +03:00
|
|
|
detail: true,
|
|
|
|
}) : null,
|
|
|
|
userId: opts.withUser ? file.userId : null,
|
2022-09-17 21:27:08 +03:00
|
|
|
user: (opts.withUser && file.userId) ? this.userEntityService.pack(file.userId) : null,
|
2022-04-17 06:59:41 +03:00
|
|
|
});
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
2022-04-17 06:59:41 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
public async packNullable(
|
2022-04-17 06:59:41 +03:00
|
|
|
src: DriveFile['id'] | DriveFile,
|
|
|
|
options?: PackOptions,
|
2021-09-22 16:35:55 +03:00
|
|
|
): Promise<Packed<'DriveFile'> | null> {
|
2019-04-07 15:50:36 +03:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
2021-12-09 16:58:30 +02:00
|
|
|
self: false,
|
2019-04-07 15:50:36 +03:00
|
|
|
}, options);
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const file = typeof src === 'object' ? src : await this.driveFilesRepository.findOneBy({ id: src });
|
2021-03-16 05:50:07 +02:00
|
|
|
if (file == null) return null;
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
return await awaitAll<Packed<'DriveFile'>>({
|
2019-04-07 15:50:36 +03:00
|
|
|
id: file.id,
|
2019-04-23 16:35:26 +03:00
|
|
|
createdAt: file.createdAt.toISOString(),
|
2019-04-07 15:50:36 +03:00
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
md5: file.md5,
|
|
|
|
size: file.size,
|
|
|
|
isSensitive: file.isSensitive,
|
2020-07-18 18:24:07 +03:00
|
|
|
blurhash: file.blurhash,
|
2021-12-03 04:19:28 +02:00
|
|
|
properties: opts.self ? file.properties : this.getPublicProperties(file),
|
2022-02-27 06:59:10 +02:00
|
|
|
url: opts.self ? file.url : this.getPublicUrl(file, false),
|
|
|
|
thumbnailUrl: this.getPublicUrl(file, true),
|
2020-10-17 14:12:00 +03:00
|
|
|
comment: file.comment,
|
2019-04-07 15:50:36 +03:00
|
|
|
folderId: file.folderId,
|
2022-09-17 21:27:08 +03:00
|
|
|
folder: opts.detail && file.folderId ? this.driveFolderEntityService.pack(file.folderId, {
|
2021-12-09 16:58:30 +02:00
|
|
|
detail: true,
|
2019-04-07 15:50:36 +03:00
|
|
|
}) : null,
|
2020-10-28 15:24:16 +02:00
|
|
|
userId: opts.withUser ? file.userId : null,
|
2022-09-17 21:27:08 +03:00
|
|
|
user: (opts.withUser && file.userId) ? this.userEntityService.pack(file.userId) : null,
|
2019-04-07 15:50:36 +03:00
|
|
|
});
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
2019-04-25 07:27:07 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
public async packMany(
|
2021-03-19 11:22:14 +02:00
|
|
|
files: (DriveFile['id'] | DriveFile)[],
|
2022-04-17 06:59:41 +03:00
|
|
|
options?: PackOptions,
|
|
|
|
): Promise<Packed<'DriveFile'>[]> {
|
|
|
|
const items = await Promise.all(files.map(f => this.packNullable(f, options)));
|
|
|
|
return items.filter((x): x is Packed<'DriveFile'> => x != null);
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
}
|