28
28
import edu .harvard .iq .dataverse .DatasetVersion ;
29
29
import edu .harvard .iq .dataverse .Embargo ;
30
30
import edu .harvard .iq .dataverse .FileMetadata ;
31
- import edu .harvard .iq .dataverse .TermsOfUseAndAccess ;
32
31
import edu .harvard .iq .dataverse .dataaccess .DataAccess ;
33
32
import edu .harvard .iq .dataverse .dataaccess .ImageThumbConverter ;
34
33
import edu .harvard .iq .dataverse .dataaccess .S3AccessIO ;
53
52
import static edu .harvard .iq .dataverse .util .xml .html .HtmlFormatUtil .formatLink ;
54
53
import static edu .harvard .iq .dataverse .util .xml .html .HtmlFormatUtil .formatTableCellAlignRight ;
55
54
import static edu .harvard .iq .dataverse .util .xml .html .HtmlFormatUtil .formatTableRow ;
56
- import java . awt . image . BufferedImage ;
55
+
57
56
import java .io .BufferedInputStream ;
58
57
import java .io .File ;
59
58
import java .io .FileInputStream ;
76
75
import java .text .MessageFormat ;
77
76
import java .text .SimpleDateFormat ;
78
77
import java .time .LocalDate ;
79
- import java .time .format .DateTimeFormatter ;
80
78
import java .util .Map ;
81
79
import java .util .MissingResourceException ;
82
80
import java .util .ArrayList ;
90
88
import javax .activation .MimetypesFileTypeMap ;
91
89
import javax .ejb .EJBException ;
92
90
import javax .enterprise .inject .spi .CDI ;
93
- import javax .faces .application .FacesMessage ;
94
- import javax .faces .component .UIComponent ;
95
- import javax .faces .component .UIInput ;
96
- import javax .faces .context .FacesContext ;
97
- import javax .faces .validator .ValidatorException ;
98
91
import javax .json .JsonArray ;
99
92
import javax .json .JsonObject ;
100
93
import javax .xml .stream .XMLStreamConstants ;
108
101
import java .util .zip .ZipInputStream ;
109
102
import org .apache .commons .io .FilenameUtils ;
110
103
111
- import com .amazonaws .AmazonServiceException ;
112
104
import edu .harvard .iq .dataverse .dataaccess .DataAccessOption ;
113
105
import edu .harvard .iq .dataverse .dataaccess .StorageIO ;
114
106
import edu .harvard .iq .dataverse .datasetutility .FileSizeChecker ;
@@ -487,22 +479,18 @@ public static String determineFileType(File f, String fileName) throws IOExcepti
487
479
// step 4:
488
480
// Additional processing; if we haven't gotten much useful information
489
481
// back from Jhove, we'll try and make an educated guess based on
490
- // the file extension:
491
-
492
- if ( fileExtension != null ) {
493
- logger .fine ("fileExtension=" +fileExtension );
482
+ // the file name and extension:
494
483
495
- if (fileType == null || fileType .startsWith ("text/plain" ) || "application/octet-stream" .equals (fileType )) {
496
- if (fileType != null && fileType .startsWith ("text/plain" ) && STATISTICAL_FILE_EXTENSION .containsKey (fileExtension )) {
497
- fileType = STATISTICAL_FILE_EXTENSION .get (fileExtension );
498
- } else {
499
- fileType = determineFileTypeByExtension (fileName );
500
- }
501
-
502
- logger .fine ("mime type recognized by extension: " +fileType );
484
+ logger .fine ("fileName=" +fileName );
485
+
486
+ if (fileType == null || fileType .startsWith ("text/plain" ) || "application/octet-stream" .equals (fileType )) {
487
+ if (fileExtension != null && fileType != null && fileType .startsWith ("text/plain" ) && STATISTICAL_FILE_EXTENSION .containsKey (fileExtension )) {
488
+ fileType = STATISTICAL_FILE_EXTENSION .get (fileExtension );
489
+ } else {
490
+ fileType = determineFileTypeByNameAndExtension (fileName );
503
491
}
504
- } else {
505
- logger .fine ("fileExtension is null" );
492
+
493
+ logger .fine ("mime type recognized by name/extension: " + fileType );
506
494
}
507
495
508
496
// step 5:
@@ -552,7 +540,7 @@ public static String determineFileType(File f, String fileName) throws IOExcepti
552
540
return fileType ;
553
541
}
554
542
555
- public static String determineFileTypeByExtension (String fileName ) {
543
+ public static String determineFileTypeByNameAndExtension (String fileName ) {
556
544
String mimetypesFileTypeMapResult = MIME_TYPE_MAP .getContentType (fileName );
557
545
logger .fine ("MimetypesFileTypeMap type by extension, for " + fileName + ": " + mimetypesFileTypeMapResult );
558
546
if (mimetypesFileTypeMapResult != null ) {
@@ -567,14 +555,19 @@ public static String determineFileTypeByExtension(String fileName) {
567
555
}
568
556
569
557
public static String lookupFileTypeFromPropertiesFile (String fileName ) {
570
- String fileExtension = FilenameUtils .getExtension (fileName );
558
+ String fileKey = FilenameUtils .getExtension (fileName );
571
559
String propertyFileName = "MimeTypeDetectionByFileExtension" ;
560
+ if (fileKey == null || fileKey .isEmpty ()) {
561
+ fileKey = fileName ;
562
+ propertyFileName = "MimeTypeDetectionByFileName" ;
563
+
564
+ }
572
565
String propertyFileNameOnDisk = propertyFileName + ".properties" ;
573
566
try {
574
- logger .fine ("checking " + propertyFileNameOnDisk + " for file extension " + fileExtension );
575
- return BundleUtil .getStringFromPropertyFile (fileExtension , propertyFileName );
567
+ logger .fine ("checking " + propertyFileNameOnDisk + " for file key " + fileKey );
568
+ return BundleUtil .getStringFromPropertyFile (fileKey , propertyFileName );
576
569
} catch (MissingResourceException ex ) {
577
- logger .info (fileExtension + " is a file extension Dataverse doesn't know about. Consider adding it to the " + propertyFileNameOnDisk + " file." );
570
+ logger .info (fileKey + " is a file extension Dataverse doesn't know about. Consider adding it to the " + propertyFileNameOnDisk + " file." );
578
571
return null ;
579
572
}
580
573
}
@@ -1145,7 +1138,7 @@ public static CreateDataFileResult createDataFiles(DatasetVersion version, Input
1145
1138
} else {
1146
1139
// Default to suppliedContentType if set or the overall undetermined default if a contenttype isn't supplied
1147
1140
finalType = StringUtils .isBlank (suppliedContentType ) ? FileUtil .MIME_TYPE_UNDETERMINED_DEFAULT : suppliedContentType ;
1148
- String type = determineFileTypeByExtension (fileName );
1141
+ String type = determineFileTypeByNameAndExtension (fileName );
1149
1142
if (!StringUtils .isBlank (type )) {
1150
1143
//Use rules for deciding when to trust browser supplied type
1151
1144
if (useRecognizedType (finalType , type )) {
0 commit comments