Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
6.8 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import crypto from 'node:crypto';
import fs from 'node:fs';
import {Config} from '@app/api/Config';
import {
	IMediaService,
	type MediaProxyMetadataRequest,
	type MediaProxyMetadataResponse,
	type MediaProxySniffResponse,
	type MediaProxyStorageObject,
	type MediaProxyStrippedObject,
} from '@app/api/infrastructure/IMediaService';
import type {IStorageService} from '@app/api/infrastructure/IStorageService';

export class TestMediaService extends IMediaService {
	constructor(private readonly storageService: IStorageService) {
		super();
	}

	async getMetadata(request: MediaProxyMetadataRequest): Promise<MediaProxyMetadataResponse | null> {
		if (request.type === 'base64') {
			return this.analyzeBase64Image(request.base64);
		}
		if (request.type === 's3') {
			let stored: Buffer | null = null;
			try {
				stored = Buffer.from(await this.storageService.readObject(request.bucket, request.key));
			} catch {
				stored = null;
			}
			const format = (stored ? this.detectImageFormat(stored) : null) ?? this.getFormatFromFilename(request.key);
			return {
				format,
				content_type: this.contentTypeForFormat(format),
				content_hash: crypto.createHash('md5').update(request.key).digest('hex'),
				size: stored?.length ?? 1024,
				width: 128,
				height: 128,
				animated: stored ? this.isAnimatedImage(stored, format) : format === 'gif',
				nsfw: false,
				placeholder: this.fakePlaceholder(request.key),
			};
		}
		if (request.type === 'upload') {
			const filename = request.upload_filename.toLowerCase();
			const format = this.getFormatFromFilename(filename);
			const uploadedFile = await this.storageService.getObjectMetadata(
				Config.s3.buckets.uploads,
				request.upload_filename,
			);
			return {
				format,
				content_type: this.contentTypeForFormat(format),
				content_hash: crypto.createHash('md5').update(request.upload_filename).digest('hex'),
				size: uploadedFile?.contentLength ?? 1024,
				width: 128,
				height: 128,
				animated: format === 'gif',
				nsfw: false,
				placeholder: this.fakePlaceholder(request.upload_filename),
			};
		}
		if (request.type === 'external') {
			return {
				format: 'png',
				content_type: 'image/png',
				content_hash: crypto.createHash('md5').update(request.url).digest('hex'),
				size: 1024,
				width: 128,
				height: 128,
				animated: false,
				nsfw: false,
				placeholder: this.fakePlaceholder(request.url),
			};
		}
		return null;
	}

	private fakePlaceholder(seed: string): string {
		return crypto.createHash('sha256').update(seed).digest('base64').slice(0, 24);
	}

	getExternalMediaProxyURL(): string {
		return 'https://media-proxy.test';
	}

	async sniffUpload(_uploadFilename: string): Promise<MediaProxySniffResponse | null> {
		return {content_type: null};
	}

	async stripObjectMetadata(
		source: MediaProxyStorageObject,
		contentType: string,
		outputPath: string,
	): Promise<MediaProxyStrippedObject | null> {
		await fs.promises.writeFile(outputPath, await this.storageService.readObject(source.bucket, source.key));
		return {contentType, width: 100, height: 100};
	}

	async stripImageMetadata(image: Uint8Array): Promise<Uint8Array | null> {
		return image;
	}

	async getAudioDurationSeconds(base64: string): Promise<number | null> {
		const buffer = Buffer.from(base64, 'base64');
		if (!this.isWav(buffer) || buffer.length < 44) {
			return null;
		}
		const byteRate = buffer.readUInt32LE(28);
		return byteRate > 0 ? buffer.readUInt32LE(40) / byteRate : null;
	}

