1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/lib/mime-utils.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

const mimeTypes = require('./mime-utils-types');
2017-06-24 20:51:43 +02:00
const mime = {
2017-11-20 00:08:58 +02:00
fromFileExtension(ext) {
ext = ext.toLowerCase();
for (let i = 0; i < mimeTypes.length; i++) {
const t = mimeTypes[i];
if (t.e.indexOf(ext) >= 0) {
return t.t;
}
}
return null;
},
fromFilename(name) {
if (!name) return null;
const splitted = name.trim().split('.');
if (splitted.length <= 1) return null;
return mime.fromFileExtension(splitted[splitted.length - 1]);
},
2017-06-24 20:51:43 +02:00
toFileExtension(mimeType) {
mimeType = mimeType.toLowerCase();
for (let i = 0; i < mimeTypes.length; i++) {
2017-06-25 01:19:11 +02:00
const t = mimeTypes[i];
2022-07-23 11:33:12 +02:00
if (mimeType === t.t) {
2017-06-25 01:19:11 +02:00
// Return the first file extension that is 3 characters long
// If none exist return the first one in the list.
for (let j = 0; j < t.e.length; j++) {
2022-07-23 11:33:12 +02:00
if (t.e[j].length === 3) return t.e[j];
2017-06-25 01:19:11 +02:00
}
return t.e[0];
}
2017-06-24 20:51:43 +02:00
}
return null;
},
2018-05-25 09:51:54 +02:00
fromDataUrl(dataUrl) {
// Example: data:image/jpeg;base64,/9j/4AAQSkZJR.....
const defaultMime = 'text/plain';
const p = dataUrl.substr(0, dataUrl.indexOf(',')).split(';');
2018-05-25 09:51:54 +02:00
let s = p[0];
s = s.split(':');
if (s.length <= 1) return defaultMime;
s = s[1];
return s.indexOf('/') >= 0 ? s : defaultMime; // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
},
2019-07-29 15:43:53 +02:00
};
2018-05-25 09:51:54 +02:00
2019-07-29 15:43:53 +02:00
module.exports = { mime };