Skip to content

Commit

Permalink
asset creation, thingiverse import, tempfile move
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoOliveira committed Jul 17, 2024
1 parent 8bfa3b0 commit 5feac8a
Show file tree
Hide file tree
Showing 13 changed files with 812 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func main() {
server.Use(middleware.Logger())
server.Use(middleware.Recover())

l, err := library.New(server.Group("/lib"))
_, err := library.New(server.Group(""))
if err != nil {
log.Fatalf("Error initializing library: %v", err)
}

l.ScanAsync()
//l.ScanAsync()

slog.Info("Starting agent")
log.Fatal(server.Start(fmt.Sprintf(":%d", config.Cfg.Server.Port)))
Expand Down
111 changes: 111 additions & 0 deletions v2/library/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package api

import (
"io"
"log/slog"
"net/http"
"os"
"path"

"github.com/eduardooliveira/stLib/v2/config"
"github.com/eduardooliveira/stLib/v2/library/repo"
"github.com/eduardooliveira/stLib/v2/utils"
"github.com/labstack/echo/v4"
)

type API struct {
r *repo.AssetRepo
l *slog.Logger
}

func New(e echo.Group, r *repo.AssetRepo) error {
api := &API{
r: r,
l: slog.With("module", "api"),
}
if err := utils.CreateFolder(path.Join(config.Cfg.Core.DataFolder, "temp")); err != nil {
return err
}
e.GET("/api/version", api.version) // octoprint / prusa connect
e.POST("/api/files/local", api.upload) // octoprint / prusa connect
e.GET("/server/info", api.info) // klipper
e.POST("/api/files/upload", api.upload) // klipper
return nil
}

func (a API) version(c echo.Context) error {
return c.JSON(http.StatusOK, struct {
API string `json:"api"`
Server string `json:"server"`
Text string `json:"text"`
}{
API: "xxx",
Server: "xxx",
Text: "OctoPrint xx",
})
}

func (a API) 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: "mmp",
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 (a API) upload(c echo.Context) error {

form, err := c.MultipartForm()
if err != nil {
a.l.Error("upload", "err", err)
return c.NoContent(http.StatusBadRequest)
}

files := form.File["file"]

if len(files) == 0 {
a.l.Error("upload", "err", "no files")
return c.NoContent(http.StatusBadRequest)
}
name := files[0].Filename

// Source
src, err := files[0].Open()
if err != nil {
a.l.Error("upload", "err", err)
return c.NoContent(http.StatusInternalServerError)
}
defer src.Close()

// Destination
dst, err := os.Create(path.Join(config.Cfg.Core.DataFolder, "temp", name))
if err != nil {
a.l.Error("upload", "err", err)
return c.NoContent(http.StatusInternalServerError)
}
defer dst.Close()

// Copy
if _, err = io.Copy(dst, src); err != nil {
a.l.Error("upload", "err", err)
return c.NoContent(http.StatusInternalServerError)
}

return c.NoContent(http.StatusOK)
}
37 changes: 37 additions & 0 deletions v2/library/downloader/downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package downloader

import (
"strings"

"github.com/eduardooliveira/stLib/v2/library/downloader/thingiverse"
"github.com/eduardooliveira/stLib/v2/library/entities"
"github.com/eduardooliveira/stLib/v2/library/process"
"github.com/eduardooliveira/stLib/v2/library/repo"
)

type DownloadInput struct {
Parent entities.Asset
URL string
R *repo.AssetRepo
P *process.Processor
}

func Download(in DownloadInput) error {
urls := strings.Split(in.URL, ",")

for _, url := range urls {
if strings.Contains(url, "thingiverse.com") || strings.Contains(url, "thing:") {
td, err := thingiverse.New(in.R, in.P)
if err != nil {
return err
}
err = td.Fetch(in.URL, &in.Parent)
if err != nil {
return err
}
} else if strings.Contains(url, "makerworld.com") {
panic("not implemented")
}
}
return nil
}
Loading

0 comments on commit 5feac8a

Please sign in to comment.