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, @RequestParam(name = "imageSize") final int imageSize, @RequestParam(name = "imageType") final String type) throws Exception {
55
        if (imageSize != 96 && imageSize != 250) throw new Exception("Image size not supported!");
56
        if (objectStoresThumbnail == null) {
57
            createObjectStoreThumbnail();
58
        }
59
        final GridFSObjectStore objectStore = (GridFSObjectStore) objectstoreDao.getObjectStore(objectStoresThumbnail.get(String.format("thumb%d", imageSize == 250 ? 256 : imageSize)));
60

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

    
78

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

    
95
        });
96

    
97

    
98
    }
99

    
100
}
    (1-1/1)