core: replace mime-db with mime and lazy load it

This commit is contained in:
Abdullah Atta
2024-11-12 13:11:22 +05:00
committed by Abdullah Atta
parent 7437b29518
commit 5f1f308540
3 changed files with 15 additions and 14 deletions

View File

@@ -28,7 +28,6 @@
"@types/event-source-polyfill": "^1.0.1",
"@types/html-to-text": "^9.0.0",
"@types/katex": "^0.16.2",
"@types/mime-db": "^1.43.1",
"@types/prismjs": "^1.26.0",
"@types/spark-md5": "^3.0.2",
"@types/streetwriters__showdown": "npm:@types/showdown@^2.0.6",
@@ -82,7 +81,7 @@
"katex": "0.16.2",
"linkedom": "^0.14.17",
"liqe": "^1.13.0",
"mime-db": "1.52.0",
"mime": "^4.0.4",
"prismjs": "^1.29.0",
"qclone": "^1.2.0",
"rfdc": "^1.3.0",

View File

@@ -171,10 +171,10 @@ export class Attachments implements ICollection {
filename:
filename ||
getFileNameWithExtension(
(await getFileNameWithExtension(
filename || hash,
mimeType || "application/octet-stream"
),
)),
hash,
hashType,
mimeType: mimeType || "application/octet-stream",

View File

@@ -17,21 +17,23 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import db from "mime-db";
export function getFileNameWithExtension(
export async function getFileNameWithExtension(
filename: string,
mime: string | undefined
): string {
): Promise<string> {
if (!mime || mime === "application/octet-stream") return filename;
const mimeData = db[mime];
if (!mimeData || !mimeData.extensions || mimeData.extensions.length === 0)
return filename;
const extension = mimeData.extensions[0];
if (mimeData.extensions.some((extension) => filename.endsWith(extension)))
return filename;
const { default: mimeDB } = await import("mime");
const extensions = mimeDB.getAllExtensions(mime);
if (!extensions || extensions.size === 0) return filename;
for (const ext of extensions) {
if (filename.endsWith(ext)) return filename;
}
const extension = extensions.values().next().value;
return `${filename}.${extension}`;
}