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.

97 lines
2.5 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. uncompressedData := pu.Data
  28. if pu.IsGzipped {
  29. uncompressedData = pu.UncompressedData
  30. }
  31. if pu.MimeType == "" {
  32. pu.MimeType = http.DetectContentType(uncompressedData)
  33. }
  34. uploadResult, uploadError := operation.Upload(urlLocation, pu.FileName, true, bytes.NewReader(uncompressedData), false, pu.MimeType, pu.PairMap, auth)
  35. if uploadError != nil {
  36. return nil, fmt.Errorf("upload to volume server: %v", uploadError)
  37. }
  38. // Save to chunk manifest structure
  39. fileChunks := []*filer_pb.FileChunk{
  40. {
  41. FileId: fileId,
  42. Offset: 0,
  43. Size: uint64(uploadResult.Size),
  44. Mtime: time.Now().UnixNano(),
  45. ETag: uploadResult.ETag,
  46. CipherKey: uploadResult.CipherKey,
  47. },
  48. }
  49. path := r.URL.Path
  50. if strings.HasSuffix(path, "/") {
  51. if pu.FileName != "" {
  52. path += pu.FileName
  53. }
  54. }
  55. entry := &filer2.Entry{
  56. FullPath: filer2.FullPath(path),
  57. Attr: filer2.Attr{
  58. Mtime: time.Now(),
  59. Crtime: time.Now(),
  60. Mode: 0660,
  61. Uid: OS_UID,
  62. Gid: OS_GID,
  63. Replication: replication,
  64. Collection: collection,
  65. TtlSec: int32(util.ParseInt(r.URL.Query().Get("ttl"), 0)),
  66. Mime: pu.MimeType,
  67. },
  68. Chunks: fileChunks,
  69. }
  70. filerResult = &FilerPostResult{
  71. Name: pu.FileName,
  72. Size: int64(pu.OriginalDataSize),
  73. }
  74. if dbErr := fs.filer.CreateEntry(ctx, entry, false); dbErr != nil {
  75. fs.filer.DeleteChunks(entry.Chunks)
  76. err = dbErr
  77. filerResult.Error = dbErr.Error()
  78. return
  79. }
  80. return
  81. }