diff --git a/build.gradle b/build.gradle index 360f6cc..947d8a0 100644 --- a/build.gradle +++ b/build.gradle @@ -5,26 +5,16 @@ plugins { } group = 'ru.di9' -version = '0.0.1-SNAPSHOT' -sourceCompatibility = '11' - -configurations { - compileOnly { - extendsFrom annotationProcessor - } -} +version = '1.0-SNAPSHOT' +sourceCompatibility = JavaVersion.VERSION_11 repositories { mavenCentral() } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-web' - compileOnly 'org.projectlombok:lombok' - annotationProcessor 'org.projectlombok:lombok' - testImplementation 'org.springframework.boot:spring-boot-starter-test' -} + annotationProcessor('org.projectlombok:lombok') + compileOnly('org.projectlombok:lombok') -tasks.named('test') { - useJUnitPlatform() + implementation('org.springframework.boot:spring-boot-starter-web') } diff --git a/src/main/java/ru/di9/mirror/ProjectMirrorApplication.java b/src/main/java/ru/di9/mirror/Application.java similarity index 67% rename from src/main/java/ru/di9/mirror/ProjectMirrorApplication.java rename to src/main/java/ru/di9/mirror/Application.java index e2c0733..b6d6414 100644 --- a/src/main/java/ru/di9/mirror/ProjectMirrorApplication.java +++ b/src/main/java/ru/di9/mirror/Application.java @@ -4,10 +4,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class ProjectMirrorApplication { +public class Application { public static void main(String[] args) { - SpringApplication.run(ProjectMirrorApplication.class, args); + SpringApplication.run(Application.class, args); } } diff --git a/src/main/java/ru/di9/mirror/controller/WebController.java b/src/main/java/ru/di9/mirror/controller/WebController.java new file mode 100644 index 0000000..a1c2650 --- /dev/null +++ b/src/main/java/ru/di9/mirror/controller/WebController.java @@ -0,0 +1,74 @@ +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.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +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 WebController(@Value("${maven.storage:./storage}") String storagePath) { + this.storagePath = Paths.get(storagePath); + } + + @RequestMapping(path = "/{*path}") + public ResponseEntity httpGetFile(@PathVariable("path") String httpPath) throws IOException { + ResponseEntity responseEntity; + + Path path = resolve(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() + "\"") + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .contentLength(size) + .body(new InputStreamResource(Files.newInputStream(path))); + } + + 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); + Files.createDirectories(path.getParent()); + + try (InputStream inputStream = httpRequest.getInputStream(); + OutputStream outputStream = Files.newOutputStream(path) + ) { + byte[] buff = new byte[8 * 1024]; + int len; + while ((len = inputStream.read(buff)) > 0) { + outputStream.write(buff, 0, len); + } + } + + return ResponseEntity.noContent().build(); + } + + private Path resolve(String httpPath) { + return storagePath.resolve(httpPath.substring(1)); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8b13789..18e838d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1 +1,8 @@ +server: + address: 127.0.0.1 + port: 8080 +debug: false + +maven: + storage: './storage' diff --git a/src/test/java/ru/di9/mirror/ProjectMirrorApplicationTests.java b/src/test/java/ru/di9/mirror/ProjectMirrorApplicationTests.java deleted file mode 100644 index d8ce26e..0000000 --- a/src/test/java/ru/di9/mirror/ProjectMirrorApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package ru.di9.mirror; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class ProjectMirrorApplicationTests { - - @Test - void contextLoads() { - } - -}