From bfa702bf0f936ead2878625f220857007166b12f Mon Sep 17 00:00:00 2001 From: lucio Date: Tue, 6 Sep 2022 17:11:27 +0200 Subject: [PATCH] download folder modified --- .../storagehub/services/ItemsManager.java | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java b/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java index c2a312d..ac77b9d 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java @@ -99,6 +99,8 @@ import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.itextpdf.text.pdf.PdfStructTreeController.returnType; + @Path("items") @ManagedBy(StorageHubAppllicationManager.class) @@ -784,7 +786,6 @@ public class ItemsManager extends Impersonable{ public Response download(@QueryParam("exclude") List excludes){ InnerMethodName.instance.set("downloadById"); Session ses = null; - Response response = null; try{ ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context)); final Node node = ses.getNodeByIdentifier(id); @@ -793,39 +794,7 @@ public class ItemsManager extends Impersonable{ if (item instanceof AbstractFileItem){ return downloadFileInternal(ses, (AbstractFileItem) item, currentUser, true); } else if (item instanceof FolderItem){ - - try { - final Deque allNodes = compressHandler.getAllNodesForZip((FolderItem)item, ses, currentUser, accountingHandler, excludes); - final org.gcube.common.storagehub.model.Path originalPath = Paths.getPath(item.getParentPath()); - StreamingOutput so = new StreamingOutput() { - - @Override - public void write(OutputStream os) { - - try(ZipOutputStream zos = new ZipOutputStream(os)){ - long start = System.currentTimeMillis(); - zos.setLevel(Deflater.BEST_COMPRESSION); - log.debug("writing StreamOutput"); - compressHandler.zipNode(zos, allNodes, currentUser, originalPath); - log.debug("StreamOutput written in {}",(System.currentTimeMillis()-start)); - } catch (Exception e) { - log.error("error writing stream",e); - } - - } - }; - - response = Response - .ok(so) - .header("content-disposition","attachment; filename = "+item.getTitle()+".zip") - .header("Content-Type", "application/zip") - .header("Content-Length", -1l) - .build(); - - accountingHandler.createReadObj(item.getTitle(), ses, (Node) item.getRelatedNode(), currentUser, false); - }finally { - if (ses!=null) ses.save(); - } + return downloadFolderInternal(ses, (FolderItem)item, true); } else throw new InvalidItemException("item type not supported for download: "+item.getClass()); }catch(RepositoryException re ){ @@ -837,10 +806,47 @@ public class ItemsManager extends Impersonable{ } finally{ if (ses!=null) ses.logout(); } - - return response; + return null; } + private Response downloadFolderInternal(Session ses, FolderItem item, boolean withAccounting ) throws StorageHubException, RepositoryException { + try { + final Deque allNodes = compressHandler.getAllNodesForZip((FolderItem)item, ses, currentUser, accountingHandler, Excludes.GET_ONLY_CONTENT); + final org.gcube.common.storagehub.model.Path originalPath = Paths.getPath(item.getParentPath()); + StreamingOutput so = new StreamingOutput() { + + @Override + public void write(OutputStream os) { + + try(ZipOutputStream zos = new ZipOutputStream(os)){ + long start = System.currentTimeMillis(); + zos.setLevel(Deflater.BEST_COMPRESSION); + log.debug("writing StreamOutput"); + compressHandler.zipNode(zos, allNodes, currentUser, originalPath); + log.debug("StreamOutput written in {}",(System.currentTimeMillis()-start)); + } catch (Exception e) { + log.error("error writing stream",e); + } + + } + }; + + Response response = Response + .ok(so) + .header("content-disposition","attachment; filename = "+item.getTitle()+".zip") + .header("Content-Type", "application/zip") + .header("Content-Length", -1l) + .build(); + + if (withAccounting) + accountingHandler.createReadObj(item.getTitle(), ses, (Node) item.getRelatedNode(), currentUser, false); + return response; + }finally { + if (ses!=null) ses.save(); + } + } + + private Response downloadFileInternal(Session ses, AbstractFileItem fileItem, String login, boolean withAccounting) throws RepositoryException, PluginInitializationException, PluginNotFoundException, StorageHubException { Content content = fileItem.getContent();