2012-08-07 08:29:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-02-27 06:54:22 +00:00
|
|
|
"code.google.com/p/weed-fs/go/operation"
|
|
|
|
"code.google.com/p/weed-fs/go/util"
|
2012-09-20 09:11:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-07-29 17:09:36 +00:00
|
|
|
"mime"
|
2012-09-20 09:11:08 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2013-01-21 03:44:23 +00:00
|
|
|
"path"
|
2013-07-13 21:14:16 +00:00
|
|
|
"path/filepath"
|
2012-09-20 09:11:08 +00:00
|
|
|
"strconv"
|
2013-07-29 17:09:36 +00:00
|
|
|
"strings"
|
2012-08-07 08:29:22 +00:00
|
|
|
)
|
|
|
|
|
2013-07-13 21:14:16 +00:00
|
|
|
var (
|
|
|
|
uploadReplication *string
|
|
|
|
uploadDir *string
|
2013-07-14 06:17:41 +00:00
|
|
|
include *string
|
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")
|
2013-07-16 04:34:04 +00:00
|
|
|
uploadReplication = cmdUpload.Flag.String("replication", "", "replication type(000,001,010,100,110,200)")
|
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-07-20 22:14:12 +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".
|
|
|
|
|
2012-08-07 08:29:22 +00:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
type AssignResult struct {
|
2013-02-10 17:44:44 +00:00
|
|
|
Fid string `json:"fid"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
PublicUrl string `json:"publicUrl"`
|
2012-09-20 09:11:08 +00:00
|
|
|
Count int
|
2013-02-10 17:44:44 +00:00
|
|
|
Error string `json:"error"`
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 19:13:10 +00:00
|
|
|
func Assign(server string, count int) (*AssignResult, error) {
|
2012-09-20 09:11:08 +00:00
|
|
|
values := make(url.Values)
|
|
|
|
values.Add("count", strconv.Itoa(count))
|
2013-07-16 04:34:04 +00:00
|
|
|
if *uploadReplication != "" {
|
|
|
|
values.Add("replication", *uploadReplication)
|
|
|
|
}
|
2013-08-06 19:13:10 +00:00
|
|
|
jsonBlob, err := util.Post("http://"+server+"/dir/assign", values)
|
2012-09-27 19:17:27 +00:00
|
|
|
debug("assign result :", string(jsonBlob))
|
2012-09-20 09:11:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ret AssignResult
|
|
|
|
err = json.Unmarshal(jsonBlob, &ret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ret.Count <= 0 {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
|
|
|
return &ret, nil
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
2012-09-26 21:28:46 +00:00
|
|
|
func upload(filename string, server string, fid string) (int, error) {
|
2012-09-27 19:17:27 +00:00
|
|
|
debug("Start uploading file:", filename)
|
2012-09-20 09:11:08 +00:00
|
|
|
fh, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2012-09-27 19:17:27 +00:00
|
|
|
debug("Failed to open file:", filename)
|
2012-09-26 21:28:46 +00:00
|
|
|
return 0, err
|
2012-09-20 09:11:08 +00:00
|
|
|
}
|
2013-07-10 07:25:14 +00:00
|
|
|
fi, fiErr := fh.Stat()
|
2013-07-12 05:45:29 +00:00
|
|
|
if fiErr != nil {
|
|
|
|
debug("Failed to stat file:", filename)
|
|
|
|
return 0, fiErr
|
2013-07-10 07:25:14 +00:00
|
|
|
}
|
2013-07-16 00:26:00 +00:00
|
|
|
filename = path.Base(filename)
|
|
|
|
isGzipped := path.Ext(filename) == ".gz"
|
|
|
|
if isGzipped {
|
2013-07-16 04:34:04 +00:00
|
|
|
filename = filename[0 : len(filename)-3]
|
2013-07-16 00:26:00 +00:00
|
|
|
}
|
2013-07-29 17:09:36 +00:00
|
|
|
mtype := mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
|
|
|
|
ret, e := operation.Upload("http://"+server+"/"+fid+"?ts="+strconv.Itoa(int(fi.ModTime().Unix())), filename, fh, isGzipped, mtype)
|
2012-09-26 21:28:46 +00:00
|
|
|
if e != nil {
|
2013-01-17 08:56:56 +00:00
|
|
|
return 0, e
|
2012-09-26 21:28:46 +00:00
|
|
|
}
|
|
|
|
return ret.Size, e
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SubmitResult struct {
|
2013-07-12 05:45:29 +00:00
|
|
|
FileName string `json:"fileName"`
|
|
|
|
FileUrl string `json:"fileUrl"`
|
|
|
|
Fid string `json:"fid"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
Error string `json:"error"`
|
2012-08-07 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 21:14:16 +00:00
|
|
|
func submit(files []string) ([]SubmitResult, error) {
|
2013-07-13 21:35:29 +00:00
|
|
|
results := make([]SubmitResult, len(files))
|
|
|
|
for index, file := range files {
|
|
|
|
results[index].FileName = file
|
|
|
|
}
|
2013-08-06 19:13:10 +00:00
|
|
|
ret, err := Assign(*server, len(files))
|
2012-09-20 09:11:08 +00:00
|
|
|
if err != nil {
|
2013-07-13 21:35:29 +00:00
|
|
|
for index, _ := range files {
|
|
|
|
results[index].Error = err.Error()
|
|
|
|
}
|
|
|
|
return results, err
|
2012-09-20 09:11:08 +00:00
|
|
|
}
|
|
|
|
for index, file := range files {
|
|
|
|
fid := ret.Fid
|
|
|
|
if index > 0 {
|
|
|
|
fid = fid + "_" + strconv.Itoa(index)
|
|
|
|
}
|
2012-09-26 21:28:46 +00:00
|
|
|
results[index].Size, err = upload(file, ret.PublicUrl, fid)
|
|
|
|
if err != nil {
|
|
|
|
fid = ""
|
|
|
|
results[index].Error = err.Error()
|
|
|
|
}
|
2012-09-20 09:11:08 +00:00
|
|
|
results[index].Fid = fid
|
2013-07-12 05:45:29 +00:00
|
|
|
results[index].FileUrl = ret.PublicUrl + "/" + fid
|
2012-09-20 09:11:08 +00:00
|
|
|
}
|
2013-07-13 21:14:16 +00:00
|
|
|
return results, nil
|
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-07-14 05:01:48 +00:00
|
|
|
results, e := submit([]string{path})
|
|
|
|
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-07-13 21:35:29 +00:00
|
|
|
results, _ := submit(args)
|
|
|
|
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
|
|
|
}
|