fix (sdk): improve sdk

This commit is contained in:
MickaelK
2026-03-31 15:16:33 +11:00
parent 8679467f00
commit a572682f94
3 changed files with 27 additions and 15 deletions
+8
View File
@@ -2,5 +2,13 @@ package sdk
type Filestash struct {
Token string
URL string
Insecure bool
}
func NewClient() Filestash {
f := Filestash{
URL: localURL(),
}
return f
}
+10 -5
View File
@@ -8,18 +8,18 @@ import (
. "github.com/mickael-kerjean/filestash/server/ctrl"
)
func NewToken(storage string, path string, formData map[string]string) (string, error) {
func (this *Filestash) NewToken(storage string, path string, formData map[string]string) error {
globalMapping := map[string]map[string]interface{}{}
if err := json.Unmarshal(
[]byte(Config.Get("middleware.attribute_mapping.params").String()),
&globalMapping,
); err != nil {
return "", err
return err
}
mapping, ok := globalMapping[storage]
if !ok {
Log.Debug("sdk action=NewToken err=unknown_storage")
return "", ErrNotValid
return ErrNotValid
}
session := formData
@@ -35,7 +35,12 @@ func NewToken(storage string, path string, formData map[string]string) (string,
s, err := json.Marshal(session)
if err != nil {
return "", err
return err
}
return EncryptString(SECRET_KEY_DERIVATE_FOR_USER, string(s))
token, err := EncryptString(SECRET_KEY_DERIVATE_FOR_USER, string(s))
if err != nil {
return err
}
this.Token = token
return nil
}
+9 -10
View File
@@ -10,16 +10,7 @@ import (
)
func (this Filestash) request(method string, url string, body io.Reader) (io.ReadCloser, http.Header, error) {
req, err := http.NewRequest(
method,
fmt.Sprintf("%s://localhost:%d%s", func() string {
if Config.Get("general.force_ssl").Bool() {
return "https"
}
return "http"
}(), Config.Get("general.port").Int(), WithBase(url)),
body,
)
req, err := http.NewRequest(method, this.URL+url, body)
if err != nil {
return nil, nil, err
}
@@ -67,3 +58,11 @@ func (this Filestash) unmarshalResults(resp io.ReadCloser, data interface{}) err
}
return nil
}
func localURL() string {
scheme := "http"
if Config.Get("general.force_ssl").Bool() {
scheme = "https"
}
return WithBase(fmt.Sprintf("%s://localhost:%d", scheme, Config.Get("general.port").Int()))
}