Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4370 - Download knowledge bases in compressed form #4372

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
*/
package de.tudarmstadt.ukp.inception.support.wicket;

import static java.util.Arrays.asList;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.Serializable;
import java.lang.invoke.MethodHandles;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.wicket.util.resource.AbstractResourceStream;
Expand All @@ -42,11 +45,13 @@ public class PipedStreamResource

private final DataSupplier supplier;
private PipedInputStream is;
private PipedOutputStream os;
private OutputStream os;
private List<Filter> filters;

public PipedStreamResource(DataSupplier aSupplier)
public PipedStreamResource(DataSupplier aSupplier, Filter... aFilters)
{
supplier = aSupplier;
filters = asList(aFilters);
}

@Override
Expand All @@ -56,6 +61,9 @@ public InputStream getInputStream() throws ResourceStreamNotFoundException

try {
os = new PipedOutputStream(is);
for (var filter : filters) {
os = filter.apply(os);
}
}
catch (IOException e) {
throw new ResourceStreamNotFoundException(e);
Expand Down Expand Up @@ -106,4 +114,11 @@ public interface DataSupplier
{
void write(OutputStream aOS) throws IOException;
}

@FunctionalInterface
public interface Filter
extends Serializable
{
OutputStream apply(OutputStream aT) throws IOException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ kb.language=Language
kb.supportConceptLinking=Supports Concept Linking
kb.queryLimit=Result Limit for SPARQL queries

kb.local.fileupload.supported.list=Supported file types: RDF (XML/JSON/Binary), JSON-LD, N-Triples, N-Quads, TriG, TriX
kb.local.fileupload.supported.headerlist: Supported file types: RDF (XML/JSON/Binary), JSON-LD, N-Triples, N-Quads, TriG, TriX
kb.local.fileupload.supported.list=Supported: RDF (XML/JSON/Binary), JSON-LD, N-Triples, N-Quads, TriG, TriX, Turtle (optionally gzipped)

kb.wizard.steps.type.language=Language
kb.wizard.steps.type.name=Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.wicket.AttributeModifier;
Expand Down Expand Up @@ -76,7 +77,7 @@
import de.tudarmstadt.ukp.inception.support.io.FileUploadDownloadHelper;
import de.tudarmstadt.ukp.inception.support.lambda.LambdaAjaxLink;
import de.tudarmstadt.ukp.inception.support.wicket.AjaxDownloadLink;
import de.tudarmstadt.ukp.inception.support.wicket.TempFileResource;
import de.tudarmstadt.ukp.inception.support.wicket.PipedStreamResource;
import de.tudarmstadt.ukp.inception.ui.kb.project.AccessSpecificSettingsPanel;
import de.tudarmstadt.ukp.inception.ui.kb.project.KnowledgeBaseInfoPanel;
import de.tudarmstadt.ukp.inception.ui.kb.project.KnowledgeBaseWrapper;
Expand Down Expand Up @@ -254,10 +255,10 @@ protected void populateItem(ListItem<String> item)
{
// creates an appropriately labeled {@link AjaxDownloadLink} which triggers the
// download of the contents of the current KB in the given format
String fileExtension = item.getModelObject();
KnowledgeBase kb = LocalRepositorySettingsPanel.this.getModel().getObject().getKb();
Model<String> exportFileNameModel = Model.of(kb.getName() + "." + fileExtension);
AjaxDownloadLink exportLink = new AjaxDownloadLink("link", exportFileNameModel,
var fileExtension = item.getModelObject();
var kb = LocalRepositorySettingsPanel.this.getModel().getObject().getKb();
var exportFileNameModel = Model.of(kb.getName() + "." + fileExtension + ".gz");
var exportLink = new AjaxDownloadLink("link", exportFileNameModel,
LoadableDetachableModel.of(() -> actionExport(fileExtension)));
exportLink.add(new Label("label", new ResourceModel("kb.export." + fileExtension)));
item.add(exportLink);
Expand Down Expand Up @@ -378,8 +379,10 @@ private void actionClear(AjaxRequestTarget aTarget)

private IResourceStream actionExport(String rdfFormatFileExt)
{
return new TempFileResource((os) -> kbService.exportData(getModel().getObject().getKb(),
getRdfFormatForFileExt(rdfFormatFileExt), os));
var kb = getModel().getObject().getKb();
var format = getRdfFormatForFileExt(rdfFormatFileExt);
return new PipedStreamResource((os) -> kbService.exportData(kb, format, os),
GZIPOutputStream::new);
}

private void actionDownloadKbAndSetIRIs(AjaxRequestTarget aTarget)
Expand Down