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.

74 lines
2.2 KiB

7 years ago
  1. package weed_server
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/chrislusf/seaweedfs/weed/storage"
  6. "github.com/chrislusf/seaweedfs/weed/storage/types"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. )
  9. func (vs *VolumeServer) getVolumeIndexContentHandler(w http.ResponseWriter, r *http.Request) {
  10. v, err := vs.getVolume("volume", r)
  11. if v == nil {
  12. writeJsonError(w, r, http.StatusBadRequest, err)
  13. return
  14. }
  15. content, err := v.IndexFileContent()
  16. if err != nil {
  17. writeJsonError(w, r, http.StatusInternalServerError, err)
  18. return
  19. }
  20. w.Write(content)
  21. }
  22. func (vs *VolumeServer) getVolumeDataContentHandler(w http.ResponseWriter, r *http.Request) {
  23. v, err := vs.getVolume("volume", r)
  24. if v == nil {
  25. writeJsonError(w, r, http.StatusBadRequest, fmt.Errorf("Not Found volume: %v", err))
  26. return
  27. }
  28. if int(v.SuperBlock.CompactRevision) != util.ParseInt(r.FormValue("revision"), 0) {
  29. writeJsonError(w, r, http.StatusExpectationFailed, fmt.Errorf("Requested Volume Revision is %s, but current revision is %d", r.FormValue("revision"), v.SuperBlock.CompactRevision))
  30. return
  31. }
  32. offset := uint32(util.ParseUint64(r.FormValue("offset"), 0))
  33. size := uint32(util.ParseUint64(r.FormValue("size"), 0))
  34. content, err := storage.ReadNeedleBlob(v.DataFile(), int64(offset)*types.NeedlePaddingSize, size, v.Version())
  35. if err != nil {
  36. writeJsonError(w, r, http.StatusInternalServerError, err)
  37. return
  38. }
  39. id, err := types.ParseNeedleId(r.FormValue("id"))
  40. if err != nil {
  41. writeJsonError(w, r, http.StatusBadRequest, err)
  42. return
  43. }
  44. n := new(storage.Needle)
  45. n.ParseNeedleHeader(content)
  46. if id != n.Id {
  47. writeJsonError(w, r, http.StatusNotFound, fmt.Errorf("Expected file entry id %d, but found %d", id, n.Id))
  48. return
  49. }
  50. w.Write(content)
  51. }
  52. func (vs *VolumeServer) getVolumeId(volumeParameterName string, r *http.Request) (storage.VolumeId, error) {
  53. volumeIdString := r.FormValue(volumeParameterName)
  54. if volumeIdString == "" {
  55. err := fmt.Errorf("Empty Volume Id: Need to pass in %s=the_volume_id.", volumeParameterName)
  56. return 0, err
  57. }
  58. vid, err := storage.NewVolumeId(volumeIdString)
  59. if err != nil {
  60. err = fmt.Errorf("Volume Id %s is not a valid unsigned integer", volumeIdString)
  61. return 0, err
  62. }
  63. return vid, err
  64. }