Skip to content

Commit

Permalink
Less calls to the server in mimetype.js
Browse files Browse the repository at this point in the history
Now we just load 1 big list of aliases and the mimetype icons we have.
This avoids lookups for each mime type. And speeds things up!
  • Loading branch information
rullzer committed Jun 3, 2015
1 parent 529df98 commit 0bf84e7
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 108 deletions.
138 changes: 30 additions & 108 deletions core/js/mimetype.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,20 @@
OC.MimeType = {

files: [],

mimeTypeAlias: {},

mimeTypeIcons: {},

init: function() {
OC.MimeType.mimeTypeAlias = {
"application/octet-stream" : "file", // use file icon as fallback

"application/illustrator" : "image/vector",
"application/postscript" : "image/vector",
"image/svg+xml" : "image/vector",

"application/coreldraw" : "image",
"application/x-gimp" : "image",
"application/x-photoshop" : "image",
"application/x-dcraw" : "image",

"application/font-sfnt" : "font",
"application/x-font" : "font",
"application/font-woff" : "font",
"application/vnd.ms-fontobject" : "font",

"application/json" : "text/code",
"application/x-perl" : "text/code",
"application/x-php" : "text/code",
"text/x-shellscript" : "text/code",
"application/yaml" : "text/code",
"application/xml" : "text/html",
"text/css" : "text/code",
"application/x-tex" : "text",

"application/x-compressed" : "package/x-generic",
"application/x-7z-compressed" : "package/x-generic",
"application/x-deb" : "package/x-generic",
"application/x-gzip" : "package/x-generic",
"application/x-rar-compressed" : "package/x-generic",
"application/x-tar" : "package/x-generic",
"application/vnd.android.package-archive" : "package/x-generic",
"application/zip" : "package/x-generic",

"application/msword" : "x-office/document",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "x-office/document",
"application/vnd.openxmlformats-officedocument.wordprocessingml.template" : "x-office/document",
"application/vnd.ms-word.document.macroEnabled.12" : "x-office/document",
"application/vnd.ms-word.template.macroEnabled.12" : "x-office/document",
"application/vnd.oasis.opendocument.text" : "x-office/document",
"application/vnd.oasis.opendocument.text-template" : "x-office/document",
"application/vnd.oasis.opendocument.text-web" : "x-office/document",
"application/vnd.oasis.opendocument.text-master" : "x-office/document",

"application/mspowerpoint" : "x-office/presentation",
"application/vnd.ms-powerpoint" : "x-office/presentation",
"application/vnd.openxmlformats-officedocument.presentationml.presentation" : "x-office/presentation",
"application/vnd.openxmlformats-officedocument.presentationml.template" : "x-office/presentation",
"application/vnd.openxmlformats-officedocument.presentationml.slideshow" : "x-office/presentation",
"application/vnd.ms-powerpoint.addin.macroEnabled.12" : "x-office/presentation",
"application/vnd.ms-powerpoint.presentation.macroEnabled.12" : "x-office/presentation",
"application/vnd.ms-powerpoint.template.macroEnabled.12" : "x-office/presentation",
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12" : "x-office/presentation",
"application/vnd.oasis.opendocument.presentation" : "x-office/presentation",
"application/vnd.oasis.opendocument.presentation-template" : "x-office/presentation",

"application/msexcel" : "x-office/spreadsheet",
"application/vnd.ms-excel" : "x-office/spreadsheet",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "x-office/spreadsheet",
"application/vnd.openxmlformats-officedocument.spreadsheetml.template" : "x-office/spreadsheet",
"application/vnd.ms-excel.sheet.macroEnabled.12" : "x-office/spreadsheet",
"application/vnd.ms-excel.template.macroEnabled.12" : "x-office/spreadsheet",
"application/vnd.ms-excel.addin.macroEnabled.12" : "x-office/spreadsheet",
"application/vnd.ms-excel.sheet.binary.macroEnabled.12" : "x-office/spreadsheet",
"application/vnd.oasis.opendocument.spreadsheet" : "x-office/spreadsheet",
"application/vnd.oasis.opendocument.spreadsheet-template" : "x-office/spreadsheet",
"text/csv" : "x-office/spreadsheet",

"application/msaccess" : "database"
};
$.getJSON(OC.webroot + '/core/mimetypes.json', function(data) {
OC.MimeType.mimeTypeAlias = data['aliases'];
OC.MimeType.files = data['files'];
});
},

mimetypeIcon: function(mimeType) {
if (mimeType === undefined) {
if (_.isUndefined(mimeType)) {
return undefined;
}

Expand All @@ -90,49 +25,36 @@ OC.MimeType = {
return OC.MimeType.mimeTypeIcons[mimeType];
}

if (mimeType == 'dir') {
return OC.webroot + '/core/img/filetypes/folder.png';
}
if (mimeType == 'dir-shared') {
return OC.webroot + '/core/img/filetypes/folder-shared.png';
}
if (mimeType == 'dir-external') {
return OC.webroot + '/core/img/filetypes/folder-external.png';
}

function checkExists(url) {
var ok;
$.ajax({
url:url,
async: false,
type:'HEAD',
error: function() {
ok = false;
},
success: function() {
ok = true;
}
});
return ok;
}


var icon = mimeType.replace(new RegExp('/', 'g'), '-');
//icon = icon.replace(new RegExp('\\', 'g'), '-');

if (checkExists(OC.webroot + '/core/img/filetypes/' + icon + '.png')) {
OC.MimeType.mimeTypeIcons[mimeType] = OC.webroot + '/core/img/filetypes/' + icon + '.png';
return OC.MimeType.mimeTypeIcons[mimeType];
var path = OC.webroot + '/core/img/filetypes/';

// Generate path
if (mimeType === 'dir') {
path += 'folder';
} else if (mimeType === 'dir-shared') {
path += 'folder-shared';
} else if (mimeType === 'dir-external') {
path += 'folder-external';
} else if ($.inArray(icon, OC.MimeType.files)) {
path += icon;
} else if ($.inArray(icon.split('-')[0], OC.MimeType.files)) {
path += icon.split('-')[0];
} else {
path += 'file';
}

mimePart = icon.split('-')[0];
if (checkExists(OC.webroot + '/core/img/filetypes/' + mimePart + '.png')) {
OC.MimeType.mimeTypeIcons[mimeType] = OC.webroot + '/core/img/filetypes/' + mimePart + '.png';
return OC.MimeType.mimeTypeIcons[mimeType];
// Use svg if we can
if(OC.Util.hasSVGSupport()){
path += '.svg';
} else {
OC.MimeType.mimeTypeIcons[mimeType] = OC.webroot + '/core/img/filetypes/file.png';
return OC.MimeType.mimeTypeIcons[mimeType];
path += '.png';
}

// Cache the result
OC.MimeType.mimeTypeIcons[mimeType] = path;
return path;
}

};
Expand Down
98 changes: 98 additions & 0 deletions core/mimetypes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"aliases": {
"application/coreldraw": "image",
"application/font-sfnt": "font",
"application/font-woff": "font",
"application/illustrator": "image/vector",
"application/json": "text/code",
"application/msaccess": "database",
"application/msexcel": "x-office/spreadsheet",
"application/mspowerpoint": "x-office/presentation",
"application/msword": "x-office/document",
"application/octet-stream": "file",
"application/postscript": "image/vector",
"application/vnd.android.package-archive": "package/x-generic",
"application/vnd.ms-excel": "x-office/spreadsheet",
"application/vnd.ms-excel.addin.macroEnabled.12": "x-office/spreadsheet",
"application/vnd.ms-excel.sheet.binary.macroEnabled.12": "x-office/spreadsheet",
"application/vnd.ms-excel.sheet.macroEnabled.12": "x-office/spreadsheet",
"application/vnd.ms-excel.template.macroEnabled.12": "x-office/spreadsheet",
"application/vnd.ms-fontobject": "font",
"application/vnd.ms-powerpoint": "x-office/presentation",
"application/vnd.ms-powerpoint.addin.macroEnabled.12": "x-office/presentation",
"application/vnd.ms-powerpoint.presentation.macroEnabled.12": "x-office/presentation",
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12": "x-office/presentation",
"application/vnd.ms-powerpoint.template.macroEnabled.12": "x-office/presentation",
"application/vnd.ms-word.document.macroEnabled.12": "x-office/document",
"application/vnd.ms-word.template.macroEnabled.12": "x-office/document",
"application/vnd.oasis.opendocument.presentation": "x-office/presentation",
"application/vnd.oasis.opendocument.presentation-template": "x-office/presentation",
"application/vnd.oasis.opendocument.spreadsheet": "x-office/spreadsheet",
"application/vnd.oasis.opendocument.spreadsheet-template": "x-office/spreadsheet",
"application/vnd.oasis.opendocument.text": "x-office/document",
"application/vnd.oasis.opendocument.text-master": "x-office/document",
"application/vnd.oasis.opendocument.text-template": "x-office/document",
"application/vnd.oasis.opendocument.text-web": "x-office/document",
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "x-office/presentation",
"application/vnd.openxmlformats-officedocument.presentationml.slideshow": "x-office/presentation",
"application/vnd.openxmlformats-officedocument.presentationml.template": "x-office/presentation",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "x-office/spreadsheet",
"application/vnd.openxmlformats-officedocument.spreadsheetml.template": "x-office/spreadsheet",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "x-office/document",
"application/vnd.openxmlformats-officedocument.wordprocessingml.template": "x-office/document",
"application/x-7z-compressed": "package/x-generic",
"application/x-compressed": "package/x-generic",
"application/x-dcraw": "image",
"application/x-deb": "package/x-generic",
"application/x-font": "font",
"application/x-gimp": "image",
"application/x-gzip": "package/x-generic",
"application/x-perl": "text/code",
"application/x-photoshop": "image",
"application/x-php": "text/code",
"application/x-rar-compressed": "package/x-generic",
"application/x-tar": "package/x-generic",
"application/x-tex": "text",
"application/xml": "text/html",
"application/yaml": "text/code",
"application/zip": "package/x-generic",
"image/svg+xml": "image/vector",
"text/css": "text/code",
"text/csv": "x-office/spreadsheet",
"text/x-shellscript": "text/code"
},
"files": [
"application",
"application-epub+zip",
"application-javascript",
"application-pdf",
"application-rss+xml",
"application-x-cbr",
"application-x-shockwave-flash",
"audio",
"database",
"file",
"folder",
"folder-drag-accept",
"folder-external",
"folder-public",
"folder-shared",
"font",
"image",
"image-vector",
"package-x-generic",
"text",
"text-calendar",
"text-code",
"text-html",
"text-vcard",
"text-x-c",
"text-x-h",
"text-x-python",
"video",
"web",
"x-office-document",
"x-office-presentation",
"x-office-spreadsheet"
]
}

This comment has been minimized.

Copy link
@oparoz

oparoz Jun 3, 2015

Contributor

newline

0 comments on commit 0bf84e7

Please sign in to comment.