web морда в стиле "Index of"

This commit is contained in:
2022-04-16 20:15:42 +03:00
parent f2b3558b52
commit 300148267a
5 changed files with 153 additions and 18 deletions

View 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();
}
}

View 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);
}
}

View File

@@ -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));
}
}
}

View File

@@ -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;
}
}

View File

@@ -1,16 +1,17 @@
package ru.di9.mirror.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
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.PutMapping;
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 java.io.IOException;
@@ -18,30 +19,29 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Slf4j
@Controller
@RequestMapping(path = "/maven")
public class WebController {
private final Path storagePath;
public class MavenController extends AbstractMavenController {
public WebController(@Value("${maven.storage:./storage}") String storagePath) {
this.storagePath = Paths.get(storagePath);
public MavenController(MavenConfig config) {
super(config);
}
@RequestMapping(path = "/{*path}")
public ResponseEntity<Resource> httpGetFile(@PathVariable("path") String httpPath) throws IOException {
@GetMapping(path = "/{*path}")
public ResponseEntity<Resource> mavenGetFile(@PathVariable("path") String httpPath) throws IOException {
ResponseEntity<Resource> responseEntity;
Path path = resolve(httpPath);
Path path = storageResolve(httpPath);
if (Files.notExists(path)) {
responseEntity = ResponseEntity.notFound().build();
} else {
long size = Files.size(path);
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)
.contentLength(size)
.body(new InputStreamResource(Files.newInputStream(path)));
@@ -50,9 +50,11 @@ public class WebController {
return responseEntity;
}
@RequestMapping(path = "/{*path}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<?> httpPutFile(@PathVariable("path") String httpPath, HttpServletRequest httpRequest) throws IOException {
Path path = resolve(httpPath);
@SuppressWarnings("java:S1452")
@PutMapping(path = "/{*path}", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<?> mavenPutFile(@PathVariable("path") String httpPath,
HttpServletRequest httpRequest) throws IOException {
Path path = storageResolve(httpPath);
Files.createDirectories(path.getParent());
try (InputStream inputStream = httpRequest.getInputStream();
@@ -67,8 +69,4 @@ public class WebController {
return ResponseEntity.noContent().build();
}
private Path resolve(String httpPath) {
return storagePath.resolve(httpPath.substring(1));
}
}