[fix] .wav .flac ファイルを再生可能にする (#10686)

* .wav .flac ファイルを再生可能にする
file-typeにより判定されたMIME TypeをHTML5 Audio/Video要素に認識されるものに書き換える

* fix typecheck error

* frontend側の FILE_TYPE_BROWSERSAFEも更新

* Update packages/backend/src/core/FileInfoService.ts

* ✌️

* 後方互換を確保

* add tests

* update changelog.md

---------

Co-authored-by: tamaina <tamaina@hotmail.co.jp>
This commit is contained in:
Yuriha
2023-04-26 02:17:58 +09:00
committed by GitHub
parent 2aa75f5489
commit a986203b38
11 changed files with 292 additions and 160 deletions

View File

@@ -5,7 +5,7 @@ import * as stream from 'node:stream';
import * as util from 'node:util';
import { Injectable } from '@nestjs/common';
import { FSWatcher } from 'chokidar';
import { fileTypeFromFile } from 'file-type';
import * as fileType from 'file-type';
import FFmpeg from 'fluent-ffmpeg';
import isSvg from 'is-svg';
import probeImageSize from 'probe-image-size';
@@ -301,21 +301,34 @@ export class FileInfoService {
return fs.promises.access(path).then(() => true, () => false);
}
@bindThis
public fixMime(mime: string | fileType.MimeType): string {
// see https://github.com/misskey-dev/misskey/pull/10686
if (mime === "audio/x-flac") {
return "audio/flac";
}
if (mime === "audio/vnd.wave") {
return "audio/wav";
}
return mime;
}
/**
* Detect MIME Type and extension
*/
@bindThis
public async detectType(path: string): Promise<{
mime: string;
ext: string | null;
}> {
mime: string;
ext: string | null;
}> {
// Check 0 byte
const fileSize = await this.getFileSize(path);
if (fileSize === 0) {
return TYPE_OCTET_STREAM;
}
const type = await fileTypeFromFile(path);
const type = await fileType.fileTypeFromFile(path);
if (type) {
// XMLはSVGかもしれない
@@ -324,7 +337,7 @@ export class FileInfoService {
}
return {
mime: type.mime,
mime: this.fixMime(type.mime),
ext: type.ext,
};
}