2019-04-07 12:50:36 +00:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { DriveFile } from '../entities/drive-file';
|
|
|
|
import { Users, DriveFolders } from '..';
|
|
|
|
import { User } from '../entities/user';
|
2019-04-09 15:59:41 +00:00
|
|
|
import { toPuny } from '../../misc/convert-host';
|
2019-04-12 16:43:22 +00:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
2019-04-23 13:35:26 +00:00
|
|
|
import { awaitAll } from '../../prelude/await-all';
|
2019-06-27 09:04:09 +00:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2019-12-19 16:54:28 +00:00
|
|
|
import config from '../../config';
|
2019-12-31 08:23:47 +00:00
|
|
|
import { query, appendQuery } from '../../prelude/url';
|
|
|
|
import { Meta } from '../entities/meta';
|
|
|
|
import { fetchMeta } from '../../misc/fetch-meta';
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
export type PackedDriveFile = SchemaType<typeof packedDriveFileSchema>;
|
2019-04-07 12:50:36 +00: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 08:23:47 +00: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 16:54:28 +00:00
|
|
|
}
|
2019-12-31 08:23:47 +00:00
|
|
|
|
|
|
|
// リモートかつ期限切れはローカルプロキシを試みる
|
|
|
|
if (file.uri != null && file.isLink && meta && meta.proxyRemoteFiles) {
|
|
|
|
const key = thumbnail ? file.thumbnailAccessKey : file.webpublicAccessKey;
|
|
|
|
|
|
|
|
if (key && !key.match('/')) { // 古いものはここにオブジェクトストレージキーが入ってるので除外
|
2020-07-09 18:52:20 +00:00
|
|
|
return `${config.url}/files/${key}`;
|
2019-12-31 08:23:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 17:45:05 +00: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 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async clacDriveUsageOf(user: User['id'] | User): Promise<number> {
|
|
|
|
const id = typeof user === 'object' ? user.id : user;
|
|
|
|
|
|
|
|
const { sum } = await this
|
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userId = :id', { id: id })
|
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async clacDriveUsageOfHost(host: string): Promise<number> {
|
|
|
|
const { sum } = await this
|
|
|
|
.createQueryBuilder('file')
|
2019-04-09 15:59:41 +00:00
|
|
|
.where('file.userHost = :host', { host: toPuny(host) })
|
2019-04-07 12:50:36 +00:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async clacDriveUsageOfLocal(): Promise<number> {
|
|
|
|
const { sum } = await this
|
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userHost IS NULL')
|
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async clacDriveUsageOfRemote(): Promise<number> {
|
|
|
|
const { sum } = await this
|
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userHost IS NOT NULL')
|
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async pack(
|
|
|
|
src: DriveFile['id'] | DriveFile,
|
|
|
|
options?: {
|
|
|
|
detail?: boolean,
|
|
|
|
self?: boolean,
|
|
|
|
withUser?: boolean,
|
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
): Promise<PackedDriveFile> {
|
2019-04-07 12:50:36 +00:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
|
|
|
self: false
|
|
|
|
}, options);
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
const file = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2019-12-31 08:23:47 +00:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2019-04-23 13:35:26 +00:00
|
|
|
return await awaitAll({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: file.id,
|
2019-04-23 13:35:26 +00:00
|
|
|
createdAt: file.createdAt.toISOString(),
|
2019-04-07 12:50:36 +00:00
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
md5: file.md5,
|
|
|
|
size: file.size,
|
|
|
|
isSensitive: file.isSensitive,
|
2020-07-18 15:24:07 +00:00
|
|
|
blurhash: file.blurhash,
|
2019-04-07 12:50:36 +00:00
|
|
|
properties: file.properties,
|
2019-12-31 08:23:47 +00:00
|
|
|
url: opts.self ? file.url : this.getPublicUrl(file, false, meta),
|
|
|
|
thumbnailUrl: this.getPublicUrl(file, true, meta),
|
2019-04-07 12:50:36 +00:00
|
|
|
folderId: file.folderId,
|
|
|
|
folder: opts.detail && file.folderId ? DriveFolders.pack(file.folderId, {
|
|
|
|
detail: true
|
|
|
|
}) : null,
|
2019-04-12 16:43:22 +00:00
|
|
|
user: opts.withUser ? Users.pack(file.userId!) : null
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
|
|
|
}
|
2019-04-25 04:27:07 +00:00
|
|
|
|
|
|
|
public packMany(
|
|
|
|
files: any[],
|
|
|
|
options?: {
|
|
|
|
detail?: boolean
|
|
|
|
self?: boolean,
|
|
|
|
withUser?: boolean,
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
return Promise.all(files.map(f => this.pack(f, options)));
|
|
|
|
}
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
export const packedDriveFileSchema = {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this Drive file.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the Drive file was created on Misskey.'
|
|
|
|
},
|
|
|
|
name: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
description: 'The file name with extension.',
|
|
|
|
example: 'lenna.jpg'
|
|
|
|
},
|
|
|
|
type: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
description: 'The MIME type of this Drive file.',
|
|
|
|
example: 'image/jpeg'
|
|
|
|
},
|
|
|
|
md5: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'md5',
|
|
|
|
description: 'The MD5 hash of this Drive file.',
|
|
|
|
example: '15eca7fba0480996e2245f5185bf39f2'
|
|
|
|
},
|
|
|
|
size: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'number' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
description: 'The size of this Drive file. (bytes)',
|
|
|
|
example: 51469
|
|
|
|
},
|
|
|
|
url: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'url',
|
|
|
|
description: 'The URL of this Drive file.',
|
|
|
|
},
|
|
|
|
folderId: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
format: 'id',
|
|
|
|
description: 'The parent folder ID of this Drive file.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
isSensitive: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
description: 'Whether this Drive file is sensitive.',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|