Compare commits

..

2 Commits

Author SHA1 Message Date
8c0bb2ce4a Refactor accessors 2026-04-18 21:32:34 +02:00
5924ca5647 More fixes in zip entries for path 2026-04-18 20:41:03 +02:00
6 changed files with 141 additions and 3 deletions

View File

@@ -0,0 +1,7 @@
package sh.rhiobet.lalafin.access;
import sh.rhiobet.lalafin.path.Path;
public interface AccessService {
boolean checkAccess(Path path);
}

View File

@@ -0,0 +1,49 @@
package sh.rhiobet.lalafin.advent;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.regex.Pattern;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import sh.rhiobet.lalafin.access.AccessService;
import sh.rhiobet.lalafin.advent.AdventConfiguration.AdventEvent;
import sh.rhiobet.lalafin.path.Path;
@ApplicationScoped
@Named("advent")
public class AdventAccessService implements AccessService {
@Inject
AdventConfiguration adventConfiguration;
@Override
public boolean checkAccess(Path path) {
String pathUri = path.getURI();
Optional<AdventEvent> matchingAdvent = adventConfiguration.events().stream()
.filter(e -> pathUri.startsWith(e.path()))
.findFirst();
if (matchingAdvent.isEmpty()) {
return true;
}
String dayString = pathUri.replaceAll(
"^" + Pattern.quote(matchingAdvent.get().path()) + "/?($1)\\.?[^.]*$",
"$1");
try {
int day = Integer.parseInt(dayString);
LocalDateTime now = LocalDateTime.now();
return ((now.getYear() > matchingAdvent.get().year())
|| (now.getYear() == matchingAdvent.get().year()
&& now.getMonthValue() > matchingAdvent.get().month())
|| (now.getYear() == matchingAdvent.get().year()
&& now.getMonthValue() == matchingAdvent.get().month()
&& now.getDayOfMonth() >= day));
} catch (NumberFormatException ignored) {
return true;
}
}
}

View File

@@ -0,0 +1,15 @@
package sh.rhiobet.lalafin.advent;
import java.util.List;
import io.smallrye.config.ConfigMapping;
@ConfigMapping(prefix = "advent")
public interface AdventConfiguration {
public List<AdventEvent> events();
public static interface AdventEvent {
public String path();
public int year();
public int month();
}
}

View File

@@ -0,0 +1,17 @@
package sh.rhiobet.lalafin.file;
import java.util.List;
import java.util.Optional;
import io.smallrye.config.ConfigMapping;
@ConfigMapping(prefix = "file")
public interface FileConfiguration {
public String directory();
public Optional<List<String>> ignored();
public List<Route> routes();
public static interface Route {
public String path();
public Optional<List<String>> roles();
}
}

View File

@@ -0,0 +1,38 @@
package sh.rhiobet.lalafin.file;
import java.util.List;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import sh.rhiobet.lalafin.access.AccessService;
import sh.rhiobet.lalafin.api.configuration.FileApiConfiguration;
import sh.rhiobet.lalafin.api.configuration.FileApiConfiguration.Route;
import sh.rhiobet.lalafin.path.Path;
@ApplicationScoped
@Named("file/role")
public class FileRoleAccessService implements AccessService {
@Inject
SecurityIdentity securityIdentity;
@Inject
FileApiConfiguration fileApiConfiguration;
public boolean checkAccess(Path path) {
String pathUri = path.getURI();
List<Route> matchingRoutes = fileApiConfiguration.routes().stream()
.filter(r -> pathUri.startsWith(r.path()))
.toList();
if (matchingRoutes.isEmpty()) {
return false;
}
return matchingRoutes.stream()
.allMatch(r -> r.roles().isEmpty()
|| this.securityIdentity.getRoles().containsAll(r.roles().get()));
}
}

View File

@@ -72,12 +72,24 @@ public final class ZipEntryPath implements Path {
@Override @Override
public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
if (this.zipFilePath.exists()) {
ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile()); ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile());
return new ZipEntryInputStream(zipFile, ZipEntry zipEntry = zipFile.getEntry(this.entryName);
zipFile.getInputStream(zipFile.getEntry(this.entryName))); if (zipEntry != null) {
return new ZipEntryInputStream(zipFile, zipFile.getInputStream(zipEntry));
} else {
throw new IOException("Zip entry does not exist.");
}
} else {
throw new IOException("Zip file does not exist.");
}
} }
private void fetchEntryData() { private void fetchEntryData() {
if (!this.zipFilePath.exists()) {
return;
}
try (ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile())) { try (ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile())) {
ZipEntry zipEntry = zipFile.getEntry(this.entryName); ZipEntry zipEntry = zipFile.getEntry(this.entryName);
this.exists = zipEntry != null; this.exists = zipEntry != null;