Skip to content

Commit 7c920b2

Browse files
UFAL/Improve file preview generating (#972)
* get name and size from metadata and header of file, avoid input stream using * remove temp file, checkstyle, do not load full file * add { } after if * added check for max preview file * used ZipFile and TarArchived for filepreview generating * added removed lines * used 7z for zip and tar files * removed 7z and used zip and tar entry * improved file previrew generating speed, used string builder, xml builder, authorization only if is required * checkstyle, return boolean from haspreview and previrews from getPreview, replaced return with continue * fix problem with hibernate session * fix .tar.gz generating * skip fully entry for tar * added indexes for speed up queries * added license header * named constant by upper case * inicialized fileInfo, refactorization of code based on copilot review --------- Co-authored-by: milanmajchrak <90026355+milanmajchrak@users.noreply.github.com>
1 parent 1136183 commit 7c920b2

16 files changed

Lines changed: 336 additions & 106 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package org.dspace.content;
99

10+
import java.io.File;
1011
import java.io.IOException;
1112
import java.io.InputStream;
1213
import java.sql.SQLException;
@@ -315,6 +316,15 @@ public InputStream retrieve(Context context, Bitstream bitstream)
315316
return bitstreamStorageService.retrieve(context, bitstream);
316317
}
317318

319+
@Override
320+
public File retrieveFile(Context context, Bitstream bitstream, boolean authorization)
321+
throws IOException, SQLException, AuthorizeException {
322+
if (authorization) {
323+
authorizeService.authorizeAction(context, bitstream, Constants.READ);
324+
}
325+
return bitstreamStorageService.retrieveFile(context, bitstream);
326+
}
327+
318328
@Override
319329
public boolean isRegisteredBitstream(Bitstream bitstream) {
320330
return bitstreamStorageService.isRegisteredBitstream(bitstream.getInternalId());

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

Lines changed: 155 additions & 85 deletions
Large diffs are not rendered by default.

dspace-api/src/main/java/org/dspace/content/dao/PreviewContentDAO.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public interface PreviewContentDAO extends GenericDAO<PreviewContent> {
3333
*/
3434
List<PreviewContent> findByBitstream(Context context, UUID bitstreamId) throws SQLException;
3535

36+
/**
37+
* Returns true if the bitstream has associated preview content.
38+
*
39+
* @param context DSpace context
40+
* @param bitstream The bitstream to get bitstream UUID
41+
* @return True if preview content exists, false otherwise
42+
* @throws SQLException If a database error occurs
43+
*/
44+
boolean hasPreview(Context context, Bitstream bitstream) throws SQLException;
45+
3646
/**
3747
* Find all preview content based on bitstream that are the root directory.
3848
*
@@ -41,5 +51,5 @@ public interface PreviewContentDAO extends GenericDAO<PreviewContent> {
4151
* @return List of found preview content
4252
* @throws SQLException If a database error occurs
4353
*/
44-
List<PreviewContent> hasPreview(Context context, Bitstream bitstream) throws SQLException;
54+
List<PreviewContent> getPreview(Context context, Bitstream bitstream) throws SQLException;
4555
}

dspace-api/src/main/java/org/dspace/content/dao/impl/PreviewContentDAOImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ public List<PreviewContent> findByBitstream(Context context, UUID bitstreamId) t
4040
}
4141

