filer_server_handlers_write_cipher.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/filer"
  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, so *operation.StorageOption) (filerResult *FilerPostResult, err error) {
  18. fileId, urlLocation, auth, err := fs.assignNewFileInfo(so)
  19. if err != nil || fileId == "" || urlLocation == "" {
  20. return nil, fmt.Errorf("fail to allocate volume for %s, collection:%s, datacenter:%s", r.URL.Path, so.Collection, so.DataCenter)
  21. }
  22. glog.V(4).Infof("write %s to %v", r.URL.Path, urlLocation)
  23. // Note: encrypt(gzip(data)), encrypt data first, then gzip
  24. sizeLimit := int64(fs.option.MaxMB) * 1024 * 1024
  25. bytesBuffer := bufPool.Get().(*bytes.Buffer)
  26. defer bufPool.Put(bytesBuffer)
  27. pu, err := needle.ParseUpload(r, sizeLimit, bytesBuffer)
  28. uncompressedData := pu.Data
  29. if pu.IsGzipped {
  30. uncompressedData = pu.UncompressedData
  31. }
  32. if pu.MimeType == "" {
  33. pu.MimeType = http.DetectContentType(uncompressedData)
  34. // println("detect2 mimetype to", pu.MimeType)
  35. }
  36. uploadResult, uploadError := operation.UploadData(urlLocation, pu.FileName, true, uncompressedData, false, pu.MimeType, pu.PairMap, auth)
  37. if uploadError != nil {
  38. return nil, fmt.Errorf("upload to volume server: %v", uploadError)
  39. }
  40. // Save to chunk manifest structure
  41. fileChunks := []*filer_pb.FileChunk{uploadResult.ToPbFileChunk(fileId, 0)}
  42. // fmt.Printf("uploaded: %+v\n", uploadResult)
  43. path := r.URL.Path
  44. if strings.HasSuffix(path, "/") {
  45. if pu.FileName != "" {
  46. path += pu.FileName
  47. }
  48. }
  49. entry := &filer.Entry{
  50. FullPath: util.FullPath(path),
  51. Attr: filer.Attr{
  52. Mtime: time.Now(),
  53. Crtime: time.Now(),
  54. Mode: 0660,
  55. Uid: OS_UID,
  56. Gid: OS_GID,
  57. Replication: so.Replication,
  58. Collection: so.Collection,
  59. TtlSec: so.TtlSeconds,
  60. DiskType: so.DiskType,
  61. Mime: pu.MimeType,
  62. Md5: util.Base64Md5ToBytes(pu.ContentMd5),
  63. },
  64. Chunks: fileChunks,
  65. }
  66. filerResult = &FilerPostResult{
  67. Name: pu.FileName,
  68. Size: int64(pu.OriginalDataSize),
  69. }
  70. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
  71. fs.filer.DeleteChunks(entry.Chunks)
  72. err = dbErr
  73. filerResult.Error = dbErr.Error()
  74. return
  75. }
  76. return
  77. }