2018-05-20 17:49:42 +02:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
import { detectFileType } from "../lib/FileType.mjs" ;
import { FILE _SIGNATURES } from "../lib/FileSignatures.mjs" ;
2018-05-20 17:49:42 +02:00
2019-10-04 18:52:09 +02:00
// Concat all supported extensions into a single flat list
const exts = [ ] . concat . apply ( [ ] , Object . keys ( FILE _SIGNATURES ) . map ( cat =>
[ ] . concat . apply ( [ ] , FILE _SIGNATURES [ cat ] . map ( sig =>
sig . extension . split ( "," )
) )
) ) . unique ( ) . sort ( ) . join ( ", " ) ;
2018-05-20 17:49:42 +02:00
/ * *
* Detect File Type operation
* /
class DetectFileType extends Operation {
/ * *
* DetectFileType constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Detect File Type" ;
this . module = "Default" ;
Added support for psb, hdr, arw, raf, mrw, bct, mdi, au, auf, aif, aifc, arj, ace, hqx, alz, kgb, and torrent file detection.
2019-07-04 19:43:38 +02:00
this . description = "Attempts to guess the MIME (Multipurpose Internet Mail Extensions) type of the data based on 'magic bytes'.<br><br>Currently supports the following file types: " +
2019-10-04 18:52:09 +02:00
exts + "." ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/List_of_file_signatures" ;
2018-05-20 17:49:42 +02:00
this . inputType = "ArrayBuffer" ;
this . outputType = "string" ;
2019-01-14 19:55:10 +01:00
this . args = Object . keys ( FILE _SIGNATURES ) . map ( cat => {
return {
name : cat ,
type : "boolean" ,
value : true
} ;
} ) ;
2018-05-20 17:49:42 +02:00
}
/ * *
* @ param { ArrayBuffer } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
const data = new Uint8Array ( input ) ,
2019-01-14 19:55:10 +01:00
categories = [ ] ;
args . forEach ( ( cat , i ) => {
if ( cat ) categories . push ( Object . keys ( FILE _SIGNATURES ) [ i ] ) ;
} ) ;
const types = detectFileType ( data , categories ) ;
2018-05-20 17:49:42 +02:00
2018-12-18 18:44:42 +01:00
if ( ! types . length ) {
2018-05-20 17:49:42 +02:00
return "Unknown file type. Have you tried checking the entropy of this data to determine whether it might be encrypted or compressed?" ;
} else {
Added support for psb, hdr, arw, raf, mrw, bct, mdi, au, auf, aif, aifc, arj, ace, hqx, alz, kgb, and torrent file detection.
2019-07-04 19:43:38 +02:00
const results = types . map ( type => {
let output = ` File type: ${ type . name }
Extension : $ { type . extension }
MIME type : $ { type . mime } \ n ` ;
2018-12-18 18:44:42 +01:00
2022-10-28 14:24:03 +02:00
if ( type ? . description ? . length ) {
Added support for psb, hdr, arw, raf, mrw, bct, mdi, au, auf, aif, aifc, arj, ace, hqx, alz, kgb, and torrent file detection.
2019-07-04 19:43:38 +02:00
output += ` Description: ${ type . description } \n ` ;
2018-12-18 18:44:42 +01:00
}
Added support for psb, hdr, arw, raf, mrw, bct, mdi, au, auf, aif, aifc, arj, ace, hqx, alz, kgb, and torrent file detection.
2019-07-04 19:43:38 +02:00
return output ;
2018-12-18 18:44:42 +01:00
} ) ;
2018-05-20 17:49:42 +02:00
Added support for psb, hdr, arw, raf, mrw, bct, mdi, au, auf, aif, aifc, arj, ace, hqx, alz, kgb, and torrent file detection.
2019-07-04 19:43:38 +02:00
return results . join ( "\n" ) ;
2018-05-20 17:49:42 +02:00
}
}
}
export default DetectFileType ;