Skip to content
Snippets Groups Projects
drive-file.ts 7.56 KiB
Newer Older
import { EntityRepository, Repository } from 'typeorm';
import { DriveFile } from '../entities/drive-file';
import { Users, DriveFolders } from '..';
import { User } from '../entities/user';
syuilo's avatar
syuilo committed
import { toPuny } from '../../misc/convert-host';
syuilo's avatar
syuilo committed
import { awaitAll } from '../../prelude/await-all';
syuilo's avatar
syuilo committed
import { SchemaType } from '../../misc/schema';
import config from '../../config';
import { query, appendQuery } from '../../prelude/url';
import { Meta } from '../entities/meta';
import { fetchMeta } from '../../misc/fetch-meta';
syuilo's avatar
syuilo committed

export type PackedDriveFile = SchemaType<typeof packedDriveFileSchema>;
type PackOptions = {
	detail?: boolean,
	self?: boolean,
	withUser?: boolean,
};

@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)
		);
	}

	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
			}));

		// リモートかつ期限切れはローカルプロキシを試みる
		if (file.uri != null && file.isLink && meta && meta.proxyRemoteFiles) {
			const key = thumbnail ? file.thumbnailAccessKey : file.webpublicAccessKey;

			if (key && !key.match('/')) {	// 古いものはここにオブジェクトストレージキーが入ってるので除外
		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);
syuilo's avatar
syuilo committed
	public async calcDriveUsageOf(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;
	}

syuilo's avatar
syuilo committed
	public async calcDriveUsageOfHost(host: string): Promise<number> {
		const { sum } = await this
			.createQueryBuilder('file')
syuilo's avatar
syuilo committed
			.where('file.userHost = :host', { host: toPuny(host) })
			.select('SUM(file.size)', 'sum')
			.getRawOne();

		return parseInt(sum, 10) || 0;
	}

syuilo's avatar
syuilo committed
	public async calcDriveUsageOfLocal(): Promise<number> {
		const { sum } = await this
			.createQueryBuilder('file')
			.where('file.userHost IS NULL')
			.select('SUM(file.size)', 'sum')
			.getRawOne();

		return parseInt(sum, 10) || 0;
	}

syuilo's avatar
syuilo committed
	public async calcDriveUsageOfRemote(): 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'], options?: PackOptions): Promise<PackedDriveFile | null>;
	public async pack(src: DriveFile, options?: PackOptions): Promise<PackedDriveFile>;
	public async pack(
		src: DriveFile['id'] | DriveFile,
		options?: PackOptions
	): Promise<PackedDriveFile | null> {
		const opts = Object.assign({
			detail: false,
			self: false
		}, options);

		const file = typeof src === 'object' ? src : await this.findOne(src);
		if (file == null) return null;
syuilo's avatar
syuilo committed
		return await awaitAll({
			id: file.id,
syuilo's avatar
syuilo committed
			createdAt: file.createdAt.toISOString(),
			name: file.name,
			type: file.type,
			md5: file.md5,
			size: file.size,
			isSensitive: file.isSensitive,
syuilo's avatar
syuilo committed
			blurhash: file.blurhash,
			properties: file.properties,
			url: opts.self ? file.url : this.getPublicUrl(file, false, meta),
			thumbnailUrl: this.getPublicUrl(file, true, meta),
syuilo's avatar
syuilo committed
			comment: file.comment,
			folderId: file.folderId,
			folder: opts.detail && file.folderId ? DriveFolders.pack(file.folderId, {
				detail: true
			}) : null,
			userId: opts.withUser ? file.userId : null,
			user: (opts.withUser && file.userId) ? Users.pack(file.userId) : null
syuilo's avatar
syuilo committed

syuilo's avatar
syuilo committed
		files: (DriveFile['id'] | DriveFile)[],
syuilo's avatar
syuilo committed
	) {
		const items = await Promise.all(files.map(f => this.pack(f, options)));
		return items.filter(x => x != null);
syuilo's avatar
syuilo committed
	}
syuilo's avatar
syuilo committed

export const packedDriveFileSchema = {
syuilo's avatar
syuilo committed
	type: 'object' as const,
	optional: false as const, nullable: false as const,
syuilo's avatar
syuilo committed
	properties: {
		id: {
syuilo's avatar
syuilo committed
			type: 'string' as const,
			optional: false as const, nullable: false as const,
syuilo's avatar
syuilo committed
			format: 'id',
			description: 'The unique identifier for this Drive file.',
			example: 'xxxxxxxxxx',
		},
		createdAt: {
syuilo's avatar
syuilo committed
			type: 'string' as const,
			optional: false as const, nullable: false as const,
syuilo's avatar
syuilo committed
			format: 'date-time',
			description: 'The date that the Drive file was created on Misskey.'
		},
		name: {
syuilo's avatar
syuilo committed
			type: 'string' as const,
			optional: false as const, nullable: false as const,
syuilo's avatar
syuilo committed
			description: 'The file name with extension.',
			example: 'lenna.jpg'
		},
		type: {
syuilo's avatar
syuilo committed
			type: 'string' as const,
			optional: false as const, nullable: false as const,
syuilo's avatar
syuilo committed
			description: 'The MIME type of this Drive file.',
			example: 'image/jpeg'
		},
		md5: {
syuilo's avatar
syuilo committed
			type: 'string' as const,
			optional: false as const, nullable: false as const,
syuilo's avatar
syuilo committed
			format: 'md5',
			description: 'The MD5 hash of this Drive file.',
			example: '15eca7fba0480996e2245f5185bf39f2'
		},
		size: {
syuilo's avatar
syuilo committed
			type: 'number' as const,
			optional: false as const, nullable: false as const,
syuilo's avatar
syuilo committed
			description: 'The size of this Drive file. (bytes)',
			example: 51469
		},
		isSensitive: {
			type: 'boolean' as const,
			optional: false as const, nullable: false as const,
			description: 'Whether this Drive file is sensitive.',
		},
		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,
MeiMei's avatar
MeiMei committed
					optional: true as const, nullable: false as const,
					example: 1280
				},
				height: {
					type: 'number' as const,
MeiMei's avatar
MeiMei committed
					optional: true as const, nullable: false as const,
					example: 720
				},
				avgColor: {
					type: 'string' as const,
					optional: true as const, nullable: false as const,
					example: 'rgb(40,65,87)'
				}
			}
		},
syuilo's avatar
syuilo committed
		url: {
syuilo's avatar
syuilo committed
			type: 'string' as const,
			optional: false as const, nullable: true as const,
syuilo's avatar
syuilo committed
			format: 'url',
			description: 'The URL of this Drive file.',
		},
		thumbnailUrl: {
			type: 'string' as const,
			optional: false as const, nullable: true as const,
			format: 'url',
			description: 'The thumbnail URL of this Drive file.',
		},
		comment: {
			type: 'string' as const,
			optional: false as const, nullable: true as const
		},
syuilo's avatar
syuilo committed
		folderId: {
syuilo's avatar
syuilo committed
			type: 'string' as const,
			optional: false as const, nullable: true as const,
syuilo's avatar
syuilo committed
			format: 'id',
			description: 'The parent folder ID of this Drive file.',
			example: 'xxxxxxxxxx',
		},
		folder: {
			type: 'object' as const,
			optional: true as const, nullable: true as const,
			description: 'The parent folder of this Drive file.',
			ref: 'DriveFolder'
syuilo's avatar
syuilo committed
		},
		userId: {
			type: 'string' as const,
			optional: false as const, nullable: true as const,
			format: 'id',
			description: 'Owner ID of this Drive file.',
			example: 'xxxxxxxxxx',
		},
		user: {
			type: 'object' as const,
			optional: true as const, nullable: true as const,
			description: 'Owner of this Drive file.',
			ref: 'User'
		}
syuilo's avatar
syuilo committed
	},
};