mime.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. export class MimeService {
  18. public static $inject = ['$log', '$translate'];
  19. private $log: ng.ILogService;
  20. private $translate: ng.translate.ITranslateService;
  21. private imageMimeTypes: string[] = ['image/png', 'image/jpg', 'image/jpeg'];
  22. private audioMimeTypes: string[] = ['audio/ogg'];
  23. private videoMimeTypes: string[] = ['video/mp4', 'video/mpg', 'video/mpeg'];
  24. constructor($log: ng.ILogService, $translate: ng.translate.ITranslateService) {
  25. this.$log = $log;
  26. this.$translate = $translate;
  27. }
  28. public isImage(mimeType: string): boolean {
  29. return this.is(mimeType, this.imageMimeTypes);
  30. }
  31. public isAudio(mimeType: string): boolean {
  32. return this.is(mimeType, this.audioMimeTypes);
  33. }
  34. public isVideo(mimeType: string): boolean {
  35. return this.is(mimeType, this.videoMimeTypes);
  36. }
  37. public getLabel(mimeType: string): string {
  38. const key = this.getKey(mimeType);
  39. if (key !== null) {
  40. return this.$translate.instant('mimeTypes.' + key);
  41. }
  42. return mimeType;
  43. }
  44. public getIconUrl(mimeType: string): string {
  45. let key = this.getKey(mimeType);
  46. if (key === undefined || key === null || key.length === 0) {
  47. key = 'generic';
  48. }
  49. return 'img/mime/ic_doc_' + key + '.png';
  50. }
  51. private getKey(mimeType: string): string {
  52. if (mimeType.startsWith('audio/')) {
  53. return 'audio';
  54. }
  55. if (mimeType.startsWith('video/')) {
  56. return 'video';
  57. }
  58. switch (mimeType) {
  59. case 'application/vnd.android.package-archive':
  60. return 'apk';
  61. case 'application/ogg':
  62. case 'application/x-flac':
  63. case 'application/mpeg3':
  64. case 'application/x-mpeg-3':
  65. return 'audio';
  66. case 'application/pgp-keys':
  67. case 'application/pgp-signature':
  68. case 'application/x-pkcs12':
  69. case 'application/x-pkcs7-certreqresp':
  70. case 'application/x-pkcs7-crl':
  71. case 'application/x-x509-ca-cert':
  72. case 'application/x-x509-user-cert':
  73. case 'application/x-pkcs7-certificates':
  74. case 'application/x-pkcs7-mime':
  75. case 'application/x-pkcs7-signature':
  76. return 'certificate';
  77. case 'application/rdf+xml':
  78. case 'application/rss+xml':
  79. case 'application/x-object':
  80. case 'application/xhtml+xml':
  81. case 'text/css':
  82. case 'text/html':
  83. case 'text/xml':
  84. case 'text/x-c++hdr':
  85. case 'text/x-c++src':
  86. case 'text/x-chdr':
  87. case 'text/x-csrc':
  88. case 'text/x-dsrc':
  89. case 'text/x-csh':
  90. case 'text/x-haskell':
  91. case 'text/x-java':
  92. case 'text/x-literate-haskell':
  93. case 'text/x-pascal':
  94. case 'text/x-tcl':
  95. case 'text/x-tex':
  96. case 'application/x-latex':
  97. case 'application/x-texinfo':
  98. case 'application/atom+xml':
  99. case 'application/ecmascript':
  100. case 'application/json':
  101. case 'application/javascript':
  102. case 'application/xml':
  103. case 'text/javascript':
  104. case 'application/x-javascript':
  105. return 'codes';
  106. case 'application/mac-binhex40':
  107. case 'application/rar':
  108. case 'application/zip':
  109. case 'application/x-apple-diskimage':
  110. case 'application/x-debian-package':
  111. case 'application/x-gtar':
  112. case 'application/x-iso9660-image':
  113. case 'application/x-lha':
  114. case 'application/x-lzh':
  115. case 'application/x-lzx':
  116. case 'application/x-stuffit':
  117. case 'application/x-tar':
  118. case 'application/x-webarchive':
  119. case 'application/x-webarchive-xml':
  120. case 'application/gzip':
  121. case 'application/x-7z-compressed':
  122. case 'application/x-deb':
  123. case 'application/x-rar-compressed':
  124. return 'compressed';
  125. case 'text/x-vcard':
  126. case 'text/vcard':
  127. return 'contact';
  128. case 'text/calendar':
  129. case 'text/x-vcalendar':
  130. return 'event';
  131. case 'application/x-font':
  132. case 'application/font-woff':
  133. case 'application/x-font-woff':
  134. case 'application/x-font-ttf':
  135. return 'font';
  136. case 'application/vnd.oasis.opendocument.graphics':
  137. case 'application/vnd.oasis.opendocument.graphics-template':
  138. case 'application/vnd.oasis.opendocument.image':
  139. case 'application/vnd.stardivision.draw':
  140. case 'application/vnd.sun.xml.draw':
  141. case 'application/vnd.sun.xml.draw.template':
  142. case 'image/jpg':
  143. case 'image/jpeg':
  144. case 'image/png':
  145. case 'image/gif':
  146. return 'image';
  147. case 'application/pdf':
  148. return 'pdf';
  149. case 'application/vnd.ms-powerpoint':
  150. case 'application/vnd.oasis.opendocument.presentation':
  151. case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
  152. case 'application/vnd.openxmlformats-officedocument.presentationml.slideshow':
  153. case 'application/vnd.openxmlformats-officedocument.presentationml.template':
  154. case 'application/vnd.stardivision.impress':
  155. case 'application/vnd.sun.xml.impress':
  156. case 'application/vnd.sun.xml.impress.template':
  157. case 'application/x-kpresenter':
  158. return 'presentation';
  159. case 'application/vnd.oasis.opendocument.spreadsheet':
  160. case 'application/vnd.oasis.opendocument.spreadsheet-template':
  161. case 'application/vnd.stardivision.calc':
  162. case 'application/vnd.sun.xml.calc':
  163. case 'application/vnd.sun.xml.calc.template':
  164. case 'application/x-kspread':
  165. case 'application/vnd.ms-excel':
  166. case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
  167. case 'application/vnd.openxmlformats-officedocument.spreadsheetml.template':
  168. return 'spreadsheet';
  169. case 'application/vnd.oasis.opendocument.text':
  170. case 'application/vnd.oasis.opendocument.text-master':
  171. case 'application/vnd.oasis.opendocument.text-template':
  172. case 'application/vnd.oasis.opendocument.text-web':
  173. case 'application/vnd.stardivision.writer':
  174. case 'application/vnd.stardivision.writer-global':
  175. case 'application/vnd.sun.xml.writer':
  176. case 'application/vnd.sun.xml.writer.global':
  177. case 'application/vnd.sun.xml.writer.template':
  178. case 'application/x-abiword':
  179. case 'application/x-kword':
  180. return 'text';
  181. case 'application/x-quicktimeplayer':
  182. case 'application/x-shockwave-flash':
  183. return 'video';
  184. case 'application/msword':
  185. case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
  186. case 'application/vnd.openxmlformats-officedocument.wordprocessingml.template':
  187. return 'word';
  188. default:
  189. return null;
  190. }
  191. }
  192. private is(mimeType, possibleTypes: string[]): boolean {
  193. return mimeType !== undefined
  194. && possibleTypes.indexOf(mimeType) !== -1;
  195. }
  196. }