	private analyzeBase64Image(base64: string): MediaProxyMetadataResponse | null {
		try {
			const buffer = Buffer.from(base64, 'base64');
			if (buffer.length === 0) {
				return null;
			}
			const format = this.detectImageFormat(buffer) ?? (this.isWav(buffer) ? 'wav' : null);
			if (!format) {
				return null;
			}
			const dimensions = this.readPngDimensions(buffer, format);
			return {
				format,
				content_type: this.contentTypeForFormat(format),
				content_hash: crypto.createHash('md5').update(buffer).digest('hex'),
				size: buffer.length,
				width: dimensions?.width ?? 128,
				height: dimensions?.height ?? 128,
				animated: this.isAnimatedImage(buffer, format),
				nsfw: false,
			};
		} catch (_error) {
			return null;
		}
	}

	private readPngDimensions(buffer: Buffer, format: string): {width: number; height: number} | null {
		if (format !== 'png' || buffer.length < 24 || buffer.toString('latin1', 12, 16) !== 'IHDR') {
			return null;
		}
		const width = buffer.readUInt32BE(16);
		const height = buffer.readUInt32BE(20);
		return width > 0 && height > 0 ? {width, height} : null;
	}

	private detectImageFormat(buffer: Buffer): string | null {
		if (this.isSvg(buffer)) {
			return 'svg';
		}
		if (buffer.length < 12) {
			return null;
		}
		const first12Bytes = buffer.subarray(0, 12);
		if (this.isPng(first12Bytes)) {
			return 'png';
		}
		if (this.isGif(first12Bytes)) {
			return 'gif';
		}
		if (this.isWebP(first12Bytes)) {
			return 'webp';
		}
		if (this.isJpeg(first12Bytes)) {
			return 'jpeg';
		}
		return null;
	}

	private isSvg(buffer: Buffer): boolean {
		const prefix = buffer
			.subarray(0, 1024)
			.toString('utf8')
			.replace(/^\uFEFF/, '')
			.trimStart()
			.toLowerCase();
		return prefix.startsWith('<svg') || (prefix.startsWith('<?xml') && prefix.includes('<svg'));
	}

	private isPng(bytes: Buffer): boolean {
		return bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47;
	}

	private isGif(bytes: Buffer): boolean {
		return (
			bytes[0] === 0x47 &&
			bytes[1] === 0x49 &&
			bytes[2] === 0x46 &&
			bytes[3] === 0x38 &&
			(bytes[4] === 0x37 || bytes[4] === 0x39) &&
			bytes[5] === 0x61
		);
	}

	private isWebP(bytes: Buffer): boolean {
		return (
			bytes[0] === 0x52 &&
			bytes[1] === 0x49 &&
			bytes[2] === 0x46 &&
			bytes[3] === 0x46 &&
			bytes[8] === 0x57 &&
			bytes[9] === 0x45 &&
			bytes[10] === 0x42 &&
			bytes[11] === 0x50
		);
	}

	private isWav(bytes: Buffer): boolean {
		return bytes.toString('latin1', 0, 4) === 'RIFF' && bytes.toString('latin1', 8, 12) === 'WAVE';
	}

	private isJpeg(bytes: Buffer): boolean {
		return bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff;
	}

	private isAnimatedImage(buffer: Buffer, format: string): boolean {
		if (format === 'gif') return true;
		if (format === 'webp') return buffer.includes(Buffer.from('ANIM'));
		if (format === 'png') return buffer.includes(Buffer.from('acTL'));
		return false;
	}

	private getFormatFromFilename(filename: string): string {
		const ext = filename.split('.').pop()?.toLowerCase() ?? 'png';
		if (['png', 'gif', 'webp', 'jpeg', 'jpg', 'svg'].includes(ext)) {
			return ext === 'jpg' ? 'jpeg' : ext;
		}
		return 'png';
	}

	private contentTypeForFormat(format: string): string {
		if (format === 'jpeg') return 'image/jpeg';
		if (format === 'svg') return 'image/svg+xml';
		if (format === 'wav') return 'audio/wav';
		return `image/${format}`;
	}
}