-
Notifications
You must be signed in to change notification settings - Fork 11
/
UC_helper_detectFileType.groovy
61 lines (52 loc) · 1.51 KB
/
UC_helper_detectFileType.groovy
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
/*** BEGIN META {
"name" : "UC_helper_detectFileType",
"comment" : "Identifies a file extension and returns a fileType label",
"parameters" : [ 'vMyFile'],
"core": "1.593",
"authors" : [
{ name : "Ioannis Moutsatsos" }
]
} END META**/
/*
Identifies a file extension and returns the fileContent type from
*/
choices=[]
fileContent='CSV_TABLE,TSV_TABLE,TEXT,HTML,IMAGE,PDF,BINARY'
def select=''
if (!binding.variables.containsKey("vMyFile") || vMyFile=='')
return ['Choose a file!']
def myFile=vMyFile
def imageExtensions = ['tif', 'tiff', 'png', 'jpeg', 'jpg', 'gif','bmp']
def binaryExtensions=['pdf','exe','doc','xls','xlsx','r','rdata']
def tabularExtensions=['csv','tsv']
def tabularData=['csv':'CSV_TABLE','tsv':'TSV_TABLE']
def textData=['txt':'TEXT','htm':'HTML','html':'HTML']
ext=getFileExtension(myFile).toLowerCase()
println ext
switch(ext){
case imageExtensions:
select='IMAGE'
break
case binaryExtensions:
select='BINARY'
break
case tabularData:
select=tabularData[ext]
break
case textData:
select=textData[ext]
break
default:
select='UNDEFINED'
}
/* one liner function returning the extension of a file */
def getFileExtension( String fileName){
theExt=fileName.lastIndexOf('.').with {it != -1 ? fileName[(it+1)..(fileName.length()-1)] : 'undefined'}
}
/* Select a value from the provided list based on the detected file type
selection is done by appending :selected to the value
*/
fileContent.replace(select,"$select:selected" ).split(',').each{
choices.add(it)
}
return choices