2022-03-26 06:34:00 +00:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { toPuny } from '@/misc/convert-host.js';
|
|
|
|
import { awaitAll, Promiseable } from '@/prelude/await-all.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { query, appendQuery } from '@/prelude/url.js';
|
|
|
|
import { Meta } from '@/models/entities/meta.js';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
2022-04-17 03:59:41 +00:00
|
|
|
import { Users, DriveFolders } from '../index.js';
|
2019-04-23 13:35:26 +00:00
|
|
|
|
2021-03-16 03:50:07 +00:00
|
|
|
type PackOptions = {
|
|
|
|
detail?: boolean,
|
|
|
|
self?: boolean,
|
|
|
|
withUser?: boolean,
|
|
|
|
};
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
export const DriveFileRepository = db.getRepository(DriveFile).extend({
|
|
|
|
validateFileName(name: string): boolean {
|
2019-04-07 12:50:36 +00:00
|
|
|
return (
|
|
|
|
(name.trim().length > 0) &&
|
|
|
|
(name.length <= 200) &&
|
|
|
|
(name.indexOf('\\') === -1) &&
|
|
|
|
(name.indexOf('/') === -1) &&
|
|
|
|
(name.indexOf('..') === -1)
|
|
|
|
);
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
getPublicProperties(file: DriveFile): DriveFile['properties'] {
|
2021-12-03 02:19:28 +00:00
|
|
|
if (file.properties.orientation != null) {
|
2022-06-04 08:26:56 +00:00
|
|
|
// TODO
|
|
|
|
//const properties = structuredClone(file.properties);
|
|
|
|
const properties = JSON.parse(JSON.stringify(file.properties));
|
2021-12-03 02:19:28 +00:00
|
|
|
if (file.properties.orientation >= 5) {
|
|
|
|
[properties.width, properties.height] = [properties.height, properties.width];
|
|
|
|
}
|
|
|
|
properties.orientation = undefined;
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.properties;
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2021-12-03 02:19:28 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
getPublicUrl(file: DriveFile, thumbnail = false): string | null {
|
2019-12-31 08:23:47 +00:00
|
|
|
// リモートかつメディアプロキシ
|
|
|
|
if (file.uri != null && file.userHost != null && config.mediaProxy != null) {
|
|
|
|
return appendQuery(config.mediaProxy, query({
|
|
|
|
url: file.uri,
|
2021-12-09 14:58:30 +00:00
|
|
|
thumbnail: thumbnail ? '1' : undefined,
|
2019-12-31 08:23:47 +00:00
|
|
|
}));
|
2019-12-19 16:54:28 +00:00
|
|
|
}
|
2019-12-31 08:23:47 +00:00
|
|
|
|
|
|
|
// リモートかつ期限切れはローカルプロキシを試みる
|
2022-02-27 04:59:10 +00:00
|
|
|
if (file.uri != null && file.isLink && config.proxyRemoteFiles) {
|
2019-12-31 08:23:47 +00:00
|
|
|
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);
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
async calcDriveUsageOf(user: User['id'] | { id: User['id'] }): Promise<number> {
|
2019-04-07 12:50:36 +00:00
|
|
|
const id = typeof user === 'object' ? user.id : user;
|
|
|
|
|
|
|
|
const { sum } = await this
|
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userId = :id', { id: id })
|
2021-05-30 04:48:23 +00:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 12:50:36 +00:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
async calcDriveUsageOfHost(host: string): Promise<number> {
|
2019-04-07 12:50:36 +00:00
|
|
|
const { sum } = await this
|
|
|
|
.createQueryBuilder('file')
|
2019-04-09 15:59:41 +00:00
|
|
|
.where('file.userHost = :host', { host: toPuny(host) })
|
2021-05-30 04:48:23 +00:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 12:50:36 +00:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
async calcDriveUsageOfLocal(): Promise<number> {
|
2019-04-07 12:50:36 +00:00
|
|
|
const { sum } = await this
|
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userHost IS NULL')
|
2021-05-30 04:48:23 +00:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 12:50:36 +00:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
async calcDriveUsageOfRemote(): Promise<number> {
|
2019-04-07 12:50:36 +00:00
|
|
|
const { sum } = await this
|
|
|
|
.createQueryBuilder('file')
|
|
|
|
.where('file.userHost IS NOT NULL')
|
2021-05-30 04:48:23 +00:00
|
|
|
.andWhere('file.isLink = FALSE')
|
2019-04-07 12:50:36 +00:00
|
|
|
.select('SUM(file.size)', 'sum')
|
|
|
|
.getRawOne();
|
|
|
|
|
|
|
|
return parseInt(sum, 10) || 0;
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
async pack(
|
2019-04-07 12:50:36 +00:00
|
|
|
src: DriveFile['id'] | DriveFile,
|
2022-04-17 03:59:41 +00:00
|
|
|
options?: PackOptions,
|
|
|
|
): Promise<Packed<'DriveFile'>> {
|
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
|
|
|
self: false,
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
const file = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
|
|
|
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
async packNullable(
|
|
|
|
src: DriveFile['id'] | DriveFile,
|
|
|
|
options?: PackOptions,
|
2021-09-22 13:35:55 +00:00
|
|
|
): Promise<Packed<'DriveFile'> | null> {
|
2019-04-07 12:50:36 +00:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
2021-12-09 14:58:30 +00:00
|
|
|
self: false,
|
2019-04-07 12:50:36 +00:00
|
|
|
}, options);
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const file = typeof src === 'object' ? src : await this.findOneBy({ id: src });
|
2021-03-16 03:50:07 +00:00
|
|
|
if (file == null) return null;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
return await awaitAll<Packed<'DriveFile'>>({
|
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,
|
2021-12-03 02:19:28 +00:00
|
|
|
properties: opts.self ? file.properties : this.getPublicProperties(file),
|
2022-02-27 04:59:10 +00:00
|
|
|
url: opts.self ? file.url : this.getPublicUrl(file, false),
|
|
|
|
thumbnailUrl: this.getPublicUrl(file, true),
|
2020-10-17 11:12:00 +00:00
|
|
|
comment: file.comment,
|
2019-04-07 12:50:36 +00:00
|
|
|
folderId: file.folderId,
|
|
|
|
folder: opts.detail && file.folderId ? DriveFolders.pack(file.folderId, {
|
2021-12-09 14:58:30 +00:00
|
|
|
detail: true,
|
2019-04-07 12:50:36 +00:00
|
|
|
}) : null,
|
2020-10-28 13:24:16 +00:00
|
|
|
userId: opts.withUser ? file.userId : null,
|
2021-12-09 14:58:30 +00:00
|
|
|
user: (opts.withUser && file.userId) ? Users.pack(file.userId) : null,
|
2019-04-07 12:50:36 +00:00
|
|
|
});
|
2022-03-26 06:34:00 +00:00
|
|
|
},
|
2019-04-25 04:27:07 +00:00
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
async packMany(
|
2021-03-19 09:22:14 +00:00
|
|
|
files: (DriveFile['id'] | DriveFile)[],
|
2022-04-17 03:59:41 +00: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-03-26 06:34:00 +00:00
|
|
|
},
|
|
|
|
});
|