Small fixes to path module
This commit is contained in:
@@ -8,6 +8,7 @@ import java.net.URLDecoder;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import sh.rhiobet.lalafin.api.configuration.FileApiConfiguration;
|
import sh.rhiobet.lalafin.api.configuration.FileApiConfiguration;
|
||||||
|
|
||||||
@@ -22,8 +23,22 @@ public class PathFactory {
|
|||||||
} else {
|
} else {
|
||||||
java.nio.file.Path rootFolderPath = Paths.get(this.fileApiConfiguration.directory());
|
java.nio.file.Path rootFolderPath = Paths.get(this.fileApiConfiguration.directory());
|
||||||
|
|
||||||
if (names.size() > 1 && names.get(names.size() - 2).getPath().endsWith(".zip")) {
|
int zipIndex = -1;
|
||||||
return new ZipEntryPath(names, rootFolderPath);
|
for (int i = 0; i < names.size(); i++) {
|
||||||
|
if (names.get(i).getPath().endsWith(".zip")) {
|
||||||
|
zipIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (zipIndex >= 0 && zipIndex < names.size() - 1) {
|
||||||
|
FileSystemPath zipFilePath =
|
||||||
|
new FileSystemPath(names.subList(0, zipIndex + 1), rootFolderPath);
|
||||||
|
String zipEntryName = names.stream()
|
||||||
|
.skip(zipIndex + 1)
|
||||||
|
.map(PathSegment::getPath)
|
||||||
|
.collect(Collectors.joining("/"));
|
||||||
|
return new ZipEntryPath(zipFilePath, zipEntryName);
|
||||||
} else {
|
} else {
|
||||||
return new FileSystemPath(names, rootFolderPath);
|
return new FileSystemPath(names, rootFolderPath);
|
||||||
}
|
}
|
||||||
@@ -38,7 +53,8 @@ public class PathFactory {
|
|||||||
if (decoded.equals(current)) break;
|
if (decoded.equals(current)) break;
|
||||||
current = decoded;
|
current = decoded;
|
||||||
}
|
}
|
||||||
return current.contains("/") || current.contains("\0") || current.equals("..") || current.equals(".");
|
return current.contains("/") || current.contains("\0") || current.equals("..")
|
||||||
|
|| current.equals(".");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,15 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
import org.jboss.resteasy.reactive.common.util.Encode;
|
import org.jboss.resteasy.reactive.common.util.Encode;
|
||||||
|
|
||||||
public final class ZipEntryPath implements Path {
|
public final class ZipEntryPath implements Path {
|
||||||
|
private long size = 0;
|
||||||
|
private boolean exists = false;
|
||||||
|
|
||||||
FileSystemPath zipFilePath;
|
FileSystemPath zipFilePath;
|
||||||
List<String> segments;
|
List<String> segments;
|
||||||
|
|
||||||
@@ -38,20 +42,18 @@ public final class ZipEntryPath implements Path {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getSize() {
|
public long getSize() {
|
||||||
try (ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile())) {
|
if (this.size == 0) {
|
||||||
return zipFile.getEntry(this.segments.getLast()).getSize();
|
fetchEntryData();
|
||||||
} catch (IOException ignored) {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
return this.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean exists() {
|
public boolean exists() {
|
||||||
try (ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile())) {
|
if (!this.exists) {
|
||||||
return zipFile.getEntry(segments.getLast()) != null;
|
fetchEntryData();
|
||||||
} catch (IOException ignored) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return this.exists;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -70,9 +72,19 @@ public final class ZipEntryPath implements Path {
|
|||||||
return new ZipEntryInputStream(zipFile,
|
return new ZipEntryInputStream(zipFile,
|
||||||
zipFile.getInputStream(zipFile.getEntry(this.segments.getLast())));
|
zipFile.getInputStream(zipFile.getEntry(this.segments.getLast())));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class ZipEntryInputStream extends InputStream {
|
private void fetchEntryData() {
|
||||||
|
try (ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile())) {
|
||||||
|
ZipEntry zipEntry = zipFile.getEntry(this.segments.getLast());
|
||||||
|
this.exists = zipEntry != null;
|
||||||
|
|
||||||
|
if (this.exists) {
|
||||||
|
this.size = zipEntry.getSize();
|
||||||
|
}
|
||||||
|
} catch (IOException ignored) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ZipEntryInputStream extends InputStream {
|
||||||
private final ZipFile zipFile;
|
private final ZipFile zipFile;
|
||||||
private final InputStream is;
|
private final InputStream is;
|
||||||
|
|
||||||
@@ -129,4 +141,5 @@ class ZipEntryInputStream extends InputStream {
|
|||||||
this.zipFile.close();
|
this.zipFile.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import java.io.OutputStream;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
@@ -62,7 +63,8 @@ public class ThumbnailPathPlugin extends PathAccessor implements PathPlugin {
|
|||||||
private Optional<java.nio.file.Path> getThumbnailPath(Path path) {
|
private Optional<java.nio.file.Path> getThumbnailPath(Path path) {
|
||||||
try {
|
try {
|
||||||
return Files.list(getAbsolutePath(path).getParent().resolve(".thumbnails"))
|
return Files.list(getAbsolutePath(path).getParent().resolve(".thumbnails"))
|
||||||
.filter(f -> f.getFileName().toString().matches("^" + path.getFilename() + "(\\.[^.]+)*$"))
|
.filter(f -> f.getFileName().toString()
|
||||||
|
.matches("^" + Pattern.quote(path.getFilename()) + "(\\.[^.]+)*$"))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
} catch (IOException ignored) {
|
} catch (IOException ignored) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|||||||
Reference in New Issue
Block a user