1
|
package eu.dnetlib.data.objectstore.s3;
|
2
|
|
3
|
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFile;
|
4
|
import eu.dnetlib.data.objectstore.rmi.Protocols;
|
5
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
6
|
import org.apache.commons.lang3.StringUtils;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
|
10
|
import org.bson.Document;
|
11
|
|
12
|
|
13
|
/**
|
14
|
* The Class ObjectStoreFileBuilder generates an objectStoreFile bean
|
15
|
*/
|
16
|
public class ObjectStoreS3Utility {
|
17
|
|
18
|
private static final int KB_SIZE = 1024;
|
19
|
|
20
|
/**
|
21
|
* The Constant log.
|
22
|
*/
|
23
|
private static final Log log = LogFactory.getLog(ObjectStoreS3Utility.class);
|
24
|
|
25
|
public static ObjectStoreFile build(final Document metadata) {
|
26
|
|
27
|
final String originalFile = metadata.getString("originalObject");
|
28
|
final ObjectStoreFile original = ObjectStoreFile.createObject(originalFile);
|
29
|
final ObjectStoreFile newFile = new ObjectStoreFile();
|
30
|
newFile.setObjectID(metadata.getString("id"));
|
31
|
newFile.setAccessProtocol(Protocols.None);
|
32
|
newFile.setMimeType( metadata.getString("mime"));
|
33
|
newFile.setMd5Sum(metadata.getString("md5Sum"));
|
34
|
try {
|
35
|
newFile.setFileSizeKB(Long.parseLong(metadata.get("size").toString()) / KB_SIZE);
|
36
|
} catch (Throwable e) {
|
37
|
log.error("Error on getting file size", e);
|
38
|
}
|
39
|
if (originalFile != null) {
|
40
|
newFile.setMetadataRelatedID(original.getMetadataRelatedID());
|
41
|
if (StringUtils.isBlank(original.getDownloadedURL())) {
|
42
|
newFile.setDownloadedURL(original.getURI());
|
43
|
} else {
|
44
|
newFile.setDownloadedURL(original.getDownloadedURL());
|
45
|
}
|
46
|
}
|
47
|
newFile.setURI(metadata.get("uri").toString());
|
48
|
return newFile;
|
49
|
}
|
50
|
|
51
|
public static UnaryFunction<String, Document> asJSON() {
|
52
|
return input -> build(input).toJSON();
|
53
|
}
|
54
|
}
|