Skip to content

Commit

Permalink
temporary file managemente endpoints #5
Browse files Browse the repository at this point in the history
temporary file discovery in start #5
Slicer integration as moonraker broaken #9
  • Loading branch information
EduardoOliveira committed Dec 12, 2023
1 parent 6017b58 commit 763a108
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 28 deletions.
3 changes: 2 additions & 1 deletion config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
port = 8000
server_hostname = "localhost"
library_path = "./testdata"
max_render_workers = 10
file_blacklist = [".potato",".example"]
file_blacklist = [".potato",".example",".gitkeep",".gitignore"]
model_render_color = "#167DF0"
model_background_color = "#FFFFFF"
#thingiverse_token = "your_thingiverse_token"
61 changes: 61 additions & 0 deletions core/discovery/tempDiscovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package discovery

import (
"fmt"
"log"
"os"
"strings"

"github.com/eduardooliveira/stLib/core/models"
"github.com/eduardooliveira/stLib/core/runtime"
"github.com/eduardooliveira/stLib/core/state"
)

func RunTempDiscovery() {
log.Println("Discovering Temp files")
entries, err := os.ReadDir("temp")
if err != nil {
log.Fatal(err)
}

for _, e := range entries {
blacklisted := false
for _, blacklist := range runtime.Cfg.FileBlacklist {
if strings.HasSuffix(e.Name(), blacklist) {
blacklisted = true
break
}
}
if blacklisted {
continue
}
fmt.Println(e.Name())
tempFile, err := DiscoverTempFile(e.Name())
if err != nil {
log.Println("Error Discovering temp file: ", err)
continue
}
state.TempFiles[tempFile.UUID] = tempFile
}
}

func DiscoverTempFile(name string) (*models.TempFile, error) {
tempFile, err := models.NewTempFile(name)
if err != nil {
return nil, err
}

token := strings.Split(strings.ToLower(name), "_")[0]

for _, p := range state.Projects {
if strings.Contains(strings.ToLower(p.Name), token) {
tempFile.AddMatch(p.UUID)
}
for _, tag := range p.Tags {
if strings.Contains(strings.ToLower(tag), token) {
tempFile.AddMatch(p.UUID)
}
}
}
return tempFile, nil
}
46 changes: 27 additions & 19 deletions core/integrations/slicer/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"log"
"net/http"
"os"
"strings"

"github.com/eduardooliveira/stLib/core/models"
"github.com/eduardooliveira/stLib/core/discovery"
"github.com/eduardooliveira/stLib/core/runtime"
"github.com/eduardooliveira/stLib/core/state"
"github.com/labstack/echo/v4"
)
Expand All @@ -20,10 +20,33 @@ func version(c echo.Context) error {
Text string `json:"text"`
}{
API: "xxx",
Server: "xx",
Server: "xxx",
Text: "OctoPrint xx",
})
}
func info(c echo.Context) error {
return c.JSON(http.StatusOK, struct {
State string `json:"state"`
StateMessage string `json:"state_message"`
Hostname string `json:"hostname"`
SoftwareVersion string `json:"software_version"`
CPUInfo string `json:"cpu_info"`
KlipperPath string `json:"klipper_path"`
PythonPath string `json:"python_path"`
LogFile string `json:"log_file"`
ConfigFile string `json:"config_file"`
}{
State: "ready",
StateMessage: "Printer is ready",
Hostname: runtime.Cfg.ServerHostname,
SoftwareVersion: "v0.9.xxx",
CPUInfo: "xxx",
KlipperPath: "/root/klipper", // This are mock values, do not run stuff as root....
PythonPath: "/root/klippy-env/bin/python",
LogFile: "/tmp/klippy.log",
ConfigFile: "/root/printer.cfg",
})
}

