Skip to content

Commit

Permalink
improve directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanwahab committed Sep 20, 2024
1 parent 745780c commit 069d9a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
38 changes: 36 additions & 2 deletions services/homelab-status-page/src/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ func SaveBase6FromClientSendServer(c echo.Context) error {
}

// Ensure the static directory exists
outputPath := "./static/sensor_data/"
outputPath := "./static/sensor_data/webcam/"
if _, err := os.Stat(outputPath); os.IsNotExist(err) {
os.Mkdir(outputPath, os.ModePerm)
}
Expand Down Expand Up @@ -1353,9 +1353,42 @@ func sendImage(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{"message": "Desktop received base64 image"})
}

func ServeLatestMutatedImage(c echo.Context) error {
// Directory where images are stored
outputPath := "./static/sensor_data/mutated_webcam/"

// Read directory and find the latest image
files, err := ioutil.ReadDir(outputPath)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to read directory"})
}

var latestFile os.FileInfo
for _, file := range files {
if !file.IsDir() && filepath.Ext(file.Name()) == ".jpg" {
if latestFile == nil || file.ModTime().After(latestFile.ModTime()) {
latestFile = file
}
}
}

if latestFile == nil {
return c.JSON(http.StatusNotFound, map[string]string{"error": "No images found"})
}

// Set headers to prevent caching
c.Response().Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
c.Response().Header().Set("Pragma", "no-cache")
c.Response().Header().Set("Expires", "0")

// Serve the latest image file
latestFilePath := filepath.Join(outputPath, latestFile.Name())
return c.File(latestFilePath)
}

func ServeLatestImage(c echo.Context) error {
// Directory where images are stored
outputPath := "./static/sensor_data/"
outputPath := "./static/sensor_data/webcam/"

// Read directory and find the latest image
files, err := ioutil.ReadDir(outputPath)
Expand Down Expand Up @@ -1461,6 +1494,7 @@ func setupRoutes(e *echo.Echo) {
e.GET("/api/hero", handleHero)

e.GET("/sensor_data/latest-webcam", ServeLatestImage)
e.GET("/sensor_data/latest-mutated-webcam", ServeLatestMutatedImage)

// e.GET("/living_research_paper", func(c echo.Context) error {

Expand Down
Binary file modified services/homelab-status-page/tmp/main
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/> -->
<img
id="liveImage"
src="/sensor_data/latest-webcam"
src="https://hashrama.blog/sensor_data/latest-webcam"
alt="Live Image"
width="640"
height="480"
Expand All @@ -18,7 +18,7 @@
<div class="w-1/2">
<img
id="jupyter-webcam-image"
src="https://hashirama.blog/static/external/jupyter_webcam.jpg"
src="https://hashrama.blog/sensor_data/latest-mutated-webcam"
class="w-full"
/>
</div>
Expand All @@ -30,6 +30,10 @@
const imageElement = document.getElementById("liveImage");
const timestamp = new Date().getTime(); // Current timestamp to prevent caching
imageElement.src = `https://hashirama.blog/sensor_data/latest-webcam?t=${timestamp}`;

const imageMutateElement = document.getElementById("jupyter-webcam-image");

imageMutateElement.src = `https://hashrama.blog/sensor_data/latest-mutated-webcam${timestamp}`;
}

// Refresh the image every 50 milliseconds (20 frames per second)
Expand Down

0 comments on commit 069d9a9

Please sign in to comment.