Project

General

Profile

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

    
58
        if(!type.equalsIgnoreCase("video") && !type.equalsIgnoreCase("image") && !type.equalsIgnoreCase("text")){
59
            type = "video";
60
        }
61
        if (objectStoresThumbnail == null) {
62
            createObjectStoreThumbnail();
63
        }
64
        final GridFSObjectStore objectStore = (GridFSObjectStore) objectstoreDao.getObjectStore(objectStoresThumbnail.get(String.format("thumb%d", imageSize == 250 ? 256 : imageSize)));
65

    
66
        final ObjectStoreFile objectStoreFile = objectStore.deliverObjectFromMetadataIdentifier(metadataId);
67
        InputStream inputStream = null;
68
        if (objectStoreFile == null) {
69
            inputStream = getClass().getResourceAsStream(String.format("/eu/dnetlib/thumbnail/NF%s_%d.jpg", type.toLowerCase(), imageSize));
70
        } else {
71
            inputStream = objectStore.deliverStream(objectStoreFile.getObjectID());
72
        }
73
        if (inputStream != null) {
74
            try {
75
                final ServletOutputStream outputStream = response.getOutputStream();
76
                IOUtils.copy(inputStream, outputStream);
77
            } finally {
78
                inputStream.close();
79
            }
80
        }
81
    }
82

    
83

    
84
    private void createObjectStoreThumbnail() throws Exception {
85
        final String query = "for $x in collection('/db/DRIVER/ObjectStoreDSResources/ObjectStoreDSResourceType')\n" +
86
                "where  $x//OBJECTSTORE_INTERPRETATION = 'thumb96' or $x//OBJECTSTORE_INTERPRETATION = 'thumb256' \n" +
87
                " return concat($x//OBJECTSTORE_INTERPRETATION, '<-->',   $x//RESOURCE_IDENTIFIER/@value/string())";
88
        final ISLookUpService lookUpService = serviceLocator.getService(ISLookUpService.class);
89
        final List<String> queryResult = lookUpService.quickSearchProfile(query);
90
        if (queryResult == null) {
91
            throw new Exception("Missing objectStore Thumbnail");
92
        }
93
        objectStoresThumbnail = new HashMap<>();
94
        queryResult.stream().forEach(item -> {
95
            final String[] splitString = item.split("<-->");
96
            if (splitString == null || splitString.length != 2)
97
                throw new RuntimeException("Error wrong splitting");
98
            objectStoresThumbnail.put(splitString[0], splitString[1]);
99

    
100
        });
101

    
102

    
103
    }
104

    
105
}
    (1-1/1)