Skip to content

Commit

Permalink
partially fix MIME type lookup by file extension
Browse files Browse the repository at this point in the history
this is not particularly efficient but it's only used in local operation
  • Loading branch information
abyrd committed Jan 26, 2024
1 parent 67fbb89 commit c0bd7ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ private Object getFile (Request req, Response res) throws Exception {
FileStorageKey key = new FileStorageKey(category, filename);
File file = fileStorage.getFile(key);
FileStorageFormat format = FileStorageFormat.fromFilename(filename);
res.type(format.mimeType);
if (format != null) {
res.type(format.mimeType);
}

// If the content-encoding is set to gzip, Spark automatically gzips the response. This double-gzips anything
// that was already gzipped. Some of our files are already gzipped, and we rely on the the client browser to
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/conveyal/file/FileStorageFormat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.conveyal.file;

import java.util.Locale;

/**
* An enumeration of all the file types we handle as uploads, derived internal data, or work products.
* Really this should be a union of several enumerated types (upload/internal/product) but Java does not allow this.
Expand Down Expand Up @@ -37,7 +39,12 @@ public enum FileStorageFormat {
}

public static FileStorageFormat fromFilename (String filename) {
String extension = filename.substring(filename.lastIndexOf(".") + 1);
return FileStorageFormat.valueOf(extension.toUpperCase());
String extension = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase(Locale.ROOT);
for (FileStorageFormat format : FileStorageFormat.values()) {
if (format.extension.equals(extension)) {
return format;
}
}
return null;
}
}

0 comments on commit c0bd7ea

Please sign in to comment.