web морда в стиле "Index of"
This commit is contained in:
12
src/main/java/ru/di9/mirror/Utils.java
Normal file
12
src/main/java/ru/di9/mirror/Utils.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package ru.di9.mirror;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
public final class Utils {
|
||||||
|
|
||||||
|
public static boolean isEmptyString(String string) {
|
||||||
|
return string == null || string.isEmpty() || string.isBlank();
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/main/java/ru/di9/mirror/config/MavenConfig.java
Normal file
19
src/main/java/ru/di9/mirror/config/MavenConfig.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package ru.di9.mirror.config;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Getter
|
||||||
|
public class MavenConfig {
|
||||||
|
|
||||||
|
private final Path storagePath;
|
||||||
|
|
||||||
|
public MavenConfig(@Value("${maven.storage:./storage}") String storagePath) {
|
||||||
|
this.storagePath = Paths.get(storagePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package ru.di9.mirror.controller;
|
||||||
|
|
||||||
|
import ru.di9.mirror.Utils;
|
||||||
|
import ru.di9.mirror.config.MavenConfig;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
public abstract class AbstractMavenController{
|
||||||
|
protected final MavenConfig config;
|
||||||
|
|
||||||
|
protected AbstractMavenController(MavenConfig config) {
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Path storageResolve(String httpPath) {
|
||||||
|
if (Utils.isEmptyString(httpPath)) {
|
||||||
|
return config.getStoragePath();
|
||||||
|
} else {
|
||||||
|
return config.getStoragePath().resolve(httpPath.substring(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package ru.di9.mirror.controller;
|
||||||
|
|
||||||
|
import org.springframework.core.io.InputStreamResource;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import ru.di9.mirror.Utils;
|
||||||
|
import ru.di9.mirror.config.MavenConfig;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(path = "/maven")
|
||||||
|
public class IndexOfMavenController extends AbstractMavenController {
|
||||||
|
|
||||||
|
public IndexOfMavenController(MavenConfig config) {
|
||||||
|
super(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(path = "/{*path}", headers = "accept=text/html")
|
||||||
|
public ResponseEntity<Object> walker(@PathVariable("path") String httpPath) throws IOException {
|
||||||
|
ResponseEntity<Object> responseEntity;
|
||||||
|
|
||||||
|
Path path = storageResolve(httpPath);
|
||||||
|
if (Files.exists(path)) {
|
||||||
|
if (Files.isDirectory(path)) {
|
||||||
|
responseEntity = generateIndexOf(httpPath, path);
|
||||||
|
} else {
|
||||||
|
responseEntity = downloadFile(path);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
responseEntity = ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||||
|
.body("<html><body><h1>404 - Not Found</h1><hr><p style=\"text-align:right\">Project-Mirror</p></body></html>");
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<Object> downloadFile(Path path) throws IOException {
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||||
|
"attachment; filename=\"" + path.getFileName().toString() + "\"")
|
||||||
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
|
.contentLength(Files.size(path))
|
||||||
|
.body(new InputStreamResource(Files.newInputStream(path)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<Object> generateIndexOf(String httpPath, Path path) throws IOException {
|
||||||
|
ResponseEntity<Object> responseEntity;
|
||||||
|
StringBuilder sb = new StringBuilder("<html><body><h1>Index of</h1><hr><ul>");
|
||||||
|
|
||||||
|
if (!Utils.isEmptyString(httpPath)) {
|
||||||
|
String levelUpPath = httpPath.substring(0, httpPath.lastIndexOf("/"));
|
||||||
|
sb.append("<li><a href=\"/maven").append(levelUpPath).append("\">..</a></li>");
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Stream<Path> walk = Files.walk(path, 1)) {
|
||||||
|
walk.skip(1)
|
||||||
|
.forEach(path1 -> {
|
||||||
|
sb.append("<li><a href=\"/maven")
|
||||||
|
.append(httpPath)
|
||||||
|
.append("/")
|
||||||
|
.append(path1.getFileName().toString())
|
||||||
|
.append("\">")
|
||||||
|
.append(path1.getFileName().toString());
|
||||||
|
if (Files.isDirectory(path1)) {
|
||||||
|
sb.append("/");
|
||||||
|
}
|
||||||
|
sb.append("</a></li>");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
responseEntity = ResponseEntity.ok(sb.append("</ul><hr><p style=\"text-align:right\">Project-Mirror</p></body></html>").toString());
|
||||||
|
return responseEntity;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,17 @@
|
|||||||
package ru.di9.mirror.controller;
|
package ru.di9.mirror.controller;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.core.io.InputStreamResource;
|
import org.springframework.core.io.InputStreamResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import ru.di9.mirror.config.MavenConfig;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -18,30 +19,29 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(path = "/maven")
|
@RequestMapping(path = "/maven")
|
||||||
public class WebController {
|
public class MavenController extends AbstractMavenController {
|
||||||
private final Path storagePath;
|
|
||||||
|
|
||||||
public WebController(@Value("${maven.storage:./storage}") String storagePath) {
|
public MavenController(MavenConfig config) {
|
||||||
this.storagePath = Paths.get(storagePath);
|
super(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = "/{*path}")
|
@GetMapping(path = "/{*path}")
|
||||||
public ResponseEntity<Resource> httpGetFile(@PathVariable("path") String httpPath) throws IOException {
|
public ResponseEntity<Resource> mavenGetFile(@PathVariable("path") String httpPath) throws IOException {
|
||||||
ResponseEntity<Resource> responseEntity;
|
ResponseEntity<Resource> responseEntity;
|
||||||
|
|
||||||
Path path = resolve(httpPath);
|
Path path = storageResolve(httpPath);
|
||||||
if (Files.notExists(path)) {
|
if (Files.notExists(path)) {
|
||||||
responseEntity = ResponseEntity.notFound().build();
|
responseEntity = ResponseEntity.notFound().build();
|
||||||
} else {
|
} else {
|
||||||
long size = Files.size(path);
|
long size = Files.size(path);
|
||||||
|
|
||||||
responseEntity = ResponseEntity.ok()
|
responseEntity = ResponseEntity.ok()
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + path.toFile().getName() + "\"")
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||||
|
"attachment; filename=\"" + path.toFile().getName() + "\"")
|
||||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||||
.contentLength(size)
|
.contentLength(size)
|
||||||
.body(new InputStreamResource(Files.newInputStream(path)));
|
.body(new InputStreamResource(Files.newInputStream(path)));
|
||||||
@@ -50,9 +50,11 @@ public class WebController {
|
|||||||
return responseEntity;
|
return responseEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = "/{*path}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
@SuppressWarnings("java:S1452")
|
||||||
public ResponseEntity<?> httpPutFile(@PathVariable("path") String httpPath, HttpServletRequest httpRequest) throws IOException {
|
@PutMapping(path = "/{*path}", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
||||||
Path path = resolve(httpPath);
|
public ResponseEntity<?> mavenPutFile(@PathVariable("path") String httpPath,
|
||||||
|
HttpServletRequest httpRequest) throws IOException {
|
||||||
|
Path path = storageResolve(httpPath);
|
||||||
Files.createDirectories(path.getParent());
|
Files.createDirectories(path.getParent());
|
||||||
|
|
||||||
try (InputStream inputStream = httpRequest.getInputStream();
|
try (InputStream inputStream = httpRequest.getInputStream();
|
||||||
@@ -67,8 +69,4 @@ public class WebController {
|
|||||||
|
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path resolve(String httpPath) {
|
|
||||||
return storagePath.resolve(httpPath.substring(1));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user