Skip to content

Commit bd7f837

Browse files
committed
UFAL/bitstream preview wrong file name according to it's mimetype (#890)
* Check if the preview file has a right name according to its mime type
1 parent ce64050 commit bd7f837

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

dspace-api/src/main/java/org/dspace/content/PreviewContentServiceImpl.java

+22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.StandardCopyOption;
2222
import java.sql.SQLException;
2323
import java.util.ArrayList;
24+
import java.util.Arrays;
2425
import java.util.Hashtable;
2526
import java.util.Iterator;
2627
import java.util.List;
@@ -196,6 +197,11 @@ public List<FileInfo> processInputStreamToFilePreview(Context context, Bitstream
196197
List<FileInfo> fileInfos = new ArrayList<>();
197198
String bitstreamMimeType = bitstream.getFormat(context).getMIMEType();
198199
if (bitstreamMimeType.equals("text/plain")) {
200+
if (!validateBitstreamNameWithType(bitstream, "zip,tar,gz,tar.gz,tar.bz2")) {
201+
throw new IOException("he file has an incorrect type according to the MIME type stored in the " +
202+
"database. This could cause the ZIP file to be previewed as a text file, potentially leading" +
203+
" to a database error.");
204+
}
199205
String data = getFileContent(inputStream, true);
200206
fileInfos.add(new FileInfo(data, false));
201207
} else if (bitstreamMimeType.equals("text/html")) {
@@ -254,6 +260,22 @@ public String composePreviewURL(Context context, Item item, Bitstream bitstream,
254260
return url;
255261
}
256262

263+
/**
264+
* Validate the bitstream name with the specified type. Check if the ZIP file is not previewed as a text file.
265+
* @param bitstream
266+
* @param forbiddenTypes "in the form of 'type1,type2,type3'"
267+
* @return
268+
*/
269+
private boolean validateBitstreamNameWithType(Bitstream bitstream, String forbiddenTypes) {
270+
ArrayList<String> forbiddenTypesList = new ArrayList(Arrays.asList(forbiddenTypes.split(",")));
271+
for (String forbiddenType : forbiddenTypesList) {
272+
if (bitstream.getName().endsWith(forbiddenType)) {
273+
return false;
274+
}
275+
}
276+
return true;
277+
}
278+
257279
/**
258280
* Define the hierarchy organization for preview content and file info.
259281
* The hierarchy is established by the sub map.

dspace-api/src/main/java/org/dspace/scripts/filepreview/FilePreview.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void generateItemFilePreviews(Context context, UUID itemUUID) throws SQL
123123
return;
124124
}
125125

126-
List<Bundle> bundles = item.getBundles();
126+
List<Bundle> bundles = item.getBundles("ORIGINAL");
127127
for (Bundle bundle : bundles) {
128128
List<Bitstream> bitstreams = bundle.getBitstreams();
129129
for (Bitstream bitstream : bitstreams) {

0 commit comments

Comments
 (0)