• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5'use strict';
6
7/**
8 * Namespace object for file type utility functions.
9 */
10var FileType = {};
11
12/**
13 * Description of known file types.
14 * Pair type-subtype defines order when sorted by file type.
15 */
16FileType.types = [
17  // Images
18  {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'JPEG',
19   pattern: /\.jpe?g$/i},
20  {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'BMP',
21   pattern: /\.bmp$/i},
22  {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'GIF',
23   pattern: /\.gif$/i},
24  {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'ICO',
25   pattern: /\.ico$/i},
26  {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'PNG',
27   pattern: /\.png$/i},
28  {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'WebP',
29   pattern: /\.webp$/i},
30  {type: 'image', name: 'IMAGE_FILE_TYPE', subtype: 'TIFF',
31   pattern: /\.tiff?$/i},
32
33  // Video
34  {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: '3GP',
35   pattern: /\.3gp$/i},
36  {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'AVI',
37   pattern: /\.avi$/i},
38  {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'QuickTime',
39   pattern: /\.mov$/i},
40  {type: 'video', name: 'VIDEO_FILE_TYPE', subtype: 'MKV',
41   pattern: /\.mkv$/i},
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',
47   pattern: /\.webm$/i},
48
49  // Audio
50  {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'AMR',
51   pattern: /\.amr$/i},
52  {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'FLAC',
53   pattern: /\.flac$/i},
54  {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'MP3',
55   pattern: /\.mp3$/i},
56  {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'MPEG',
57   pattern: /\.m4a$/i},
58  {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'OGG',
59   pattern: /\.og(a|g)$/i},
60  {type: 'audio', name: 'AUDIO_FILE_TYPE', subtype: 'WAV',
61   pattern: /\.wav$/i},
62
63  // Text
64  {type: 'text', name: 'PLAIN_TEXT_FILE_TYPE', subtype: 'TXT',
65   pattern: /\.txt$/i},
66
67  // Archive
68  {type: 'archive', name: 'ZIP_ARCHIVE_FILE_TYPE', subtype: 'ZIP',
69   pattern: /\.zip$/i},
70  {type: 'archive', name: 'RAR_ARCHIVE_FILE_TYPE', subtype: 'RAR',
71   pattern: /\.rar$/i},
72  {type: 'archive', name: 'TAR_ARCHIVE_FILE_TYPE', subtype: 'TAR',
73   pattern: /\.tar$/i},
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},
78
79  // Hosted docs.
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},
94
95  // Others
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}
106];
107
108/**
109 * A special type for directory.
110 */
111FileType.DIRECTORY = {name: 'FOLDER', type: '.folder', icon: 'folder'};
112
113/**
114 * Returns the file path extension for a given file.
115 *
116 * @param {Entry} entry Reference to the file.
117 * @return {string} The extension including a leading '.', or empty string if
118 *     not found.
119 */
120FileType.getExtension = function(entry) {
121  // No extension for a directory.
122  if (entry.isDirectory)
123    return '';
124
125  var extensionStartIndex = entry.name.lastIndexOf('.');
126  if (extensionStartIndex === -1 ||
127      extensionStartIndex === entry.name.length - 1) {
128    return '';
129  }
130
131  return entry.name.substr(extensionStartIndex);
132};
133
134/**
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.
137 *
138 * @param {string} name Name of the file.
139 * @return {Object} The matching file type object or an empty object.
140 */
141FileType.getTypeForName = function(name) {
142  var types = FileType.types;
143  for (var i = 0; i < types.length; i++) {
144    if (types[i].pattern.test(name))
145      return types[i];
146  }
147
148  // Unknown file type.
149  var extension = util.splitExtension(name)[1];
150  if (extension === '') {
151    return { name: 'NO_EXTENSION_FILE_TYPE', type: 'UNKNOWN', icon: '' };
152  }
153  // subtype is the extension excluding the first dot.
154  return { name: 'GENERIC_FILE_TYPE', type: 'UNKNOWN',
155           subtype: extension.substr(1).toUpperCase(), icon: '' };
156};
157
158/**
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.
162 */
163FileType.getType = function(entry) {
164  if (entry.isDirectory)
165    return FileType.DIRECTORY;
166
167  var types = FileType.types;
168  for (var i = 0; i < types.length; i++) {
169    if (types[i].pattern.test(entry.name))
170      return types[i];
171  }
172
173  // Unknown file type.
174  var extension = FileType.getExtension(entry);
175  if (extension === '') {
176    return { name: 'NO_EXTENSION_FILE_TYPE', type: 'UNKNOWN', icon: '' };
177  }
178  // subtype is the extension excluding the first dot.
179  return { name: 'GENERIC_FILE_TYPE', type: 'UNKNOWN',
180           subtype: extension.substr(1).toUpperCase(), icon: '' };
181};
182
183/**
184 * @param {Object} fileType Type object returned by FileType.getType().
185 * @return {string} Localized string representation of file type.
186 */
187FileType.typeToString = function(fileType) {
188  if (fileType.subtype)
189    return strf(fileType.name, fileType.subtype);
190  else
191    return str(fileType.name);
192};
193
194/**
195 * Gets the media type for a given file.
196 *
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.
200 */
201FileType.getMediaType = function(entry) {
202  return FileType.getType(entry).type;
203};
204
205/**
206 * @param {Entry} entry Reference to the file.
207 * @return {boolean} True if audio file.
208 */
209FileType.isAudio = function(entry) {
210  return FileType.getMediaType(entry) === 'audio';
211};
212
213/**
214 * @param {Entry} entry Reference to the file.
215 * @return {boolean} True if image file.
216 */
217FileType.isImage = function(entry) {
218  return FileType.getMediaType(entry) === 'image';
219};
220
221/**
222 * @param {Entry} entry Reference to the file.
223 * @return {boolean} True if video file.
224 */
225FileType.isVideo = function(entry) {
226  return FileType.getMediaType(entry) === 'video';
227};
228
229
230/**
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.
234 */
235FileType.isImageOrVideo = function(entry) {
236  var type = FileType.getMediaType(entry);
237  return type === 'image' || type === 'video';
238};
239
240/**
241 * @param {Entry} entry Reference to the file.
242 * @return {boolean} Returns true if the file is hosted.
243 */
244FileType.isHosted = function(entry) {
245  return FileType.getType(entry).type === 'hosted';
246};
247
248/**
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'.
252 */
253FileType.getIcon = function(entry) {
254  var fileType = FileType.getType(entry);
255  return fileType.icon || fileType.type || 'unknown';
256};
257
258