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.

110 lines
3.8 KiB

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