Skip to content

Commit b4f782b

Browse files
author
Roeland Douma
committed
Merge pull request #16724 from owncloud/mimetype-js
Javascript to fetch mimetype icons
2 parents e84806f + 868bc42 commit b4f782b

File tree

11 files changed

+560
-4
lines changed

11 files changed

+560
-4
lines changed

apps/files_sharing/api/local.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public static function getAllShares($params) {
6969
if (\OC::$server->getPreviewManager()->isMimeSupported($share['mimetype'])) {
7070
$share['isPreviewAvailable'] = true;
7171
}
72-
$share['icon'] = substr(\OC_Helper::mimetypeIcon($share['mimetype']), 0, -3) . 'svg';
7372
}
7473

7574
if (!is_null($share['token'])) {

apps/files_sharing/js/sharedfilelist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
176176
.map(function(share) {
177177
var file = {
178178
id: share.file_source,
179-
icon: share.icon,
179+
icon: OC.MimeType.getIconUrl(share.mimetype),
180180
mimetype: share.mimetype
181181
};
182182
if (share.item_type === 'folder') {

config/mimetypealiases.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"_comment" : "When this file is changed make sure to run",
3+
"_comment2": "./occ maintenance:mimetypesjs",
4+
"_comment3": "Otherwise your update won't propagate through the system",
5+
6+
7+
"application/coreldraw": "image",
8+
"application/font-sfnt": "font",
9+
"application/font-woff": "font",
10+
"application/illustrator": "image/vector",
11+
"application/json": "text/code",
12+
"application/msaccess": "database",
13+
"application/msexcel": "x-office/spreadsheet",
14+
"application/mspowerpoint": "x-office/presentation",
15+
"application/msword": "x-office/document",
16+
"application/octet-stream": "file",
17+
"application/postscript": "image/vector",
18+
"application/vnd.android.package-archive": "package/x-generic",
19+
"application/vnd.ms-excel": "x-office/spreadsheet",
20+
"application/vnd.ms-excel.addin.macroEnabled.12": "x-office/spreadsheet",
21+
"application/vnd.ms-excel.sheet.binary.macroEnabled.12": "x-office/spreadsheet",
22+
"application/vnd.ms-excel.sheet.macroEnabled.12": "x-office/spreadsheet",
23+
"application/vnd.ms-excel.template.macroEnabled.12": "x-office/spreadsheet",
24+
"application/vnd.ms-fontobject": "font",
25+
"application/vnd.ms-powerpoint": "x-office/presentation",
26+
"application/vnd.ms-powerpoint.addin.macroEnabled.12": "x-office/presentation",
27+
"application/vnd.ms-powerpoint.presentation.macroEnabled.12": "x-office/presentation",
28+
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12": "x-office/presentation",
29+
"application/vnd.ms-powerpoint.template.macroEnabled.12": "x-office/presentation",
30+
"application/vnd.ms-word.document.macroEnabled.12": "x-office/document",
31+
"application/vnd.ms-word.template.macroEnabled.12": "x-office/document",
32+
"application/vnd.oasis.opendocument.presentation": "x-office/presentation",
33+
"application/vnd.oasis.opendocument.presentation-template": "x-office/presentation",
34+
"application/vnd.oasis.opendocument.spreadsheet": "x-office/spreadsheet",
35+
"application/vnd.oasis.opendocument.spreadsheet-template": "x-office/spreadsheet",
36+
"application/vnd.oasis.opendocument.text": "x-office/document",
37+
"application/vnd.oasis.opendocument.text-master": "x-office/document",
38+
"application/vnd.oasis.opendocument.text-template": "x-office/document",
39+
"application/vnd.oasis.opendocument.text-web": "x-office/document",
40+
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "x-office/presentation",
41+
"application/vnd.openxmlformats-officedocument.presentationml.slideshow": "x-office/presentation",
42+
"application/vnd.openxmlformats-officedocument.presentationml.template": "x-office/presentation",
43+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "x-office/spreadsheet",
44+
"application/vnd.openxmlformats-officedocument.spreadsheetml.template": "x-office/spreadsheet",
45+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "x-office/document",
46+
"application/vnd.openxmlformats-officedocument.wordprocessingml.template": "x-office/document",
47+
"application/x-7z-compressed": "package/x-generic",
48+
"application/x-compressed": "package/x-generic",
49+
"application/x-dcraw": "image",
50+
"application/x-deb": "package/x-generic",
51+
"application/x-font": "font",
52+
"application/x-gimp": "image",
53+
"application/x-gzip": "package/x-generic",
54+
"application/x-perl": "text/code",
55+
"application/x-photoshop": "image",
56+
"application/x-php": "text/code",
57+
"application/x-rar-compressed": "package/x-generic",
58+
"application/x-tar": "package/x-generic",
59+
"application/x-tex": "text",
60+
"application/xml": "text/html",
61+
"application/yaml": "text/code",
62+
"application/zip": "package/x-generic",
63+
"image/svg+xml": "image/vector",
64+
"text/css": "text/code",
65+
"text/csv": "x-office/spreadsheet",
66+
"text/x-shellscript": "text/code"
67+
}
68+
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
*
4+
* @copyright Copyright (c) 2015, ownCloud, Inc.
5+
* @license AGPL-3.0
6+
*
7+
* This code is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License, version 3,
9+
* as published by the Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License, version 3,
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>
18+
*
19+
*/
20+
21+
namespace OC\Core\Command\Maintenance;
22+
23+
use Symfony\Component\Console\Command\Command;
24+
use Symfony\Component\Console\Input\InputInterface;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
27+
class MimeTypesJS extends Command {
28+
protected function configure() {
29+
$this
30+
->setName('maintenance:mimetypesjs')
31+
->setDescription('Update mimetypelist.js');
32+
}
33+
34+
protected function execute(InputInterface $input, OutputInterface $output) {
35+
// Fetch all the aliases
36+
$aliases = json_decode(file_get_contents(dirname(__DIR__) . '/../../config/mimetypealiases.json'), true);
37+
38+
// Remove comments
39+
$keys = array_filter(array_keys($aliases), function($k) {
40+
return $k[0] === '_';
41+
});
42+
foreach($keys as $key) {
43+
unset($aliases[$key]);
44+
}
45+
46+
// Fetch all files
47+
$dir = new \DirectoryIterator(dirname(__DIR__) . '/../img/filetypes');
48+
49+
$files = [];
50+
foreach($dir as $fileInfo) {
51+
if ($fileInfo->isFile()) {
52+
$file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename());
53+
$files[] = $file;
54+
}
55+
}
56+
57+
//Remove duplicates
58+
$files = array_values(array_unique($files));
59+
60+
61+
// Fetch all themes!
62+
$themes = [];
63+
$dirs = new \DirectoryIterator(dirname(__DIR__) . '/../../themes/');
64+
foreach($dirs as $dir) {
65+
//Valid theme dir
66+
if ($dir->isFile() || $dir->isDot()) {
67+
continue;
68+
}
69+
70+
$theme = $dir->getFilename();
71+
$themeDir = $dir->getPath() . '/' . $theme . '/core/img/filetypes/';
72+
// Check if this theme has its own filetype icons
73+
if (!file_exists($themeDir)) {
74+
continue;
75+
}
76+
77+
$themes[$theme] = [];
78+
// Fetch all the theme icons!
79+
$themeIt = new \DirectoryIterator($themeDir);
80+
foreach ($themeIt as $fileInfo) {
81+
if ($fileInfo->isFile()) {
82+
$file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename());
83+
$themes[$theme][] = $file;
84+
}
85+
}
86+
87+
//Remove Duplicates
88+
$themes[$theme] = array_values(array_unique($themes[$theme]));
89+
}
90+
91+
//Generate the JS
92+
$js = '/**
93+
* This file is automatically generated
94+
* DO NOT EDIT MANUALLY!
95+
*
96+
* You can update the list of MimeType Aliases in config/mimetypealiases.json
97+
* The list of files is fetched from core/img/filetypes
98+
* To regenerate this file run ./occ maintenance:mimetypesjs
99+
*/
100+
OC.MimeTypeList={
101+
aliases: ' . json_encode($aliases, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . ',
102+
files: ' . json_encode($files, JSON_PRETTY_PRINT) . ',
103+
themes: ' . json_encode($themes, JSON_PRETTY_PRINT) . '
104+
};
105+
';
106+
107+
//Output the JS
108+
file_put_contents(dirname(__DIR__) . '/../js/mimetypelist.js', $js);
109+
110+
$output->writeln('<info>mimetypelist.js is updated');
111+
}
112+
}

core/js/config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@
128128
'slogan' => $defaults->getSlogan(),
129129
'logoClaim' => $defaults->getLogoClaim(),
130130
'shortFooter' => $defaults->getShortFooter(),
131-
'longFooter' => $defaults->getLongFooter()
131+
'longFooter' => $defaults->getLongFooter(),
132+
'folder' => OC_Util::getTheme(),
132133
)
133134
)
134135
);

