1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * Namespace object for file type utility functions.
13 * Description of known file types.
14 * Pair type-subtype defines order when sorted by file type.
18 {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'JPEG',
19 pattern: /\.jpe?g$/i},
20 {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'BMP',
22 {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'GIF',
24 {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'ICO',
26 {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'PNG',
28 {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'WebP',
30 {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'TIFF',
31 pattern: /\.tiff?$/i},
34 {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: '3GP',
36 {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'AVI',
38 {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'QuickTime',
40 {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'MKV',
42 {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'MPEG',
43 pattern: /\.m(p4|4v|pg|peg|pg4|peg4)$/i},
44 {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'OGG',
45 pattern: /\.og(m|v|x)$/i},
46 {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'WebM',
50 {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'AMR',
52 {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'FLAC',
54 {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'MP3',
56 {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'MPEG',
58 {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'OGG',
59 pattern: /\.og(a|g)$/i},
60 {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'WAV',
64 {type: 'text', name: 'PLAIN_TEXT_FILE_TYPE', subtype: 'TXT',
68 {type: 'archive', name: 'ZIP_ARCHIVE_FILE_TYPE', subtype: 'ZIP',
70 {type: 'archive', name: 'RAR_ARCHIVE_FILE_TYPE', subtype: 'RAR',
72 {type: 'archive', name: 'TAR_ARCHIVE_FILE_TYPE', subtype: 'TAR',
74 {type: 'archive', name: 'TAR_BZIP2_ARCHIVE_FILE_TYPE', subtype: 'TBZ2',
75 pattern: /\.(tar\.bz2|tbz|tbz2)$/i},
76 {type: 'archive', name: 'TAR_GZIP_ARCHIVE_FILE_TYPE', subtype: 'TGZ',
77 pattern: /\.(tar\.|t)gz$/i},
80 {type: 'hosted', icon: 'gdoc', name: 'GDOC_DOCUMENT_FILE_TYPE',
81 subtype: 'doc', pattern: /\.gdoc$/i},
82 {type: 'hosted', icon: 'gsheet', name: 'GSHEET_DOCUMENT_FILE_TYPE',
83 subtype: 'sheet', pattern: /\.gsheet$/i},
84 {type: 'hosted', icon: 'gslides', name: 'GSLIDES_DOCUMENT_FILE_TYPE',
85 subtype: 'slides', pattern: /\.gslides$/i},
86 {type: 'hosted', icon: 'gdraw', name: 'GDRAW_DOCUMENT_FILE_TYPE',
87 subtype: 'draw', pattern: /\.gdraw$/i},
88 {type: 'hosted', icon: 'gtable', name: 'GTABLE_DOCUMENT_FILE_TYPE',
89 subtype: 'table', pattern: /\.gtable$/i},
90 {type: 'hosted', icon: 'glink', name: 'GLINK_DOCUMENT_FILE_TYPE',
91 subtype: 'glink', pattern: /\.glink$/i},
92 {type: 'hosted', icon: 'gform', name: 'GFORM_DOCUMENT_FILE_TYPE',
93 subtype: 'form', pattern: /\.gform$/i},
96 {type: 'document', icon: 'pdf', name: 'PDF_DOCUMENT_FILE_TYPE',
97 subtype: 'PDF', pattern: /\.pdf$/i},
98 {type: 'document', name: 'HTML_DOCUMENT_FILE_TYPE',
99 subtype: 'HTML', pattern: /\.(html?|mht|mhtml)$/i},
100 {type: 'document', icon: 'word', name: 'WORD_DOCUMENT_FILE_TYPE',
101 subtype: 'Word', pattern: /\.(doc|docx)$/i},
102 {type: 'document', icon: 'ppt', name: 'POWERPOINT_PRESENTATION_FILE_TYPE',
103 subtype: 'PPT', pattern: /\.(ppt|pptx)$/i},
104 {type: 'document', icon: 'excel', name: 'EXCEL_FILE_TYPE',
105 subtype: 'Excel', pattern: /\.(xls|xlsx)$/i}
109 * A special type for directory.
111 FileType.DIRECTORY = {name: 'FOLDER', type: '.folder', icon: 'folder'};
114 * Returns the file path extension for a given file.
116 * @param {Entry} entry Reference to the file.
117 * @return {string} The extension including a leading '.', or empty string if
120 FileType.getExtension = function(entry) {
121 // No extension for a directory.
122 if (entry.isDirectory)
125 var extensionStartIndex = entry.name.lastIndexOf('.');
126 if (extensionStartIndex === -1 ||
127 extensionStartIndex === entry.name.length - 1) {
131 return entry.name.substr(extensionStartIndex);
135 * Gets the file type object for a given file name (base name). Use getType()
136 * if possible, since this method can't recognize directories.
138 * @param {string} name Name of the file.
139 * @return {Object} The matching file type object or an empty object.
141 FileType.getTypeForName = function(name) {
142 var types = FileType.types;
143 for (var i = 0; i < types.length; i++) {
144 if (types[i].pattern.test(name))
148 // Unknown file type.
149 var extension = PathUtil.splitExtension(name)[1];
150 if (extension === '') {
151 return { name: 'NO_EXTENSION_FILE_TYPE', type: 'UNKNOWN', icon: '' };
153 // subtype is the extension excluding the first dot.
154 return { name: 'GENERIC_FILE_TYPE', type: 'UNKNOWN',
155 subtype: extension.substr(1).toUpperCase(), icon: '' };
159 * Gets the file type object for a given file.
160 * @param {Entry} entry Reference to the file.
161 * @return {Object} The matching file type object or an empty object.
163 FileType.getType = function(entry) {
164 if (entry.isDirectory)
165 return FileType.DIRECTORY;
167 var types = FileType.types;
168 for (var i = 0; i < types.length; i++) {
169 if (types[i].pattern.test(entry.name))
173 // Unknown file type.
174 var extension = FileType.getExtension(entry);
175 if (extension === '') {
176 return { name: 'NO_EXTENSION_FILE_TYPE', type: 'UNKNOWN', icon: '' };
178 // subtype is the extension excluding the first dot.
179 return { name: 'GENERIC_FILE_TYPE', type: 'UNKNOWN',
180 subtype: extension.substr(1).toUpperCase(), icon: '' };
184 * @param {Object} fileType Type object returned by FileType.getType().
185 * @return {string} Localized string representation of file type.
187 FileType.typeToString = function(fileType) {
188 if (fileType.subtype)
189 return strf(fileType.name, fileType.subtype);
191 return str(fileType.name);
195 * Gets the media type for a given file.
197 * @param {Entry} entry Reference to the file.
198 * @return {string} The value of 'type' property from one of the elements in
199 * FileType.types or undefined.
201 FileType.getMediaType = function(entry) {
202 return FileType.getType(entry).type;
206 * @param {Entry} entry Reference to the file.
207 * @return {boolean} True if audio file.
209 FileType.isAudio = function(entry) {
210 return FileType.getMediaType(entry) === 'audio';
214 * @param {Entry} entry Reference to the file.
215 * @return {boolean} True if image file.
217 FileType.isImage = function(entry) {
218 return FileType.getMediaType(entry) === 'image';
222 * @param {Entry} entry Reference to the file.
223 * @return {boolean} True if video file.
225 FileType.isVideo = function(entry) {
226 return FileType.getMediaType(entry) === 'video';
231 * Files with more pixels won't have preview.
232 * @param {Entry} entry Reference to the file.
233 * @return {boolean} True if image or video.
235 FileType.isImageOrVideo = function(entry) {
236 var type = FileType.getMediaType(entry);
237 return type === 'image' || type === 'video';
241 * @param {Entry} entry Reference to the file.
242 * @return {boolean} Returns true if the file is hosted.
244 FileType.isHosted = function(entry) {
245 return FileType.getType(entry).type === 'hosted';
249 * @param {Entry} entry Reference to the file.
250 * @return {string} Returns string that represents the file icon.
251 * It refers to a file 'images/filetype_' + icon + '.png'.
253 FileType.getIcon = function(entry) {
254 var fileType = FileType.getType(entry);
255 return fileType.icon || fileType.type || 'unknown';