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