2020-01-24 12:46:13 +01:00
|
|
|
package org.gcube.data.publishing.ckan2zenodo.clients;
|
|
|
|
|
2020-01-31 14:56:51 +01:00
|
|
|
import java.nio.file.Files;
|
2022-03-02 11:46:23 +01:00
|
|
|
import java.util.*;
|
2020-01-24 12:46:13 +01:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.CkanResource;
|
2020-01-31 14:56:51 +01:00
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.DownloadedFile;
|
2020-01-24 12:46:13 +01:00
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.FileDeposition;
|
|
|
|
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.ZenodoDeposition;
|
|
|
|
|
2020-01-31 14:56:51 +01:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
2020-01-24 12:46:13 +01:00
|
|
|
public class UploadFilesCall implements Callable<ZenodoDeposition>{
|
|
|
|
|
|
|
|
private Collection<CkanResource> toUpload;
|
|
|
|
private ZenodoDeposition deposition;
|
|
|
|
private Zenodo z;
|
2020-01-31 14:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public UploadFilesCall(Collection<CkanResource> toUpload, ZenodoDeposition deposition,
|
2020-01-24 12:46:13 +01:00
|
|
|
Zenodo z) {
|
|
|
|
super();
|
|
|
|
this.toUpload = toUpload;
|
|
|
|
this.deposition = deposition;
|
|
|
|
this.z = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ZenodoDeposition call() throws Exception {
|
|
|
|
ZenodoDeposition dep=deposition;
|
|
|
|
|
2020-01-31 14:56:51 +01:00
|
|
|
log.debug("Starting file transfer for deposition "+deposition.getTitle()+" id : "+deposition.getId());
|
2020-01-24 12:46:13 +01:00
|
|
|
|
2020-01-31 14:56:51 +01:00
|
|
|
log.debug("Removing not referenced files..");
|
|
|
|
|
2022-03-02 11:46:23 +01:00
|
|
|
HashMap<String,DownloadedFile> resourceMap=new HashMap<>();
|
|
|
|
for(CkanResource r:toUpload) {
|
|
|
|
DownloadedFile d=new DownloadedFile(r);
|
|
|
|
resourceMap.put(d.getToUseFileName(),d);
|
|
|
|
}
|
|
|
|
Set<String> alreadyExistingFiles=new HashSet<>();
|
|
|
|
|
2020-01-31 14:56:51 +01:00
|
|
|
for(FileDeposition f:dep.getFiles()) {
|
|
|
|
CkanResource found=null;
|
2022-03-02 11:46:23 +01:00
|
|
|
if(resourceMap.containsKey(f.getFilename())){
|
|
|
|
alreadyExistingFiles.add(f.getFilename());
|
|
|
|
try{
|
|
|
|
// check for update
|
|
|
|
DownloadedFile downloaded = resourceMap.get(f.getFilename());
|
|
|
|
if(!downloaded.getMD5().equals(f.getChecksum())) {
|
|
|
|
log.debug("MD5 differ, going to update : "+downloaded+" - "+f);
|
|
|
|
z.deleteFile(dep, f);
|
|
|
|
z.uploadFile(dep, found.getName(), downloaded.getFile());
|
|
|
|
}
|
|
|
|
}catch (Throwable t){
|
|
|
|
log.warn("Unable to update "+f,t);
|
2020-01-24 12:46:13 +01:00
|
|
|
}
|
2022-03-02 11:46:23 +01:00
|
|
|
}else {
|
2020-01-31 14:56:51 +01:00
|
|
|
try{
|
2022-03-02 11:46:23 +01:00
|
|
|
// remove File not present in current toUpload set
|
2020-01-31 14:56:51 +01:00
|
|
|
log.debug("Remote file "+f+" is not in requested set. Deleting it..");
|
|
|
|
z.deleteFile(dep, f);
|
|
|
|
}catch(Throwable t) {
|
2020-02-03 18:39:11 +01:00
|
|
|
log.warn("Unable to delete "+f,t);
|
2020-01-24 12:46:13 +01:00
|
|
|
}
|
2020-01-31 14:56:51 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 12:46:13 +01:00
|
|
|
|
2020-01-31 14:56:51 +01:00
|
|
|
|
|
|
|
log.debug("Going to push additional resources for "+deposition.getTitle()+" ID : "+deposition.getId());
|
|
|
|
|
2022-03-02 11:46:23 +01:00
|
|
|
for(Map.Entry<String,DownloadedFile> e : resourceMap.entrySet()){
|
|
|
|
DownloadedFile downloadedFile=e.getValue();
|
|
|
|
if(!alreadyExistingFiles.contains(e.getKey()))
|
|
|
|
try{
|
|
|
|
// Upload new file
|
|
|
|
z.uploadFile(dep,downloadedFile.getToUseFileName(),downloadedFile.getFile());
|
2020-01-24 12:46:13 +01:00
|
|
|
}catch(Throwable t) {
|
2022-03-02 11:46:23 +01:00
|
|
|
log.warn("Unable to upload "+downloadedFile.getSource().getName(),t);
|
2020-01-24 12:46:13 +01:00
|
|
|
}
|
2020-01-31 14:56:51 +01:00
|
|
|
}
|
2022-03-02 11:46:23 +01:00
|
|
|
|
2020-01-24 12:46:13 +01:00
|
|
|
return z.readDeposition(dep.getId());
|
|
|
|
}
|
2020-01-31 14:56:51 +01:00
|
|
|
|
|
|
|
|
2020-01-24 12:46:13 +01:00
|
|
|
}
|