2012-08-07 08:29:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-09-20 09:11:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2013-07-13 21:14:16 +00:00
|
|
|
"path/filepath"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2014-12-14 08:20:21 +00:00
|
|
|
"github.com/mcqueenorama/weed-fs/go/operation"
|
2012-08-07 08:29:22 +00:00
|
|
|
)
|
|
|
|
|
2013-07-13 21:14:16 +00:00
|
|
|
var (
|
|
|
|
uploadReplication *string
|
2014-03-10 06:54:07 +00:00
|
|
|
uploadCollection *string
|
2013-07-13 21:14:16 +00:00
|
|
|
uploadDir *string
|
2014-09-20 19:38:59 +00:00
|
|
|
uploadTtl *string
|
2013-07-14 06:17:41 +00:00
|
|
|
include *string
|
2013-11-19 05:47:31 +00:00
|
|
|
maxMB *int
|
2013-07-13 21:14:16 +00:00
|
|
|
)
|
2012-09-20 09:11:08 +00:00
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
func init() {
|
2012-09-20 09:11:08 +00:00
|
|
|
cmdUpload.Run = runUpload // break init cycle
|
2013-01-20 03:49:57 +00:00
|
|
|
cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
|
2012-09-20 09:11:08 +00:00
|
|
|
server = cmdUpload.Flag.String("server", "localhost:9333", "weedfs master location")
|
2013-07-13 21:14:16 +00:00
|
|
|
uploadDir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
|
2013-07-14 06:17:41 +00:00
|
|
|
include = cmdUpload.Flag.String("include", "", "pattens of files to upload, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
|
2014-03-10 06:54:07 +00:00
|
|
|
uploadReplication = cmdUpload.Flag.String("replication", "", "replication type")
|
|
|
|
uploadCollection = cmdUpload.Flag.String("collection", "", "optional collection name")
|
2014-09-20 19:38:59 +00:00
|
|
|
uploadTtl = cmdUpload.Flag.String("ttl", "", "time to live, e.g.: 1m, 1h, 1d, 1M, 1y")
|
2013-11-19 05:47:31 +00:00
|
|
|
maxMB = cmdUpload.Flag.Int("maxMB", 0, "split files larger than the limit")
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cmdUpload = &Command{
|
2013-07-14 06:19:21 +00:00
|
|
|
UsageLine: "upload -server=localhost:9333 file1 [file2 file3]\n upload -server=localhost:9333 -dir=one_directory -include=*.pdf",
|
2012-09-20 09:11:08 +00:00
|
|
|
Short: "upload one or a list of files",
|
2013-07-13 21:14:16 +00:00
|
|
|
Long: `upload one or a list of files, or batch upload one whole folder recursively.
|
2013-11-19 07:04:33 +00:00
|
|
|
|
|
|
|
If uploading a list of files:
|
2012-09-20 09:11:08 +00:00
|
|
|
It uses consecutive file keys for the list of files.
|
2012-08-07 08:29:22 +00:00
|
|
|
e.g. If the file1 uses key k, file2 can be read via k_1
|
|
|
|
|
2013-07-20 22:14:12 +00:00
|
|
|
If uploading a whole folder recursively:
|
|
|
|
All files under the folder and subfolders will be uploaded, each with its own file key.
|
|
|
|
Optional parameter "-include" allows you to specify the file name patterns.
|
|
|
|
|
|
|
|
If any file has a ".gz" extension, the content are considered gzipped already, and will be stored as is.
|
|
|
|
This can save volume server's gzipped processing and allow customizable gzip compression level.
|
|
|
|
The file name will strip out ".gz" and stored. For example, "jquery.js.gz" will be stored as "jquery.js".
|
2013-11-19 05:47:31 +00:00
|
|
|
|
|
|
|
If "maxMB" is set to a positive number, files larger than it would be split into chunks and uploaded separatedly.
|
|
|
|
The list of file ids of those chunks would be stored in an additional chunk, and this additional chunk's file id would be returned.
|
2013-07-20 22:14:12 +00:00
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runUpload(cmd *Command, args []string) bool {
|
2012-09-20 09:11:08 +00:00
|
|
|
if len(cmdUpload.Flag.Args()) == 0 {
|
2013-07-13 21:14:16 +00:00
|
|
|
if *uploadDir == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
filepath.Walk(*uploadDir, func(path string, info os.FileInfo, err error) error {
|
2013-07-14 05:01:48 +00:00
|
|
|
if err == nil {
|
|
|
|
if !info.IsDir() {
|
2013-07-14 06:17:41 +00:00
|
|
|
if *include != "" {
|
|
|
|
if ok, _ := filepath.Match(*include, filepath.Base(path)); !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2013-08-14 17:07:42 +00:00
|
|
|
parts, e := operation.NewFileParts([]string{path})
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2014-09-20 19:38:59 +00:00
|
|
|
results, e := operation.SubmitFiles(*server, parts, *uploadReplication, *uploadCollection, *uploadTtl, *maxMB)
|
2013-07-14 05:01:48 +00:00
|
|
|
bytes, _ := json.Marshal(results)
|
|
|
|
fmt.Println(string(bytes))
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2013-07-13 21:14:16 +00:00
|
|
|
}
|
2013-07-14 05:01:48 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println(err)
|
2013-07-13 21:14:16 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
} else {
|
2013-08-14 17:07:42 +00:00
|
|
|
parts, e := operation.NewFileParts(args)
|
|
|
|
if e != nil {
|
|
|
|
fmt.Println(e.Error())
|
|
|
|
}
|
2014-09-20 19:38:59 +00:00
|
|
|
results, _ := operation.SubmitFiles(*server, parts, *uploadReplication, *uploadCollection, *uploadTtl, *maxMB)
|
2013-07-13 21:35:29 +00:00
|
|
|
bytes, _ := json.Marshal(results)
|
|
|
|
fmt.Println(string(bytes))
|
2012-09-20 09:11:08 +00:00
|
|
|
}
|
|
|
|
return true
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|