filer_server_handlers_write_cipher.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. DiskType: so.DiskType,
  58. Mime: pu.MimeType,
  59. Md5: util.Base64Md5ToBytes(pu.ContentMd5),
  60. },
  61. Chunks: fileChunks,
  62. }
  63. filerResult = &FilerPostResult{
  64. Name: pu.FileName,
  65. Size: int64(pu.OriginalDataSize),
  66. }
  67. if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
  68. fs.filer.DeleteChunks(entry.Chunks)
  69. err = dbErr
  70. filerResult.Error = dbErr.Error()
  71. return
  72. }
  73. return
  74. }