Skip to content

Commit

Permalink
[Issue comixed#197] Move image type identification in the base archiv…
Browse files Browse the repository at this point in the history
…e adaptor.
  • Loading branch information
mcpierce committed Mar 24, 2020
1 parent 8d9e0cb commit 25d738f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.utils.IOUtils;
Expand All @@ -45,6 +42,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
* <code>AbstractArchiveAdaptor</code> provides a foundation for creating new instances of {@link
Expand All @@ -61,10 +59,13 @@
public abstract class AbstractArchiveAdaptor<I> implements ArchiveAdaptor, InitializingBean {
@Autowired protected FileTypeIdentifier fileTypeIdentifier;
@Autowired protected ComicInfoEntryAdaptor comicInfoEntryAdaptor;
protected List<EntryLoaderForType> loaders = new ArrayList<>();
protected Map<String, EntryLoader> entryLoaders = new HashMap<>();
@Autowired private ApplicationContext context;
@Autowired private ComicFileHandler comicFileHandler;

protected List<EntryLoaderForType> loaders = new ArrayList<>();
protected Map<String, EntryLoader> entryLoaders = new HashMap<>();
private Set<String> imageTypes = new HashSet<>();

private String defaultExtension;

public AbstractArchiveAdaptor(String defaultExtension) {
Expand All @@ -79,6 +80,10 @@ public void afterPropertiesSet() throws Exception {
if (entry.isValid()) {
if (this.context.containsBean(entry.bean)) {
this.entryLoaders.put(entry.type, (EntryLoader) this.context.getBean(entry.bean));
if (entry.entryType == ArchiveEntryType.IMAGE) {
this.log.debug("Adding image adaptor: {}={}", entry.entryType, entry.bean);
this.imageTypes.add(entry.type);
}
} else {
this.log.debug("No such entry adaptor bean: {}", entry.bean);
}
Expand Down Expand Up @@ -295,7 +300,7 @@ public String getFirstImageFileName(String filename) throws ArchiveAdaptorExcept
byte[] content = this.loadSingleFileInternal(archiveRef, entry);
String contentType = this.fileTypeIdentifier.subtypeFor(new ByteArrayInputStream(content));

if (contentType != null && FileTypeIdentifier.IMAGE_TYPES.contains(contentType)) {
if (contentType != null && this.imageTypes.contains(contentType)) {
result = entry;
break;
}
Expand All @@ -315,12 +320,12 @@ public byte[] encodeFileToStream(Map<String, byte[]> entries)
public static class EntryLoaderForType {
private String type;
private String bean;
private ArchiveEntryType entryType;

public boolean isValid() {
return (this.type != null)
&& !this.type.isEmpty()
&& (this.bean != null)
&& !this.bean.isEmpty();
return !StringUtils.isEmpty(this.type)
&& !StringUtils.isEmpty(this.bean)
&& this.entryType != null;
}

public void setBean(String bean) {
Expand All @@ -330,5 +335,9 @@ public void setBean(String bean) {
public void setType(String type) {
this.type = type;
}

public void setEntryType(ArchiveEntryType entryType) {
this.entryType = entryType;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixed.adaptors.archive;

public enum ArchiveEntryType {
FILE,
IMAGE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.apache.tika.Tika;
import org.apache.tika.metadata.Metadata;
Expand All @@ -37,10 +35,6 @@
@Component
@Log4j2
public class FileTypeIdentifier {
// TODO this needs to be loaded from the entryloaders.properties file
public static final List<String> IMAGE_TYPES =
Arrays.asList(new String[] {"jpeg", "png", "gif", "webp"});

@Autowired private Tika tika;
@Autowired private Metadata metadata;

Expand Down
4 changes: 4 additions & 0 deletions comixed-library/src/main/resources/entryloaders.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Comic entry loaders
comic.entry.loaders[0].type=xml
comic.entry.loaders[0].entryType=FILE
comic.entry.loaders[0].bean=filenameEntryLoader
comic.entry.loaders[1].entryType=IMAGE
comic.entry.loaders[1].type=jpeg
comic.entry.loaders[1].bean=imageEntryLoader
comic.entry.loaders[2].entryType=IMAGE
comic.entry.loaders[2].type=png
comic.entry.loaders[2].bean=imageEntryLoader
comic.entry.loaders[3].entryType=IMAGE
comic.entry.loaders[3].type=webp
comic.entry.loaders[3].bean=imageEntryLoader

Expand Down

0 comments on commit 25d738f

Please sign in to comment.