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.

103 lines
3.5 KiB

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