implement main functionality
This commit is contained in:
@ -1,4 +1,102 @@
|
||||
package ua.com.dxrkness.controller;
|
||||
|
||||
import jakarta.servlet.ServletException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.function.ServerRequest;
|
||||
import org.springframework.web.servlet.function.ServerResponse;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
import ua.com.dxrkness.model.DirectoryPath;
|
||||
import ua.com.dxrkness.service.S3Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class S3FileHandler {
|
||||
private final S3Service service;
|
||||
|
||||
public S3FileHandler(S3Service service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public ServerResponse createDirectory(ServerRequest request) throws ServletException, IOException {
|
||||
return Optional.of(request.body(DirectoryPath.class))
|
||||
.map(DirectoryPath::path)
|
||||
.map(body -> {
|
||||
try {
|
||||
service.createDirectory(body);
|
||||
return ServerResponse.created(body).build();
|
||||
} catch (S3Exception exception) {
|
||||
return ServerResponse.badRequest().build();
|
||||
}
|
||||
}).get();
|
||||
}
|
||||
|
||||
public ServerResponse downloadFile(ServerRequest request) throws ServletException, IOException {
|
||||
return Optional.of(request.body(DirectoryPath.class))
|
||||
.map(DirectoryPath::path)
|
||||
.map(URI::toString)
|
||||
.map(service::downloadFile)
|
||||
.map(reader -> {
|
||||
try (reader) {
|
||||
return ServerResponse
|
||||
.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.body(reader.readAllBytes());
|
||||
} catch (IOException e) {
|
||||
return ServerResponse.badRequest().build();
|
||||
}
|
||||
}).get();
|
||||
}
|
||||
|
||||
public ServerResponse uploadFile(ServerRequest request) throws ServletException, IOException {
|
||||
return Optional.of(request.multipartData())
|
||||
.map(data -> data.getFirst("file"))
|
||||
.map(file -> {
|
||||
try {
|
||||
final var path = request.param("path")
|
||||
.map(URI::create)
|
||||
.orElse(URI.create(""));
|
||||
|
||||
service.uploadFile(file, path);
|
||||
return ServerResponse
|
||||
.created(URI.create("/files"))
|
||||
.body(new DirectoryPath(
|
||||
path.resolve(
|
||||
file.getSubmittedFileName()
|
||||
)
|
||||
));
|
||||
} catch (IOException e) {
|
||||
return ServerResponse.badRequest().build();
|
||||
}
|
||||
})
|
||||
.get();
|
||||
}
|
||||
|
||||
public ServerResponse listing(ServerRequest request) throws ServletException, IOException {
|
||||
return Optional.of(request.body(DirectoryPath.class))
|
||||
.map(key -> service.listing(key.path()))
|
||||
.map(objects -> ServerResponse.ok().body(objects))
|
||||
.get();
|
||||
}
|
||||
|
||||
public ServerResponse delete(ServerRequest request) throws ServletException, IOException {
|
||||
return Optional.of(request.body(DirectoryPath.class))
|
||||
.map(DirectoryPath::path)
|
||||
.map(path -> {
|
||||
try {
|
||||
service.delete(path);
|
||||
return ServerResponse.noContent().build();
|
||||
} catch (S3Exception e) {
|
||||
return ServerResponse.badRequest().build();
|
||||
}
|
||||
})
|
||||
.get();
|
||||
}
|
||||
|
||||
public ServerResponse listBuckets(ServerRequest request) {
|
||||
return ServerResponse.ok().body(service.listBuckets());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user