mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
Add support to upload a directory recursively.
This commit is contained in:
parent
1165632fa0
commit
208ed2a991
|
@ -9,22 +9,27 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var uploadReplication *string
|
var (
|
||||||
|
uploadReplication *string
|
||||||
|
uploadDir *string
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdUpload.Run = runUpload // break init cycle
|
cmdUpload.Run = runUpload // break init cycle
|
||||||
cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
|
cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
|
||||||
server = cmdUpload.Flag.String("server", "localhost:9333", "weedfs master location")
|
server = cmdUpload.Flag.String("server", "localhost:9333", "weedfs master location")
|
||||||
|
uploadDir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
|
||||||
uploadReplication = cmdUpload.Flag.String("replication", "000", "replication type(000,001,010,100,110,200)")
|
uploadReplication = cmdUpload.Flag.String("replication", "000", "replication type(000,001,010,100,110,200)")
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmdUpload = &Command{
|
var cmdUpload = &Command{
|
||||||
UsageLine: "upload -server=localhost:9333 file1 [file2 file3]",
|
UsageLine: "upload -server=localhost:9333 file1 [file2 file3]\n upload -server=localhost:9333 -dir=one_directory",
|
||||||
Short: "upload one or a list of files",
|
Short: "upload one or a list of files",
|
||||||
Long: `upload one or a list of files.
|
Long: `upload one or a list of files, or batch upload one whole folder recursively.
|
||||||
It uses consecutive file keys for the list of files.
|
It uses consecutive file keys for the list of files.
|
||||||
e.g. If the file1 uses key k, file2 can be read via k_1
|
e.g. If the file1 uses key k, file2 can be read via k_1
|
||||||
|
|
||||||
|
@ -86,11 +91,10 @@ type SubmitResult struct {
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func submit(files []string) []SubmitResult {
|
func submit(files []string) ([]SubmitResult, error) {
|
||||||
ret, err := assign(len(files))
|
ret, err := assign(len(files))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return nil, err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
results := make([]SubmitResult, len(files))
|
results := make([]SubmitResult, len(files))
|
||||||
for index, file := range files {
|
for index, file := range files {
|
||||||
|
@ -107,15 +111,35 @@ func submit(files []string) []SubmitResult {
|
||||||
results[index].Fid = fid
|
results[index].Fid = fid
|
||||||
results[index].FileUrl = ret.PublicUrl + "/" + fid
|
results[index].FileUrl = ret.PublicUrl + "/" + fid
|
||||||
}
|
}
|
||||||
return results
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func runUpload(cmd *Command, args []string) bool {
|
func runUpload(cmd *Command, args []string) bool {
|
||||||
if len(cmdUpload.Flag.Args()) == 0 {
|
if len(cmdUpload.Flag.Args()) == 0 {
|
||||||
|
if *uploadDir == "" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
results := submit(args)
|
filepath.Walk(*uploadDir, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if !info.IsDir() {
|
||||||
|
if results, err := submit([]string{path}); err == nil {
|
||||||
bytes, _ := json.Marshal(results)
|
bytes, _ := json.Marshal(results)
|
||||||
fmt.Print(string(bytes))
|
if bytes != nil {
|
||||||
|
fmt.Println(string(bytes))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println(err, "when uploading", path)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
if results, err := submit(args); err == nil {
|
||||||
|
bytes, _ := json.Marshal(results)
|
||||||
|
fmt.Println(string(bytes))
|
||||||
|
} else {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue