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.

155 lines
3.9 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", "000", "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. values.Add("replication", *uploadReplication)
  46. jsonBlob, err := util.Post("http://"+*server+"/dir/assign", values)
  47. debug("assign result :", string(jsonBlob))
  48. if err != nil {
  49. return nil, err
  50. }
  51. var ret AssignResult
  52. err = json.Unmarshal(jsonBlob, &ret)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if ret.Count <= 0 {
  57. return nil, errors.New(ret.Error)
  58. }
  59. return &ret, nil
  60. }
  61. func upload(filename string, server string, fid string) (int, error) {
  62. debug("Start uploading file:", filename)
  63. fh, err := os.Open(filename)
  64. if err != nil {
  65. debug("Failed to open file:", filename)
  66. return 0, err
  67. }
  68. fi, fiErr := fh.Stat()
  69. if fiErr != nil {
  70. debug("Failed to stat file:", filename)
  71. return 0, fiErr
  72. }
  73. ret, e := operation.Upload("http://"+server+"/"+fid+"?ts="+strconv.Itoa(int(fi.ModTime().Unix())), path.Base(filename), fh)
  74. if e != nil {
  75. return 0, e
  76. }
  77. return ret.Size, e
  78. }
  79. type SubmitResult struct {
  80. FileName string `json:"fileName"`
  81. FileUrl string `json:"fileUrl"`
  82. Fid string `json:"fid"`
  83. Size int `json:"size"`
  84. Error string `json:"error"`
  85. }
  86. func submit(files []string) ([]SubmitResult, error) {
  87. results := make([]SubmitResult, len(files))
  88. for index, file := range files {
  89. results[index].FileName = file
  90. }
  91. ret, err := assign(len(files))
  92. if err != nil {
  93. for index, _ := range files {
  94. results[index].Error = err.Error()
  95. }
  96. return results, err
  97. }
  98. for index, file := range files {
  99. fid := ret.Fid
  100. if index > 0 {
  101. fid = fid + "_" + strconv.Itoa(index)
  102. }
  103. results[index].Size, err = upload(file, ret.PublicUrl, fid)
  104. if err != nil {
  105. fid = ""
  106. results[index].Error = err.Error()
  107. }
  108. results[index].Fid = fid
  109. results[index].FileUrl = ret.PublicUrl + "/" + fid
  110. }
  111. return results, nil
  112. }
  113. func runUpload(cmd *Command, args []string) bool {
  114. if len(cmdUpload.Flag.Args()) == 0 {
  115. if *uploadDir == "" {
  116. return false
  117. }
  118. filepath.Walk(*uploadDir, func(path string, info os.FileInfo, err error) error {
  119. if err == nil {
  120. if !info.IsDir() {
  121. if *include != "" {
  122. if ok, _ := filepath.Match(*include, filepath.Base(path)); !ok {
  123. return nil
  124. }
  125. }
  126. results, e := submit([]string{path})
  127. bytes, _ := json.Marshal(results)
  128. fmt.Println(string(bytes))
  129. if e != nil {
  130. return e
  131. }
  132. }
  133. } else {
  134. fmt.Println(err)
  135. }
  136. return err
  137. })
  138. } else {
  139. results, _ := submit(args)
  140. bytes, _ := json.Marshal(results)
  141. fmt.Println(string(bytes))
  142. }
  143. return true
  144. }