feat: implemented upload cmd and pkg
This commit is contained in:
parent
fbed466c3d
commit
13b02c03e8
2 changed files with 52 additions and 7 deletions
|
|
@ -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