#12 App widgets
This commit is contained in:
@@ -12,9 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -160,7 +158,7 @@ public class FileSystemService {
|
||||
Files.walkFileTree(targetPath, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
if(Files.isDirectory(file)) {
|
||||
if (Files.isDirectory(file)) {
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@@ -207,7 +205,7 @@ public class FileSystemService {
|
||||
}
|
||||
}
|
||||
|
||||
private long getSize(Path name) {
|
||||
public long getSize(Path name) {
|
||||
try {
|
||||
return Files.size(name);
|
||||
} catch (IOException e) {
|
||||
@@ -215,6 +213,34 @@ public class FileSystemService {
|
||||
}
|
||||
}
|
||||
|
||||
public String getDevice() {
|
||||
try {
|
||||
final Process process = Runtime.getRuntime()
|
||||
.exec(new String[]{"findmnt", "-n", "-o", "SOURCE", "--target", this.locationTracker.getBaseDirPath().toString()});
|
||||
final String device = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine();
|
||||
|
||||
process.destroy();
|
||||
|
||||
return device;
|
||||
} catch (IOException e) {
|
||||
throw new FileSystemServiceException("Could not get file", e);
|
||||
}
|
||||
}
|
||||
|
||||
public long getSpaceOnDevice(String device) {
|
||||
try {
|
||||
final Process process = Runtime.getRuntime()
|
||||
.exec(new String[]{"lsblk", "-b", "-o", "SIZE", "-n", device});
|
||||
final String space = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine();
|
||||
|
||||
process.destroy();
|
||||
|
||||
return Long.parseLong(space);
|
||||
} catch (IOException e) {
|
||||
throw new FileSystemServiceException("Could not get file", e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getMimeType(String name) {
|
||||
try {
|
||||
final String detectedMimeType = this.mimeTypeDetector.detectMimeType(this.locationTracker.resolve(name));
|
||||
|
||||
@@ -1,19 +1,27 @@
|
||||
package de.nbscloud.files;
|
||||
|
||||
import de.nbscloud.files.widget.DiskUsageWidget;
|
||||
import de.nbscloud.webcontainer.registry.App;
|
||||
import de.nbscloud.webcontainer.registry.AppRegistry;
|
||||
import de.nbscloud.webcontainer.registry.Widget;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class FilesApp implements App, InitializingBean {
|
||||
public static final String ID = "files";
|
||||
|
||||
@Autowired
|
||||
private AppRegistry appRegistry;
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "files";
|
||||
return ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,6 +39,11 @@ public class FilesApp implements App, InitializingBean {
|
||||
return 10;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Widget> getWidgets() {
|
||||
return List.of(new DiskUsageWidget());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.appRegistry.registerApp(this);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package de.nbscloud.files.controller;
|
||||
|
||||
import de.nbscloud.files.FileSystemService;
|
||||
import de.nbscloud.files.LocationTracker;
|
||||
import de.nbscloud.files.config.FilesConfig;
|
||||
import de.nbscloud.webcontainer.shared.config.WebContainerSharedConfig;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Controller
|
||||
public class WidgetController {
|
||||
private static final Logger logger = LoggerFactory.getLogger(WidgetController.class);
|
||||
|
||||
@Autowired
|
||||
private FilesConfig filesConfig;
|
||||
@Autowired
|
||||
private LocationTracker locationTracker;
|
||||
@Autowired
|
||||
private WebContainerSharedConfig webContainerSharedConfig;
|
||||
@Autowired
|
||||
private FileSystemService fileSystemService;
|
||||
|
||||
@GetMapping("files/widgets/diskUsage")
|
||||
public String getDiskUsage(HttpServletRequest request, HttpServletResponse response, Model model) {
|
||||
final String device = this.fileSystemService.getDevice();
|
||||
|
||||
model.addAttribute("baseDir", this.filesConfig.getBaseDir());
|
||||
model.addAttribute("device", device);
|
||||
model.addAttribute("available", this.fileSystemService.getSpaceOnDevice(device));
|
||||
model.addAttribute("used", FileUtils.sizeOfDirectory(Paths.get(this.filesConfig.getBaseDir()).toFile()));
|
||||
|
||||
return "files/widgets/diskUsage :: file-disk-usage";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package de.nbscloud.files.widget;
|
||||
|
||||
import de.nbscloud.files.FilesApp;
|
||||
import de.nbscloud.webcontainer.registry.Widget;
|
||||
|
||||
public class DiskUsageWidget implements Widget {
|
||||
@Override
|
||||
public String getPath() {
|
||||
return FilesApp.ID + "/widgets/diskUsage";
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,12 @@ nbscloud.files.share.submit=Share
|
||||
|
||||
nbscloud.files.share-message=Shared file\:\u0020
|
||||
|
||||
nbscloud.files.file-disk-usage-widget.heading=files disk usage
|
||||
nbscloud.files.file-disk-usage-widget-table.basedir=Base dir\:
|
||||
nbscloud.files.file-disk-usage-widget-table.device=Host device\:
|
||||
nbscloud.files.file-disk-usage-widget-table.available=Available\:
|
||||
nbscloud.files.file-disk-usage-widget-table.used=Used\:
|
||||
|
||||
nbscloud.files.delete.success=File deleted
|
||||
nbscloud.files.rename.success=File renamed
|
||||
nbscloud.files.created.file.success=File created
|
||||
|
||||
@@ -24,6 +24,12 @@ nbscloud.files.share.submit=Teilen
|
||||
|
||||
nbscloud.files.share-message=Datei geteilt\:\u0020
|
||||
|
||||
nbscloud.files.file-disk-usage-widget.heading=Dateien Festplattennutzung
|
||||
nbscloud.files.file-disk-usage-widget-table.basedir=Verzeichnis\:
|
||||
nbscloud.files.file-disk-usage-widget-table.device=Ger\u00E4t\:
|
||||
nbscloud.files.file-disk-usage-widget-table.available=Verf\u00FCgbar\:
|
||||
nbscloud.files.file-disk-usage-widget-table.used=Belegt\:
|
||||
|
||||
nbscloud.files.delete.success=Datei gel\u00F6scht
|
||||
nbscloud.files.rename.success=Datei verschoben/umbenannt
|
||||
nbscloud.files.created.file.success=Datei erstellt
|
||||
|
||||
@@ -174,4 +174,8 @@
|
||||
|
||||
#gallery-link {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
#file-disk-usage-widget {
|
||||
display: block;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<div id="file-disk-usage-widget" class="widget" th:fragment="file-disk-usage">
|
||||
<p class="widget-heading" th:text="#{nbscloud.files.file-disk-usage-widget.heading}"/>
|
||||
<table id="file-disk-usage-widget-table">
|
||||
<tr>
|
||||
<td th:text="#{nbscloud.files.file-disk-usage-widget-table.basedir}" />
|
||||
<td th:text="${baseDir}" />
|
||||
</tr>
|
||||
<tr>
|
||||
<td th:text="#{nbscloud.files.file-disk-usage-widget-table.device}" />
|
||||
<td th:text="${device}" />
|
||||
</tr>
|
||||
<tr>
|
||||
<td th:text="#{nbscloud.files.file-disk-usage-widget-table.available}" />
|
||||
<td th:text="${@filesFormatter.formatSize(available)}" />
|
||||
</tr>
|
||||
<tr>
|
||||
<td th:text="#{nbscloud.files.file-disk-usage-widget-table.used}" />
|
||||
<td th:text="${@filesFormatter.formatSize(used)}" />
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
Reference in New Issue
Block a user