use of nl.knaw.huygens.timbuctoo.remote.rs.download.RemoteFile in project timbuctoo by HuygensING.
the class Import method importData.
@POST
@Produces("application/json")
public Response importData(@HeaderParam("Authorization") String authorization, @QueryParam("forceCreation") boolean forceCreation, ImportData importData) throws DataStoreCreationException {
final Either<Response, Response> responses = authCheck.getOrCreate(authorization, importData.userId, importData.dataSetId, forceCreation).flatMap(userAndDs -> authCheck.hasAdminAccess(userAndDs.getLeft(), userAndDs.getRight())).map(userAndDs -> {
final DataSet dataSet = userAndDs.getRight();
ImportManager importManager = dataSet.getImportManager();
try {
LOG.info("Loading files");
Iterator<RemoteFile> files = resourceSyncFileLoader.loadFiles(importData.source.toString()).iterator();
LOG.info("Found files '{}'", files.hasNext());
ResourceSyncResport resourceSyncResport = new ResourceSyncResport();
while (files.hasNext()) {
RemoteFile file = files.next();
MediaType parsedMediatype = MediaType.APPLICATION_OCTET_STREAM_TYPE;
try {
parsedMediatype = MediaType.valueOf(file.getMimeType());
} catch (IllegalArgumentException e) {
LOG.error("Failed to get mediatype", e);
}
if (importManager.isRdfTypeSupported(parsedMediatype)) {
resourceSyncResport.importedFiles.add(file.getUrl());
importManager.addLog(dataSet.getMetadata().getBaseUri(), dataSet.getMetadata().getBaseUri(), file.getUrl().substring(file.getUrl().lastIndexOf('/') + 1), file.getData(), Optional.of(Charsets.UTF_8), parsedMediatype);
} else {
resourceSyncResport.ignoredFiles.add(file.getUrl());
importManager.addFile(file.getData(), file.getUrl(), parsedMediatype);
}
}
return Response.ok(resourceSyncResport).build();
} catch (Exception e) {
LOG.error("Could not read files to import", e);
return Response.serverError().build();
}
});
if (responses.isLeft()) {
return responses.getLeft();
} else {
return responses.get();
}
}
Aggregations