Skip to content

Commit 0bf84e7

Browse files
committed
Less calls to the server in mimetype.js
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!
1 parent 529df98 commit 0bf84e7

File tree

2 files changed

+128
-108
lines changed

2 files changed

+128
-108
lines changed

core/js/mimetype.js

Lines changed: 30 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,20 @@
11
OC.MimeType = {
22

3+
files: [],
4+
35
mimeTypeAlias: {},
46

57
mimeTypeIcons: {},
68

79
init: function() {
8-
OC.MimeType.mimeTypeAlias = {
9-
"application/octet-stream" : "file", // use file icon as fallback
10-
11-
"application/illustrator" : "image/vector",
12-
"application/postscript" : "image/vector",
13-
"image/svg+xml" : "image/vector",
14-
15-
"application/coreldraw" : "image",
16-
"application/x-gimp" : "image",
17-
"application/x-photoshop" : "image",
18-
"application/x-dcraw" : "image",
19-
20-
"application/font-sfnt" : "font",
21-
"application/x-font" : "font",
22-
"application/font-woff" : "font",
23-
"application/vnd.ms-fontobject" : "font",
24-
25-
"application/json" : "text/code",
26-
"application/x-perl" : "text/code",
27-
"application/x-php" : "text/code",
28-
"text/x-shellscript" : "text/code",
29-
"application/yaml" : "text/code",
30-
"application/xml" : "text/html",
31-
"text/css" : "text/code",
32-
"application/x-tex" : "text",
33-
34-
"application/x-compressed" : "package/x-generic",
35-
"application/x-7z-compressed" : "package/x-generic",
36-
"application/x-deb" : "package/x-generic",
37-
"application/x-gzip" : "package/x-generic",
38-
"application/x-rar-compressed" : "package/x-generic",
39-
"application/x-tar" : "package/x-generic",
40-
"application/vnd.android.package-archive" : "package/x-generic",
41-
"application/zip" : "package/x-generic",
42-
43-
"application/msword" : "x-office/document",
44-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "x-office/document",
45-
"application/vnd.openxmlformats-officedocument.wordprocessingml.template" : "x-office/document",
46-
"application/vnd.ms-word.document.macroEnabled.12" : "x-office/document",
47-
"application/vnd.ms-word.template.macroEnabled.12" : "x-office/document",
48-
"application/vnd.oasis.opendocument.text" : "x-office/document",
49-
"application/vnd.oasis.opendocument.text-template" : "x-office/document",
50-
"application/vnd.oasis.opendocument.text-web" : "x-office/document",
51-
"application/vnd.oasis.opendocument.text-master" : "x-office/document",
52-
53-
"application/mspowerpoint" : "x-office/presentation",
54-
"application/vnd.ms-powerpoint" : "x-office/presentation",
55-
"application/vnd.openxmlformats-officedocument.presentationml.presentation" : "x-office/presentation",
56-
"application/vnd.openxmlformats-officedocument.presentationml.template" : "x-office/presentation",
57-
"application/vnd.openxmlformats-officedocument.presentationml.slideshow" : "x-office/presentation",
58-
"application/vnd.ms-powerpoint.addin.macroEnabled.12" : "x-office/presentation",
59-
"application/vnd.ms-powerpoint.presentation.macroEnabled.12" : "x-office/presentation",
60-
"application/vnd.ms-powerpoint.template.macroEnabled.12" : "x-office/presentation",
61-
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12" : "x-office/presentation",
62-
"application/vnd.oasis.opendocument.presentation" : "x-office/presentation",
63-
"application/vnd.oasis.opendocument.presentation-template" : "x-office/presentation",
64-
65-
"application/msexcel" : "x-office/spreadsheet",
66-
"application/vnd.ms-excel" : "x-office/spreadsheet",
67-
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "x-office/spreadsheet",
68-
"application/vnd.openxmlformats-officedocument.spreadsheetml.template" : "x-office/spreadsheet",
69-
"application/vnd.ms-excel.sheet.macroEnabled.12" : "x-office/spreadsheet",
70-
"application/vnd.ms-excel.template.macroEnabled.12" : "x-office/spreadsheet",
71-
"application/vnd.ms-excel.addin.macroEnabled.12" : "x-office/spreadsheet",
72-
"application/vnd.ms-excel.sheet.binary.macroEnabled.12" : "x-office/spreadsheet",
73-
"application/vnd.oasis.opendocument.spreadsheet" : "x-office/spreadsheet",
74-
"application/vnd.oasis.opendocument.spreadsheet-template" : "x-office/spreadsheet",
75-
"text/csv" : "x-office/spreadsheet",
76-
77-
"application/msaccess" : "database"
78-
};
10+
$.getJSON(OC.webroot + '/core/mimetypes.json', function(data) {
11+
OC.MimeType.mimeTypeAlias = data['aliases'];
12+
OC.MimeType.files = data['files'];
13+
});
7914
},
8015

8116
mimetypeIcon: function(mimeType) {
82-
if (mimeType === undefined) {
17+
if (_.isUndefined(mimeType)) {
8318
return undefined;
8419
}
8520

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

93-
if (mimeType == 'dir') {
94-
return OC.webroot + '/core/img/filetypes/folder.png';
95-
}
96-
if (mimeType == 'dir-shared') {
97-
return OC.webroot + '/core/img/filetypes/folder-shared.png';
98-
}
99-
if (mimeType == 'dir-external') {
100-
return OC.webroot + '/core/img/filetypes/folder-external.png';
101-
}
102-
103-
function checkExists(url) {
104-
var ok;
105-
$.ajax({
106-
url:url,
107-
async: false,
108-
type:'HEAD',
109-
error: function() {
110-
ok = false;
111-
},
112-
success: function() {
113-
ok = true;
114-
}
115-
});
116-
return ok;
117-
}
118-
119-
12028
var icon = mimeType.replace(new RegExp('/', 'g'), '-');
12129
//icon = icon.replace(new RegExp('\\', 'g'), '-');
12230

123-
if (checkExists(OC.webroot + '/core/img/filetypes/' + icon + '.png')) {
124-
OC.MimeType.mimeTypeIcons[mimeType] = OC.webroot + '/core/img/filetypes/' + icon + '.png';
125-
return OC.MimeType.mimeTypeIcons[mimeType];
31+
var path = OC.webroot + '/core/img/filetypes/';
32+
33+
// Generate path
34+
if (mimeType === 'dir') {
35+
path += 'folder';
36+
} else if (mimeType === 'dir-shared') {
37+
path += 'folder-shared';
38+
} else if (mimeType === 'dir-external') {
39+
path += 'folder-external';
40+
} else if ($.inArray(icon, OC.MimeType.files)) {
41+
path += icon;
42+
} else if ($.inArray(icon.split('-')[0], OC.MimeType.files)) {
43+
path += icon.split('-')[0];
44+
} else {
45+
path += 'file';
12646
}
12747

128-
mimePart = icon.split('-')[0];
129-
if (checkExists(OC.webroot + '/core/img/filetypes/' + mimePart + '.png')) {
130-
OC.MimeType.mimeTypeIcons[mimeType] = OC.webroot + '/core/img/filetypes/' + mimePart + '.png';
131-
return OC.MimeType.mimeTypeIcons[mimeType];
48+
// Use svg if we can
49+
if(OC.Util.hasSVGSupport()){
50+
path += '.svg';
13251
} else {
133-
OC.MimeType.mimeTypeIcons[mimeType] = OC.webroot + '/core/img/filetypes/file.png';
134-
return OC.MimeType.mimeTypeIcons[mimeType];
52+
path += '.png';
13553
}
54+
55+
// Cache the result
56+
OC.MimeType.mimeTypeIcons[mimeType] = path;
57+
return path;
13658
}
13759

13860
};

core/mimetypes.json

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"aliases": {
3+
"application/coreldraw": "image",
4+
"application/font-sfnt": "font",
5+
"application/font-woff": "font",
6+
"application/illustrator": "image/vector",
7+
"application/json": "text/code",
8+
"application/msaccess": "database",
9+
"application/msexcel": "x-office/spreadsheet",
10+
"application/mspowerpoint": "x-office/presentation",
11+
"application/msword": "x-office/document",
12+
"application/octet-stream": "file",
13+
"application/postscript": "image/vector",
14+
"application/vnd.android.package-archive": "package/x-generic",
15+
"application/vnd.ms-excel": "x-office/spreadsheet",
16+
"application/vnd.ms-excel.addin.macroEnabled.12": "x-office/spreadsheet",
17+
"application/vnd.ms-excel.sheet.binary.macroEnabled.12": "x-office/spreadsheet",
18+
"application/vnd.ms-excel.sheet.macroEnabled.12": "x-office/spreadsheet",
19+
"application/vnd.ms-excel.template.macroEnabled.12": "x-office/spreadsheet",
20+
"application/vnd.ms-fontobject": "font",
21+
"application/vnd.ms-powerpoint": "x-office/presentation",
22+
"application/vnd.ms-powerpoint.addin.macroEnabled.12": "x-office/presentation",
23+
"application/vnd.ms-powerpoint.presentation.macroEnabled.12": "x-office/presentation",
24+
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12": "x-office/presentation",
25+
"application/vnd.ms-powerpoint.template.macroEnabled.12": "x-office/presentation",
26+
"application/vnd.ms-word.document.macroEnabled.12": "x-office/document",
27+
"application/vnd.ms-word.template.macroEnabled.12": "x-office/document",
28+
"application/vnd.oasis.opendocument.presentation": "x-office/presentation",
29+
"application/vnd.oasis.opendocument.presentation-template": "x-office/presentation",
30+
"application/vnd.oasis.opendocument.spreadsheet": "x-office/spreadsheet",
31+
"application/vnd.oasis.opendocument.spreadsheet-template": "x-office/spreadsheet",
32+
"application/vnd.oasis.opendocument.text": "x-office/document",
33+
"application/vnd.oasis.opendocument.text-master": "x-office/document",
34+
"application/vnd.oasis.opendocument.text-template": "x-office/document",
35+
"application/vnd.oasis.opendocument.text-web": "x-office/document",
36+
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "x-office/presentation",
37+
"application/vnd.openxmlformats-officedocument.presentationml.slideshow": "x-office/presentation",
38+
"application/vnd.openxmlformats-officedocument.presentationml.template": "x-office/presentation",
39+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "x-office/spreadsheet",
40+
"application/vnd.openxmlformats-officedocument.spreadsheetml.template": "x-office/spreadsheet",
41+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "x-office/document",
42+
"application/vnd.openxmlformats-officedocument.wordprocessingml.template": "x-office/document",
43+
"application/x-7z-compressed": "package/x-generic",
44+
"application/x-compressed": "package/x-generic",
45+
"application/x-dcraw": "image",
46+
"application/x-deb": "package/x-generic",
47+
"application/x-font": "font",
48+
"application/x-gimp": "image",
49+
"application/x-gzip": "package/x-generic",
50+
"application/x-perl": "text/code",
51+
"application/x-photoshop": "image",
52+
"application/x-php": "text/code",
53+
"application/x-rar-compressed": "package/x-generic",
54+
"application/x-tar": "package/x-generic",
55+
"application/x-tex": "text",
56+
"application/xml": "text/html",
57+
"application/yaml": "text/code",
58+
"application/zip": "package/x-generic",
59+
"image/svg+xml": "image/vector",
60+
"text/css": "text/code",
61+
"text/csv": "x-office/spreadsheet",
62+
"text/x-shellscript": "text/code"
63+
},
64+
"files": [
65+
"application",
66+
"application-epub+zip",
67+
"application-javascript",
68+
"application-pdf",
69+
"application-rss+xml",
70+
"application-x-cbr",
71+
"application-x-shockwave-flash",
72+
"audio",
73+
"database",
74+
"file",
75+
"folder",
76+
"folder-drag-accept",
77+
"folder-external",
78+
"folder-public",
79+
"folder-shared",
80+
"font",
81+
"image",
82+
"image-vector",
83+
"package-x-generic",
84+
"text",
85+
"text-calendar",
86+
"text-code",
87+
"text-html",
88+
"text-vcard",
89+
"text-x-c",
90+
"text-x-h",
91+
"text-x-python",
92+
"video",
93+
"web",
94+
"x-office-document",
95+
"x-office-presentation",
96+
"x-office-spreadsheet"
97+
]
98+
}

0 commit comments

Comments
 (0)