summaryrefslogtreecommitdiff
path: root/packages/backend/src
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-01-27 11:44:14 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2023-01-27 11:44:14 +0900
commitf4bee24ccf7fd1ceaf9184a6d6dd62d4a285f812 (patch)
tree19ae57b7388a7a9d87851ccadd0cd1c29d7c0709 /packages/backend/src
parentMerge branch 'develop' (diff)
parent13.2.4 (diff)
downloadmisskey-f4bee24ccf7fd1ceaf9184a6d6dd62d4a285f812.tar.gz
misskey-f4bee24ccf7fd1ceaf9184a6d6dd62d4a285f812.tar.bz2
misskey-f4bee24ccf7fd1ceaf9184a6d6dd62d4a285f812.zip
Merge branch 'develop'
Diffstat (limited to 'packages/backend/src')
-rw-r--r--packages/backend/src/core/CustomEmojiService.ts146
-rw-r--r--packages/backend/src/core/ImageProcessingService.ts32
-rw-r--r--packages/backend/src/core/entities/NoteEntityService.ts8
-rw-r--r--packages/backend/src/core/entities/NotificationEntityService.ts2
-rw-r--r--packages/backend/src/core/entities/UserEntityService.ts1
-rw-r--r--packages/backend/src/logger.ts1
-rw-r--r--packages/backend/src/queue/processors/AggregateRetentionProcessorService.ts9
-rw-r--r--packages/backend/src/server/FileServerService.ts386
-rw-r--r--packages/backend/src/server/MediaProxyServerService.ts177
-rw-r--r--packages/backend/src/server/ServerModule.ts2
-rw-r--r--packages/backend/src/server/ServerService.ts5
-rw-r--r--packages/backend/src/server/api/endpoints/users/reactions.ts2
12 files changed, 506 insertions, 265 deletions
diff --git a/packages/backend/src/core/CustomEmojiService.ts b/packages/backend/src/core/CustomEmojiService.ts
index 1f0b214159..39814e1be6 100644
--- a/packages/backend/src/core/CustomEmojiService.ts
+++ b/packages/backend/src/core/CustomEmojiService.ts
@@ -6,22 +6,35 @@ import { EmojiEntityService } from '@/core/entities/EmojiEntityService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import type { DriveFile } from '@/models/entities/DriveFile.js';
import type { Emoji } from '@/models/entities/Emoji.js';
-import type { EmojisRepository } from '@/models/index.js';
+import type { EmojisRepository, Note } from '@/models/index.js';
import { bindThis } from '@/decorators.js';
+import { Cache } from '@/misc/cache.js';
+import { UtilityService } from '@/core/UtilityService.js';
+import type { Config } from '@/config.js';
+import { ReactionService } from '@/core/ReactionService.js';
+import { query } from '@/misc/prelude/url.js';
@Injectable()
export class CustomEmojiService {
+ private cache: Cache<Emoji | null>;
+
constructor(
+ @Inject(DI.config)
+ private config: Config,
+
@Inject(DI.db)
private db: DataSource,
@Inject(DI.emojisRepository)
private emojisRepository: EmojisRepository,
+ private utilityService: UtilityService,
private idService: IdService,
private emojiEntityService: EmojiEntityService,
private globalEventService: GlobalEventService,
+ private reactionService: ReactionService,
) {
+ this.cache = new Cache<Emoji | null>(1000 * 60 * 60 * 12);
}
@bindThis
@@ -44,12 +57,135 @@ export class CustomEmojiService {
type: data.driveFile.webpublicType ?? data.driveFile.type,
}).then(x => this.emojisRepository.findOneByOrFail(x.identifiers[0]));
- await this.db.queryResultCache!.remove(['meta_emojis']);
+ if (data.host == null) {
+ await this.db.queryResultCache!.remove(['meta_emojis']);
- this.globalEventService.publishBroadcastStream('emojiAdded', {
- emoji: await this.emojiEntityService.pack(emoji.id),
- });
+ this.globalEventService.publishBroadcastStream('emojiAdded', {
+ emoji: await this.emojiEntityService.pack(emoji.id),
+ });
+ }
return emoji;
}
+
+ @bindThis
+ private normalizeHost(src: string | undefined, noteUserHost: string | null): string | null {
+ // クエリに使うホスト
+ let host = src === '.' ? null // .はローカルホスト (ここがマッチするのはリアクションのみ)
+ : src === undefined ? noteUserHost // ノートなどでホスト省略表記の場合はローカルホスト (ここがリアクションにマッチすることはない)
+ : this.utilityService.isSelfHost(src) ? null // 自ホスト指定
+ : (src || noteUserHost); // 指定されたホスト || ノートなどの所有者のホスト (こっちがリアクションにマッチすることはない)
+
+ host = this.utilityService.toPunyNullable(host);
+
+ return host;
+ }
+
+ @bindThis
+ private parseEmojiStr(emojiName: string, noteUserHost: string | null) {
+ const match = emojiName.match(/^(\w+)(?:@([\w.-]+))?$/);
+ if (!match) return { name: null, host: null };
+
+ const name = match[1];
+
+ // ホスト正規化
+ const host = this.utilityService.toPunyNullable(this.normalizeHost(match[2], noteUserHost));
+
+ return { name, host };
+ }
+
+ /**
+ * 添付用(リモート)カスタム絵文字URLを解決する
+ * @param emojiName ノートやユーザープロフィールに添付された、またはリアクションのカスタム絵文字名 (:は含めない, リアクションでローカルホストの場合は@.を付ける (これはdecodeReactionで可能))
+ * @param noteUserHost ノートやユーザープロフィールの所有者のホスト
+ * @returns URL, nullは未マッチを意味する
+ */
+ @bindThis
+ public async populateEmoji(emojiName: string, noteUserHost: string | null): Promise<string | null> {
+ const { name, host } = this.parseEmojiStr(emojiName, noteUserHost);
+ if (name == null) return null;
+ if (host == null) return null;
+
+ const queryOrNull = async () => (await this.emojisRepository.findOneBy({
+ name,
+ host: host ?? IsNull(),
+ })) ?? null;
+
+ const emoji = await this.cache.fetch(`${name} ${host}`, queryOrNull);
+
+ if (emoji == null) return null;
+
+ const isLocal = emoji.host == null;
+ const emojiUrl = emoji.publicUrl || emoji.originalUrl; // || emoji.originalUrl してるのは後方互換性のため(publicUrlはstringなので??はだめ)
+ const url = isLocal
+ ? emojiUrl
+ : this.config.proxyRemoteFiles
+ ? `${this.config.url}/proxy/${encodeURIComponent((new URL(emojiUrl)).pathname)}?${query({ url: emojiUrl })}`
+ : emojiUrl;
+
+ return url;
+ }
+
+ /**
+ * 複数の添付用(リモート)カスタム絵文字URLを解決する (キャシュ付き, 存在しないものは結果から除外される)
+ */
+ @bindThis
+ public async populateEmojis(emojiNames: string[], noteUserHost: string | null): Promise<Record<string, string>> {
+ const emojis = await Promise.all(emojiNames.map(x => this.populateEmoji(x, noteUserHost)));
+ const res = {} as any;
+ for (let i = 0; i < emojiNames.length; i++) {
+ if (emojis[i] != null) {
+ res[emojiNames[i]] = emojis[i];
+ }
+ }
+ return res;
+ }
+
+ @bindThis
+ public aggregateNoteEmojis(notes: Note[]) {
+ let emojis: { name: string | null; host: string | null; }[] = [];
+ for (const note of notes) {
+ emojis = emojis.concat(note.emojis
+ .map(e => this.parseEmojiStr(e, note.userHost)));
+ if (note.renote) {
+ emojis = emojis.concat(note.renote.emojis
+ .map(e => this.parseEmojiStr(e, note.renote!.userHost)));
+ if (note.renote.user) {
+ emojis = emojis.concat(note.renote.user.emojis
+ .map(e => this.parseEmojiStr(e, note.renote!.userHost)));
+ }
+ }
+ const customReactions = Object.keys(note.reactions).map(x => this.reactionService.decodeReaction(x)).filter(x => x.name != null) as typeof emojis;
+ emojis = emojis.concat(customReactions);
+ if (note.user) {
+ emojis = emojis.concat(note.user.emojis
+ .map(e => this.parseEmojiStr(e, note.userHost)));
+ }
+ }
+ return emojis.filter(x => x.name != null && x.host != null) as { name: string; host: string; }[];
+ }
+
+ /**
+ * 与えられた絵文字のリストをデータベースから取得し、キャッシュに追加します
+ */
+ @bindThis
+ public async prefetchEmojis(emojis: { name: string; host: string | null; }[]): Promise<void> {
+ const notCachedEmojis = emojis.filter(emoji => this.cache.get(`${emoji.name} ${emoji.host}`) == null);
+ const emojisQuery: any[] = [];
+ const hosts = new Set(notCachedEmojis.map(e => e.host));
+ for (const host of hosts) {
+ if (host == null) continue;
+ emojisQuery.push({
+ name: In(notCachedEmojis.filter(e => e.host === host).map(e => e.name)),
+ host: host,
+ });
+ }
+ const _emojis = emojisQuery.length > 0 ? await this.emojisRepository.find({
+ where: emojisQuery,
+ select: ['name', 'host', 'originalUrl', 'publicUrl'],
+ }) : [];
+ for (const emoji of _emojis) {
+ this.cache.set(`${emoji.name} ${emoji.host}`, emoji);
+ }
+ }
}
diff --git a/packages/backend/src/core/ImageProcessingService.ts b/packages/backend/src/core/ImageProcessingService.ts
index 312189eea4..fbc02f504b 100644
--- a/packages/backend/src/core/ImageProcessingService.ts
+++ b/packages/backend/src/core/ImageProcessingService.ts
@@ -9,6 +9,14 @@ export type IImage = {
type: string;
};
+export type IImageStream = {
+ data: Readable;
+ ext: string | null;
+ type: string;
+};
+
+export type IImageStreamable = IImage | IImageStream;
+
export const webpDefault: sharp.WebpOptions = {
quality: 85,
alphaQuality: 95,
@@ -19,6 +27,7 @@ export const webpDefault: sharp.WebpOptions = {
};
import { bindThis } from '@/decorators.js';
+import { Readable } from 'node:stream';
@Injectable()
export class ImageProcessingService {
@@ -64,7 +73,7 @@ export class ImageProcessingService {
*/
@bindThis
public async convertToWebp(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): Promise<IImage> {
- return this.convertSharpToWebp(await sharp(path), width, height, options);
+ return this.convertSharpToWebp(sharp(path), width, height, options);
}
@bindThis
@@ -85,6 +94,27 @@ export class ImageProcessingService {
};
}
+ @bindThis
+ public convertToWebpStream(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
+ return this.convertSharpToWebpStream(sharp(path), width, height, options);
+ }
+
+ @bindThis
+ public convertSharpToWebpStream(sharp: sharp.Sharp, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
+ const data = sharp
+ .resize(width, height, {
+ fit: 'inside',
+ withoutEnlargement: true,
+ })
+ .rotate()
+ .webp(options)
+
+ return {
+ data,
+ ext: 'webp',
+ type: 'image/webp',
+ };
+ }
/**
* Convert to PNG
* with resize, remove metadata, resolve orientation, stop animation
diff --git a/packages/backend/src/core/entities/NoteEntityService.ts b/packages/backend/src/core/entities/NoteEntityService.ts
index 2b179643f3..bd6971adb3 100644
--- a/packages/backend/src/core/entities/NoteEntityService.ts
+++ b/packages/backend/src/core/entities/NoteEntityService.ts
@@ -282,7 +282,9 @@ export class NoteEntityService implements OnModuleInit {
: await this.channelsRepository.findOneBy({ id: note.channelId })
: null;
- const reactionEmojiNames = Object.keys(note.reactions).filter(x => x.startsWith(':')).map(x => this.reactionService.decodeReaction(x).reaction).map(x => x.replace(/:/g, ''));
+ const reactionEmojiNames = Object.keys(note.reactions)
+ .filter(x => x.startsWith(':') && x.includes('@') && !x.includes('@.')) // リモートカスタム絵文字のみ
+ .map(x => this.reactionService.decodeReaction(x).reaction.replaceAll(':', ''));
const packed: Packed<'Note'> = await awaitAll({
id: note.id,
@@ -299,6 +301,8 @@ export class NoteEntityService implements OnModuleInit {
renoteCount: note.renoteCount,
repliesCount: note.repliesCount,
reactions: this.reactionService.convertLegacyReactions(note.reactions),
+ reactionEmojis: this.customEmojiService.populateEmojis(reactionEmojiNames, host),
+ emojis: host != null ? this.customEmojiService.populateEmojis(note.emojis, host) : undefined,
tags: note.tags.length > 0 ? note.tags : undefined,
fileIds: note.fileIds,
files: this.driveFileEntityService.packMany(note.fileIds),
@@ -384,6 +388,8 @@ export class NoteEntityService implements OnModuleInit {
}
}
+ await this.customEmojiService.prefetchEmojis(this.customEmojiService.aggregateNoteEmojis(notes));
+
return await Promise.all(notes.map(n => this.pack(n, me, {
...options,
_hint_: {
diff --git a/packages/backend/src/core/entities/NotificationEntityService.ts b/packages/backend/src/core/entities/NotificationEntityService.ts
index a8210eea02..ded1b512a1 100644
--- a/packages/backend/src/core/entities/NotificationEntityService.ts
+++ b/packages/backend/src/core/entities/NotificationEntityService.ts
@@ -146,6 +146,8 @@ export class NotificationEntityService implements OnModuleInit {
myReactionsMap.set(target, myReactions.find(reaction => reaction.noteId === target) ?? null);
}
+ await this.customEmojiService.prefetchEmojis(this.customEmojiService.aggregateNoteEmojis(notes));
+
return await Promise.all(notifications.map(x => this.pack(x, {
_hintForEachNotes_: {
myReactions: myReactionsMap,
diff --git a/packages/backend/src/core/entities/UserEntityService.ts b/packages/backend/src/core/entities/UserEntityService.ts
index f532b5bf6e..546e61a26e 100644
--- a/packages/backend/src/core/entities/UserEntityService.ts
+++ b/packages/backend/src/core/entities/UserEntityService.ts
@@ -413,6 +413,7 @@ export class UserEntityService implements OnModuleInit {
faviconUrl: instance.faviconUrl,
themeColor: instance.themeColor,
} : undefined) : undefined,
+ emojis: this.customEmojiService.populateEmojis(user.emojis, user.host),
onlineStatus: this.getOnlineStatus(user),
...(opts.detail ? {
diff --git a/packages/backend/src/logger.ts b/packages/backend/src/logger.ts
index e7d7051630..5d275bc7b2 100644
--- a/packages/backend/src/logger.ts
+++ b/packages/backend/src/logger.ts
@@ -68,6 +68,7 @@ export default class Logger {
if (envOption.withLogTime) log = chalk.gray(time) + ' ' + log;
console.log(important ? chalk.bold(log) : log);
+ if (level === 'error' && data) console.log(data);
if (store) {
if (this.syslogClient) {
diff --git a/packages/backend/src/queue/processors/AggregateRetentionProcessorService.ts b/packages/backend/src/queue/processors/AggregateRetentionProcessorService.ts
index 4650da76bb..da4ae88557 100644
--- a/packages/backend/src/queue/processors/AggregateRetentionProcessorService.ts
+++ b/packages/backend/src/queue/processors/AggregateRetentionProcessorService.ts
@@ -57,8 +57,15 @@ export class AggregateRetentionProcessorService {
usersCount: targetUserIds.length,
});
+ // 今日活動したユーザーを全て取得
+ const activeUsers = await this.usersRepository.findBy({
+ host: IsNull(),
+ lastActiveDate: MoreThan(new Date(Date.now() - (1000 * 60 * 60 * 24))),
+ });
+ const activeUsersIds = activeUsers.map(u => u.id);
+
for (const record of pastRecords) {
- const retention = record.userIds.filter(id => targetUserIds.includes(id)).length;
+ const retention = record.userIds.filter(id => activeUsersIds.includes(id)).length;
const data = deepClone(record.data);
data[dateKey] = retention;
diff --git a/packages/backend/src/server/FileServerService.ts b/packages/backend/src/server/FileServerService.ts
index 134b3df327..40024270ae 100644
--- a/packages/backend/src/server/FileServerService.ts
+++ b/packages/backend/src/server/FileServerService.ts
@@ -5,14 +5,14 @@ import { Inject, Injectable } from '@nestjs/common';
import fastifyStatic from '@fastify/static';
import rename from 'rename';
import type { Config } from '@/config.js';
-import type { DriveFilesRepository } from '@/models/index.js';
+import type { DriveFile, DriveFilesRepository } from '@/models/index.js';
import { DI } from '@/di-symbols.js';
import { createTemp } from '@/misc/create-temp.js';
import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
import { StatusError } from '@/misc/status-error.js';
import type Logger from '@/logger.js';
import { DownloadService } from '@/core/DownloadService.js';
-import { ImageProcessingService } from '@/core/ImageProcessingService.js';
+import { IImageStreamable, ImageProcessingService, webpDefault } from '@/core/ImageProcessingService.js';
import { VideoProcessingService } from '@/core/VideoProcessingService.js';
import { InternalStorageService } from '@/core/InternalStorageService.js';
import { contentDisposition } from '@/misc/content-disposition.js';
@@ -20,6 +20,8 @@ import { FileInfoService } from '@/core/FileInfoService.js';
import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from 'fastify';
+import { isMimeImage } from '@/misc/is-mime-image.js';
+import sharp from 'sharp';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
@@ -57,7 +59,7 @@ export class FileServerService {
reply.header('Cache-Control', 'max-age=300');
};
}
-
+
@bindThis
public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
fastify.addHook('onRequest', (request, reply, done) => {
@@ -70,113 +72,351 @@ export class FileServerService {
serve: false,
});
- fastify.get('/app-default.jpg', (request, reply) => {
+ fastify.get('/files/app-default.jpg', (request, reply) => {
const file = fs.createReadStream(`${_dirname}/assets/dummy.png`);
reply.header('Content-Type', 'image/jpeg');
reply.header('Cache-Control', 'max-age=31536000, immutable');
return reply.send(file);
});
- fastify.get<{ Params: { key: string; } }>('/:key', async (request, reply) => await this.sendDriveFile(request, reply));
- fastify.get<{ Params: { key: string; } }>('/:key/*', async (request, reply) => await this.sendDriveFile(request, reply));
+ fastify.get<{ Params: { key: string; } }>('/files/:key', async (request, reply) => {
+ return await this.sendDriveFile(request, reply)
+ .catch(err => this.errorHandler(request, reply, err));
+ });
+ fastify.get<{ Params: { key: string; } }>('/files/:key/*', async (request, reply) => {
+ return await this.sendDriveFile(request, reply)
+ .catch(err => this.errorHandler(request, reply, err));
+ });
+
+ fastify.get<{
+ Params: { url: string; };
+ Querystring: { url?: string; };
+ }>('/proxy/:url*', async (request, reply) => {
+ return await this.proxyHandler(request, reply)
+ .catch(err => this.errorHandler(request, reply, err));
+ });
done();
}
@bindThis
- private async sendDriveFile(request: FastifyRequest<{ Params: { key: string; } }>, reply: FastifyReply) {
- const key = request.params.key;
+ private async errorHandler(request: FastifyRequest<{ Params?: { [x: string]: any }; Querystring?: { [x: string]: any }; }>, reply: FastifyReply, err?: any) {
+ this.logger.error(`${err}`);
- // Fetch drive file
- const file = await this.driveFilesRepository.createQueryBuilder('file')
- .where('file.accessKey = :accessKey', { accessKey: key })
- .orWhere('file.thumbnailAccessKey = :thumbnailAccessKey', { thumbnailAccessKey: key })
- .orWhere('file.webpublicAccessKey = :webpublicAccessKey', { webpublicAccessKey: key })
- .getOne();
+ reply.header('Cache-Control', 'max-age=300');
- if (file == null) {
- reply.code(404);
- reply.header('Cache-Control', 'max-age=86400');
+ if (request.query && 'fallback' in request.query) {
return reply.sendFile('/dummy.png', assets);
}
- const isThumbnail = file.thumbnailAccessKey === key;
- const isWebpublic = file.webpublicAccessKey === key;
+ if (err instanceof StatusError && (err.statusCode === 302 || err.isClientError)) {
+ reply.code(err.statusCode);
+ return;
+ }
- if (!file.storedInternal) {
- if (file.isLink && file.uri) { // 期限切れリモートファイル
- const [path, cleanup] = await createTemp();
+ reply.code(500);
+ return;
+ }
- try {
- await this.downloadService.downloadUrl(file.uri, path);
+ @bindThis
+ private async sendDriveFile(request: FastifyRequest<{ Params: { key: string; } }>, reply: FastifyReply) {
+ const key = request.params.key;
+ const file = await this.getFileFromKey(key).then();
- const { mime, ext } = await this.fileInfoService.detectType(path);
+ if (file === '404') {
+ reply.code(404);
+ reply.header('Cache-Control', 'max-age=86400');
+ return reply.sendFile('/dummy.png', assets);
+ }
- const convertFile = async () => {
- if (isThumbnail) {
- if (['image/jpeg', 'image/webp', 'image/avif', 'image/png', 'image/svg+xml'].includes(mime)) {
- return await this.imageProcessingService.convertToWebp(path, 498, 280);
- } else if (mime.startsWith('video/')) {
- return await this.videoProcessingService.generateVideoThumbnail(path);
- }
+ if (file === '204') {
+ reply.code(204);
+ reply.header('Cache-Control', 'max-age=86400');
+ return;
+ }
+
+ try {
+ if (file.state === 'remote') {
+ const convertFile = async () => {
+ if (file.fileRole === 'thumbnail') {
+ if (['image/jpeg', 'image/webp', 'image/avif', 'image/png', 'image/svg+xml'].includes(file.mime)) {
+ return this.imageProcessingService.convertToWebpStream(
+ file.path,
+ 498,
+ 280
+ );
+ } else if (file.mime.startsWith('video/')) {
+ return await this.videoProcessingService.generateVideoThumbnail(file.path);
}
+ }
- if (isWebpublic) {
- if (['image/svg+xml'].includes(mime)) {
- return await this.imageProcessingService.convertToPng(path, 2048, 2048);
- }
+ if (file.fileRole === 'webpublic') {
+ if (['image/svg+xml'].includes(file.mime)) {
+ return this.imageProcessingService.convertToWebpStream(
+ file.path,
+ 2048,
+ 2048,
+ { ...webpDefault, lossless: true }
+ )
}
+ }
- return {
- data: fs.readFileSync(path),
- ext,
- type: mime,
- };
+ return {
+ data: fs.createReadStream(file.path),
+ ext: file.ext,
+ type: file.mime,
};
+ };
- const image = await convertFile();
- reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream');
- reply.header('Cache-Control', 'max-age=31536000, immutable');
- return image.data;
- } catch (err) {
- this.logger.error(`${err}`);
+ const image = await convertFile();
- if (err instanceof StatusError && err.isClientError) {
- reply.code(err.statusCode);
- reply.header('Cache-Control', 'max-age=86400');
- } else {
- reply.code(500);
- reply.header('Cache-Control', 'max-age=300');
- }
- } finally {
- cleanup();
+ if ('pipe' in image.data && typeof image.data.pipe === 'function') {
+ // image.dataがstreamなら、stream終了後にcleanup
+ image.data.on('end', file.cleanup);
+ image.data.on('close', file.cleanup);
+ } else {
+ // image.dataがstreamでないなら直ちにcleanup
+ file.cleanup();
}
- return;
+
+ reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(image.type) ? image.type : 'application/octet-stream');
+ reply.header('Cache-Control', 'max-age=31536000, immutable');
+ return image.data;
}
+ if (file.fileRole !== 'original') {
+ const filename = rename(file.file.name, {
+ suffix: file.fileRole === 'thumbnail' ? '-thumb' : '-web',
+ extname: file.ext ? `.${file.ext}` : undefined,
+ }).toString();
+
+ reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.mime) ? file.mime : 'application/octet-stream');
+ reply.header('Cache-Control', 'max-age=31536000, immutable');
+ reply.header('Content-Disposition', contentDisposition('inline', filename));
+ return fs.createReadStream(file.path);
+ } else {
+ const stream = fs.createReadStream(file.path);
+ stream.on('error', this.commonReadableHandlerGenerator(reply));
+ reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.file.type) ? file.file.type : 'application/octet-stream');
+ reply.header('Cache-Control', 'max-age=31536000, immutable');
+ reply.header('Content-Disposition', contentDisposition('inline', file.file.name));
+ return stream;
+ }
+ } catch (e) {
+ if ('cleanup' in file) file.cleanup();
+ throw e;
+ }
+ }
+
+ @bindThis
+ private async proxyHandler(request: FastifyRequest<{ Params: { url: string; }; Querystring: { url?: string; }; }>, reply: FastifyReply) {
+ const url = 'url' in request.query ? request.query.url : 'https://' + request.params.url;
+
+ if (typeof url !== 'string') {
+ reply.code(400);
+ return;
+ }
+
+ // Create temp file
+ const file = await this.getStreamAndTypeFromUrl(url);
+ if (file === '404') {
+ reply.code(404);
+ reply.header('Cache-Control', 'max-age=86400');
+ return reply.sendFile('/dummy.png', assets);
+ }
+
+ if (file === '204') {
reply.code(204);
reply.header('Cache-Control', 'max-age=86400');
return;
}
- if (isThumbnail || isWebpublic) {
- const { mime, ext } = await this.fileInfoService.detectType(this.internalStorageService.resolvePath(key));
- const filename = rename(file.name, {
- suffix: isThumbnail ? '-thumb' : '-web',
- extname: ext ? `.${ext}` : undefined,
- }).toString();
+ try {
+ const isConvertibleImage = isMimeImage(file.mime, 'sharp-convertible-image');
+ const isAnimationConvertibleImage = isMimeImage(file.mime, 'sharp-animation-convertible-image');
- reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(mime) ? mime : 'application/octet-stream');
- reply.header('Cache-Control', 'max-age=31536000, immutable');
- reply.header('Content-Disposition', contentDisposition('inline', filename));
- return this.internalStorageService.read(key);
- } else {
- const readable = this.internalStorageService.read(file.accessKey!);
- readable.on('error', this.commonReadableHandlerGenerator(reply));
- reply.header('Content-Type', FILE_TYPE_BROWSERSAFE.includes(file.type) ? file.type : 'application/octet-stream');
+ let image: IImageStreamable | null = null;
+ if ('emoji' in request.query && isConvertibleImage) {
+ if (!isAnimationConvertibleImage && !('static' in request.query)) {
+ image = {
+ data: fs.createReadStream(file.path),
+ ext: file.ext,
+ type: file.mime,
+ };
+ } else {
+ const data = sharp(file.path, { animated: !('static' in request.query) })
+ .resize({
+ height: 128,
+ withoutEnlargement: true,
+ })
+ .webp(webpDefault);
+
+ image = {
+ data,
+ ext: 'webp',
+ type: 'image/webp',
+ };
+ }
+ } else if ('static' in request.query && isConvertibleImage) {
+ image = this.imageProcessingService.convertToWebpStream(file.path, 498, 280);
+ } else if ('preview' in request.query && isConvertibleImage) {
+ image = this.imageProcessingService.convertToWebpStream(file.path, 200, 200);
+ } else if ('badge' in request.query) {
+ if (!isConvertibleImage) {
+ // 画像でないなら404でお茶を濁す
+ throw new StatusError('Unexpected mime', 404);
+ }
+
+ const mask = sharp(file.path)
+ .resize(96, 96, {
+ fit: 'inside',
+ withoutEnlargement: false,
+ })
+ .greyscale()
+ .normalise()
+ .linear(1.75, -(128 * 1.75) + 128) // 1.75x contrast
+ .flatten({ background: '#000' })
+ .toColorspace('b-w');
+
+ const stats = await mask.clone().stats();
+
+ if (stats.entropy < 0.1) {
+ // エントロピーがあまりない場合は404にする
+ throw new StatusError('Skip to provide badge', 404);
+ }
+
+ const data = sharp({
+ create: { width: 96, height: 96, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } },
+ })
+ .pipelineColorspace('b-w')
+ .boolean(await mask.png().toBuffer(), 'eor');
+
+ image = {
+ data: await data.png().toBuffer(),
+ ext: 'png',
+ type: 'image/png',
+ };
+ } else if (file.mime === 'image/svg+xml') {
+ image = this.imageProcessingService.convertToWebpStream(file.path, 2048, 2048);
+ } else if (!file.mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(file.mime)) {
+ throw new StatusError('Rejected type', 403, 'Rejected type');
+ }
+
+ if (!image) {
+ image = {
+ data: fs.createReadStream(file.path),
+ ext: file.ext,
+ type: file.mime,
+ };
+ }
+
+ if ('cleanup' in file) {
+ if ('pipe' in image.data && typeof image.data.pipe === 'function') {
+ // image.dataがstreamなら、stream終了後にcleanup
+ image.data.on('end', file.cleanup);
+ image.data.on('close', file.cleanup);
+ } else {
+ // image.dataがstreamでないなら直ちにcleanup
+ file.cleanup();
+ }
+ }
+
+ reply.header('Content-Type', image.type);
reply.header('Cache-Control', 'max-age=31536000, immutable');
- reply.header('Content-Disposition', contentDisposition('inline', file.name));
- return readable;
+ return image.data;
+ } catch (e) {
+ if ('cleanup' in file) file.cleanup();
+ throw e;
+ }
+ }
+
+ @bindThis
+ private async getStreamAndTypeFromUrl(url: string): Promise<
+ { state: 'remote'; fileRole?: 'thumbnail' | 'webpublic' | 'original'; file?: DriveFile; mime: string; ext: string | null; path: string; cleanup: () => void; }
+ | { state: 'stored_internal'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: DriveFile; mime: string; ext: string | null; path: string; }
+ | '404'
+ | '204'
+ > {
+ if (url.startsWith(`${this.config.url}/files/`)) {
+ const key = url.replace(`${this.config.url}/files/`, '').split('/').shift();
+ if (!key) throw new StatusError('Invalid File Key', 400, 'Invalid File Key');
+
+ return await this.getFileFromKey(key);
+ }
+
+ return await this.downloadAndDetectTypeFromUrl(url);
+ }
+
+ @bindThis
+ private async downloadAndDetectTypeFromUrl(url: string): Promise<
+ { state: 'remote' ; mime: string; ext: string | null; path: string; cleanup: () => void; }
+ > {
+ const [path, cleanup] = await createTemp();
+ try {
+ await this.downloadService.downloadUrl(url, path);
+
+ const { mime, ext } = await this.fileInfoService.detectType(path);
+
+ return {
+ state: 'remote',
+ mime, ext,
+ path, cleanup,
+ }
+ } catch (e) {
+ cleanup();
+ throw e;
+ }
+ }
+
+ @bindThis
+ private async getFileFromKey(key: string): Promise<
+ { state: 'remote'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: DriveFile; mime: string; ext: string | null; path: string; cleanup: () => void; }
+ | { state: 'stored_internal'; fileRole: 'thumbnail' | 'webpublic' | 'original'; file: DriveFile; mime: string; ext: string | null; path: string; }
+ | '404'
+ | '204'
+ > {
+ // Fetch drive file
+ const file = await this.driveFilesRepository.createQueryBuilder('file')
+ .where('file.accessKey = :accessKey', { accessKey: key })
+ .orWhere('file.thumbnailAccessKey = :thumbnailAccessKey', { thumbnailAccessKey: key })
+ .orWhere('file.webpublicAccessKey = :webpublicAccessKey', { webpublicAccessKey: key })
+ .getOne();
+
+ if (file == null) return '404';
+
+ const isThumbnail = file.thumbnailAccessKey === key;
+ const isWebpublic = file.webpublicAccessKey === key;
+
+ if (!file.storedInternal) {
+ if (!(file.isLink && file.uri)) return '204';
+ const result = await this.downloadAndDetectTypeFromUrl(file.uri);
+ return {
+ ...result,
+ fileRole: isThumbnail ? 'thumbnail' : isWebpublic ? 'webpublic' : 'original',
+ file,
+ }
+ }
+
+ const path = this.internalStorageService.resolvePath(key);
+
+ if (isThumbnail || isWebpublic) {
+ const { mime, ext } = await this.fileInfoService.detectType(path);
+ return {
+ state: 'stored_internal',
+ fileRole: isThumbnail ? 'thumbnail' : 'webpublic',
+ file,
+ mime, ext,
+ path,
+ };
+ }
+
+ return {
+ state: 'stored_internal',
+ fileRole: 'original',
+ file,
+ mime: file.type,
+ ext: null,
+ path,
}
}
}
diff --git a/packages/backend/src/server/MediaProxyServerService.ts b/packages/backend/src/server/MediaProxyServerService.ts
deleted file mode 100644
index 5b76f15020..0000000000
--- a/packages/backend/src/server/MediaProxyServerService.ts
+++ /dev/null
@@ -1,177 +0,0 @@
-import * as fs from 'node:fs';
-import { fileURLToPath } from 'node:url';
-import { dirname } from 'node:path';
-import { Inject, Injectable } from '@nestjs/common';
-import sharp from 'sharp';
-import fastifyStatic from '@fastify/static';
-import { DI } from '@/di-symbols.js';
-import type { Config } from '@/config.js';
-import { isMimeImage } from '@/misc/is-mime-image.js';
-import { createTemp } from '@/misc/create-temp.js';
-import { DownloadService } from '@/core/DownloadService.js';
-import { ImageProcessingService, webpDefault } from '@/core/ImageProcessingService.js';
-import type { IImage } from '@/core/ImageProcessingService.js';
-import { FILE_TYPE_BROWSERSAFE } from '@/const.js';
-import { StatusError } from '@/misc/status-error.js';
-import type Logger from '@/logger.js';
-import { FileInfoService } from '@/core/FileInfoService.js';
-import { LoggerService } from '@/core/LoggerService.js';
-import { bindThis } from '@/decorators.js';
-import type { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest } from 'fastify';
-
-const _filename = fileURLToPath(import.meta.url);
-const _dirname = dirname(_filename);
-
-const assets = `${_dirname}/../../src/server/assets/`;
-
-@Injectable()
-export class MediaProxyServerService {
- private logger: Logger;
-
- constructor(
- @Inject(DI.config)
- private config: Config,
-
- private fileInfoService: FileInfoService,
- private downloadService: DownloadService,
- private imageProcessingService: ImageProcessingService,
- private loggerService: LoggerService,
- ) {
- this.logger = this.loggerService.getLogger('server', 'gray', false);
-
- //this.createServer = this.createServer.bind(this);
- }
-
- @bindThis
- public createServer(fastify: FastifyInstance, options: FastifyPluginOptions, done: (err?: Error) => void) {
- fastify.addHook('onRequest', (request, reply, done) => {
- reply.header('Content-Security-Policy', 'default-src \'none\'; img-src \'self\'; media-src \'self\'; style-src \'unsafe-inline\'');
- done();
- });
-
- fastify.register(fastifyStatic, {
- root: _dirname,
- serve: false,
- });
-
- fastify.get<{
- Params: { url: string; };
- Querystring: { url?: string; };
- }>('/:url*', async (request, reply) => await this.handler(request, reply));
-
- done();
- }
-
- @bindThis
- private async handler(request: FastifyRequest<{ Params: { url: string; }; Querystring: { url?: string; }; }>, reply: FastifyReply) {
- const url = 'url' in request.query ? request.query.url : 'https://' + request.params.url;
-
- if (typeof url !== 'string') {
- reply.code(400);
- return;
- }
-
- // Create temp file
- const [path, cleanup] = await createTemp();
-
- try {
- await this.downloadService.downloadUrl(url, path);
-
- const { mime, ext } = await this.fileInfoService.detectType(path);
- const isConvertibleImage = isMimeImage(mime, 'sharp-convertible-image');
- const isAnimationConvertibleImage = isMimeImage(mime, 'sharp-animation-convertible-image');
-
- let image: IImage;
- if ('emoji' in request.query && isConvertibleImage) {
- if (!isAnimationConvertibleImage && !('static' in request.query)) {
- image = {
- data: fs.readFileSync(path),
- ext,
- type: mime,
- };
- } else {
- const data = await sharp(path, { animated: !('static' in request.query) })
- .resize({
- height: 128,
- withoutEnlargement: true,
- })
- .webp(webpDefault)
- .toBuffer();
-
- image = {
- data,
- ext: 'webp',
- type: 'image/webp',
- };
- }
- } else if ('static' in request.query && isConvertibleImage) {
- image = await this.imageProcessingService.convertToWebp(path, 498, 280);
- } else if ('preview' in request.query && isConvertibleImage) {
- image = await this.imageProcessingService.convertToWebp(path, 200, 200);
- } else if ('badge' in request.query) {
- if (!isConvertibleImage) {
- // 画像でないなら404でお茶を濁す
- throw new StatusError('Unexpected mime', 404);
- }
-
- const mask = sharp(path)
- .resize(96, 96, {
- fit: 'inside',
- withoutEnlargement: false,
- })
- .greyscale()
- .normalise()
- .linear(1.75, -(128 * 1.75) + 128) // 1.75x contrast
- .flatten({ background: '#000' })
- .toColorspace('b-w');
-
- const stats = await mask.clone().stats();
-
- if (stats.entropy < 0.1) {
- // エントロピーがあまりない場合は404にする
- throw new StatusError('Skip to provide badge', 404);
- }
-
- const data = sharp({
- create: { width: 96, height: 96, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } },
- })
- .pipelineColorspace('b-w')
- .boolean(await mask.png().toBuffer(), 'eor');
-
- image = {
- data: await data.png().toBuffer(),
- ext: 'png',
- type: 'image/png',
- };
- } else if (mime === 'image/svg+xml') {
- image = await this.imageProcessingService.convertToWebp(path, 2048, 2048, webpDefault);
- } else if (!mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(mime)) {
- throw new StatusError('Rejected type', 403, 'Rejected type');
- } else {
- image = {
- data: fs.readFileSync(path),
- ext,
- type: mime,
- };
- }
-
- reply.header('Content-Type', image.type);
- reply.header('Cache-Control', 'max-age=31536000, immutable');
- return image.data;
- } catch (err) {
- this.logger.error(`${err}`);
-
- if ('fallback' in request.query) {
- return reply.sendFile('/dummy.png', assets);
- }
-
- if (err instanceof StatusError && (err.statusCode === 302 || err.isClientError)) {
- reply.code(err.statusCode);
- } else {
- reply.code(500);
- }
- } finally {
- cleanup();
- }
- }
-}
diff --git a/packages/backend/src/server/ServerModule.ts b/packages/backend/src/server/ServerModule.ts
index 474edafe41..9dc1527698 100644
--- a/packages/backend/src/server/ServerModule.ts
+++ b/packages/backend/src/server/ServerModule.ts
@@ -3,7 +3,6 @@ import { EndpointsModule } from '@/server/api/EndpointsModule.js';
import { CoreModule } from '@/core/CoreModule.js';
import { ApiCallService } from './api/ApiCallService.js';
import { FileServerService } from './FileServerService.js';
-import { MediaProxyServerService } from './MediaProxyServerService.js';
import { NodeinfoServerService } from './NodeinfoServerService.js';
import { ServerService } from './ServerService.js';
import { WellKnownServerService } from './WellKnownServerService.js';
@@ -51,7 +50,6 @@ import { UserListChannelService } from './api/stream/channels/user-list.js';
UrlPreviewService,
ActivityPubServerService,
FileServerService,
- MediaProxyServerService,
NodeinfoServerService,
ServerService,
WellKnownServerService,
diff --git a/packages/backend/src/server/ServerService.ts b/packages/backend/src/server/ServerService.ts
index eb6a3795eb..beb3a34ecd 100644
--- a/packages/backend/src/server/ServerService.ts
+++ b/packages/backend/src/server/ServerService.ts
@@ -20,7 +20,6 @@ import { NodeinfoServerService } from './NodeinfoServerService.js';
import { ApiServerService } from './api/ApiServerService.js';
import { StreamingApiServerService } from './api/StreamingApiServerService.js';
import { WellKnownServerService } from './WellKnownServerService.js';
-import { MediaProxyServerService } from './MediaProxyServerService.js';
import { FileServerService } from './FileServerService.js';
import { ClientServerService } from './web/ClientServerService.js';
@@ -48,7 +47,6 @@ export class ServerService {
private wellKnownServerService: WellKnownServerService,
private nodeinfoServerService: NodeinfoServerService,
private fileServerService: FileServerService,
- private mediaProxyServerService: MediaProxyServerService,
private clientServerService: ClientServerService,
private globalEventService: GlobalEventService,
private loggerService: LoggerService,
@@ -73,8 +71,7 @@ export class ServerService {
}
fastify.register(this.apiServerService.createServer, { prefix: '/api' });
- fastify.register(this.fileServerService.createServer, { prefix: '/files' });
- fastify.register(this.mediaProxyServerService.createServer, { prefix: '/proxy' });
+ fastify.register(this.fileServerService.createServer);
fastify.register(this.activityPubServerService.createServer);
fastify.register(this.nodeinfoServerService.createServer);
fastify.register(this.wellKnownServerService.createServer);
diff --git a/packages/backend/src/server/api/endpoints/users/reactions.ts b/packages/backend/src/server/api/endpoints/users/reactions.ts
index 9ec911f322..ac401a60ee 100644
--- a/packages/backend/src/server/api/endpoints/users/reactions.ts
+++ b/packages/backend/src/server/api/endpoints/users/reactions.ts
@@ -61,7 +61,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
super(meta, paramDef, async (ps, me) => {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: ps.userId });
- if (me == null || (me.id !== ps.userId && !profile.publicReactions)) {
+ if ((me == null || me.id !== ps.userId) && !profile.publicReactions) {
throw new ApiError(meta.errors.reactionsNotPublic);
}