Skip to content

Commit

Permalink
Mime: Use FP approach instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
Davit Barbakadze committed Oct 3, 2017
1 parent d871991 commit 4de6a26
Showing 1 changed file with 151 additions and 144 deletions.
295 changes: 151 additions & 144 deletions src/javascript/core/utils/Mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ define("moxie/core/utils/Mime", [
"moxie/core/utils/Basic",
"moxie/core/I18n"
], function(Basic, I18n) {

var mimeData = "" +
"application/msword,doc dot," +
"application/pdf,pdf," +
Expand Down Expand Up @@ -69,167 +69,174 @@ define("moxie/core/utils/Mime", [
"video/3gpp,3gpp 3gp," +
"video/3gpp2,3g2," +
"video/vnd.rn-realvideo,rv," +
"video/ogg,ogv," +
"video/ogg,ogv," +
"video/x-matroska,mkv," +
"application/vnd.oasis.opendocument.formula-template,otf," +
"application/octet-stream,exe";


var Mime = {

/**
* Map of mimes to extensions
*
* @property mimes
* @type {Object}
*/
mimes: {},

/**
* Map of extensions to mimes
*
* @property extensions
* @type {Object}
*/
extensions: {},

/**
* Parses mimeData string into a mimes and extensions lookup maps. String should have the
* following format:
*
* application/msword,doc dot,application/pdf,pdf, ...
*
* so mime-type followed by comma and followed by space-separated list of associated extensions,
* then comma again and then another mime-type, etc.
*
* If invoked externally will replace override internal lookup maps with user-provided data.
*
* @method addMimeType
* @param {String} mimeData
*/
addMimeType: function (mimeData) {
var items = mimeData.split(/,/), i, ii, ext;

for (i = 0; i < items.length; i += 2) {
ext = items[i + 1].split(/ /);

// extension to mime lookup
for (ii = 0; ii < ext.length; ii++) {
this.mimes[ext[ii]] = items[i];
}
// mime to extension lookup
this.extensions[items[i]] = ext;
}
},


extList2mimes: function (filters, addMissingExtensions) {
var self = this, ext, i, ii, type, mimes = [];

// convert extensions to mime types list
for (i = 0; i < filters.length; i++) {
ext = filters[i].extensions.toLowerCase().split(/\s*,\s*/);

for (ii = 0; ii < ext.length; ii++) {

// if there's an asterisk in the list, then accept attribute is not required
if (ext[ii] === '*') {
return [];
}

type = self.mimes[ext[ii]];

// future browsers should filter by extension, finally
if (addMissingExtensions && /^\w+$/.test(ext[ii])) {
mimes.push('.' + ext[ii]);
} else if (type && Basic.inArray(type, mimes) === -1) {
mimes.push(type);
} else if (!type) {
// if we have no type in our map, then accept all
return [];
}
}


/**
* Map of mimes to extensions
*
* @property mimes
* @type {Object}
*/
var mimes = {};

/**
* Map of extensions to mimes
*
* @property extensions
* @type {Object}
*/
var extensions = {};


/**
* Parses mimeData string into a mimes and extensions lookup maps. String should have the
* following format:
*
* application/msword,doc dot,application/pdf,pdf, ...
*
* so mime-type followed by comma and followed by space-separated list of associated extensions,
* then comma again and then another mime-type, etc.
*
* If invoked externally will replace override internal lookup maps with user-provided data.
*
* @method addMimeType
* @param {String} mimeData
*/
var addMimeType = function (mimeData) {
var items = mimeData.split(/,/), i, ii, ext;

for (i = 0; i < items.length; i += 2) {
ext = items[i + 1].split(/ /);

// extension to mime lookup
for (ii = 0; ii < ext.length; ii++) {
mimes[ext[ii]] = items[i];
}
return mimes;
},
// mime to extension lookup
extensions[items[i]] = ext;
}
};


mimes2exts: function(mimes) {
var self = this, exts = [];

Basic.each(mimes, function(mime) {
mime = mime.toLowerCase();
var extList2mimes = function (filters, addMissingExtensions) {
var ext, i, ii, type, mimes = [];

if (mime === '*') {
exts = [];
return false;
// convert extensions to mime types list
for (i = 0; i < filters.length; i++) {
ext = filters[i].extensions.toLowerCase().split(/\s*,\s*/);

for (ii = 0; ii < ext.length; ii++) {

// if there's an asterisk in the list, then accept attribute is not required
if (ext[ii] === '*') {
return [];
}

// check if this thing looks like mime type
var m = mime.match(/^(\w+)\/(\*|\w+)$/);
if (m) {
if (m[2] === '*') {
// wildcard mime type detected
Basic.each(self.extensions, function(arr, mime) {
if ((new RegExp('^' + m[1] + '/')).test(mime)) {
[].push.apply(exts, self.extensions[mime]);
}
});
} else if (self.extensions[mime]) {
[].push.apply(exts, self.extensions[mime]);
}
type = mimes[ext[ii]];

// future browsers should filter by extension, finally
if (addMissingExtensions && /^\w+$/.test(ext[ii])) {
mimes.push('.' + ext[ii]);
} else if (type && Basic.inArray(type, mimes) === -1) {
mimes.push(type);
} else if (!type) {
// if we have no type in our map, then accept all
return [];
}
});
return exts;
},
}
}
return mimes;
};


var mimes2exts = function(mimes) {
var exts = [];

mimes2extList: function(mimes) {
var accept = [], exts = [];
Basic.each(mimes, function(mime) {
mime = mime.toLowerCase();

if (Basic.typeOf(mimes) === 'string') {
mimes = Basic.trim(mimes).split(/\s*,\s*/);
if (mime === '*') {
exts = [];
return false;
}

exts = this.mimes2exts(mimes);

accept.push({
title: I18n.translate('Files'),
extensions: exts.length ? exts.join(',') : '*'
});

return accept;
},

/**
* Extract extension from the given filename
*
* @method getFileExtension
* @param {String} fileName
* @return {String} File extension
*/
getFileExtension: function(fileName) {
var matches = fileName && fileName.match(/\.([^.]+)$/);
if (matches) {
return matches[1].toLowerCase();
// check if this thing looks like mime type
var m = mime.match(/^(\w+)\/(\*|\w+)$/);
if (m) {
if (m[2] === '*') {
// wildcard mime type detected
Basic.each(extensions, function(arr, mime) {
if ((new RegExp('^' + m[1] + '/')).test(mime)) {
[].push.apply(exts, extensions[mime]);
}
});
} else if (extensions[mime]) {
[].push.apply(exts, extensions[mime]);
}
}
return '';
},

/**
* Get file mime-type from it's filename - will try to match the extension
* against internal mime-type lookup map
*
* @method getFileMime
* @param {String} fileName
* @return File mime-type if found or an empty string if not
*/
getFileMime: function(fileName) {
return this.mimes[this.getFileExtension(fileName)] || '';
});
return exts;
};


var mimes2extList = function(mimes) {
var accept = [], exts = [];

if (Basic.typeOf(mimes) === 'string') {
mimes = Basic.trim(mimes).split(/\s*,\s*/);
}

exts = mimes2exts(mimes);

accept.push({
title: I18n.translate('Files'),
extensions: exts.length ? exts.join(',') : '*'
});

return accept;
};

Mime.addMimeType(mimeData);
/**
* Extract extension from the given filename
*
* @method getFileExtension
* @param {String} fileName
* @return {String} File extension
*/
var getFileExtension = function(fileName) {
var matches = fileName && fileName.match(/\.([^.]+)$/);
if (matches) {
return matches[1].toLowerCase();
}
return '';
};


/**
* Get file mime-type from it's filename - will try to match the extension
* against internal mime-type lookup map
*
* @method getFileMime
* @param {String} fileName
* @return File mime-type if found or an empty string if not
*/
var getFileMime = function(fileName) {
return mimes[getFileExtension(fileName)] || '';
};


addMimeType(mimeData);

return Mime;
return {
addMimeType: addMimeType,
extList2mimes: extList2mimes,
mimes2exts: mimes2exts,
mimes2extList: mimes2extList,
getFileExtension: getFileExtension,
getFileMime: getFileMime
}
});

0 comments on commit 4de6a26

Please sign in to comment.