|
21 | 21 | import java.nio.file.StandardCopyOption;
|
22 | 22 | import java.sql.SQLException;
|
23 | 23 | import java.util.ArrayList;
|
| 24 | +import java.util.Arrays; |
24 | 25 | import java.util.Hashtable;
|
25 | 26 | import java.util.Iterator;
|
26 | 27 | import java.util.List;
|
@@ -196,6 +197,11 @@ public List<FileInfo> processInputStreamToFilePreview(Context context, Bitstream
|
196 | 197 | List<FileInfo> fileInfos = new ArrayList<>();
|
197 | 198 | String bitstreamMimeType = bitstream.getFormat(context).getMIMEType();
|
198 | 199 | 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 | + } |
199 | 205 | String data = getFileContent(inputStream, true);
|
200 | 206 | fileInfos.add(new FileInfo(data, false));
|
201 | 207 | } else if (bitstreamMimeType.equals("text/html")) {
|
@@ -254,6 +260,22 @@ public String composePreviewURL(Context context, Item item, Bitstream bitstream,
|
254 | 260 | return url;
|
255 | 261 | }
|
256 | 262 |
|
| 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 | + |
257 | 279 | /**
|
258 | 280 | * Define the hierarchy organization for preview content and file info.
|
259 | 281 | * The hierarchy is established by the sub map.
|
|
0 commit comments