2019-04-07 15:50:36 +03:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2021-08-19 16:04:15 +03:00
|
|
|
import { DriveFile } from '@/models/entities/drive-file';
|
2021-08-19 15:55:45 +03:00
|
|
|
import { Users, DriveFolders } from '../index';
|
2021-08-19 16:04:15 +03:00
|
|
|
import { User } from '@/models/entities/user';
|
2021-08-19 15:55:45 +03:00
|
|
|
import { toPuny } from '@/misc/convert-host';
|
2021-08-19 16:04:15 +03:00
|
|
|
import { awaitAll } from '@/prelude/await-all';
|
2021-08-19 15:55:45 +03:00
|
|
|
import { SchemaType } from '@/misc/schema';
|
|
|
|
import config from '@/config/index';
|
2021-08-19 16:04:15 +03:00
|
|
|
import { query, appendQuery } from '@/prelude/url';
|
|
|
|
import { Meta } from '@/models/entities/meta';
|
2021-08-19 15:55:45 +03:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
2019-04-23 16:35:26 +03:00
|
|
|
|
|
|
|
export type PackedDriveFile = SchemaType<typeof packedDriveFileSchema>;
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2021-03-16 05:50:07 +02:00
|
|
|
type PackOptions = {
|
|
|
|
detail?: boolean,
|
|
|
|
self?: boolean,
|
|
|
|
withUser?: boolean,
|
|
|
|
};
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
@EntityRepository(DriveFile)
|
|
|
|
export class DriveFileRepository extends Repository<DriveFile> {
|
|
|
|
public validateFileName(name: string): boolean {
|
|
|
|
return (
|
|
|
|
(name.trim().length > 0) &&
|
|
|
|
(name.length <= 200) &&
|
|
|
|
(name.indexOf('\\') === -1) &&
|
|
|
|
(name.indexOf('/') === -1) &&
|
|
|
|
(name.indexOf('..') === -1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-31 10:23:47 +02:00
|
|
|
public getPublicUrl(file: DriveFile, thumbnail = false, meta?: Meta): string | null {
|
|
|
|
// リモートかつメディアプロキシ
|
|
|
|
if (file.uri != null && file.userHost != null && config.mediaProxy != null) {
|
|
|
|
return appendQuery(config.mediaProxy, query({
|
|
|
|
url: file.uri,
|
|
|
|
thumbnail: thumbnail ? '1' : undefined
|
|
|
|
}));
|
2019-12-19 18:54:28 +02:00
|
|
|
}
|
2019-12-31 10:23:47 +02:00
|
|
|
|
|
|
|
// リモートかつ期限切れはローカルプロキシを試みる
|
|
|
|
if (file.uri != null && file.isLink && meta && meta.proxyRemoteFiles) {
|
|
|
|
const key = thumbnail ? file.thumbnailAccessKey : file.webpublicAccessKey;
|
|
|
|
|
|
|
|
if (key && !key.match('/')) { // 古いものはここにオブジェクトストレージキーが入ってるので除外
|
2020-07-09 21:52:20 +03:00
|
|
|
return `${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);
|
|
|
|
|
|
|
|
return thumbnail ? (file.thumbnailUrl || (isImage ? (file.webpublicUrl || file.url) : null)) : (file.webpublicUrl || file.url);
|
2019-04-07 15:50:36 +03:00
|
|
|
}
|
|
|
|
|
2021-03-24 04:05:37 +02: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;
|
|
|
|
|
|
|
|
const { sum } = await this
|
|
|
|
.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();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
|
|
|
}
|
|
|
|
|
2020-11-25 14:31:34 +02:00
|
|
|
public async calcDriveUsageOfHost(host: string): Promise<number> {
|
2019-04-07 15:50:36 +03:00
|
|
|
const { sum } = await this
|
|
|
|
.createQueryBuilder('file')
|
2019-04-09 18:59:41 +03:00
|
|
|
.where('file.userHost = :host', { host: 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();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
|
|
|
}
|
|
|
|
|
2020-11-25 14:31:34 +02:00
|
|
|
public async calcDriveUsageOfLocal(): Promise<number> {
|
2019-04-07 15:50:36 +03:00
|
|
|
const { sum } = await this
|
|
|
|
.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();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
|
|
|
}
|
|
|
|
|
2020-11-25 14:31:34 +02:00
|
|
|
public async calcDriveUsageOfRemote(): Promise<number> {
|
2019-04-07 15:50:36 +03:00
|
|
|
const { sum } = await this
|
|
|
|
.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();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
|
|
|
}
|
|
|
|
|
2021-03-16 05:50:07 +02:00
|
|
|
public async pack(src: DriveFile['id'], options?: PackOptions): Promise<PackedDriveFile | null>;
|
|
|
|
public async pack(src: DriveFile, options?: PackOptions): Promise<PackedDriveFile>;
|
2019-04-07 15:50:36 +03:00
|
|
|
public async pack(
|
|
|
|
src: DriveFile['id'] | DriveFile,
|
2021-03-16 05:50:07 +02:00
|
|
|
options?: PackOptions
|
|
|
|
): Promise<PackedDriveFile | null> {
|
2019-04-07 15:50:36 +03:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
|
|
|
self: false
|
|
|
|
}, options);
|
|
|
|
|
2021-03-16 05:50:07 +02:00
|
|
|
const file = typeof src === 'object' ? src : await this.findOne(src);
|
|
|
|
if (file == null) return null;
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2019-12-31 10:23:47 +02:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2019-04-23 16:35:26 +03:00
|
|
|
return await awaitAll({
|
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,
|
2019-04-07 15:50:36 +03:00
|
|
|
properties: file.properties,
|
2019-12-31 10:23:47 +02:00
|
|
|
url: opts.self ? file.url : this.getPublicUrl(file, false, meta),
|
|
|
|
thumbnailUrl: this.getPublicUrl(file, true, meta),
|
2020-10-17 14:12:00 +03:00
|
|
|
comment: file.comment,
|
2019-04-07 15:50:36 +03:00
|
|
|
folderId: file.folderId,
|
|
|
|
folder: opts.detail && file.folderId ? DriveFolders.pack(file.folderId, {
|
|
|
|
detail: true
|
|
|
|
}) : null,
|
2020-10-28 15:24:16 +02:00
|
|
|
userId: opts.withUser ? file.userId : null,
|
|
|
|
user: (opts.withUser && file.userId) ? Users.pack(file.userId) : null
|
2019-04-07 15:50:36 +03:00
|
|
|
});
|
|
|
|
}
|
2019-04-25 07:27:07 +03:00
|
|
|
|
2021-03-16 05:50:07 +02:00
|
|
|
public async packMany(
|
2021-03-19 11:22:14 +02:00
|
|
|
files: (DriveFile['id'] | DriveFile)[],
|
2021-03-16 05:50:07 +02:00
|
|
|
options?: PackOptions
|
2019-04-25 07:27:07 +03:00
|
|
|
) {
|
2021-03-16 05:50:07 +02:00
|
|
|
const items = await Promise.all(files.map(f => this.pack(f, options)));
|
|
|
|
return items.filter(x => x != null);
|
2019-04-25 07:27:07 +03:00
|
|
|
}
|
2019-04-07 15:50:36 +03:00
|
|
|
}
|
2019-04-23 16:35:26 +03:00
|
|
|
|
|
|
|
export const packedDriveFileSchema = {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
name: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
example: 'lenna.jpg'
|
|
|
|
},
|
|
|
|
type: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
example: 'image/jpeg'
|
|
|
|
},
|
|
|
|
md5: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
format: 'md5',
|
|
|
|
example: '15eca7fba0480996e2245f5185bf39f2'
|
|
|
|
},
|
|
|
|
size: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'number' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
example: 51469
|
|
|
|
},
|
2021-03-06 15:34:11 +02:00
|
|
|
isSensitive: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
},
|
|
|
|
blurhash: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const
|
|
|
|
},
|
|
|
|
properties: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
properties: {
|
|
|
|
width: {
|
|
|
|
type: 'number' as const,
|
2021-03-13 15:15:20 +02:00
|
|
|
optional: true as const, nullable: false as const,
|
2021-03-06 15:34:11 +02:00
|
|
|
example: 1280
|
|
|
|
},
|
|
|
|
height: {
|
|
|
|
type: 'number' as const,
|
2021-03-13 15:15:20 +02:00
|
|
|
optional: true as const, nullable: false as const,
|
2021-03-06 15:34:11 +02:00
|
|
|
example: 720
|
|
|
|
},
|
|
|
|
avgColor: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
|
|
|
example: 'rgb(40,65,87)'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-04-23 16:35:26 +03:00
|
|
|
url: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
format: 'url',
|
|
|
|
},
|
2021-03-06 15:34:11 +02:00
|
|
|
thumbnailUrl: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
|
|
|
format: 'url',
|
|
|
|
},
|
|
|
|
comment: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const
|
|
|
|
},
|
2019-04-23 16:35:26 +03:00
|
|
|
folderId: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
2021-03-06 15:34:11 +02:00
|
|
|
folder: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2021-09-11 19:12:23 +03:00
|
|
|
ref: 'DriveFolder' as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
},
|
2021-03-06 15:34:11 +02:00
|
|
|
userId: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2021-09-11 19:12:23 +03:00
|
|
|
ref: 'User' as const,
|
2021-03-06 15:34:11 +02:00
|
|
|
}
|
2019-04-23 16:35:26 +03:00
|
|
|
},
|
|
|
|
};
|