1
|
package eu.dnetlib.efg.thumbnails;
|
2
|
|
3
|
import eu.dnetlib.data.objectstore.connector.ObjectStore;
|
4
|
import eu.dnetlib.data.objectstore.gridFS.GridFSObjectStore;
|
5
|
import eu.dnetlib.data.objectstore.gridFS.GridFSObjectstoreDaoImpl;
|
6
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
7
|
import eu.dnetlib.miscutils.collections.Pair;
|
8
|
import eu.dnetlib.rmi.data.ObjectStoreFile;
|
9
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
10
|
import org.apache.commons.io.IOUtils;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.stereotype.Controller;
|
13
|
import org.springframework.ui.ModelMap;
|
14
|
import org.springframework.web.bind.annotation.RequestMapping;
|
15
|
import org.springframework.web.bind.annotation.RequestParam;
|
16
|
import org.springframework.web.bind.annotation.ResponseBody;
|
17
|
|
18
|
import javax.servlet.ServletOutputStream;
|
19
|
import javax.servlet.http.HttpServletResponse;
|
20
|
import java.io.InputStream;
|
21
|
import java.net.URL;
|
22
|
import java.util.HashMap;
|
23
|
import java.util.List;
|
24
|
import java.util.Map;
|
25
|
|
26
|
/**
|
27
|
* Created by sandro on 4/12/17.
|
28
|
*/
|
29
|
@Controller
|
30
|
public class ThumbnailController {
|
31
|
|
32
|
|
33
|
@Autowired
|
34
|
UniqueServiceLocator serviceLocator;
|
35
|
|
36
|
@Autowired
|
37
|
GridFSObjectstoreDaoImpl objectstoreDao;
|
38
|
|
39
|
|
40
|
private Map<String, String> objectStoresThumbnail;
|
41
|
|
42
|
|
43
|
@RequestMapping(value = "/ui/thumbnail/objectStoresThumbnail")
|
44
|
@ResponseBody
|
45
|
public Map<String, String> getObjectStoresThumbnail(ModelMap map) throws Exception {
|
46
|
if (objectStoresThumbnail == null) {
|
47
|
createObjectStoreThumbnail();
|
48
|
}
|
49
|
return objectStoresThumbnail;
|
50
|
}
|
51
|
|
52
|
|
53
|
@RequestMapping(value = "/ui/thumbnail/getThumbnail")
|
54
|
public void getThumbnail(final HttpServletResponse response, @RequestParam(name = "metadataId") final String metadataId,
|
55
|
@RequestParam(name = "imageSize") final int imageSize, @RequestParam(name = "imageType") String type) throws Exception {
|
56
|
if (imageSize != 96 && imageSize != 250) throw new Exception("Image size not supported!");
|
57
|
InputStream inputStream = null;
|
58
|
if(!type.equalsIgnoreCase("video") && !type.equalsIgnoreCase("image") && !type.equalsIgnoreCase("text")){
|
59
|
type = "video";
|
60
|
}
|
61
|
if (objectStoresThumbnail == null) {
|
62
|
createObjectStoreThumbnail();
|
63
|
}
|
64
|
|
65
|
if (objectStoresThumbnail.size() == 0){
|
66
|
inputStream = getClass().getResourceAsStream(String.format("/eu/dnetlib/thumbnail/NF%s_%d.jpg", type.toLowerCase(), imageSize));
|
67
|
}else{
|
68
|
final GridFSObjectStore objectStore = (GridFSObjectStore) objectstoreDao.getObjectStore(objectStoresThumbnail.get(String.format("thumb%d", imageSize == 250 ? 256 : imageSize)));
|
69
|
|
70
|
final ObjectStoreFile objectStoreFile = objectStore.deliverObjectFromMetadataIdentifier(metadataId);
|
71
|
|
72
|
if (objectStoreFile == null) {
|
73
|
inputStream = getClass().getResourceAsStream(String.format("/eu/dnetlib/thumbnail/NF%s_%d.jpg", type.toLowerCase(), imageSize));
|
74
|
} else {
|
75
|
inputStream = objectStore.deliverStream(objectStoreFile.getObjectID());
|
76
|
}
|
77
|
}
|
78
|
|
79
|
if (inputStream != null) {
|
80
|
try {
|
81
|
final ServletOutputStream outputStream = response.getOutputStream();
|
82
|
IOUtils.copy(inputStream, outputStream);
|
83
|
} finally {
|
84
|
inputStream.close();
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
|
89
|
|
90
|
private void createObjectStoreThumbnail() throws Exception {
|
91
|
final String query = "for $x in collection('/db/DRIVER/ObjectStoreDSResources/ObjectStoreDSResourceType')\n" +
|
92
|
"where $x//OBJECTSTORE_INTERPRETATION = 'thumb96' or $x//OBJECTSTORE_INTERPRETATION = 'thumb256' \n" +
|
93
|
" return concat($x//OBJECTSTORE_INTERPRETATION, '<-->', $x//RESOURCE_IDENTIFIER/@value/string())";
|
94
|
final ISLookUpService lookUpService = serviceLocator.getService(ISLookUpService.class);
|
95
|
final List<String> queryResult = lookUpService.quickSearchProfile(query);
|
96
|
if (queryResult == null) {
|
97
|
throw new Exception("Missing objectStore Thumbnail");
|
98
|
}
|
99
|
objectStoresThumbnail = new HashMap<>();
|
100
|
queryResult.stream().forEach(item -> {
|
101
|
final String[] splitString = item.split("<-->");
|
102
|
if (splitString == null || splitString.length != 2)
|
103
|
throw new RuntimeException("Error wrong splitting");
|
104
|
objectStoresThumbnail.put(splitString[0], splitString[1]);
|
105
|
|
106
|
});
|
107
|
|
108
|
|
109
|
|
110
|
}
|
111
|
|
112
|
}
|