mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-21 14:09:34 +01:00
core: add getFileNameWithExtension utility
This commit is contained in:
committed by
Abdullah Atta
parent
a454ec4944
commit
1fdede5518
@@ -24,6 +24,7 @@ import { EV, EVENTS, sendAttachmentsProgressEvent } from "../common";
|
|||||||
import dataurl from "../utils/dataurl";
|
import dataurl from "../utils/dataurl";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import setManipulator from "../utils/set";
|
import setManipulator from "../utils/set";
|
||||||
|
import { getFileNameWithExtension } from "../utils/filename";
|
||||||
|
|
||||||
export default class Attachments extends Collection {
|
export default class Attachments extends Collection {
|
||||||
constructor(db, name, cached) {
|
constructor(db, name, cached) {
|
||||||
@@ -131,7 +132,7 @@ export default class Attachments extends Collection {
|
|||||||
metadata: {
|
metadata: {
|
||||||
hash,
|
hash,
|
||||||
hashType,
|
hashType,
|
||||||
filename,
|
filename: getFileNameWithExtension(filename, type),
|
||||||
type: type || "application/octet-stream"
|
type: type || "application/octet-stream"
|
||||||
},
|
},
|
||||||
dateCreated: attachment.dateCreated || Date.now(),
|
dateCreated: attachment.dateCreated || Date.now(),
|
||||||
|
|||||||
5
packages/core/package-lock.json
generated
5
packages/core/package-lock.json
generated
@@ -19,6 +19,7 @@
|
|||||||
"htmlparser2": "^8.0.1",
|
"htmlparser2": "^8.0.1",
|
||||||
"linkedom": "^0.14.17",
|
"linkedom": "^0.14.17",
|
||||||
"liqe": "^1.13.0",
|
"liqe": "^1.13.0",
|
||||||
|
"mime-db": "1.52.0",
|
||||||
"qclone": "^1.2.0",
|
"qclone": "^1.2.0",
|
||||||
"spark-md5": "^3.0.2"
|
"spark-md5": "^3.0.2"
|
||||||
},
|
},
|
||||||
@@ -8138,7 +8139,6 @@
|
|||||||
"version": "1.52.0",
|
"version": "1.52.0",
|
||||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
|
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
|
||||||
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
|
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
|
||||||
"dev": true,
|
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 0.6"
|
"node": ">= 0.6"
|
||||||
}
|
}
|
||||||
@@ -16180,8 +16180,7 @@
|
|||||||
"mime-db": {
|
"mime-db": {
|
||||||
"version": "1.52.0",
|
"version": "1.52.0",
|
||||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
|
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
|
||||||
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
|
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"mime-types": {
|
"mime-types": {
|
||||||
"version": "2.1.35",
|
"version": "2.1.35",
|
||||||
|
|||||||
@@ -47,7 +47,8 @@
|
|||||||
"linkedom": "^0.14.17",
|
"linkedom": "^0.14.17",
|
||||||
"liqe": "^1.13.0",
|
"liqe": "^1.13.0",
|
||||||
"qclone": "^1.2.0",
|
"qclone": "^1.2.0",
|
||||||
"spark-md5": "^3.0.2"
|
"spark-md5": "^3.0.2",
|
||||||
|
"mime-db": "1.52.0"
|
||||||
},
|
},
|
||||||
"overrides": {
|
"overrides": {
|
||||||
"htmlparser2": "^8.0.1"
|
"htmlparser2": "^8.0.1"
|
||||||
|
|||||||
40
packages/core/utils/filename.ts
Normal file
40
packages/core/utils/filename.ts
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
type MimeTypeInfo = {
|
||||||
|
source: string;
|
||||||
|
extensions?: string[];
|
||||||
|
charset?: string;
|
||||||
|
compressible?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
let db: Record<string, MimeTypeInfo>;
|
||||||
|
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}`;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user