You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.9 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2018 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_web_utils.mimetype', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var utils = require('web.utils');
  23. var QWeb = core.qweb;
  24. var _t = core._t;
  25. var mapping = [
  26. ['file-image-o', /^image\//],
  27. ['file-audio-o', /^audio\//],
  28. ['file-video-o', /^video\//],
  29. ['file-pdf-o', 'application/pdf'],
  30. ['file-text-o', 'text/plain'],
  31. ['file-code-o', [
  32. 'text/html',
  33. 'text/javascript',
  34. 'application/javascript'
  35. ]],
  36. ['file-archive-o', [
  37. /^application\/x-(g?tar|xz|compress|bzip2|g?zip)$/,
  38. /^application\/x-(7z|rar|zip)-compressed$/,
  39. /^application\/(zip|gzip|tar)$/
  40. ]],
  41. ['file-word-o', [
  42. /ms-?word/, 'application/vnd.oasis.opendocument.text',
  43. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  44. ]],
  45. ['file-powerpoint-o', [
  46. /ms-?powerpoint/,
  47. 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
  48. ]],
  49. ['file-excel-o', [
  50. /ms-?excel/,
  51. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  52. ]],
  53. ['file-o' ]
  54. ];
  55. function match(mimetype, cond) {
  56. if (Array.isArray(cond)) {
  57. return cond.reduce(function(v, c) {
  58. return v || match(mimetype, c);
  59. }, false);
  60. } else if (cond instanceof RegExp) {
  61. return cond.test(mimetype);
  62. } else if (cond === undefined) {
  63. return true;
  64. } else {
  65. return mimetype === cond;
  66. }
  67. }
  68. var cache = {};
  69. function resolve(mimetype) {
  70. if (cache[mimetype]) {
  71. return cache[mimetype];
  72. }
  73. for (var i = 0; i < mapping.length; i++) {
  74. if (match(mimetype, mapping[i][1])) {
  75. cache[mimetype] = mapping[i][0];
  76. return mapping[i][0];
  77. }
  78. }
  79. }
  80. function mimetype2fa(mimetype, options) {
  81. if (typeof mimetype === 'object') {
  82. options = mimetype;
  83. return function(mimetype) {
  84. return mimetype2fa(mimetype, options);
  85. };
  86. } else {
  87. var icon = resolve(mimetype);
  88. if (icon && options && options.prefix) {
  89. return options.prefix + icon;
  90. } else {
  91. return icon;
  92. }
  93. }
  94. }
  95. return {
  96. mimetype2fa: mimetype2fa,
  97. };
  98. });