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
2.7 KiB

  1. package weed_server
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/filer2"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/operation"
  12. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. // handling single chunk POST or PUT upload
  17. func (fs *FilerServer) encrypt(ctx context.Context, w http.ResponseWriter, r *http.Request,
  18. replication string, collection string, dataCenter string) (filerResult *FilerPostResult, err error) {
  19. fileId, urlLocation, auth, err := fs.assignNewFileInfo(w, r, replication, collection, dataCenter)
  20. if err != nil || fileId == "" || urlLocation == "" {
  21. return nil, fmt.Errorf("fail to allocate volume for %s, collection:%s, datacenter:%s", r.URL.Path, collection, dataCenter)
  22. }
  23. glog.V(4).Infof("write %s to %v", r.URL.Path, urlLocation)
  24. // Note: gzip(cipher(data)), cipher data first, then gzip
  25. sizeLimit := int64(fs.option.MaxMB) * 1024 * 1024
  26. pu, err := needle.ParseUpload(r, sizeLimit)
  27. data := pu.Data
  28. uncompressedData := pu.Data
  29. cipherKey := util.GenCipherKey()
  30. if pu.IsGzipped {
  31. uncompressedData = pu.UncompressedData
  32. data, err = util.Encrypt(pu.UncompressedData, cipherKey)
  33. if err != nil {
  34. return nil, fmt.Errorf("encrypt input: %v", err)
  35. }
  36. }
  37. if pu.MimeType == "" {
  38. pu.MimeType = http.DetectContentType(uncompressedData)
  39. }
  40. uploadResult, uploadError := operation.Upload(urlLocation, pu.FileName, true, bytes.NewReader(data), pu.IsGzipped, "", pu.PairMap, auth)
  41. if uploadError != nil {
  42. return nil, fmt.Errorf("upload to volume server: %v", uploadError)
  43. }
  44. // Save to chunk manifest structure
  45. fileChunks := []*filer_pb.FileChunk{
  46. {
  47. FileId: fileId,
  48. Offset: 0,
  49. Size: uint64(uploadResult.Size),
  50. Mtime: time.Now().UnixNano(),
  51. ETag: uploadResult.ETag,
  52. CipherKey: uploadResult.CipherKey,
  53. },
  54. }
  55. path := r.URL.Path
  56. if strings.HasSuffix(path, "/") {
  57. if pu.FileName != "" {
  58. path += pu.FileName
  59. }
  60. }
  61. entry := &filer2.Entry{
  62. FullPath: filer2.FullPath(path),
  63. Attr: filer2.Attr{
  64. Mtime: time.Now(),
  65. Crtime: time.Now(),
  66. Mode: 0660,
  67. Uid: OS_UID,
  68. Gid: OS_GID,
  69. Replication: replication,
  70. Collection: collection,
  71. TtlSec: int32(util.ParseInt(r.URL.Query().Get("ttl"), 0)),
  72. Mime: pu.MimeType,
  73. },
  74. Chunks: fileChunks,
  75. }
  76. filerResult = &FilerPostResult{
  77. Name: pu.FileName,
  78. Size: int64(pu.OriginalDataSize),
  79. }
  80. if dbErr := fs.filer.CreateEntry(ctx, entry, false); dbErr != nil {
  81. fs.filer.DeleteChunks(entry.Chunks)
  82. err = dbErr
  83. filerResult.Error = dbErr.Error()
  84. return
  85. }
  86. return
  87. }