func upload(c echo.Context) error {

Expand Down Expand Up @@ -63,22 +86,7 @@ func upload(c echo.Context) error {
return c.NoContent(http.StatusInternalServerError)
}

tempFile, _ := models.NewTempFile(name)

token := strings.Split(strings.ToLower(name), "_")[0]

for _, p := range state.Projects {
if strings.Contains(strings.ToLower(p.Name), token) {
tempFile.AddMatch(p.UUID)
log.Println(fmt.Sprintf("Found project match: %s -> %s", p.Name, token))
}
for _, tag := range p.Tags {
if strings.Contains(strings.ToLower(tag), token) {
tempFile.AddMatch(p.UUID)
log.Println(fmt.Sprintf("Found tag match: %s -> %s", tag, token))
}
}
}
tempFile, _ := discovery.DiscoverTempFile(name)

state.TempFiles[tempFile.UUID] = tempFile

Expand Down
6 changes: 4 additions & 2 deletions core/integrations/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var group *echo.Group

func Register(e *echo.Group) {
group = e
group.GET("/api/version", version)
group.POST("/api/files/local", upload)
group.GET("/api/version", version) // octoprint / prusa connect
group.POST("/api/files/local", upload) // octoprint / prusa connect
group.GET("/server/info", info) // klipper
group.POST("/api/files/upload", upload) // klipper
}
1 change: 0 additions & 1 deletion core/projects/utils.go

This file was deleted.

3 changes: 3 additions & 0 deletions core/runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Config struct {
LibraryPath string `toml:"library_path"`
Port int `toml:"port"`
ServerHostname string `toml:"server_hostname"`
MaxRenderWorkers int `toml:"max_render_workers"`
FileBlacklist []string `toml:"file_blacklist"`
ModelRenderColor string `toml:"model_render_color"`
Expand All @@ -23,6 +24,7 @@ func init() {

viper.SetDefault("port", 8000)
viper.SetDefault("library_path", "./library")
viper.SetDefault("server_hostname", "localhost")
viper.SetDefault("max_render_workers", 5)
viper.SetDefault("file_blacklist", []string{})
viper.SetDefault("model_render_color", "#ffffff")
Expand All @@ -44,6 +46,7 @@ func init() {
Cfg = &Config{
LibraryPath: viper.GetString("library_path"),
Port: viper.GetInt("port"),
ServerHostname: viper.GetString("server_hostname"),
MaxRenderWorkers: viper.GetInt("max_render_workers"),
FileBlacklist: append(viper.GetStringSlice("file_blacklist"), ".gitignore", ".gitkeep", ".DS_Store", ".project.stlib", ".thumb.png", ".gitkeep"),
ModelRenderColor: viper.GetString("model_render_color"),
Expand Down
2 changes: 2 additions & 0 deletions core/stlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func Run() {
}

discovery.Run(runtime.Cfg.LibraryPath)
discovery.RunTempDiscovery()

fmt.Println("starting server...")
e := echo.New()
e.Use(middleware.CORS())
Expand Down
78 changes: 74 additions & 4 deletions core/tempfiles/endpoints.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package tempfiles

import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"

"github.com/eduardooliveira/stLib/core/discovery"
"github.com/eduardooliveira/stLib/core/models"
"github.com/eduardooliveira/stLib/core/state"
"github.com/eduardooliveira/stLib/core/utils"
"github.com/labstack/echo/v4"
)

Expand All @@ -17,9 +23,73 @@ func index(c echo.Context) error {
}

func move(c echo.Context) error {
rtn := make([]*models.TempFile, 0)
for _, t := range state.TempFiles {
rtn = append(rtn, t)
uuid := c.Param("uuid")

if uuid == "" {
return c.NoContent(http.StatusBadRequest)
}
return c.JSON(http.StatusOK, rtn)

_, ok := state.TempFiles[uuid]

if !ok {
return c.NoContent(http.StatusNotFound)
}

tempFile := &models.TempFile{}

if err := c.Bind(tempFile); err != nil {
log.Println(err)
return c.NoContent(http.StatusBadRequest)
}

if uuid != tempFile.UUID {
return c.NoContent(http.StatusBadRequest)
}

project, ok := state.Projects[tempFile.ProjectUUID]

if !ok {
return c.NoContent(http.StatusNotFound)
}

err := os.Rename(filepath.Clean(fmt.Sprintf("temp/%s", tempFile.Name)), fmt.Sprintf("%s/%s", utils.ToLibPath(project.FullPath()), tempFile.Name))

if err != nil {
log.Println("Error moving temp file: ", err)
return c.NoContent(http.StatusInternalServerError)
}

err = discovery.DiscoverProjectAssets(project)

if err != nil {
log.Println("Error discovering project assets: ", err)
return c.NoContent(http.StatusInternalServerError)
}

delete(state.TempFiles, uuid)
return c.NoContent(http.StatusOK)
}

func deleteTempFile(c echo.Context) error {

uuid := c.Param("uuid")

if uuid == "" {
return c.NoContent(http.StatusBadRequest)
}

tempFile, ok := state.TempFiles[uuid]

if !ok {
return c.NoContent(http.StatusNotFound)
}

err := os.Remove(fmt.Sprintf("temp/%s", tempFile.Name))
if err != nil {
return c.NoContent(http.StatusInternalServerError)
}

delete(state.TempFiles, uuid)

return c.NoContent(http.StatusOK)
}
3 changes: 2 additions & 1 deletion core/tempfiles/tempFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ var group *echo.Group
func Register(e *echo.Group) {
group = e
group.GET("", index)
group.POST("move", move)
group.POST("/:uuid", move)
group.POST("/:uuid/delete", deleteTempFile)
}

0 comments on commit 763a108

Please sign in to comment.