Skip to content

Commit

Permalink
improve robustness of archive reader and library metadata reading
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermarian committed Aug 10, 2024
1 parent 572e4c0 commit 3c999ac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
4 changes: 4 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>vorbisspi</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.io.IOUtils;
import studio.core.v1.Constants;
import studio.core.v1.model.*;
Expand All @@ -22,22 +24,20 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ArchiveStoryPackReader {

public StoryPackMetadata readMetadata(InputStream inputStream) throws IOException {
// Zip archive contains a json file and separate assets
ZipInputStream zis = new ZipInputStream(inputStream);
ZipArchiveInputStream zis = new ZipArchiveInputStream(inputStream);

// Pack metadata model
StoryPackMetadata metadata = new StoryPackMetadata(Constants.PACK_FORMAT_ARCHIVE);

boolean hasStoryJsonEntry = false;


ZipEntry entry;
ZipArchiveEntry entry;
while((entry = zis.getNextEntry()) != null) {
// Story descriptor file: story.json
if (!entry.isDirectory() && entry.getName().equalsIgnoreCase("story.json")) {
Expand Down Expand Up @@ -81,7 +81,7 @@ else if (!entry.isDirectory() && entry.getName().startsWith("assets/")) {
public StoryPack read(InputStream inputStream) throws IOException {

// Zip archive contains a json file and separate assets
ZipInputStream zis = new ZipInputStream(inputStream);
ZipArchiveInputStream zis = new ZipArchiveInputStream(inputStream);

// Store assets bytes
TreeMap<String, byte[]> assets = new TreeMap<>();
Expand All @@ -100,7 +100,7 @@ public StoryPack read(InputStream inputStream) throws IOException {
boolean nightModeAvailable = false;


ZipEntry entry;
ZipArchiveEntry entry;
while((entry = zis.getNextEntry()) != null) {
// Story descriptor file: story.json
if (!entry.isDirectory() && entry.getName().equalsIgnoreCase("story.json")) {
Expand Down
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.27.0</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
7 changes: 5 additions & 2 deletions web-ui/src/main/java/studio/webui/service/LibraryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,9 @@ private Optional<LibraryPack> readPackFile(Path path) {
if (meta != null) {
return Optional.of(new LibraryPack(path, Files.getLastModifiedTime(path).toMillis() , meta));
}
LOGGER.warn("Failed to read metadata for story pack: " + path.toString());
return Optional.empty();
} catch (IOException e) {
} catch (Exception e) {
LOGGER.error("Failed to read archive-format pack " + path.toString() + " from local library", e);
return Optional.empty();
}
Expand All @@ -456,8 +457,9 @@ private Optional<LibraryPack> readPackFile(Path path) {
meta.setSectorSize(packSectorSize);
return Optional.of(new LibraryPack(path, Files.getLastModifiedTime(path).toMillis() , meta));
}
LOGGER.warn("Failed to read metadata for story pack: " + path.toString());
return Optional.empty();
} catch (IOException e) {
} catch (Exception e) {
LOGGER.error("Failed to read raw format pack " + path.toString() + " from local library", e);
return Optional.empty();
}
Expand All @@ -471,6 +473,7 @@ private Optional<LibraryPack> readPackFile(Path path) {
meta.setSectorSize(packSectorSize);
return Optional.of(new LibraryPack(path, Files.getLastModifiedTime(path).toMillis() , meta));
}
LOGGER.warn("Failed to read metadata for story pack: " + path.toString());
return Optional.empty();
} catch (Exception e) {
LOGGER.error("Failed to read FS format pack " + path.toString() + " from local library", e);
Expand Down

0 comments on commit 3c999ac

Please sign in to comment.