snapshot_downloader.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package downloader
  15. import (
  16. "net/http"
  17. "yunion.io/x/onecloud/pkg/util/fileutils2"
  18. )
  19. type SSnapshotDownloadProvider struct {
  20. *SDownloadProvider
  21. snapshotPath string
  22. }
  23. func NewSnapshotDownloadProvider(
  24. w http.ResponseWriter, compress, sparse bool, rateLimit int, snapshotPath string,
  25. ) *SSnapshotDownloadProvider {
  26. return &SSnapshotDownloadProvider{
  27. SDownloadProvider: NewDownloadProvider(w, compress, sparse, rateLimit),
  28. snapshotPath: snapshotPath,
  29. }
  30. }
  31. func (s *SSnapshotDownloadProvider) getHeaders() http.Header {
  32. hdrs := http.Header{}
  33. hdrs.Set("X-Image-Meta-Disk_format", "")
  34. return hdrs
  35. }
  36. func (s *SSnapshotDownloadProvider) HandlerHead() error {
  37. headers := s.getHeaders()
  38. if fileutils2.Exists(s.snapshotPath) {
  39. chksum, err := fileutils2.MD5(s.snapshotPath)
  40. if err != nil {
  41. return err
  42. }
  43. headers.Set("X-Image-Meta-Checksum", chksum)
  44. }
  45. s.w.WriteHeader(200)
  46. return nil
  47. }
  48. func (s *SSnapshotDownloadProvider) downloadFilePath() string {
  49. return s.snapshotPath
  50. }
  51. func (s *SSnapshotDownloadProvider) Start() error {
  52. return s.SDownloadProvider.Start(nil, nil, s.downloadFilePath(), s.getHeaders())
  53. }