download folder modified

This commit is contained in:
Lucio Lelii 2022-09-06 17:11:27 +02:00
parent 50124d8a49
commit bfa702bf0f
1 changed files with 42 additions and 36 deletions

View File

@ -99,6 +99,8 @@ import org.gcube.smartgears.utils.InnerMethodName;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
@Path("items") @Path("items")
@ManagedBy(StorageHubAppllicationManager.class) @ManagedBy(StorageHubAppllicationManager.class)
@ -784,7 +786,6 @@ public class ItemsManager extends Impersonable{
public Response download(@QueryParam("exclude") List<String> excludes){ public Response download(@QueryParam("exclude") List<String> excludes){
InnerMethodName.instance.set("downloadById"); InnerMethodName.instance.set("downloadById");
Session ses = null; Session ses = null;
Response response = null;
try{ try{
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context)); ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
final Node node = ses.getNodeByIdentifier(id); final Node node = ses.getNodeByIdentifier(id);
@ -793,39 +794,7 @@ public class ItemsManager extends Impersonable{
if (item instanceof AbstractFileItem){ if (item instanceof AbstractFileItem){
return downloadFileInternal(ses, (AbstractFileItem) item, currentUser, true); return downloadFileInternal(ses, (AbstractFileItem) item, currentUser, true);
} else if (item instanceof FolderItem){ } else if (item instanceof FolderItem){
return downloadFolderInternal(ses, (FolderItem)item, true);
try {
final Deque<Item> 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();
}
} else throw new InvalidItemException("item type not supported for download: "+item.getClass()); } else throw new InvalidItemException("item type not supported for download: "+item.getClass());
}catch(RepositoryException re ){ }catch(RepositoryException re ){
@ -837,10 +806,47 @@ public class ItemsManager extends Impersonable{
} finally{ } finally{
if (ses!=null) ses.logout(); if (ses!=null) ses.logout();
} }
return null;
return response;
} }
private Response downloadFolderInternal(Session ses, FolderItem item, boolean withAccounting ) throws StorageHubException, RepositoryException {
try {
final Deque<Item> 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 { private Response downloadFileInternal(Session ses, AbstractFileItem fileItem, String login, boolean withAccounting) throws RepositoryException, PluginInitializationException, PluginNotFoundException, StorageHubException {
Content content = fileItem.getContent(); Content content = fileItem.getContent();