gcube-cms-suite/geoportal-common/src/main/java/org/gcube/application/geoportal/common/utils/FileSets.java

95 lines
3.5 KiB
Java
Raw Normal View History

2021-09-21 17:45:18 +02:00
package org.gcube.application.geoportal.common.utils;
2021-09-20 16:47:35 +02:00
2022-01-31 10:49:53 +01:00
import org.bson.Document;
2021-09-20 16:47:35 +02:00
import org.gcube.application.geoportal.common.model.legacy.InputStreamDescriptor;
2022-01-27 15:02:53 +01:00
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.model.rest.TempFile;
2021-09-20 16:47:35 +02:00
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class FileSets {
public static class RequestBuilder {
2022-01-27 15:02:53 +01:00
RegisterFileSetRequest theRequest=new RegisterFileSetRequest();
2021-09-20 16:47:35 +02:00
public RequestBuilder add(TempFile... f){
if(theRequest.getStreams()==null)
theRequest.setStreams(new ArrayList<TempFile>());
for(TempFile temp: f )
theRequest.getStreams().add(temp);
return this;
}
public RequestBuilder add(TempFile f){
if(theRequest.getStreams()==null)
theRequest.setStreams(new ArrayList<TempFile>());
theRequest.getStreams().add(f);
return this;
}
2022-01-31 10:46:14 +01:00
public RequestBuilder setFieldPath(String path){
2021-09-20 16:47:35 +02:00
theRequest.setDestinationPath(path);
return this;
}
2022-01-31 10:46:14 +01:00
public RequestBuilder setFieldDescriptionPath(String path){
theRequest.setFieldPath(path);
return this;
}
2021-09-20 16:47:35 +02:00
2022-01-31 10:46:14 +01:00
public RequestBuilder setClashPolicy(RegisterFileSetRequest.ClashOptions path){
theRequest.setClashOption(path);
return this;
}
2021-09-20 16:47:35 +02:00
2022-01-31 10:49:53 +01:00
public RequestBuilder setAttributes(Document attributes){
theRequest.setAttributes(attributes);
return this;
}
2021-09-20 16:47:35 +02:00
2022-01-27 15:02:53 +01:00
public RegisterFileSetRequest getTheRequest(){return theRequest;}
2021-09-20 16:47:35 +02:00
}
public static RequestBuilder build(String path) {
2022-01-31 10:46:14 +01:00
return new RequestBuilder().setFieldPath(path);
2021-09-20 16:47:35 +02:00
}
public static RequestBuilder build(String path, TempFile...files) {
2022-01-31 10:46:14 +01:00
return new RequestBuilder().setFieldPath(path).add(files);
2021-09-20 16:47:35 +02:00
}
public static TempFile asTemp(StorageUtils storage,InputStreamDescriptor descriptor) throws RemoteBackendException, FileNotFoundException {
return storage.putOntoStorage(descriptor.getStream(), descriptor.getFilename());
}
public static TempFile[] asTemp(StorageUtils storage,InputStreamDescriptor... descriptors) throws RemoteBackendException, FileNotFoundException {
ArrayList<TempFile> toReturn=new ArrayList<TempFile>();
for(InputStreamDescriptor desc:descriptors)
toReturn.add(storage.putOntoStorage(desc.getStream(), desc.getFilename()));
return toReturn.toArray(new TempFile[toReturn.size()]);
}
2022-01-27 15:02:53 +01:00
public static RegisterFileSetRequest prepareRequestFromFolder(StorageUtils storage, String path, File directory) throws FileNotFoundException {
2021-09-20 16:47:35 +02:00
File[] children =directory.listFiles();
InputStreamDescriptor[] iss=new InputStreamDescriptor[children.length];
return prepareRequest(storage,path,children);
}
2022-01-27 15:02:53 +01:00
public static RegisterFileSetRequest prepareRequest(StorageUtils storage, String path, File... toUpload) throws FileNotFoundException {
2021-09-20 16:47:35 +02:00
FileSets.RequestBuilder builder = FileSets.build(path);
for (File f : toUpload) {
if(!f.isDirectory())
builder.add(FileSets.asTemp(storage, new InputStreamDescriptor(new FileInputStream(f), f.getName())));
}
return builder.getTheRequest();
}
}