Skip to content

Commit

Permalink
Send to printer klipper upload
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoOliveira committed Dec 18, 2023
1 parent 2cfd695 commit 3ffe5c1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
63 changes: 63 additions & 0 deletions core/integrations/klipper/api.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package klipper

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"

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

func (p *KipplerPrinter) serverInfo() (*Result, error) {
Expand All @@ -22,3 +31,57 @@ func (p *KipplerPrinter) serverInfo() (*Result, error) {

return r.Result, nil
}

func (p *KipplerPrinter) ServerFilesUpload(asset *models.ProjectAsset) error {

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

if !ok {
return errors.New("project not found")
}

file, err := os.Open(utils.ToLibPath(fmt.Sprintf("%s/%s", project.FullPath(), asset.Name)))
if err != nil {
return err
}
defer file.Close()

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", asset.Name)
if err != nil {
return err
}
_, err = io.Copy(part, file)
if err != nil {
return err
}

err = writer.Close()
if err != nil {
return err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/server/files/upload", p.Address), body)
req.Header.Set("Content-Type", writer.FormDataContentType())
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
} else {
if resp.StatusCode != 201 {
body := &bytes.Buffer{}
body.ReadFrom(resp.Body)
resp.Body.Close()
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header)
fmt.Println(body)
return errors.New("unknown error uploading file")
}
}

return nil
}
6 changes: 6 additions & 0 deletions core/integrations/klipper/klipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ func ConntectionStatus(printer *models.Printer) error {

return nil
}

func UploadFile(printer *models.Printer, asset *models.ProjectAsset) error {
kp := &KipplerPrinter{printer}

return kp.ServerFilesUpload(asset)
}
27 changes: 27 additions & 0 deletions core/integrations/printers/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ func deleteHandler(c echo.Context) error {
return c.JSON(http.StatusOK, printer)
}

func sendHandler(c echo.Context) error {
uuid := c.Param("uuid")
printer, ok := state.Printers[uuid]

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

sha1 := c.Param("sha1")
asset, ok := state.Assets[sha1]

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

var err error
if printer.Type == "klipper" {
err = klipper.UploadFile(printer, asset)
}
if err != nil {
log.Println(err)
return c.NoContent(http.StatusInternalServerError)
}

return c.NoContent(http.StatusOK)
}

func new(c echo.Context) error {

pPrinter := &models.Printer{}
Expand Down
1 change: 1 addition & 0 deletions core/integrations/printers/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func Register(e *echo.Group) {
group.GET("/:uuid", show)
group.POST("/:uuid", edit)
group.POST("/:uuid/delete", deleteHandler)
group.GET("/:uuid/send/:sha1", sendHandler)
group.POST("/test", testConnection)

go checkConnection()
Expand Down

0 comments on commit 3ffe5c1

Please sign in to comment.