4242
@Override
43-
public List<PreviewContent> hasPreview(Context context, Bitstream bitstream) throws SQLException {
43+
public boolean hasPreview(Context context, Bitstream bitstream) throws SQLException {
44+
Query query = createQuery(context,
45+
"SELECT COUNT(pc) FROM " + PreviewContent.class.getSimpleName() +
46+
" pc WHERE pc.bitstream.id = :bitstream_id");
47+
query.setParameter("bitstream_id", bitstream.getID());
48+
return count(query) > 0;
49+
}
50+
51+
@Override
52+
public List<PreviewContent> getPreview(Context context, Bitstream bitstream) throws SQLException {
4453
// select only data from the previewcontent table whose ID is not a child in the preview2preview table
4554
Query query = getHibernateSession(context).createNativeQuery(
4655
"SELECT pc.* FROM previewcontent pc " +

dspace-api/src/main/java/org/dspace/content/service/BitstreamService.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package org.dspace.content.service;
99

10+
import java.io.File;
1011
import java.io.IOException;
1112
import java.io.InputStream;
1213
import java.sql.SQLException;
@@ -154,7 +155,7 @@ public Bitstream register(Context context, int assetstore, String bitstreamPath)
154155
public void setFormat(Context context, Bitstream bitstream, BitstreamFormat bitstreamFormat) throws SQLException;
155156

156157
/**
157-
* Retrieve the contents of the bitstream
158+
* Retrieve the contents of the bitstream.
158159
*
159160
* @param context DSpace context object
160161
* @param bitstream DSpace bitstream
@@ -166,6 +167,20 @@ public Bitstream register(Context context, int assetstore, String bitstreamPath)
166167
public InputStream retrieve(Context context, Bitstream bitstream)
167168
throws IOException, SQLException, AuthorizeException;
168169

170+
/**
171+
* Retrieve the contents of the bitstream.
172+
*
173+
* @param context DSpace context object
174+
* @param bitstream DSpace bitstream
175+
* @param authorization true if authorization is required else false
176+
* @return a File from which the bitstream can be read.
177+
* @throws IOException if IO error
178+
* @throws SQLException if database error
179+
* @throws AuthorizeException if authorization error
180+
*/
181+
public File retrieveFile(Context context, Bitstream bitstream, boolean authorization)
182+
throws IOException, SQLException, AuthorizeException;
183+
169184
/**
170185
* Determine if this bitstream is registered (available elsewhere on
171186
* filesystem than in assetstore). More about registered items:

dspace-api/src/main/java/org/dspace/content/service/PreviewContentService.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
package org.dspace.content.service;
99

10-
import java.io.InputStream;
10+
import java.io.File;
1111
import java.sql.SQLException;
1212
import java.util.List;
1313
import java.util.Map;
@@ -82,14 +82,25 @@ PreviewContent create(Context context, Bitstream bitstream, String name, String
8282
*/
8383
List<PreviewContent> findByBitstream(Context context, UUID bitstream_id) throws SQLException;
8484

85+
/**
86+
* Returns true if the bitstream has associated preview content.
87+
*
88+
* @param context DSpace context
89+
* @param bitstream The bitstream to get bitstream UUID
90+
* @return True if preview content exists, false otherwise
91+
* @throws SQLException If a database error occurs
92+
*/
93+
boolean hasPreview(Context context, Bitstream bitstream) throws SQLException;
94+
8595
/**
8696
* Find all preview content based on bitstream that are the root directory.
8797
*
8898
* @param context DSpace context
8999
* @param bitstream The bitstream to get bitstream UUID
100+
* @return List of preview contents
90101
* @throws SQLException If a database error occurs
91102
*/
92-
List<PreviewContent> hasPreview(Context context, Bitstream bitstream) throws SQLException;
103+
List<PreviewContent> getPreview(Context context, Bitstream bitstream) throws SQLException;
93104

94105
/**
95106
* Find all preview contents from database.
@@ -104,9 +115,11 @@ PreviewContent create(Context context, Bitstream bitstream, String name, String
104115
*
105116
* @param context DSpace context object
106117
* @param bitstream check if this bitstream could be previewed
118+
* @param authorization true if authorization is required else false
107119
* @return true if the bitstream could be previewed, false otherwise
108120
*/
109-
boolean canPreview(Context context, Bitstream bitstream) throws SQLException, AuthorizeException;
121+
boolean canPreview(Context context, Bitstream bitstream, boolean authorization)
122+
throws SQLException, AuthorizeException;
110123

111124
/**
112125
* Return converted ZIP file content into FileInfo classes.
@@ -143,13 +156,13 @@ PreviewContent create(Context context, Bitstream bitstream, String name, String
143156
FileInfo createFileInfo(PreviewContent pc);
144157

145158
/**
146-
* Convert InputStream of the ZIP file into FileInfo classes.
159+
* Convert File of the ZIP file into FileInfo classes.
147160
*
148161
* @param context DSpace context object
149162
* @param bitstream previewing bitstream
150-
* @param inputStream content of the zip file
163+
* @param file content of the zip file
151164
* @return List of FileInfo classes where is wrapped ZIP file content
152165
*/
153-
List<FileInfo> processInputStreamToFilePreview(Context context, Bitstream bitstream, InputStream inputStream)
166+
List<FileInfo> processFileToFilePreview(Context context, Bitstream bitstream, File file)
154167
throws Exception;
155168
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.dspace.content.Bitstream;
2323
import org.dspace.content.Bundle;
2424
import org.dspace.content.Item;
25-
import org.dspace.content.PreviewContent;
2625
import org.dspace.content.factory.ContentServiceFactory;
2726
import org.dspace.content.service.ItemService;
2827
import org.dspace.content.service.PreviewContentService;
@@ -147,21 +146,20 @@ private void generateItemFilePreviews(Context context, UUID itemUUID) throws Exc
147146
for (Bundle bundle : bundles) {
148147
List<Bitstream> bitstreams = bundle.getBitstreams();
149148
for (Bitstream bitstream : bitstreams) {
150-
boolean canPreview = previewContentService.canPreview(context, bitstream);
149+
boolean canPreview = previewContentService.canPreview(context, bitstream, false);
151150
if (!canPreview) {
152-
return;
151+
continue;
153152
}
154-
List<PreviewContent> prContents = previewContentService.hasPreview(context, bitstream);
155153
// Generate new content if we didn't find any
156-
if (!prContents.isEmpty()) {
157-
return;
154+
if (previewContentService.hasPreview(context, bitstream)) {
155+
continue;
158156
}
159157

160158
List<FileInfo> fileInfos = previewContentService.getFilePreviewContent(context, bitstream);
161159
// Do not store HTML content in the database because it could be longer than the limit
162160
// of the database column
163161
if (StringUtils.equals("text/html", bitstream.getFormat(context).getMIMEType())) {
164-
return;
162+
continue;
165163
}
166164

167165
for (FileInfo fi : fileInfos) {

dspace-api/src/main/java/org/dspace/storage/bitstore/BitStoreService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package org.dspace.storage.bitstore;
99

10+
import java.io.File;
1011
import java.io.IOException;
1112
import java.io.InputStream;
1213
import java.util.List;
@@ -45,6 +46,16 @@ public interface BitStoreService {
4546
*/
4647
public InputStream get(Bitstream bitstream) throws IOException;
4748

49+
/**
50+
* Retrieve the bits for bitstream
51+
*
52+
* @param bitstream DSpace Bitstream object
53+
* @return The File
54+
* @throws java.io.IOException If a problem occurs while retrieving the bits, or if no
55+
* asset with ID exists in the store
56+
*/
57+
public File getFile(Bitstream bitstream) throws IOException;
58+
4859
/**
4960
* Store a stream of bits.
5061
*

dspace-api/src/main/java/org/dspace/storage/bitstore/BitstreamStorageServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package org.dspace.storage.bitstore;
99

10+
import java.io.File;
1011
import java.io.IOException;
1112
import java.io.InputStream;
1213
import java.sql.SQLException;
@@ -214,6 +215,13 @@ public InputStream retrieve(Context context, Bitstream bitstream)
214215
return this.getStore(storeNumber).get(bitstream);
215216
}
216217

218+
@Override
219+
public File retrieveFile(Context context, Bitstream bitstream)
220+
throws IOException {
221+
Integer storeNumber = bitstream.getStoreNumber();
222+
return this.getStore(storeNumber).getFile(bitstream);
223+
}
224+
217225
@Override
218226
public void cleanup(boolean deleteDbRecords, boolean verbose) throws SQLException, IOException, AuthorizeException {
219227
Context context = new Context(Context.Mode.BATCH_EDIT);

dspace-api/src/main/java/org/dspace/storage/bitstore/DSBitStoreService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private synchronized static void deleteParents(File file) {
208208
* @return The corresponding file in the file system, or <code>null</code>
209209
* @throws IOException If a problem occurs while determining the file
210210
*/
211-
protected File getFile(Bitstream bitstream) throws IOException {
211+
public File getFile(Bitstream bitstream) throws IOException {
212212
// Check that bitstream is not null
213213
if (bitstream == null) {
214214
return null;

0 commit comments

Comments
 (0)