Skip to content

Commit

Permalink
[kbss-cvut/termit-ui#449] Modify Excel template download so that it (…
Browse files Browse the repository at this point in the history
…hopefully) works also in a Docker container.
  • Loading branch information
ledsoft committed Aug 17, 2024
1 parent 7d0ecbd commit ac67590
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

FROM maven:3-eclipse-temurin-17 as build
FROM maven:3-eclipse-temurin-17 AS build

WORKDIR /termit

Expand All @@ -31,7 +31,7 @@ COPY src src

RUN mvn package -B -P graphdb,standalone -DskipTests=true

FROM eclipse-temurin:17-jdk-alpine as runtime
FROM eclipse-temurin:17-jdk-alpine AS runtime
COPY --from=build /termit/target/termit.jar termit.jar

EXPOSE 8080
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import cz.cvut.kbss.termit.dto.listing.VocabularyDto;
import cz.cvut.kbss.termit.event.VocabularyCreatedEvent;
import cz.cvut.kbss.termit.exception.NotFoundException;
import cz.cvut.kbss.termit.exception.TermItException;
import cz.cvut.kbss.termit.model.Vocabulary;
import cz.cvut.kbss.termit.model.acl.AccessControlList;
import cz.cvut.kbss.termit.model.acl.AccessControlRecord;
Expand All @@ -44,6 +43,7 @@
import cz.cvut.kbss.termit.service.security.authorization.VocabularyAuthorizationService;
import cz.cvut.kbss.termit.service.snapshot.SnapshotProvider;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.TypeAwareClasspathResource;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand All @@ -61,7 +61,6 @@

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -259,15 +258,14 @@ public Vocabulary importVocabulary(URI vocabularyIri, MultipartFile file) {
*/
public TypeAwareResource getExcelTemplateFile() {
final Configuration config = context.getBean(Configuration.class);
final File templateFile = config.getTemplate().getExcelImport().map(File::new).orElseGet(() -> {
try {
assert getClass().getClassLoader().getResource("template/termit-import.xlsx") != null;
return new File(getClass().getClassLoader().getResource("template/termit-import.xlsx").toURI());
} catch (URISyntaxException e) {
throw new TermItException("Fatal error, unable to load Excel template file.", e);
}
});
return new TypeAwareFileSystemResource(templateFile, ExportFormat.EXCEL.getMediaType());
return config.getTemplate().getExcelImport().map(File::new)
.map(f -> (TypeAwareResource) new TypeAwareFileSystemResource(f,
ExportFormat.EXCEL.getMediaType()))
.orElseGet(() -> {
assert getClass().getClassLoader().getResource("template/termit-import.xlsx") != null;
return new TypeAwareClasspathResource("template/termit-import.xlsx",
ExportFormat.EXCEL.getMediaType());
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TypeAwareByteArrayResource)) {
if (!(o instanceof TypeAwareByteArrayResource that)) {
return false;
}
if (!super.equals(o)) {
return false;
}
TypeAwareByteArrayResource that = (TypeAwareByteArrayResource) o;
return Objects.equals(mediaType, that.mediaType) &&
Objects.equals(fileExtension, that.fileExtension);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cz.cvut.kbss.termit.util;

import org.springframework.core.io.ClassPathResource;

import java.util.Optional;

/**
* Implementation of {@link TypeAwareResource} for files on classpath.
*/
public class TypeAwareClasspathResource extends ClassPathResource implements TypeAwareResource {

private final String mediaType;

public TypeAwareClasspathResource(String path, String mediaType) {
super(path);
this.mediaType = mediaType;
}

@Override
public Optional<String> getMediaType() {
return Optional.ofNullable(mediaType);
}

@Override
public Optional<String> getFileExtension() {
return getPath().contains(".") ? Optional.of(getPath().substring(getPath().lastIndexOf("."))) :
Optional.empty();
}
}
2 changes: 2 additions & 0 deletions src/main/java/cz/cvut/kbss/termit/util/TypeAwareResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* <p>
* Allows to get MIME type of the resource and the associated file extension. However, both methods return
* {@link Optional} to accommodate resources which may not support this feature.
*
* TODO Move all implementations into the same package!
*/
public interface TypeAwareResource extends Resource {

Expand Down

0 comments on commit ac67590

Please sign in to comment.