EntryDownloader with files

This commit is contained in:
gexce
2017-09-22 16:22:04 +02:00
parent d04c957680
commit cee435f981
5 changed files with 158 additions and 55 deletions
@@ -1,5 +1,13 @@
package com.bayer.edam.migration;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@@ -17,7 +25,7 @@ import com.bayer.edam.migration.mam.Entry.Download;
import com.bayer.edam.migration.mam.Entrylist;
import com.fasterxml.jackson.databind.ObjectMapper;
//@SpringBootApplication
@SpringBootApplication
@PropertySource("file:secret.properties")
public class EntryDownloader implements CommandLineRunner {
@@ -37,7 +45,7 @@ public class EntryDownloader implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
public void run(String... strings) {
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Authorization", "Basic " + mamAuth);
@@ -51,18 +59,69 @@ public class EntryDownloader implements CommandLineRunner {
lastPage = entrylist.lastPage;
for (int i = 0; i < entrylist.entries.length; i++) {
String entryJson = restTemplate.exchange(mamHost + "/rest/migration/entry/{id}", HttpMethod.GET, entity, String.class, entrylist.entries[i].mediaAssistantId).getBody();
Entry entry = jsonMapper.readValue(entryJson, Entry.class);
long aggregatedFilesize = 0;
for (Download download : entry.downloadList) {
aggregatedFilesize += download.size;
try {
String entryJson = restTemplate.exchange(mamHost + "/rest/migration/entry/{id}", HttpMethod.GET, entity, String.class, entrylist.entries[i].mediaAssistantId).getBody();
Entry entry = jsonMapper.readValue(entryJson, Entry.class);
List<Download> downloadSelection = new ArrayList<Download>();
// Select download: Download original only. If original is not available, download largest file
Download selectedDownload = null;
for (Download download : entry.downloadList) {
if (download.entryFileVariation.equals("ORIGINAL")) {
selectedDownload = download;
break;
}
if (selectedDownload == null) {
selectedDownload = download;
}
else if (selectedDownload.size < download.size) {
selectedDownload = download;
}
}
if (selectedDownload != null)
downloadSelection.add(selectedDownload);
// TODO extract, if zip --> postponed
for (Download download : entry.downloadListPublicFolder) {
downloadSelection.add(download);
}
// clean download selection
HashSet<String> hrefs = new HashSet<String>();
List<Download> cleanedDownloads = new ArrayList<Download>();
for (Download download : downloadSelection) {
if (hrefs.add(download.href)) {
cleanedDownloads.add(download);
}
}
long aggregatedFilesize = 0;
for (Download download : cleanedDownloads) {
aggregatedFilesize += download.size;
}
log.info(entry.id + separator + entry.mediaAssistantId + separator + entry.stateId + separator + aggregatedFilesize + separator + entryJson);
new File("E:\\" + entry.id).mkdir();
for (Download download : cleanedDownloads) {
String filenameToUse;
if (download.fileNameOnDisc != null)
filenameToUse = download.fileNameOnDisc;
else
filenameToUse = download.fileName;
FileUtils.copyURLToFile(new URL(download.href), new File("E:\\" + entry.id + "\\" + filenameToUse), 4000, 240000);
}
}
for (Download download : entry.downloadListPublicFolder) {
aggregatedFilesize += download.size;
catch (IOException ioe) {
log.error(ioe.toString());
ioe.printStackTrace();
}
log.info(entry.id + separator + entry.mediaAssistantId + separator + entry.stateId + separator + aggregatedFilesize + separator + entryJson);
}
}
}