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.

109 lines
2.9 KiB

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