core/js/core.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"multiselect.js",
2828
"oc-requesttoken.js",
2929
"setupchecks.js",
30-
"../search/js/search.js"
30+
"../search/js/search.js",
31+
"mimetype.js",
32+
"mimetypelist.js"
3133
]
3234
}

core/js/mimetype.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @author Roeland Jago Douma <roeland@famdouma.nl>
3+
*
4+
* @copyright Copyright (c) 2015, ownCloud, Inc.
5+
* @license AGPL-3.0
6+
*
7+
* This code is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License, version 3,
9+
* as published by the Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License, version 3,
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>
18+
*
19+
*/
20+
21+
/**
22+
* Namespace to hold functions related to convert mimetype to icons
23+
*
24+
* @namespace
25+
*/
26+
OC.MimeType = {
27+
28+
/**
29+
* Cache that maps mimeTypes to icon urls
30+
*/
31+
_mimeTypeIcons: {},
32+
33+
/**
34+
* Return the file icon we want to use for the given mimeType.
35+
* The file needs to be present in the supplied file list
36+
*
37+
* @param {string} mimeType The mimeType we want an icon for
38+
* @param {array} files The available icons in this theme
39+
* @return {string} The icon to use or null if there is no match
40+
*/
41+
_getFile: function(mimeType, files) {
42+
var icon = mimeType.replace(new RegExp('/', 'g'), '-');
43+
44+
// Generate path
45+
if (mimeType === 'dir' && $.inArray('folder', files) !== -1) {
46+
return 'folder';
47+
} else if (mimeType === 'dir-shared' && $.inArray('folder-shared', files) !== -1) {
48+
return 'folder-shared';
49+
} else if (mimeType === 'dir-external' && $.inArray('folder-external', files) !== -1) {
50+
return 'folder-external';
51+
} else if ($.inArray(icon, files) !== -1) {
52+
return icon;
53+
} else if ($.inArray(icon.split('-')[0], files) !== -1) {
54+
return icon.split('-')[0];
55+
} else if ($.inArray('file', files) !== -1) {
56+
return 'file';
57+
}
58+
59+
return null;
60+
},
61+
62+
/**
63+
* Return the url to icon of the given mimeType
64+
*
65+
* @param {string} mimeType The mimeType to get the icon for
66+
* @return {string} Url to the icon for mimeType
67+
*/
68+
getIconUrl: function(mimeType) {
69+
if (_.isUndefined(mimeType)) {
70+
return undefined;
71+
}
72+
73+
if (mimeType in OC.MimeTypeList.aliases) {
74+
mimeType = OC.MimeTypeList.aliases[mimeType];
75+
}
76+
if (mimeType in OC.MimeType._mimeTypeIcons) {
77+
return OC.MimeType._mimeTypeIcons[mimeType];
78+
}
79+
80+
// First try to get the correct icon from the current theme
81+
var gotIcon = null;
82+
var path = '';
83+
if (OC.theme.folder !== '' && $.isArray(OC.MimeTypeList.themes[OC.theme.folder])) {
84+
path = OC.webroot + '/themes/' + OC.theme.folder + '/core/img/filetypes/';
85+
var icon = OC.MimeType._getFile(mimeType, OC.MimeTypeList.themes[OC.theme.folder]);
86+
87+
if (icon !== null) {
88+
gotIcon = true;
89+
path += icon;
90+
}
91+
}
92+
93+
// If we do not yet have an icon fall back to the default
94+
if (gotIcon === null) {
95+
path = OC.webroot + '/core/img/filetypes/';
96+
path += OC.MimeType._getFile(mimeType, OC.MimeTypeList.files);
97+
}
98+
99+
// Use svg if we can
100+
if(OC.Util.hasSVGSupport()){
101+
path += '.svg';
102+
} else {
103+
path += '.png';
104+
}
105+
106+
// Cache the result
107+
OC.MimeType._mimeTypeIcons[mimeType] = path;
108+
return path;
109+
}
110+
111+
};
112+
113+

0 commit comments

Comments
 (0)