本文整理汇总了TypeScript中file-type类的典型用法代码示例。如果您正苦于以下问题:TypeScript file-type类的具体用法?TypeScript file-type怎么用?TypeScript file-type使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了file-type类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: detect
export function detect(filename: string=null, buffer: Buffer=null): DetectionResult | null {
if (filename !== null) {
const mimeType = filenameToTextMimetype(filename);
if (mimeType !== null) {
return {mimeType, charset: null };
}
const imageMimeType = mimeTypes.lookup(filename);
if (imageMimeType !== null && imageMimeType !== false) {
return { mimeType: imageMimeType, charset: null };
}
}
// Check the data directly.
if (buffer !== null) {
const fileTypeResult = filetype(buffer);
if (fileTypeResult !== null) {
return {mimeType: fileTypeResult.mime, charset: null };
}
if ( ! isNotText(buffer)) {
const result = jschardet.detect(buffer.slice(0, SAMPLE_SIZE).toString());
if (result.encoding !== null && result.confidence > 0.8) {
return { mimeType: "text/plain", charset: result.encoding };
}
}
}
return { mimeType: "application/octet-stream", charset: null };
}
示例2: fileType
return getBuffer.then(data => {
headers["Content-Type"] = contentType || fileType(data).mime;
headers["Content-Length"] = data.length;
headers["User-Agent"] = userAgent;
return axios
.post(url, data, { headers })
.then(res => res.data)
.catch(wrapError);
});
示例3: fileType
return getBuffer.then(data => {
return this.instance
.post(url, data, {
headers: {
"Content-Type": contentType || fileType(data).mime,
"Content-Length": data.length,
},
})
.then(res => res.data);
});
示例4: excludeBinaryFile
function excludeBinaryFile(file: eclint.IEditorConfigLintFile) {
return !(file && file.isBuffer() && fileType(file.contents));
}
示例5: openFile
export async function openFile(data: any){
let file_path = paths.projects+data.currentProject+'/'+data.newFile;
try{
var file_stat = await file_manager.stat_file(file_path);
}
catch(e){
// if we are trying to open an example or template and we can't find the file, we are (probably)
// trying to open a pd or supercollider project, so open _main* if it exists instead
if (typeof data.exampleName !== 'undefined' || data.func === 'newProject'){
for(let file of data.fileList){
if (file.name.includes('_main')){
data.newFile = file.name;
await openFile(data);
return;
}
}
}
data.error = 'error opening file '+data.newFile+': '+e.toString();
data.fileData = 'Error opening file. Please open a different file to continue';
data.fileName = data.newFile;
data.newFile = undefined;
data.readOnly = true;
data.fileType = 0;
return;
}
if (file_stat.size > max_file_size){
data.error = 'file is too large: '+(file_stat.size/1000000)+'Mb';
data.fileData = "The IDE can't open files larger than "+(max_file_size/1000000)+"Mb";
data.readOnly = true;
data.fileName = data.newFile;
data.newFile = undefined;
data.fileType = 0;
return;
}
let chunk: Buffer = await readChunk(file_path, 0, 4100);
let file_type = await fileType(chunk);
if (file_type && (file_type.mime.includes('image') || file_type.mime.includes('audio'))){
await file_manager.empty_directory(paths.media);
await file_manager.make_symlink(file_path, paths.media+data.newFile);
data.fileData = '';
data.readOnly = true;
data.fileName = data.newFile;
data.newFile = undefined;
data.fileType = file_type.mime;
return;
}
let is_binary = await file_manager.is_binary(file_path);
if (is_binary){
data.error = 'can\'t open binary files';
data.fileData = 'Binary files can not be edited in the IDE';
data.fileName = data.newFile;
data.newFile = undefined;
data.readOnly = true;
data.fileType = 0;
return;
}
try{
data.fileData = await file_manager.read_file(file_path);
}
catch(e){
data.error = 'error opening file '+data.newFile+': '+e.toString();
data.fileData = 'Error opening file. Please open a different file to continue';
data.fileName = data.newFile;
data.newFile = undefined;
data.readOnly = true;
data.fileType = 0;
return;
}
if (data.newFile.split && data.newFile.includes('.')){
data.fileType = data.newFile.split('.').pop();
} else {
data.fileType = 0;
}
data.fileName = data.newFile;
data.newFile = undefined;
data.readOnly = false;
return;
}