mirror of
https://github.com/HeyPuter/puter-fuse.git
synced 2026-11-13 20:12:49 -10:00
129 lines
2.7 KiB
Go
129 lines
2.7 KiB
Go
package putersdk
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func (sdk *PuterSDK) Write(path string, data []byte) (*CloudItem, error) {
|
|
cloudItem, err := sdk.write(path, data, "")
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
}
|
|
return cloudItem, err
|
|
}
|
|
|
|
func (sdk *PuterSDK) Symlink(path, target string) (*CloudItem, error) {
|
|
parent := filepath.Dir(path)
|
|
name := filepath.Base(path)
|
|
batchResponse, err := sdk.Batch([]Operation{
|
|
{
|
|
"op": "symlink",
|
|
"path": parent,
|
|
"name": name,
|
|
"target": target,
|
|
},
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(batchResponse.Results) != 1 {
|
|
return nil, fmt.Errorf("unexpected batch response length: %d", len(batchResponse.Results))
|
|
}
|
|
|
|
// Get CloudItem from batch response at index 0
|
|
cloudItem := &CloudItem{}
|
|
err = unmarshalIntoStruct(batchResponse.Results[0], cloudItem)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cloudItem, nil
|
|
}
|
|
|
|
func (sdk *PuterSDK) write(path string, data []byte, target string) (*CloudItem, error) {
|
|
fmt.Printf("write(%s)\n", path)
|
|
filename := filepath.Base(path)
|
|
path = filepath.Dir(path)
|
|
body := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(body)
|
|
{
|
|
fw, _ := writer.CreateFormField("path")
|
|
io.Copy(fw, strings.NewReader(path))
|
|
}
|
|
{
|
|
fw, _ := writer.CreateFormField("overwrite")
|
|
io.Copy(fw, strings.NewReader("true"))
|
|
}
|
|
{
|
|
fw, _ := writer.CreateFormField("size")
|
|
io.Copy(fw, strings.NewReader(fmt.Sprintf("%d", len(data))))
|
|
}
|
|
if target != "" {
|
|
fw, _ := writer.CreateFormField("symlink_path")
|
|
io.Copy(fw, strings.NewReader(target))
|
|
}
|
|
{
|
|
fw, _ := writer.CreateFormFile("file", filename)
|
|
fw.Write(data)
|
|
}
|
|
writer.Close()
|
|
|
|
// fmt.Printf("Did it work? [%s]\n", body.String())
|
|
|
|
u := sdk.GetEndpointURL("write")
|
|
|
|
req, err := http.NewRequest("POST", u.String(), body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+sdk.PuterAuthToken)
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
|
|
fmt.Printf("Content-Type: %s\n", writer.FormDataContentType())
|
|
|
|
resp, err := sdk.Client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
respBytes, _ := io.ReadAll(resp.Body)
|
|
err = fmt.Errorf(
|
|
"unexpected status: %d"+
|
|
"\nbody: |%s|",
|
|
resp.StatusCode,
|
|
string(respBytes),
|
|
)
|
|
return nil, err
|
|
}
|
|
|
|
bgetbody := new(strings.Builder)
|
|
rgetbody, _ := req.GetBody()
|
|
io.Copy(bgetbody, rgetbody)
|
|
|
|
respBytes, _ := io.ReadAll(resp.Body)
|
|
|
|
cloudItem := &CloudItem{}
|
|
err = json.Unmarshal(respBytes, cloudItem)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Printf("status? [%d] when sending [%s]\n",
|
|
resp.StatusCode,
|
|
bgetbody.String(),
|
|
)
|
|
|
|
return cloudItem, nil
|
|
}
|