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.

85 lines
2.7 KiB

12 years ago
  1. package main
  2. import (
  3. "code.google.com/p/weed-fs/go/operation"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. )
  9. var (
  10. uploadReplication *string
  11. uploadDir *string
  12. include *string
  13. )
  14. func init() {
  15. cmdUpload.Run = runUpload // break init cycle
  16. cmdUpload.IsDebug = cmdUpload.Flag.Bool("debug", false, "verbose debug information")
  17. server = cmdUpload.Flag.String("server", "localhost:9333", "weedfs master location")
  18. uploadDir = cmdUpload.Flag.String("dir", "", "Upload the whole folder recursively if specified.")
  19. include = cmdUpload.Flag.String("include", "", "pattens of files to upload, e.g., *.pdf, *.html, ab?d.txt, works together with -dir")
  20. uploadReplication = cmdUpload.Flag.String("replication", "", "replication type(000,001,010,100,110,200)")
  21. }
  22. var cmdUpload = &Command{
  23. UsageLine: "upload -server=localhost:9333 file1 [file2 file3]\n upload -server=localhost:9333 -dir=one_directory -include=*.pdf",
  24. Short: "upload one or a list of files",
  25. Long: `upload one or a list of files, or batch upload one whole folder recursively.
  26. If uploading a list of files:
  27. It uses consecutive file keys for the list of files.
  28. e.g. If the file1 uses key k, file2 can be read via k_1
  29. If uploading a whole folder recursively:
  30. All files under the folder and subfolders will be uploaded, each with its own file key.
  31. Optional parameter "-include" allows you to specify the file name patterns.
  32. If any file has a ".gz" extension, the content are considered gzipped already, and will be stored as is.
  33. This can save volume server's gzipped processing and allow customizable gzip compression level.
  34. The file name will strip out ".gz" and stored. For example, "jquery.js.gz" will be stored as "jquery.js".
  35. `,
  36. }
  37. func runUpload(cmd *Command, args []string) bool {
  38. if len(cmdUpload.Flag.Args()) == 0 {
  39. if *uploadDir == "" {
  40. return false
  41. }
  42. filepath.Walk(*uploadDir, func(path string, info os.FileInfo, err error) error {
  43. if err == nil {
  44. if !info.IsDir() {
  45. if *include != "" {
  46. if ok, _ := filepath.Match(*include, filepath.Base(path)); !ok {
  47. return nil
  48. }
  49. }
  50. parts, e := operation.NewFileParts([]string{path})
  51. if e != nil {
  52. return e
  53. }
  54. results, e := operation.SubmitFiles(*server, parts, *uploadReplication)
  55. bytes, _ := json.Marshal(results)
  56. fmt.Println(string(bytes))
  57. if e != nil {
  58. return e
  59. }
  60. }
  61. } else {
  62. fmt.Println(err)
  63. }
  64. return err
  65. })
  66. } else {
  67. parts, e := operation.NewFileParts(args)
  68. if e != nil {
  69. fmt.Println(e.Error())
  70. }
  71. results, _ := operation.SubmitFiles(*server, parts, *uploadReplication)
  72. bytes, _ := json.Marshal(results)
  73. fmt.Println(string(bytes))
  74. }
  75. return true
  76. }