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.

99 lines
2.6 KiB

5 years ago
5 years ago
  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. "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,
  17. replication string, collection string, dataCenter string, ttlSeconds int32, ttlString string) (filerResult *FilerPostResult, err error) {
  18. fileId, urlLocation, auth, err := fs.assignNewFileInfo(w, r, replication, collection, dataCenter, ttlString)
  19. if err != nil || fileId == "" || urlLocation == "" {
  20. return nil, fmt.Errorf("fail to allocate volume for %s, collection:%s, datacenter:%s", r.URL.Path, collection, 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. pu, err := needle.ParseUpload(r, sizeLimit)
  26. uncompressedData := pu.Data
  27. if pu.IsGzipped {
  28. uncompressedData = pu.UncompressedData
  29. }
  30. if pu.MimeType == "" {
  31. pu.MimeType = http.DetectContentType(uncompressedData)
  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{
  39. {
  40. FileId: fileId,
  41. Offset: 0,
  42. Size: uint64(uploadResult.Size),
  43. Mtime: time.Now().UnixNano(),
  44. ETag: uploadResult.Md5,
  45. CipherKey: uploadResult.CipherKey,
  46. IsGzipped: uploadResult.Gzip > 0,
  47. },
  48. }
  49. fmt.Printf("uploaded: %+v\n", uploadResult)
  50. path := r.URL.Path
  51. if strings.HasSuffix(path, "/") {
  52. if pu.FileName != "" {
  53. path += pu.FileName
  54. }
  55. }
  56. entry := &filer2.Entry{
  57. FullPath: util.FullPath(path),
  58. Attr: filer2.Attr{
  59. Mtime: time.Now(),
  60. Crtime: time.Now(),
  61. Mode: 0660,
  62. Uid: OS_UID,
  63. Gid: OS_GID,
  64. Replication: replication,
  65. Collection: collection,
  66. TtlSec: ttlSeconds,
  67. Mime: pu.MimeType,
  68. },
  69. Chunks: fileChunks,
  70. }
  71. filerResult = &FilerPostResult{
  72. Name: pu.FileName,
  73. Size: int64(pu.OriginalDataSize),
  74. }
  75. if dbErr := fs.filer.CreateEntry(ctx, entry, false); dbErr != nil {
  76. fs.filer.DeleteChunks(entry.Chunks)
  77. err = dbErr
  78. filerResult.Error = dbErr.Error()
  79. return
  80. }
  81. return
  82. }