2020-12-07 15:16:20 +08:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/wdclient"
|
2020-12-09 17:34:24 +08:00
|
|
|
"io/ioutil"
|
2020-12-07 15:16:20 +08:00
|
|
|
"math"
|
|
|
|
"net/http"
|
2020-12-10 16:15:22 +08:00
|
|
|
"time"
|
2020-12-07 15:16:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func ReadEntry(masterClient *wdclient.MasterClient, filerClient filer_pb.SeaweedFilerClient, dir, name string, byteBuffer *bytes.Buffer) error {
|
|
|
|
|
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
respLookupEntry, err := filer_pb.LookupEntry(filerClient, request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(respLookupEntry.Entry.Content) > 0 {
|
|
|
|
_, err = byteBuffer.Write(respLookupEntry.Entry.Content)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-16 17:15:17 +08:00
|
|
|
return StreamContent(masterClient, byteBuffer, respLookupEntry.Entry.Chunks, 0, math.MaxInt64, false)
|
2020-12-07 15:16:20 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadContent(filerAddress string, dir, name string) ([]byte, error) {
|
|
|
|
|
|
|
|
target := fmt.Sprintf("http://%s%s/%s", filerAddress, dir, name)
|
|
|
|
|
2021-03-16 15:33:14 +08:00
|
|
|
data, _, err := util.Get(target)
|
2020-12-07 15:16:20 +08:00
|
|
|
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveAs(host string, port int, dir, name string, contentType string, byteBuffer *bytes.Buffer) error {
|
2020-12-10 20:03:55 +08:00
|
|
|
var target string
|
|
|
|
if port == 0 {
|
|
|
|
target = fmt.Sprintf("http://%s%s/%s", host, dir, name)
|
|
|
|
} else {
|
|
|
|
target = fmt.Sprintf("http://%s:%d%s/%s", host, port, dir, name)
|
|
|
|
}
|
2020-12-07 15:16:20 +08:00
|
|
|
|
|
|
|
// set the HTTP method, url, and request body
|
|
|
|
req, err := http.NewRequest(http.MethodPut, target, byteBuffer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the request header Content-Type for json
|
|
|
|
if contentType != "" {
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-09 17:34:24 +08:00
|
|
|
defer util.CloseResponse(resp)
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return fmt.Errorf("%s: %s %v", target, resp.Status, string(b))
|
|
|
|
}
|
2020-12-07 15:16:20 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2020-12-10 16:15:22 +08:00
|
|
|
|
|
|
|
func SaveInsideFiler(client filer_pb.SeaweedFilerClient, dir, name string, content []byte) error {
|
|
|
|
|
|
|
|
resp, err := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: dir,
|
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == filer_pb.ErrNotFound {
|
|
|
|
err = filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
|
2020-12-22 18:34:08 +08:00
|
|
|
Directory: dir,
|
|
|
|
Entry: &filer_pb.Entry{
|
2020-12-10 16:15:22 +08:00
|
|
|
Name: name,
|
|
|
|
IsDirectory: false,
|
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
FileMode: uint32(0644),
|
|
|
|
Collection: "",
|
|
|
|
Replication: "",
|
2020-12-22 18:34:08 +08:00
|
|
|
FileSize: uint64(len(content)),
|
2020-12-10 16:15:22 +08:00
|
|
|
},
|
|
|
|
Content: content,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else if err == nil {
|
|
|
|
entry := resp.Entry
|
|
|
|
entry.Content = content
|
|
|
|
entry.Attributes.Mtime = time.Now().Unix()
|
|
|
|
entry.Attributes.FileSize = uint64(len(content))
|
|
|
|
err = filer_pb.UpdateEntry(client, &filer_pb.UpdateEntryRequest{
|
2020-12-22 18:34:08 +08:00
|
|
|
Directory: dir,
|
|
|
|
Entry: entry,
|
2020-12-10 16:15:22 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2020-12-22 18:34:08 +08:00
|
|
|
}
|