diff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.js
index efb28fcce..fcd98a50b 100644
--- a/packages/core/collections/attachments.js
+++ b/packages/core/collections/attachments.js
@@ -24,6 +24,7 @@ import { EV, EVENTS, sendAttachmentsProgressEvent } from "../common";
import dataurl from "../utils/dataurl";
import dayjs from "dayjs";
import setManipulator from "../utils/set";
+import { getFileNameWithExtension } from "../utils/filename";
export default class Attachments extends Collection {
constructor(db, name, cached) {
@@ -131,7 +132,7 @@ export default class Attachments extends Collection {
metadata: {
hash,
hashType,
- filename,
+ filename: getFileNameWithExtension(filename, type),
type: type || "application/octet-stream"
},
dateCreated: attachment.dateCreated || Date.now(),
diff --git a/packages/core/package-lock.json b/packages/core/package-lock.json
index 0bc6c9f86..6b931bec1 100644
--- a/packages/core/package-lock.json
+++ b/packages/core/package-lock.json
@@ -19,6 +19,7 @@
"htmlparser2": "^8.0.1",
"linkedom": "^0.14.17",
"liqe": "^1.13.0",
+ "mime-db": "1.52.0",
"qclone": "^1.2.0",
"spark-md5": "^3.0.2"
},
@@ -8138,7 +8139,6 @@
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
- "dev": true,
"engines": {
"node": ">= 0.6"
}
@@ -16180,8 +16180,7 @@
"mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
- "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
- "dev": true
+ "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg=="
},
"mime-types": {
"version": "2.1.35",
diff --git a/packages/core/package.json b/packages/core/package.json
index 3f47d13c1..ac1c5be30 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -47,7 +47,8 @@
"linkedom": "^0.14.17",
"liqe": "^1.13.0",
"qclone": "^1.2.0",
- "spark-md5": "^3.0.2"
+ "spark-md5": "^3.0.2",
+ "mime-db": "1.52.0"
},
"overrides": {
"htmlparser2": "^8.0.1"
diff --git a/packages/core/utils/filename.ts b/packages/core/utils/filename.ts
new file mode 100644
index 000000000..fb704d808
--- /dev/null
+++ b/packages/core/utils/filename.ts
@@ -0,0 +1,40 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see .
+*/
+
+type MimeTypeInfo = {
+ source: string;
+ extensions?: string[];
+ charset?: string;
+ compressible?: boolean;
+};
+
+let db: Record;
+export function getFileNameWithExtension(filename: string, mime?: string) {
+ if (!mime || mime === "application/octet-stream") return filename;
+ if (!db) db = require("mime-db");
+ 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;
+
+ return `${filename}.${extension}`;
+}