Repository navigation
Expand file tree
/
Copy pathfile_detector.js
More file actions
150 lines (130 loc) · 3.55 KB
/
Copy pathfile_detector.js
File metadata and controls
150 lines (130 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* @fileoverview File detector for the Coding with Chrome editor.
*
* @license Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author mbordihn@google.com (Markus Bordihn)
*/
goog.provide('cwc.file.detector');
goog.require('cwc.file.Type');
goog.require('cwc.fileFormat.FILE_HEADER');
/**
* @param {string|Object=} opt_content
* @param {string=} opt_filename
* @constructor
* @struct
* @final
*/
cwc.file.detector = function(opt_content, opt_filename) {
if (opt_content) {
return cwc.file.detector.detectType(opt_content,
opt_filename);
}
};
/**
* Detects the file type of the given content and/or file_name.
*
* @param {!string|Object} content
* @param {string=} opt_filename
* @return {!cwc.file.Type}
*/
cwc.file.detector.detectType = function(content, opt_filename) {
var filename = opt_filename || 'unknown';
var data = content;
var jsonData = cwc.file.detector.getJsonData(content);
if (jsonData) {
// JSON file
var jsonFormat = jsonData['format'] || '';
if (jsonFormat == cwc.fileFormat.FILE_HEADER) {
return jsonData['type'] || cwc.file.Type.UNKNOWN;
} else {
return cwc.file.Type.JSON;
}
} else if (cwc.file.detector.isValidString(data)) {
// CoffeeScript file
if (filename.endsWith('.coffee')) {
return cwc.file.Type.COFFEESCRIPT;
// Python file
} else if (filename.endsWith('.py')) {
return cwc.file.Type.PYTHON;
// HMTL file
} else if (data.includes('<html') ||
filename.endsWith('.htm') ||
filename.endsWith('.html')) {
return cwc.file.Type.HTML;
// JavaScript file
} else if (data.includes('document.body') ||
data.includes('document.getElementById') ||
filename.endsWith('.js')) {
return cwc.file.Type.JAVASCRIPT;
}
// Text as default
return cwc.file.Type.TEXT;
}
console.warn('Unable to detect file type.');
console.warn('Filename:', filename);
console.warn('Content:', data);
return cwc.file.Type.RAW;
};
/**
* @param {Object|string} content
* @return {*}
*/
cwc.file.detector.getJsonData = function(content) {
var jsonData = null;
if (typeof content == 'object') {
return content;
} else {
try {
jsonData = JSON.parse(content);
} catch (error) {
return null;
}
}
return jsonData;
};
/**
* @param {string} content
* @return {boolean}
*/
cwc.file.detector.isChrogFile = function(content) {
var jsonData = cwc.file.detector.getJsonData(content);
if (jsonData) {
var jsonFormat = jsonData['format'] || '';
if (jsonFormat == cwc.fileFormat.FILE_HEADER) {
return true;
}
}
return false;
};
/**
* @param {Object|string|null} content
* @return {boolean}
*/
cwc.file.detector.isValidJson = function(content) {
try {
JSON.parse(content);
} catch (error) {
return false;
}
return true;
};
/**
* @param {Object|string|null} content
* @return {boolean}
*/
cwc.file.detector.isValidString = function(content) {
return goog.isString(content);
};