2016-06-03 01:09:14 +00:00
|
|
|
package command
|
2013-11-19 07:41:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-12-15 06:38:58 +00:00
|
|
|
"io/ioutil"
|
2013-11-19 07:41:00 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2013-11-19 07:41:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-06-02 02:25:01 +00:00
|
|
|
d DownloadOptions
|
2013-11-19 07:41:00 +00:00
|
|
|
)
|
|
|
|
|
2015-06-02 02:25:01 +00:00
|
|
|
type DownloadOptions struct {
|
|
|
|
server *string
|
|
|
|
dir *string
|
|
|
|
}
|
|
|
|
|
2013-11-19 07:41:00 +00:00
|
|
|
func init() {
|
|
|
|
cmdDownload.Run = runDownload // break init cycle
|
2015-06-02 02:25:01 +00: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 07:41:00 +00: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-16 21:45:07 +00:00
|
|
|
|
2013-11-19 07:41:00 +00: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 {
|
|
|
|
for _, fid := range args {
|
2020-07-17 05:50:14 +00:00
|
|
|
if e := downloadToFile(*d.server, fid, util.ResolvePath(*d.dir)); e != nil {
|
2015-12-14 16:30:18 +00:00
|
|
|
fmt.Println("Download Error: ", fid, e)
|
2013-11-19 07:41:00 +00:00
|
|
|
}
|
2015-12-14 16:14:02 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadToFile(server, fileId, saveDir string) error {
|
|
|
|
fileUrl, lookupError := operation.LookupFileId(server, fileId)
|
|
|
|
if lookupError != nil {
|
|
|
|
return lookupError
|
|
|
|
}
|
2018-09-21 08:54:29 +00:00
|
|
|
filename, _, rc, err := util.DownloadFile(fileUrl)
|
2015-12-14 16:14:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
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 {
|
2020-02-14 17:09:15 +00:00
|
|
|
io.Copy(ioutil.Discard, rc)
|
2015-12-14 16:14:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if isFileList {
|
|
|
|
content, err := ioutil.ReadAll(rc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-11-19 07:41:00 +00:00
|
|
|
}
|
2015-12-14 16:14:02 +00:00
|
|
|
fids := strings.Split(string(content), "\n")
|
|
|
|
for _, partId := range fids {
|
|
|
|
var n int
|
|
|
|
_, part, err := fetchContent(*d.server, partId)
|
|
|
|
if err == nil {
|
|
|
|
n, err = f.Write(part)
|
2013-11-19 07:41:00 +00:00
|
|
|
}
|
2015-12-14 16:14:02 +00:00
|
|
|
if err == nil && n < len(part) {
|
|
|
|
err = io.ErrShortWrite
|
2013-11-19 07:41:00 +00:00
|
|
|
}
|
2015-12-14 16:14:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err = io.Copy(f, rc); err != nil {
|
|
|
|
return err
|
2013-11-19 07:41:00 +00:00
|
|
|
}
|
2015-12-14 16:14:02 +00:00
|
|
|
|
2013-11-19 07:41:00 +00:00
|
|
|
}
|
2015-12-14 16:14:02 +00:00
|
|
|
return nil
|
2013-11-19 07:41:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 16:14:02 +00:00
|
|
|
func fetchContent(server string, fileId string) (filename string, content []byte, e error) {
|
2014-03-30 18:28:04 +00:00
|
|
|
fileUrl, lookupError := operation.LookupFileId(server, fileId)
|
2013-11-19 07:41:00 +00:00
|
|
|
if lookupError != nil {
|
|
|
|
return "", nil, lookupError
|
|
|
|
}
|
2015-12-14 16:14:02 +00:00
|
|
|
var rc io.ReadCloser
|
2018-09-21 08:54:29 +00:00
|
|
|
if filename, _, rc, e = util.DownloadFile(fileUrl); e != nil {
|
2015-12-14 16:14:02 +00:00
|
|
|
return "", nil, e
|
|
|
|
}
|
|
|
|
content, e = ioutil.ReadAll(rc)
|
|
|
|
rc.Close()
|
2013-11-19 07:41:00 +00: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
|
|
|
|
}
|