Skip to content

Commit

Permalink
Merge pull request #9 from slavus/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
slavus authored Aug 9, 2022
2 parents 6a7ab14 + 475ddd5 commit fbaae6a
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 243 deletions.
15 changes: 5 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.slavus.logcock</groupId>
<artifactId>logcock-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.1</version>
<packaging>jar</packaging>

<name>logcock-app</name>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<version>2.7.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

Expand Down Expand Up @@ -71,12 +71,13 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<!--
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
-->

<dependency>
<groupId>org.webjars</groupId>
Expand Down Expand Up @@ -194,13 +195,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.24.RELEASE</version>
</dependency>
</dependencies>

</plugin>
</plugins>
</build>
Expand Down
241 changes: 134 additions & 107 deletions src/main/java/net/slavus/logcock/files/FilesController.java
Original file line number Diff line number Diff line change
@@ -1,107 +1,134 @@
package net.slavus.logcock.files;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import net.slavus.logcock.LogcockProperties;
import reactor.core.publisher.Mono;

/**
* @author slavus
*
*/
@Controller
public class FilesController {

@Autowired
private LogcockProperties properties;

@GetMapping("/")
public String index() {
return "redirect:/b/";
}

@GetMapping("/b")
public String browseFolder(Model model) {
model.addAttribute("basePath", "/");
model.addAttribute("baseId", "");
model.addAttribute("filesInDir", properties.getFolders());
model.addAttribute("breadcrumbs", new ArrayList<>());
return "index";
}

@GetMapping("/b/{id}/{*webPath}")
public String browseFolder(@PathVariable Integer id, @PathVariable String webPath, Model model) {
String basePath = properties.getFolders().get(id).getBasePath();

String path = basePath + File.separator + webPath;
model.addAttribute("basePath", basePath);
model.addAttribute("baseId", id);
model.addAttribute("filesInDir", fileList(path));
model.addAttribute("breadcrumbs", breadcrumbs(path, basePath));
return "index";
}

@GetMapping("/d/{id}/{*webPath}")
@ResponseBody
private Mono<ResponseEntity<Resource>> downloadFile(@PathVariable Integer id, @PathVariable String webPath, Model model) {
String basePath = properties.getFolders().get(id).getBasePath();
String filePath = basePath + File.separator + webPath;
FileSystemResource fileSystemResource = new FileSystemResource(filePath);
return Mono.just(ResponseEntity
.ok()
.header("Content-Type", "application/octet-stream")
.header("Content-Disposition", "attachment; filename=" + fileSystemResource.getFilename())
.body(fileSystemResource)
);
}


private List<File> breadcrumbs(String path, String basePath) {
final List<File> crumbs = new ArrayList<>();
File f = new File(path);
while(StringUtils.startsWith(f.getAbsolutePath(), basePath)) {
crumbs.add(f);
f = f.getParentFile();
}
Collections.reverse(crumbs);
return crumbs;
}


public static List<File> fileList(String path) {
File dir = new File(path);
if(!dir.exists()) {
return new ArrayList<>();
}
List<File> files = Arrays.stream(dir.listFiles()).sorted((o1, o2) -> {
boolean o1dir = o1.isDirectory();
boolean o2dir = o2.isDirectory();

if (o1dir == o2dir)
return 0;
if (o1dir)
return -1;
if (o2dir)
return 1;
return 0;
}).collect(Collectors.toList());
return dir.exists() ? files : new ArrayList<>();
}

}
package net.slavus.logcock.files;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import net.slavus.logcock.LogcockProperties;
import reactor.core.publisher.Mono;

/**
* @author slavus
*
*/
@Controller
public class FilesController {

@Autowired
private LogcockProperties properties;

@Autowired
private ServerProperties serverProperties;

@Autowired
Files files;

@GetMapping("/")
public String index() {
return "redirect:/b/";
}

@GetMapping("/b")
public String browseFolder(Model model) {
model.addAttribute("basePath", "/");
model.addAttribute("baseId", "");
model.addAttribute("filesInDir", properties.getFolders());
model.addAttribute("breadcrumbs", new ArrayList<>());
return "index";
}

@GetMapping("/b/{id}/{*webPath}")
public String browseFolder(@PathVariable Integer id, @PathVariable String webPath, Model model) {
String basePath = properties.getFolders().get(id).getBasePath();

String path = basePath + File.separator + webPath;
model.addAttribute("basePath", basePath);
model.addAttribute("baseId", id);
model.addAttribute("filesInDir", fileList(path));
model.addAttribute("breadcrumbs", breadcrumbs(path, basePath));
return "index";
}


@ResponseBody
@GetMapping(path = "/b/{id}/{*webPath}", consumes = MediaType.APPLICATION_JSON_VALUE)
public List<String> jsonBrowseFolder(@PathVariable Integer id, @PathVariable String webPath, Model model, ServerHttpRequest req) {
String basePath = properties.getFolders().get(id).getBasePath();

String scheme = req.getURI().getScheme();
String host = req.getHeaders().getHost().toString(); // includes server name and server port

String serverPath = scheme + "://" + host + StringUtils.defaultString(serverProperties.getServlet().getContextPath());

String path = basePath + File.separator + webPath;
return fileList(path)
.stream().map(f->serverPath + "/d/" + id + files.linkPath(f, basePath))
.collect(Collectors.toList());
}


@GetMapping("/d/{id}/{*webPath}")
@ResponseBody
private Mono<ResponseEntity<Resource>> downloadFile(@PathVariable Integer id, @PathVariable String webPath, Model model) {
String basePath = properties.getFolders().get(id).getBasePath();
String filePath = basePath + File.separator + webPath;
FileSystemResource fileSystemResource = new FileSystemResource(filePath);
return Mono.just(ResponseEntity
.ok()
.header("Content-Type", "application/octet-stream")
.header("Content-Disposition", "attachment; filename=" + fileSystemResource.getFilename())
.body(fileSystemResource)
);
}


private List<File> breadcrumbs(String path, String basePath) {
final List<File> crumbs = new ArrayList<>();
File f = new File(path);
while(StringUtils.startsWith(f.getAbsolutePath(), basePath)) {
crumbs.add(f);
f = f.getParentFile();
}
Collections.reverse(crumbs);
return crumbs;
}


public static List<File> fileList(String path) {
File dir = new File(path);
if(!dir.exists()) {
return new ArrayList<>();
}
List<File> files = Arrays.stream(dir.listFiles()).sorted((o1, o2) -> {
boolean o1dir = o1.isDirectory();
boolean o2dir = o2.isDirectory();

if (o1dir == o2dir)
return 0;
if (o1dir)
return -1;
if (o2dir)
return 1;
return 0;
}).collect(Collectors.toList());
return dir.exists() ? files : new ArrayList<>();
}

}
Loading

0 comments on commit fbaae6a

Please sign in to comment.