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 {string|Entry} file Reference to the file.
117 * Can be a name, a path, a url or a File API Entry.
118 * @return {string} The extension including a leading '.', or empty string if
121 FileType.getExtension = function(file) {
123 if (typeof file == 'object') {
124 if (file.isDirectory) {
125 // No extension for a directory.
128 fileName = file.name;
134 var extensionStartIndex = fileName.lastIndexOf('.');
135 if (extensionStartIndex == -1 || extensionStartIndex == fileName.length - 1) {
138 return fileName.substr(extensionStartIndex);
142 * Get the file type object for a given file.
144 * @param {string|Entry} file Reference to the file.
145 * Can be a name, a path, a url or a File API Entry.
146 * @return {Object} The matching file type object or an empty object.
148 FileType.getType = function(file) {
149 if (typeof file == 'object') {
150 if (file.isDirectory)
151 return FileType.DIRECTORY;
155 var types = FileType.types;
156 for (var i = 0; i < types.length; i++) {
157 if (types[i].pattern.test(file)) {
162 // Unknown file type.
163 var extension = FileType.getExtension(file);
164 if (extension == '') {
165 return { name: 'NO_EXTENSION_FILE_TYPE', type: 'UNKNOWN', icon: '' };
167 // subtype is the extension excluding the first dot.
168 return { name: 'GENERIC_FILE_TYPE', type: 'UNKNOWN',
169 subtype: extension.substr(1).toUpperCase(), icon: '' };
173 * @param {string|Entry} file Reference to the file.
174 * Can be a name, a path, a url or a File API Entry.
175 * @return {string} Localized string representation of file type.
177 FileType.getTypeString = function(file) {
178 var fileType = FileType.getType(file);
179 if (fileType.subtype)
180 return strf(fileType.name, fileType.subtype);
182 return str(fileType.name);
186 * Pattern for urls pointing to Google Drive files.
188 FileType.DRIVE_URL_PATTERN =
189 new RegExp('^filesystem:[^/]*://[^/]*/[^/]*/drive/(.*)');
192 * Pattern for file paths pointing to Google Drive files.
194 FileType.DRIVE_PATH_PATTERN =
195 new RegExp('^/drive/');
198 * @param {string|Entry} file The url string or entry.
199 * @return {boolean} Whether this provider supports the url.
201 FileType.isOnDrive = function(file) {
202 return typeof file == 'string' ?
203 FileType.DRIVE_URL_PATTERN.test(file) :
204 FileType.DRIVE_PATH_PATTERN.test(file.fullPath);
209 * Get the media type for a given file.
211 * @param {string|Entry} file Reference to the file.
212 * @return {string} The value of 'type' property from one of the elements in
213 * FileType.types or undefined.
215 FileType.getMediaType = function(file) {
216 return FileType.getType(file).type;
220 * @param {string|Entry} file Reference to the file.
221 * @return {boolean} True if audio file.
223 FileType.isAudio = function(file) {
224 return FileType.getMediaType(file) == 'audio';
228 * @param {string|Entry} file Reference to the file.
229 * @return {boolean} True if image file.
231 FileType.isImage = function(file) {
232 return FileType.getMediaType(file) == 'image';
236 * @param {string|Entry} file Reference to the file.
237 * @return {boolean} True if video file.
239 FileType.isVideo = function(file) {
240 return FileType.getMediaType(file) == 'video';
245 * Files with more pixels won't have preview.
246 * @param {string|Entry} file Reference to the file.
247 * @return {boolean} True if image or video.
249 FileType.isImageOrVideo = function(file) {
250 var type = FileType.getMediaType(file);
251 return type == 'image' || type == 'video';
255 * @param {string|Entry} file Reference to the file.
256 * @return {boolean} Returns true if the file is hosted.
258 FileType.isHosted = function(file) {
259 return FileType.getType(file).type === 'hosted';
263 * @param {string|Entry} file Reference to the file.
264 * @return {boolean} Returns true if the file is not hidden, and we should
267 FileType.isVisible = function(file) {
268 if (typeof file == 'object') {
272 var path = util.extractFilePath(file);
273 if (path) file = path;
275 file = file.split('/').pop();
276 return file.indexOf('.') != 0 && !(file in FileType.HIDDEN_NAMES);
280 * File/directory names that we know are usually hidden.
282 FileType.HIDDEN_NAMES = {
287 * @param {string|Entry} file Reference to the file.
288 * @return {string} Returns string that represents the file icon.
289 * It refers to a file 'images/filetype_' + icon + '.png'.
291 FileType.getIcon = function(file) {
292 var fileType = FileType.getType(file);
293 return fileType.icon || fileType.type || 'unknown';