filer_server_handlers_write_cipher.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/filer"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. // handling single chunk POST or PUT upload
  16. func (fs *FilerServer) encrypt(ctx context.Context, w http.ResponseWriter, r *http.Request, so *operation.StorageOption) (filerResult *FilerPostResult, err error) {
  17. fileId, urlLocation, auth, err := fs.assignNewFileInfo(so)
  18. if err != nil || fileId == "" || urlLocation == "" {
  19. return nil, fmt.Errorf("fail to allocate volume for %s, collection:%s, datacenter:%s", r.URL.Path, so.Collection, so.DataCenter)
  20. }
  21. glog.V(4).Infof("write %s to %v", r.URL.Path, urlLocation)
  22. // Note: encrypt(gzip(data)), encrypt data first, then gzip
  23. sizeLimit := int64(fs.option.MaxMB) * 1024 * 1024
  24. pu, err := needle.ParseUpload(r, sizeLimit)
  25. uncompressedData := pu.Data
  26. if pu.IsGzipped {
  27. uncompressedData = pu.UncompressedData
  28. }
  29. if pu.MimeType == "" {
  30. pu.MimeType = http.DetectContentType(uncompressedData)
  31. // println("detect2 mimetype to", pu.MimeType)
  32. }
  33. uploadResult, uploadError := operation.UploadData(urlLocation, pu.FileName, true, uncompressedData, false, pu.MimeType, pu.PairMap, auth)
  34. if uploadError != nil {
  35. return nil, fmt.Errorf("upload to volume server: %v", uploadError)
  36. }
  37. // Save to chunk manifest structure
  38. fileChunks := []*filer_pb.FileChunk{uploadResult.ToPbFileChunk(fileId, 0)}
  39. // fmt.Printf("uploaded: %+v\n", uploadResult)
  40. path := r.URL.Path
  41. if strings.HasSuffix(path, "/") {
  42. if pu.FileName != "" {
  43. path += pu.FileName
  44. }
  45. }
  46. entry := &filer.Entry{
  47. FullPath: util.FullPath(path),
  48. Attr: filer.Attr{
  49. Mtime: time.Now(),
  50. Crtime: time.Now(),
  51. Mode: 0660,
  52. Uid: OS_UID,
  53. Gid: OS_GID,
  54. Replication: so.Replication,
  55. Collection: so.Collection,
  56. TtlSec: so.TtlSeconds,
  57. Mime: pu.MimeType,
  58. Md5: util.Base64Md5ToBytes(pu.ContentMd5),
  59. },
  60. Chunks: fileChunks,
  61. }
  62. filerResult = &FilerPostResult{
  63. Name: pu.FileName,
  64. Size: int64(pu.OriginalDataSize),
  65. }
  66. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
  67. fs.filer.DeleteChunks(entry.Chunks)
  68. err = dbErr
  69. filerResult.Error = dbErr.Error()
  70. return
  71. }
  72. return
  73. }