You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.1 KiB

12 years ago
12 years ago
12 years ago
  1. package main
  2. import (
  3. "code.google.com/p/weed-fs/go/operation"
  4. "code.google.com/p/weed-fs/go/util"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "net/url"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strconv"
  13. )
  14. var (
  15. uploadReplication *string
  16. uploadDir *string
  17. include *string
  18. )
  19. func init() {
  20. cmdUpload.Run = runUpload // break init cycle
  21. cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
  22. server = cmdUpload.Flag.String("server", "localhost:9333", "weedfs master location")
  23. uploadDir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
  24. include = cmdUpload.Flag.String("include", "", "pattens of files to upload, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
  25. uploadReplication = cmdUpload.Flag.String("replication", "", "replication type(000,001,010,100,110,200)")
  26. }
  27. var cmdUpload = &Command{
  28. UsageLine: "upload -server=localhost:9333 file1 [file2 file3]\n upload -server=localhost:9333 -dir=one_directory -include=*.pdf",
  29. Short: "upload one or a list of files",
  30. Long: `upload one or a list of files, or batch upload one whole folder recursively.
  31. It uses consecutive file keys for the list of files.
  32. e.g. If the file1 uses key k, file2 can be read via k_1
  33. `,
  34. }
  35. type AssignResult struct {
  36. Fid string `json:"fid"`
  37. Url string `json:"url"`
  38. PublicUrl string `json:"publicUrl"`
  39. Count int
  40. Error string `json:"error"`
  41. }
  42. func assign(count int) (*AssignResult, error) {
  43. values := make(url.Values)
  44. values.Add("count", strconv.Itoa(count))
  45. if *uploadReplication != "" {
  46. values.Add("replication", *uploadReplication)
  47. }
  48. jsonBlob, err := util.Post("http://"+*server+"/dir/assign", values)
  49. debug("assign result :", string(jsonBlob))
  50. if err != nil {
  51. return nil, err
  52. }
  53. var ret AssignResult
  54. err = json.Unmarshal(jsonBlob, &ret)
  55. if err != nil {
  56. return nil, err
  57. }
  58. if ret.Count <= 0 {
  59. return nil, errors.New(ret.Error)
  60. }
  61. return &ret, nil
  62. }
  63. func upload(filename string, server string, fid string) (int, error) {
  64. debug("Start uploading file:", filename)
  65. fh, err := os.Open(filename)
  66. if err != nil {
  67. debug("Failed to open file:", filename)
  68. return 0, err
  69. }
  70. fi, fiErr := fh.Stat()
  71. if fiErr != nil {
  72. debug("Failed to stat file:", filename)
  73. return 0, fiErr
  74. }
  75. filename = path.Base(filename)
  76. isGzipped := path.Ext(filename) == ".gz"
  77. if isGzipped {
  78. filename = filename[0 : len(filename)-3]
  79. }
  80. ret, e := operation.Upload("http://"+server+"/"+fid+"?ts="+strconv.Itoa(int(fi.ModTime().Unix())), filename, fh, isGzipped)
  81. if e != nil {
  82. return 0, e
  83. }
  84. return ret.Size, e
  85. }
  86. type SubmitResult struct {
  87. FileName string `json:"fileName"`
  88. FileUrl string `json:"fileUrl"`
  89. Fid string `json:"fid"`
  90. Size int `json:"size"`
  91. Error string `json:"error"`
  92. }
  93. func submit(files []string) ([]SubmitResult, error) {
  94. results := make([]SubmitResult, len(files))
  95. for index, file := range files {
  96. results[index].FileName = file
  97. }
  98. ret, err := assign(len(files))
  99. if err != nil {
  100. for index, _ := range files {
  101. results[index].Error = err.Error()
  102. }
  103. return results, err
  104. }
  105. for index, file := range files {
  106. fid := ret.Fid
  107. if index > 0 {
  108. fid = fid + "_" + strconv.Itoa(index)
  109. }
  110. results[index].Size, err = upload(file, ret.PublicUrl, fid)
  111. if err != nil {
  112. fid = ""
  113. results[index].Error = err.Error()
  114. }
  115. results[index].Fid = fid
  116. results[index].FileUrl = ret.PublicUrl + "/" + fid
  117. }
  118. return results, nil
  119. }
  120. func runUpload(cmd *Command, args []string) bool {
  121. if len(cmdUpload.Flag.Args()) == 0 {
  122. if *uploadDir == "" {
  123. return false
  124. }
  125. filepath.Walk(*uploadDir, func(path string, info os.FileInfo, err error) error {
  126. if err == nil {
  127. if !info.IsDir() {
  128. if *include != "" {
  129. if ok, _ := filepath.Match(*include, filepath.Base(path)); !ok {
  130. return nil
  131. }
  132. }
  133. results, e := submit([]string{path})
  134. bytes, _ := json.Marshal(results)
  135. fmt.Println(string(bytes))
  136. if e != nil {
  137. return e
  138. }
  139. }
  140. } else {
  141. fmt.Println(err)
  142. }
  143. return err
  144. })
  145. } else {
  146. results, _ := submit(args)
  147. bytes, _ := json.Marshal(results)
  148. fmt.Println(string(bytes))
  149. }
  150. return true
  151. }