mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 06:59:31 +01:00
core: replace mime-db with mime and lazy load it
This commit is contained in:
committed by
Abdullah Atta
parent
7437b29518
commit
5f1f308540
@@ -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",
|
||||
|
||||
@@ -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}`;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user