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.

108 lines
3.7 KiB

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