Add content selection before removing content
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / FileUtil.js
1 /**
2 * Provides helper functions for file handling.
3 *
4 * @author Matthias Schmidt
5 * @copyright 2001-2018 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/FileUtil
8 */
9 define(['Dictionary', 'StringUtil'], function(Dictionary, StringUtil) {
10 "use strict";
11
12 var _fileExtensionIconMapping = Dictionary.fromObject({
13 // archive
14 zip: 'archive',
15 rar: 'archive',
16 tar: 'archive',
17 gz: 'archive',
18
19 // audio
20 mp3: 'audio',
21 ogg: 'audio',
22 wav: 'audio',
23
24 // code
25 php: 'code',
26 html: 'code',
27 htm: 'code',
28 tpl: 'code',
29 js: 'code',
30
31 // excel
32 xls: 'excel',
33 ods: 'excel',
34 xlsx: 'excel',
35
36 // image
37 gif: 'image',
38 jpg: 'image',
39 jpeg: 'image',
40 png: 'image',
41 bmp: 'image',
42
43 // video
44 avi: 'video',
45 wmv: 'video',
46 mov: 'video',
47 mp4: 'video',
48 mpg: 'video',
49 mpeg: 'video',
50 flv: 'video',
51
52 // pdf
53 pdf: 'pdf',
54
55 // powerpoint
56 ppt: 'powerpoint',
57 pptx: 'powerpoint',
58
59 // text
60 txt: 'text',
61
62 // word
63 doc: 'word',
64 docx: 'word',
65 odt: 'word'
66 });
67
68 return {
69 /**
70 * Formats the given filesize.
71 *
72 * @param {integer} byte number of bytes
73 * @param {integer} precision number of decimals
74 * @return {string} formatted filesize
75 */
76 formatFilesize: function(byte, precision) {
77 if (precision === undefined) {
78 precision = 2;
79 }
80
81 var symbol = 'Byte';
82 if (byte >= 1000) {
83 byte /= 1000;
84 symbol = 'kB';
85 }
86 if (byte >= 1000) {
87 byte /= 1000;
88 symbol = 'MB';
89 }
90 if (byte >= 1000) {
91 byte /= 1000;
92 symbol = 'GB';
93 }
94 if (byte >= 1000) {
95 byte /= 1000;
96 symbol = 'TB';
97 }
98
99 return StringUtil.formatNumeric(byte, -precision) + ' ' + symbol;
100 },
101
102 /**
103 * Returns the icon name for given filename.
104 *
105 * Note: For any file icon name like `fa-file-word`, only `word`
106 * will be returned by this method.
107 *
108 * @parsm {string} filename name of file for which icon name will be returned
109 * @return {string} FontAwesome icon name
110 */
111 getIconNameByFilename: function(filename) {
112 var lastDotPosition = filename.lastIndexOf('.');
113 if (lastDotPosition !== false) {
114 var extension = filename.substr(lastDotPosition + 1);
115
116 if (_fileExtensionIconMapping.has(extension)) {
117 return _fileExtensionIconMapping.get(extension);
118 }
119 }
120
121 return '';
122 }
123 };
124 });