fix bugs and add html page

This commit is contained in:
2025-05-25 05:53:19 +03:00
parent 21f3d7e6ce
commit 0a7de4fb9b
4 changed files with 276 additions and 13 deletions

View File

@ -34,10 +34,9 @@ public class S3FileHandler {
}).get();
}
public ServerResponse downloadFile(ServerRequest request) throws ServletException, IOException {
return Optional.of(request.body(DirectoryPath.class))
.map(DirectoryPath::path)
.map(URI::toString)
public ServerResponse downloadFile(ServerRequest request) {
return Optional.of(request.pathVariable("path").substring(1))
.map(URI::create)
.map(service::downloadFile)
.map(reader -> {
try (reader) {
@ -75,9 +74,9 @@ public class S3FileHandler {
.get();
}
public ServerResponse listing(ServerRequest request) throws ServletException, IOException {
return Optional.of(request.body(DirectoryPath.class))
.map(key -> service.listing(key.path()))
public ServerResponse listing(ServerRequest request) {
return Optional.of(request.pathVariable("path").substring(1))
.map(key -> service.listing(URI.create(key)))
.map(objects -> ServerResponse.ok().body(objects))
.get();
}

View File

@ -2,21 +2,23 @@ package ua.com.dxrkness.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;
@Configuration
public class S3FileHandlerConfig {
public class S3FileHandlerConfig implements WebMvcConfigurer {
@Bean
public RouterFunction<ServerResponse> router(S3FileHandler handler) {
return RouterFunctions.route()
.path("/files", b -> b
.GET(handler::downloadFile)
.GET("/{*path}", handler::downloadFile)
.POST(handler::uploadFile)
)
.path("/dirs", b -> b
.GET(handler::listing)
.GET("/{*path}", handler::listing)
.POST(handler::createDirectory)
)
.path("/common", b -> b
@ -25,4 +27,12 @@ public class S3FileHandlerConfig {
)
.build();
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("*")
.allowedHeaders("*")
.allowedOriginPatterns("*");
}
}

View File

@ -14,7 +14,6 @@ import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
@ -108,12 +107,12 @@ public class S3Service {
RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
}
public BufferedInputStream downloadFile(String key) {
public BufferedInputStream downloadFile(URI key) {
return new BufferedInputStream(
Channels.newInputStream(
Channels.newChannel(client.getObject(b -> b
.bucket(bucketName)
.key(key)))));
.key(key.toString())))));
}
public List<String> listBuckets() {