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.

90 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/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, replication string, collection string, dataCenter string, rack string, ttlSeconds int32, ttlString string, fsync bool) (filerResult *FilerPostResult, err error) {
  17. fileId, urlLocation, auth, err := fs.assignNewFileInfo(replication, collection, dataCenter, rack, ttlString, fsync)
  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. // 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: replication,
  55. Collection: collection,
  56. TtlSec: 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. }