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.

95 lines
3.3 KiB

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