-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: Split file reader/writer registries and resource factory
- Loading branch information
Showing
10 changed files
with
222 additions
and
212 deletions.
There are no files selected for viewing
43 changes: 0 additions & 43 deletions
43
core/riot-file/src/main/java/com/redis/riot/file/AbstractRegistry.java
This file was deleted.
Oops, something went wrong.
47 changes: 10 additions & 37 deletions
47
core/riot-file/src/main/java/com/redis/riot/file/FileReaderRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
core/riot-file/src/main/java/com/redis/riot/file/FileUtils.java
This file was deleted.
Oops, something went wrong.
60 changes: 10 additions & 50 deletions
60
core/riot-file/src/main/java/com/redis/riot/file/FileWriterRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
core/riot-file/src/main/java/com/redis/riot/file/ResourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.redis.riot.file; | ||
|
||
import java.io.IOException; | ||
import java.net.FileNameMap; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.zip.GZIPInputStream; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
import org.springframework.core.io.ProtocolResolver; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.WritableResource; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.MimeType; | ||
import org.springframework.util.MimeTypeUtils; | ||
|
||
public class ResourceFactory { | ||
|
||
public static final String GZ_SUFFIX = ".gz"; | ||
|
||
public static final MimeType CSV = new MimeType("text", "csv"); | ||
public static final MimeType PSV = new MimeType("text", "psv"); | ||
public static final MimeType TSV = new MimeType("text", "tsv"); | ||
public static final MimeType TEXT = new MimeType("text", "plain"); | ||
public static final MimeType JSON = MimeTypeUtils.APPLICATION_JSON; | ||
public static final MimeType JSON_LINES = new MimeType("application", "jsonlines"); | ||
public static final MimeType XML = MimeTypeUtils.APPLICATION_XML; | ||
|
||
private ResourceMap resourceMap = defaultResourceMap(); | ||
private Set<ProtocolResolver> protocolResolvers = new HashSet<>(); | ||
|
||
public MimeType type(Resource resource) { | ||
return MimeType.valueOf(resourceMap.getContentTypeFor(resource)); | ||
} | ||
|
||
public MimeType type(Resource resource, FileOptions options) { | ||
if (options.getContentType() == null) { | ||
return MimeType.valueOf(resourceMap.getContentTypeFor(resource)); | ||
} | ||
return options.getContentType(); | ||
} | ||
|
||
private static class JsonLinesFileNameMap implements FileNameMap { | ||
|
||
public static final String JSONL_SUFFIX = ".jsonl"; | ||
|
||
@Override | ||
public String getContentTypeFor(String fileName) { | ||
if (fileName == null) { | ||
return null; | ||
} | ||
if (fileName.endsWith(JSONL_SUFFIX)) { | ||
return JSON_LINES.toString(); | ||
} | ||
return null; | ||
} | ||
|
||
} | ||
|
||
public static ResourceMap defaultResourceMap() { | ||
RiotResourceMap resourceMap = new RiotResourceMap(); | ||
resourceMap.addFileNameMap(new JsonLinesFileNameMap()); | ||
return resourceMap; | ||
} | ||
|
||
public void addProtocolResolver(ProtocolResolver protocolResolver) { | ||
protocolResolvers.add(protocolResolver); | ||
} | ||
|
||
public Resource resource(String location, FileOptions options) throws IOException { | ||
Resource resource = createResource(location, options); | ||
if (isGzip(resource, options)) { | ||
GZIPInputStream gzipInputStream = new GZIPInputStream(resource.getInputStream()); | ||
return new NamedInputStreamResource(gzipInputStream, resource.getFilename(), resource.getDescription()); | ||
} | ||
return resource; | ||
} | ||
|
||
private boolean isGzip(Resource resource, FileOptions options) { | ||
return options.isGzip() || isGzip(resource.getFilename()); | ||
} | ||
|
||
public WritableResource writableResource(String location, FileOptions options) throws IOException { | ||
Resource resource = createResource(location, options); | ||
Assert.isInstanceOf(WritableResource.class, resource); | ||
if (options.isGzip() || isGzip(resource.getFilename())) { | ||
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(((WritableResource) resource).getOutputStream()); | ||
return new OutputStreamResource(gzipOutputStream, resource.getFilename(), resource.getDescription()); | ||
} | ||
return (WritableResource) resource; | ||
} | ||
|
||
private Resource createResource(String location, FileOptions options) { | ||
RiotResourceLoader resourceLoader = new RiotResourceLoader(); | ||
protocolResolvers.forEach(resourceLoader::addProtocolResolver); | ||
resourceLoader.getS3ProtocolResolver().setClientSupplier(options.getS3Options()::client); | ||
resourceLoader.getGoogleStorageProtocolResolver() | ||
.setStorageSupplier(options.getGoogleStorageOptions()::storage); | ||
return resourceLoader.getResource(location); | ||
} | ||
|
||
public static boolean isGzip(String filename) { | ||
return filename.endsWith(GZ_SUFFIX); | ||
} | ||
|
||
public static String stripGzipSuffix(String filename) { | ||
if (isGzip(filename)) { | ||
return filename.substring(0, filename.length() - GZ_SUFFIX.length()); | ||
} | ||
return filename; | ||
} | ||
|
||
public ResourceMap getResourceMap() { | ||
return resourceMap; | ||
} | ||
|
||
public void setResourceMap(ResourceMap resourceMap) { | ||
this.resourceMap = resourceMap; | ||
} | ||
|
||
} |
Oops, something went wrong.