Content handler modified
This commit is contained in:
parent
55b6d8e09a
commit
9b568a09ec
|
@ -7,10 +7,18 @@ import org.gcube.common.storagehub.model.items.nodes.Content;
|
|||
|
||||
public interface ContentHandler {
|
||||
|
||||
void initiliseSpecificContent(InputStream is, String fileName, String mimeType, long size) throws Exception;
|
||||
boolean requiresInputStream();
|
||||
|
||||
default void initiliseSpecificContent(InputStream is, String fileName, String mimeType, long size) throws Exception{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
default void initiliseSpecificContent(String fileName, String mimeType) throws Exception {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
Content getContent();
|
||||
|
||||
|
||||
AbstractFileItem buildItem(String name, String description, String login);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.gcube.data.access.storagehub.handlers.content;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.gcube.common.storagehub.model.items.GenericFileItem;
|
||||
|
@ -11,12 +10,17 @@ public class GenericFileHandler implements ContentHandler{
|
|||
|
||||
Content content = new Content();
|
||||
|
||||
|
||||
@Override
|
||||
public void initiliseSpecificContent(InputStream is, String filename, String mimeType, long size) throws Exception {
|
||||
public boolean requiresInputStream() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initiliseSpecificContent(String filename, String mimeType) throws Exception {
|
||||
content.setMimeType(mimeType);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Content getContent() {
|
||||
return content;
|
||||
|
|
|
@ -28,17 +28,24 @@ public class ImageHandler implements ContentHandler{
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ImageHandler.class);
|
||||
|
||||
|
||||
@Override
|
||||
public boolean requiresInputStream() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initiliseSpecificContent(InputStream is, String fileName, String mimeType, long size) throws Exception {
|
||||
Image image = javax.imageio.ImageIO.read(is);
|
||||
if (size<5242880) {
|
||||
Image image = javax.imageio.ImageIO.read(is);
|
||||
|
||||
int width = image.getWidth(null);
|
||||
int height = image.getHeight(null);
|
||||
int width = image.getWidth(null);
|
||||
int height = image.getHeight(null);
|
||||
|
||||
content.setWidth(Long.valueOf(width));
|
||||
content.setHeight(Long.valueOf(height));
|
||||
|
||||
content.setWidth(Long.valueOf(width));
|
||||
content.setHeight(Long.valueOf(height));
|
||||
|
||||
if (size<5242880)
|
||||
try {
|
||||
int[] dimension = getThumbnailDimension(width, height);
|
||||
|
||||
|
@ -51,6 +58,7 @@ public class ImageHandler implements ContentHandler{
|
|||
}catch(Throwable t) {
|
||||
logger.warn("thumbnail for file {} cannot be created ", fileName,t);
|
||||
}
|
||||
}
|
||||
|
||||
content.setMimeType(mimeType);
|
||||
|
||||
|
|
|
@ -16,6 +16,11 @@ public class OfficeAppHandler implements ContentHandler{
|
|||
|
||||
Content content = new Content();
|
||||
|
||||
@Override
|
||||
public boolean requiresInputStream() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initiliseSpecificContent(InputStream is, String filename, String mimeType, long size) throws Exception {
|
||||
//detecting the file type
|
||||
|
|
|
@ -26,6 +26,11 @@ public class PdfHandler implements ContentHandler {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PdfHandler.class);
|
||||
|
||||
@Override
|
||||
public boolean requiresInputStream() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initiliseSpecificContent(InputStream is, String fileName, String mimeType, long size) throws Exception {
|
||||
try {
|
||||
|
|
|
@ -243,28 +243,28 @@ public class ItemHandler {
|
|||
}
|
||||
|
||||
private Node createFileItemInternally(Session ses, Node destinationNode, InputStream stream, String name, String description, FormDataContentDisposition fileDetails, String login, boolean withLock) throws RepositoryException, StorageHubException{
|
||||
|
||||
|
||||
log.trace("UPLOAD: starting preparing file");
|
||||
|
||||
|
||||
Node newNode;
|
||||
FolderItem destinationItem = node2Item.getItem(destinationNode, Excludes.ALL);
|
||||
|
||||
|
||||
StorageBackendFactory sbf = storageBackendHandler.get(destinationItem.getBackend());
|
||||
StorageBackend sb = sbf.create(destinationItem.getBackend());
|
||||
|
||||
|
||||
String relativePath = destinationNode.getPath();
|
||||
|
||||
|
||||
|
||||
|
||||
String newNodePath = Paths.append(Paths.getPath(destinationNode.getPath()), name).toPath();
|
||||
log.info("new node path is {}", newNodePath);
|
||||
|
||||
|
||||
if (ses.nodeExists(newNodePath)) {
|
||||
|
||||
newNode = ses.getNode(newNodePath);
|
||||
authChecker.checkWriteAuthorizationControl(ses, login, newNode.getIdentifier(), false);
|
||||
|
||||
AbstractFileItem item = fillItemWithContent(stream, sb, name, description, fileDetails, relativePath,login);
|
||||
|
||||
|
||||
if (withLock) {
|
||||
try {
|
||||
ses.getWorkspace().getLockManager().lock(newNode.getPath(), true, true, 0,login);
|
||||
|
@ -307,14 +307,14 @@ public class ItemHandler {
|
|||
versionHandler.makeVersionableContent(newNode);
|
||||
accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), item.getContent().getMimeType(), ses, login, destinationNode, false);
|
||||
}
|
||||
|
||||
|
||||
//TODO: Utils.updateParentSize()
|
||||
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private AbstractFileItem fillItemWithContent(InputStream stream, StorageBackend storageBackend, String name, String description, FormDataContentDisposition fileDetails, String relPath, String login) throws BackendGenericError{
|
||||
log.trace("UPLOAD: filling content");
|
||||
ContentHandler handler = getContentHandler(stream, storageBackend, name, fileDetails, relPath, login);
|
||||
|
@ -323,7 +323,7 @@ public class ItemHandler {
|
|||
}
|
||||
|
||||
private ContentHandler getContentHandler(InputStream stream, StorageBackend storageBackend, String name, FormDataContentDisposition fileDetails, String relPath, String login) throws BackendGenericError {
|
||||
|
||||
|
||||
log.trace("UPLOAD: handling content");
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
|
@ -334,7 +334,7 @@ public class ItemHandler {
|
|||
try(InputStream is1 = stream){
|
||||
log.debug("UPLOAD: upload on {} - start (size {})",storageBackend.getClass(), fileDetails.getSize());
|
||||
if (fileDetails !=null && fileDetails.getSize()>0)
|
||||
info = storageBackend.upload(is1, relPath, name, fileDetails.getSize());
|
||||
info = storageBackend.upload(is1, relPath, name, fileDetails.getSize());
|
||||
else
|
||||
info = storageBackend.upload(is1, relPath, name);
|
||||
log.debug("UPLOAD: upload on storage - stop");
|
||||
|
@ -342,36 +342,41 @@ public class ItemHandler {
|
|||
log.error("error writing content",e );
|
||||
throw e;
|
||||
}
|
||||
|
||||
|
||||
ContentHandler handler =null;
|
||||
String mimeType;
|
||||
log.debug("UPLOAD: reading the mimetype - start");
|
||||
try(InputStream is1 = new BufferedInputStream(storageBackend.download(info.getStorageId()), 1024*64)){
|
||||
try(InputStream is1 = new BufferedInputStream(storageBackend.download(info.getStorageId()))){
|
||||
org.apache.tika.mime.MediaType mediaType = null;
|
||||
TikaConfig config = TikaConfig.getDefaultConfig();
|
||||
Detector detector = config.getDetector();
|
||||
TikaInputStream tikastream = TikaInputStream.get(is1);
|
||||
Metadata metadata = new Metadata();
|
||||
mediaType = detector.detect(tikastream, metadata);
|
||||
String mimeType = mediaType.getBaseType().toString();
|
||||
|
||||
mimeType = mediaType.getBaseType().toString();
|
||||
handler = contenthandlerFactory.create(mimeType);
|
||||
|
||||
is1.reset();
|
||||
handler.initiliseSpecificContent(is1, name, mimeType, info.getSize());
|
||||
|
||||
log.debug("UPLOAD: reading the mimetype - finished in {}",System.currentTimeMillis()-start);
|
||||
log.debug("UPLOAD: reading the mimetype {} - finished in {}",mimeType, System.currentTimeMillis()-start);
|
||||
} catch (Throwable e) {
|
||||
log.error("error retrieving mimeType",e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
if (handler.requiresInputStream())
|
||||
try (InputStream is1 = new BufferedInputStream(storageBackend.download(info.getStorageId()))) {
|
||||
log.debug("UPLOAD: the file type requires input stream");
|
||||
handler.initiliseSpecificContent(is1, name, mimeType, info.getSize());
|
||||
}
|
||||
else {
|
||||
log.debug("UPLOAD: the file type doesn't requires input stream");
|
||||
handler.initiliseSpecificContent(name, mimeType);
|
||||
}
|
||||
log.debug("UPLOAD: writing the stream - finished in {}",System.currentTimeMillis()-start);
|
||||
handler.getContent().setData(NodeConstants.CONTENT_NAME);
|
||||
handler.getContent().setStorageId(info.getStorageId());
|
||||
handler.getContent().setSize(info.getSize());
|
||||
handler.getContent().setRemotePath(info.getRemotePath());
|
||||
|
||||
|
||||
handler.getContent().setPayloadBackend(info.getPayloadBackend());
|
||||
log.debug("UPLOAD: content payload seta as {} ", handler.getContent().getPayloadBackend());
|
||||
return handler;
|
||||
|
|
Loading…
Reference in New Issue