Skip to content

Commit

Permalink
added a result browser for the temp folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ZihengSun committed Sep 14, 2024
1 parent 9d62ea5 commit 039403f
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/main/java/com/gw/web/ResultBrowserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.gw.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
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.ResponseBody;

import com.gw.utils.BaseTool;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.*;
import java.util.stream.Collectors;
import java.util.List;

@Controller
public class ResultBrowserController {

@Autowired BaseTool bt;

// Inject the directory path from application.properties

// Endpoint to list image files in the directory
@GetMapping("/results")
@ResponseBody
public List<String> listImageFiles() throws IOException {
String resultfolder = bt.getFileTransferFolder();
Path rootLocation = Paths.get(resultfolder);
return Files.walk(rootLocation, 1) // 1: only files in the current folder
.filter(Files::isRegularFile)
.map(path -> path.getFileName().toString()) // Return file names
.collect(Collectors.toList());
}

// Endpoint to serve images by filename
@GetMapping("/results/{filename:.+}")
@ResponseBody
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
try {
String resultfolder = bt.getFileTransferFolder();
Path file = Paths.get(resultfolder).resolve(filename);
Resource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable()) {
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
"inline; filename=\"" + filename + "\"")
.body(resource);
} else {
throw new RuntimeException("Could not read file: " + filename);
}
} catch (MalformedURLException e) {
throw new RuntimeException("Could not read file: " + filename, e);
}
}
}
44 changes: 43 additions & 1 deletion src/main/resources/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,46 @@ form .progress {
transform: translateX(-50%);
cursor: pointer;

}
}

.result-full-height {
height: calc(100%-20px);
}
#result-left-panel {
height: 100%; /* Full height within the parent */
overflow-y: auto; /* Scrollable when content overflows */
}
#result-file-list {
max-height: 100vh; /* Limit the height to viewport height */
overflow-y: auto; /* Scrollable */
padding: 0;
}
.result-list-group-item {
cursor: pointer;
transition: background-color 0.2s, color 0.2s;
}
.result-list-group-item:hover {
background-color: #007bff;
color: #fff;
}
.result-card {
border-radius: 0.5rem;
}
.result-card-body {
padding: 1.25rem;
}
.result-dark-theme {
background-color: #343a40;
color: #f8f9fa;
}
.result-dark-theme .card {
background-color: #495057;
border: 1px solid #6c757d;
}
.result-dark-theme .list-group-item {
background-color: #495057;
border-color: #6c757d;
}
.result-dark-theme .list-group-item:hover {
background-color: #6c757d;
}
2 changes: 2 additions & 0 deletions src/main/resources/static/js/gw.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ GW.main = {

GW.menu.init();

GW.result.browser.init();

//session id is a server side thing and it is not reasonable to get it on the client
// var current_jssessionid = GW.main.getJSessionId();

Expand Down
59 changes: 59 additions & 0 deletions src/main/resources/static/js/gw.result.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

GW.result.browser = {

init: function () {

GW.result.browser.loadFileList();

},

loadFileList: function() {
$.ajax({
url: '/Geoweaver/results',
method: 'GET',
success: function(data) {
// Populate the file list
$('#result-file-list').empty();
data.forEach(function(filename) {
$('#result-file-list').append(`
<li class="list-group-item file-item" data-filename="${filename}">${filename}</li>
`);
});

// Add click event to each file item
$('.file-item').click(function() {
let selectedFile = $(this).data('filename');
GW.result.browser.previewFile(selectedFile);
});
},
error: function(err) {
console.error("Error fetching file list", err);
}
});
},

previewFile: function(filename) {
// Determine if file is an image or text based on file extension
const isImage = /\.(jpg|jpeg|png|gif)$/.test(filename);

if (isImage) {
// Display image
$('#image-preview').attr('src', `/Geoweaver/results/${filename}`).show();
$('#text-preview').hide();
} else {
// Display text content
$.ajax({
url: `/Geoweaver/results/${filename}`,
method: 'GET',
success: function(data) {
$('#text-preview').text(data).show();
$('#image-preview').hide();
},
error: function(err) {
console.error("Error fetching file content", err);
}
});
}
}

}
1 change: 1 addition & 0 deletions src/main/resources/templates/fragments/config/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<script src="../js/gw.toolbar.js"></script>
<script src="../js/gw.menu.js"></script>
<script src="../js/gw.home.js"></script>
<script src="../js/gw.result.browser.js"></script>
<script src="../js/gw.main.js"></script>

<link rel="stylesheet" href="../css/common.css" />
3 changes: 3 additions & 0 deletions src/main/resources/templates/fragments/content/workspace.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<!--Dashboard Page -->
<div th:replace="fragments/content/workspace/dashboard"></div>

<!--Result Page-->
<div th:replace="fragments/content/workspace/result-browser"></div>

<div id="main-host-info" class="tabcontent" style="height:100%; left:0; margin:0; padding: 0;padding-bottom:0px;">
<div id="main-host-content" style="height:100%; width: 100%; overflow-y: scroll; padding: 0px; ">
<h2 style="color:black">Please select a host on the left panel or <button id="host-entry-create-btn" type="button" class="btn btn-success" onclick="GW.host.newDialog()">create</button> a new host! </h2>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

<div id="main-result" class="tabcontent" style="height:100%; left:0; margin:0; padding: 0;padding-bottom:0px;">
<div id="main-result-content" style="width:100%; height:100%; overflow-y: scroll; padding: 10px; ">
<div class="container-fluid">
<div class="row result-full-height">
<!-- Left Panel: Full height and scrollable -->
<div id="left-panel" class="col-md-4">
<ul class="result-list-group" id="result-file-list">
<!-- Files will be populated here dynamically -->
<!-- Add more items as needed -->
</ul>
</div>

<!-- Right Panel: Preview Section -->
<div id="right-panel" class="col-md-8">
<div id="result-file-preview" class="result-card">
<div class="result-card-body">
<img id="image-preview" class="img-fluid" style="display: none;" />
<pre id="text-preview" style="display: none;"></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<button class="tablinks toptoolbarbtn" id="main-workspace-tab" onclick="openCity(event, 'workspace')">
Weaver
</button>
<button class="tablinks toptoolbarbtn" id="main-result-tab" onclick="openCity(event, 'main-result')">
Result
</button>
<button class="tablinks toptoolbarbtn" id="main-console-tab" onclick="openCity(event, 'main-console')">
Log
</button>
Expand Down

0 comments on commit 039403f

Please sign in to comment.