Skip to content

Commit

Permalink
Merge pull request #4372 from inception-project/feature/4370-Download…
Browse files Browse the repository at this point in the history
…-knowledge-bases-in-compressed-form

#4370 - Download knowledge bases in compressed form
  • Loading branch information
reckart authored Dec 10, 2023
2 parents a92f2a6 + f8e7791 commit 3a52674
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
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

0 comments on commit 3a52674

Please sign in to comment.