feat: implemented upload cmd and pkg
This commit is contained in:
parent
fbed466c3d
commit
13b02c03e8
2 changed files with 52 additions and 7 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.towk2.me/towk/makeshift/internal/format"
|
||||
|
|
@ -42,18 +43,37 @@ var uploadCmd = &cobra.Command{
|
|||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
host, _ = cmd.Flags().GetString("host")
|
||||
path, _ = cmd.Flags().GetString("path")
|
||||
dataArgs, _ = cmd.Flags().GetStringArray("data")
|
||||
|
||||
inputData = processFiles(dataArgs)
|
||||
useDirectoryPath = len(inputData) > 1
|
||||
c = client.New(host)
|
||||
res *http.Response
|
||||
query string
|
||||
err error
|
||||
)
|
||||
for path, contents := range inputData {
|
||||
for inputPath, contents := range inputData {
|
||||
log.Info().Str("path", path).Int("size", len(contents)).Send()
|
||||
if useDirectoryPath {
|
||||
|
||||
query = path + "/" + filepath.Clean(inputPath)
|
||||
} else {
|
||||
|
||||
// use flag value if supplied
|
||||
if cmd.Flags().Changed("path") {
|
||||
query = path
|
||||
} else {
|
||||
query = inputPath
|
||||
}
|
||||
}
|
||||
|
||||
query = fmt.Sprintf("/upload/%s", query)
|
||||
res, _, err = c.MakeRequest(client.HTTPEnvelope{
|
||||
Path: query,
|
||||
Method: http.MethodPost,
|
||||
Body: contents,
|
||||
})
|
||||
handleResponseError(res, host, query, err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
@ -70,10 +90,7 @@ var uploadProfilesCmd = &cobra.Command{
|
|||
Args: cobra.NoArgs,
|
||||
Short: "Upload a new profile",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// make one request be host positional argument (restricted to 1 for now)
|
||||
var (
|
||||
// inputData []map[string]any = append(handleArgs(args), processDataArgs(dataArgs)...)
|
||||
host, _ = cmd.Flags().GetString("host")
|
||||
dataArgs, _ = cmd.Flags().GetStringArray("data")
|
||||
profiles = processProfiles(dataArgs)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package service
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
|
|
@ -182,9 +183,36 @@ func (s *Service) Download() http.HandlerFunc {
|
|||
func (s *Service) Upload() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
_ = s.PathForData() + strings.TrimPrefix(r.URL.Path, "/upload")
|
||||
path = s.PathForData() + strings.TrimPrefix(r.URL.Path, "/upload")
|
||||
body []byte
|
||||
dirpath string
|
||||
err error
|
||||
)
|
||||
|
||||
// show what we're uploading
|
||||
log.Debug().Str("path", path).Msg("Service.Upload()")
|
||||
|
||||
// take the provided path and store the file contents
|
||||
dirpath = filepath.Dir(path)
|
||||
err = os.MkdirAll(dirpath, 0o777)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// write file to disk
|
||||
body, err = io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = os.WriteFile(path, body, 0o777)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue