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.

98 lines
2.6 KiB

  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  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. )
  14. // handling single chunk POST or PUT upload
  15. func (fs *FilerServer) encrypt(ctx context.Context, w http.ResponseWriter, r *http.Request,
  16. replication string, collection string, dataCenter string, ttlSeconds int32, ttlString string) (filerResult *FilerPostResult, err error) {
  17. fileId, urlLocation, auth, err := fs.assignNewFileInfo(w, r, replication, collection, dataCenter, ttlString)
  18. if err != nil || fileId == "" || urlLocation == "" {
  19. return nil, fmt.Errorf("fail to allocate volume for %s, collection:%s, datacenter:%s", r.URL.Path, collection, 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. }
  32. uploadResult, uploadError := operation.UploadData(urlLocation, pu.FileName, true, uncompressedData, false, pu.MimeType, pu.PairMap, auth)
  33. if uploadError != nil {
  34. return nil, fmt.Errorf("upload to volume server: %v", uploadError)
  35. }
  36. // Save to chunk manifest structure
  37. fileChunks := []*filer_pb.FileChunk{
  38. {
  39. FileId: fileId,
  40. Offset: 0,
  41. Size: uint64(uploadResult.Size),
  42. Mtime: time.Now().UnixNano(),
  43. ETag: uploadResult.Md5,
  44. CipherKey: uploadResult.CipherKey,
  45. IsGzipped: uploadResult.Gzip > 0,
  46. },
  47. }
  48. fmt.Printf("uploaded: %+v\n", uploadResult)
  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: ttlSeconds,
  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. }