Skip to content

Commit

Permalink
Implemented Add asset to project
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoOliveira committed Dec 10, 2023
1 parent 32825dc commit 725bcbc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ func Register(e *echo.Group) {

e.POST("/:sha1/delete", deleteAsset)
e.POST("/:sha1", save)
e.POST("", new)
}
65 changes: 65 additions & 0 deletions core/assets/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package assets

import (
"fmt"
"io"
"log"
"net/http"
"os"

"github.com/eduardooliveira/stLib/core/discovery"
"github.com/eduardooliveira/stLib/core/models"
"github.com/eduardooliveira/stLib/core/state"
"github.com/eduardooliveira/stLib/core/utils"
Expand Down Expand Up @@ -97,6 +99,69 @@ func save(c echo.Context) error {
return c.NoContent(http.StatusOK)
}

func new(c echo.Context) error {

pAsset := &models.ProjectAsset{}
err := c.Bind(pAsset)

if err != nil {
return c.NoContent(http.StatusBadRequest)
}

form, err := c.MultipartForm()

files := form.File["files"]

if len(files) == 0 {
log.Println("No files")
return c.NoContent(http.StatusBadRequest)
}

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

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

path := utils.ToLibPath(fmt.Sprintf("%s/%s", project.FullPath(), pAsset.Name))

// Source
src, err := files[0].Open()
if err != nil {
log.Println(err)
return c.NoContent(http.StatusInternalServerError)
}
defer src.Close()

// Destination
dst, err := os.Create(fmt.Sprintf("%s/%s", path, files[0].Filename))
if err != nil {
log.Println(err)
return c.NoContent(http.StatusInternalServerError)
}
defer dst.Close()

// Copy
if _, err = io.Copy(dst, src); err != nil {
log.Println(err)
return c.NoContent(http.StatusInternalServerError)
}

err = discovery.DiscoverProjectAssets(project)
if err != nil {
log.Printf("error loading the project %q: %v\n", path, err)
return err
}

for _, a := range state.Projects[project.UUID].Assets {
if a.Name == files[0].Filename {
return c.JSON(http.StatusOK, a)
}
}

return c.NoContent(http.StatusInternalServerError)
}

func deleteAsset(c echo.Context) error {

sha1 := c.Param("sha1")
Expand Down

0 comments on commit 725bcbc

Please sign in to comment.