Skip to content

Commit

Permalink
G2-1648 Implement & use file extensions as paths
Browse files Browse the repository at this point in the history
  • Loading branch information
gdgib committed Sep 25, 2024
1 parent cc1efcf commit 06f86da
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 161 deletions.
78 changes: 0 additions & 78 deletions ax-java/src/main/java/com/g2forge/alexandria/java/io/Filename.java

This file was deleted.

50 changes: 23 additions & 27 deletions ax-java/src/main/java/com/g2forge/alexandria/java/io/HPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@
@Helpers
public class HPath {
/**
* Get the file extension.
* Return a path in the specified filesystem which is equivalent to {@code path}. Maybe return {@code path} if it is already in the appropriate
* {@code fileSystem}.
*
* @param path The path to the file to get the extension of.
* @return The extension, including the period.
* @param fileSystem The target filesystem.
* @param path The path.
* @return A path with all the same components as {@code path} in the file system {@code fileSystem}.
*/
public String getExtension(Path path) {
return new Filename(path).getLastExtension();
public static Path ensureFS(final FileSystem fileSystem, final Path path) {
if (path.getFileSystem().equals(fileSystem)) return path;

if (path.isAbsolute() && ((HCollection.toCollection(fileSystem.getRootDirectories()).size() > 1) || (HCollection.toCollection(path.getFileSystem().getRootDirectories()).size() > 1))) throw new NotYetImplementedError("Can't translate paths between filesystems when the root directories are complex!");
Path current = fileSystem.getPath(path.isAbsolute() ? fileSystem.getSeparator() : "");
// Resolve all the names, if there are any non-empty ones
if ((path.getNameCount() != 1) || (path.getName(0).getFileName() != null)) {
for (final Path name : path) {
current = current.resolve(name.getFileName().toString());
}
}
return current;
}

/**
Expand All @@ -33,6 +45,12 @@ public static boolean isEmpty(Path path) {
return path.getNameCount() == 1 && path.getName(0).toString().isEmpty();
}

public static Path replaceFilename(Path path, final String filename) {
final Path parent = path.getParent();
if ((parent == null) || (!path.isAbsolute() && (path.getNameCount() == 1))) return path.getFileSystem().getPath(filename);
return parent.resolve(filename);
}

/**
* Resolve the {@code path} against the {@code base} after ensuring they're from the same filesystem.
*
Expand All @@ -45,26 +63,4 @@ public static boolean isEmpty(Path path) {
public static Path resolveFS(Path base, Path path) {
return base.resolve(ensureFS(base.getFileSystem(), path));
}

/**
* Return a path in the specified filesystem which is equivalent to {@code path}. Maybe return {@code path} if it is already in the appropriate
* {@code fileSystem}.
*
* @param fileSystem The target filesystem.
* @param path The path.
* @return A path with all the same components as {@code path} in the file system {@code fileSystem}.
*/
public static Path ensureFS(final FileSystem fileSystem, final Path path) {
if (path.getFileSystem().equals(fileSystem)) return path;

if (path.isAbsolute() && ((HCollection.toCollection(fileSystem.getRootDirectories()).size() > 1) || (HCollection.toCollection(path.getFileSystem().getRootDirectories()).size() > 1))) throw new NotYetImplementedError("Can't translate paths between filesystems when the root directories are complex!");
Path current = fileSystem.getPath(path.isAbsolute() ? fileSystem.getSeparator() : "");
// Resolve all the names, if there are any non-empty ones
if ((path.getNameCount() != 1) || (path.getName(0).getFileName() != null)) {
for (final Path name : path) {
current = current.resolve(name.getFileName().toString());
}
}
return current;
}
}

This file was deleted.

12 changes: 11 additions & 1 deletion ax-media/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<artifactId>ax-media</artifactId>
Expand All @@ -13,4 +15,12 @@

<name>Alexandria Media</name>
<description>Media types and file extensions.</description>

<dependencies>
<dependency>
<groupId>com.g2forge.alexandria</groupId>
<artifactId>ax-path</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
import java.nio.file.Path;

import com.g2forge.alexandria.java.core.marker.Helpers;
import com.g2forge.alexandria.java.io.Filename;

import lombok.experimental.UtilityClass;

@Helpers
@UtilityClass
public class HMedia {
public static IMediaType getMediaType(String filename) {
return MediaType.getRegistry().computeMediaType(new Filename(filename));
public static IMediaType getMediaType(Path path) {
return MediaType.getRegistry().computeMediaType(path);
}

public static IMediaType getMediaType(Path path) {
return MediaType.getRegistry().computeMediaType(new Filename(path));
public static IMediaType getMediaType(String filename) {
return MediaType.getRegistry().computeMediaType(filename);
}

public static boolean isText(Path path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
import java.util.Objects;

import com.g2forge.alexandria.java.core.helpers.HStream;
import com.g2forge.alexandria.java.io.Filename;
import com.g2forge.alexandria.path.path.filename.Filename;

public interface IMediaRegistry {
public default IMediaType computeMediaType(Filename filename) {
final String extension = filename.getLastExtension();
final String extension = filename.getLast();
return HStream.findOneOptional(getMediaTypes().stream().filter(mediaType -> mediaType.getFileExtensions().isMatch(extension)), () -> null);
}

public default IMediaType computeMediaType(Path filename) {
return computeMediaType(new Filename(filename));
return computeMediaType(Filename.fromPath(filename));
}

public default IMediaType computeMediaType(String filename) {
return computeMediaType(new Filename(filename));
return computeMediaType(Filename.fromString(filename));
}

public Collection<IMediaType> getMediaTypes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public enum MediaType implements IMediaType {
SVG(true, new SimpleFileExtensions("svg"), new MimeType("image", "svg+xml")),
HTML(true, new SimpleFileExtensions("html", "htm"), new MimeType("text", "html")),
CSS(true, new SimpleFileExtensions("css"), new MimeType("text", "css")),
Markdown(true, new SimpleFileExtensions("md"), new MimeType("text", "markdown"));
Markdown(true, new SimpleFileExtensions("md"), new MimeType("text", "markdown")),
MP3(true, new SimpleFileExtensions("mp3"), new MimeType("audio", "mpeg")),
M4A(true, new SimpleFileExtensions("m4a"), new MimeType("audio", "mp4")),
M4V(true, new SimpleFileExtensions("m4v"), new MimeType("video", "mp4"));

@Getter(lazy = true)
private static final IMediaRegistry registry = new MediaRegistry(MediaType.values());
Expand All @@ -32,7 +35,7 @@ public enum MediaType implements IMediaType {
protected final IFileExtensions fileExtensions;

protected final MimeType mimeType;

private MediaType(boolean text, IFileExtensions fileExtensions) {
this(text, fileExtensions, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

import org.junit.Test;

import com.g2forge.alexandria.java.io.Filename;
import com.g2forge.alexandria.test.HAssert;

public class TestHMedia {
@Test
public void foo() {
HAssert.assertNull(MediaType.getRegistry().computeMediaType(new Filename("file.foo")));
HAssert.assertNull(MediaType.getRegistry().computeMediaType("file.foo"));
}

@Test
public void jpg() {
HAssert.assertEquals(MediaType.JPG, MediaType.getRegistry().computeMediaType(new Filename("file.jpg")));
HAssert.assertEquals(MediaType.JPG, MediaType.getRegistry().computeMediaType(new Filename("file.jpeg")));
HAssert.assertEquals(MediaType.JPG, MediaType.getRegistry().computeMediaType("file.jpg"));
HAssert.assertEquals(MediaType.JPG, MediaType.getRegistry().computeMediaType("file.jpeg"));
}

@Test
public void xml() {
final IMediaType mediaType = MediaType.getRegistry().computeMediaType(new Filename("file.xml"));
final IMediaType mediaType = MediaType.getRegistry().computeMediaType("file.xml");
HAssert.assertEquals(MediaType.XML, mediaType);
HAssert.assertTrue(mediaType.isText());
}
Expand Down
Loading

0 comments on commit 06f86da

Please sign in to comment.