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

@@ -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}`;
}