2016-06-03 09:09:14 +08:00
|
|
|
package command
|
2013-11-19 15:41:00 +08:00
|
|
|
|
|
|
|
import (
|
2024-06-15 02:40:34 +08:00
|
|
|
"context"
|
2013-11-19 15:41:00 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-09-09 18:53:09 +08:00
|
|
|
"net/http"
|
2013-11-19 15:41:00 +08:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
2021-10-14 12:27:58 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/operation"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2013-11-19 15:41:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-06-02 10:25:01 +08:00
|
|
|
d DownloadOptions
|
2013-11-19 15:41:00 +08:00
|
|
|
)
|
|
|
|
|
2015-06-02 10:25:01 +08:00
|
|
|
type DownloadOptions struct {
|
|
|
|
server *string
|
|
|
|
dir *string
|
|
|
|
}
|
|
|
|
|
2013-11-19 15:41:00 +08:00
|
|
|
func init() {
|
|
|
|
cmdDownload.Run = runDownload // break init cycle
|
2015-06-02 10:25:01 +08:00
|
|
|
d.server = cmdDownload.Flag.String("server", "localhost:9333", "SeaweedFS master location")
|
|
|
|
d.dir = cmdDownload.Flag.String("dir", ".", "Download the whole folder recursively if specified.")
|
2013-11-19 15:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmdDownload = &Command{
|
|
|
|
UsageLine: "download -server=localhost:9333 -dir=one_directory fid1 [fid2 fid3 ...]",
|
|
|
|
Short: "download files by file id",
|
|
|
|
Long: `download files by file id.
|
2015-04-17 05:45:07 +08:00
|
|
|
|
2013-11-19 15:41:00 +08:00
|
|
|
Usually you just need to use curl to lookup the file's volume server, and then download them directly.
|
|
|
|
This download tool combine the two steps into one.
|
|
|
|
|
|
|
|
What's more, if you use "weed upload -maxMB=..." option to upload a big file divided into chunks, you can
|
|
|
|
use this tool to download the chunks and merge them automatically.
|
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runDownload(cmd *Command, args []string) bool {
|
2024-07-17 00:15:55 +08:00
|
|
|
util.LoadSecurityConfiguration()
|
2021-08-13 12:40:33 +08:00
|
|
|
grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
|
|
|
|
|
2013-11-19 15:41:00 +08:00
|
|
|
for _, fid := range args {
|
2024-06-15 02:40:34 +08:00
|
|
|
if e := downloadToFile(func(_ context.Context) pb.ServerAddress { return pb.ServerAddress(*d.server) }, grpcDialOption, fid, util.ResolvePath(*d.dir)); e != nil {
|
2015-12-15 00:30:18 +08:00
|
|
|
fmt.Println("Download Error: ", fid, e)
|
2013-11-19 15:41:00 +08:00
|
|
|
}
|
2015-12-15 00:14:02 +08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-08-13 12:40:33 +08:00
|
|
|
func downloadToFile(masterFn operation.GetMasterFn, grpcDialOption grpc.DialOption, fileId, saveDir string) error {
|
|
|
|
fileUrl, jwt, lookupError := operation.LookupFileId(masterFn, grpcDialOption, fileId)
|
2015-12-15 00:14:02 +08:00
|
|
|
if lookupError != nil {
|
|
|
|
return lookupError
|
|
|
|
}
|
2021-08-13 12:40:33 +08:00
|
|
|
filename, _, rc, err := util.DownloadFile(fileUrl, jwt)
|
2015-12-15 00:14:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-09 18:53:09 +08:00
|
|
|
defer util.CloseResponse(rc)
|
2015-12-15 00:14:02 +08:00
|
|
|
if filename == "" {
|
|
|
|
filename = fileId
|
|
|
|
}
|
|
|
|
isFileList := false
|
|
|
|
if strings.HasSuffix(filename, "-list") {
|
|
|
|
// old command compatible
|
|
|
|
isFileList = true
|
|
|
|
filename = filename[0 : len(filename)-len("-list")]
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path.Join(saveDir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if isFileList {
|
2021-10-14 12:27:58 +08:00
|
|
|
content, err := io.ReadAll(rc.Body)
|
2015-12-15 00:14:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-11-19 15:41:00 +08:00
|
|
|
}
|
2015-12-15 00:14:02 +08:00
|
|
|
fids := strings.Split(string(content), "\n")
|
|
|
|
for _, partId := range fids {
|
|
|
|
var n int
|
2021-08-13 12:40:33 +08:00
|
|
|
_, part, err := fetchContent(masterFn, grpcDialOption, partId)
|
2015-12-15 00:14:02 +08:00
|
|
|
if err == nil {
|
|
|
|
n, err = f.Write(part)
|
2013-11-19 15:41:00 +08:00
|
|
|
}
|
2015-12-15 00:14:02 +08:00
|
|
|
if err == nil && n < len(part) {
|
|
|
|
err = io.ErrShortWrite
|
2013-11-19 15:41:00 +08:00
|
|
|
}
|
2015-12-15 00:14:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-09 18:53:09 +08:00
|
|
|
if _, err = io.Copy(f, rc.Body); err != nil {
|
2015-12-15 00:14:02 +08:00
|
|
|
return err
|
2013-11-19 15:41:00 +08:00
|
|
|
}
|
2015-12-15 00:14:02 +08:00
|
|
|
|
2013-11-19 15:41:00 +08:00
|
|
|
}
|
2015-12-15 00:14:02 +08:00
|
|
|
return nil
|
2013-11-19 15:41:00 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 12:40:33 +08:00
|
|
|
func fetchContent(masterFn operation.GetMasterFn, grpcDialOption grpc.DialOption, fileId string) (filename string, content []byte, e error) {
|
|
|
|
fileUrl, jwt, lookupError := operation.LookupFileId(masterFn, grpcDialOption, fileId)
|
2013-11-19 15:41:00 +08:00
|
|
|
if lookupError != nil {
|
|
|
|
return "", nil, lookupError
|
|
|
|
}
|
2020-09-09 18:53:09 +08:00
|
|
|
var rc *http.Response
|
2021-08-13 12:40:33 +08:00
|
|
|
if filename, _, rc, e = util.DownloadFile(fileUrl, jwt); e != nil {
|
2015-12-15 00:14:02 +08:00
|
|
|
return "", nil, e
|
|
|
|
}
|
2020-09-09 18:53:09 +08:00
|
|
|
defer util.CloseResponse(rc)
|
2021-10-14 12:27:58 +08:00
|
|
|
content, e = io.ReadAll(rc.Body)
|
2013-11-19 15:41:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteFile(filename string, data []byte, perm os.FileMode) error {
|
|
|
|
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err := f.Write(data)
|
|
|
|
f.Close()
|
|
|
|
if err == nil && n < len(data) {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|