image_downloader.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "os"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/utils"
  20. "yunion.io/x/onecloud/pkg/hostman/storageman"
  21. "yunion.io/x/onecloud/pkg/util/fileutils2"
  22. "yunion.io/x/onecloud/pkg/util/qemuimg"
  23. "yunion.io/x/onecloud/pkg/util/tarutils"
  24. )
  25. type SImageDownloadProvider struct {
  26. *SDownloadProvider
  27. disk storageman.IDisk
  28. compressFormat string
  29. }
  30. func NewImageDownloadProvider(w http.ResponseWriter, compress, sparse bool, rateLimit int, disk storageman.IDisk, compressFormat string) *SImageDownloadProvider {
  31. return &SImageDownloadProvider{
  32. SDownloadProvider: NewDownloadProvider(w, compress, sparse, rateLimit),
  33. disk: disk,
  34. compressFormat: compressFormat,
  35. }
  36. }
  37. func (i *SImageDownloadProvider) fullPath() string {
  38. return i.disk.GetPath()
  39. }
  40. func (i *SImageDownloadProvider) downloadFilePath() string {
  41. if utils.IsInStringArray(i.compressFormat, []string{"qcow2", "tar"}) {
  42. return i.fullPath() + "." + i.compressFormat
  43. } else {
  44. return i.fullPath()
  45. }
  46. }
  47. func (i *SImageDownloadProvider) prepareDownload() error {
  48. if i.fullPath() != i.downloadFilePath() {
  49. log.Infof("Compress %s %s to %s", i.compressFormat, i.fullPath(), i.downloadFilePath())
  50. }
  51. switch i.compressFormat {
  52. case "qcow2":
  53. img, err := qemuimg.NewQemuImage(i.fullPath())
  54. if err != nil {
  55. return err
  56. }
  57. _, err = img.CloneQcow2(i.downloadFilePath(), true)
  58. return err
  59. case "tar":
  60. return tarutils.TarSparseFile(i.fullPath(), i.downloadFilePath())
  61. default:
  62. return nil
  63. }
  64. }
  65. func (i *SImageDownloadProvider) onDownloadComplete() {
  66. if i.downloadFilePath() != i.fullPath() && fileutils2.Exists(i.downloadFilePath()) {
  67. os.Remove(i.downloadFilePath())
  68. }
  69. }
  70. func (i *SImageDownloadProvider) getHeaders() http.Header {
  71. hdrs := http.Header{}
  72. if utils.IsInStringArray(i.compressFormat, []string{"qcow2", "tar"}) {
  73. hdrs.Set("X-Image-Meta-Disk_format", i.compressFormat)
  74. }
  75. return hdrs
  76. }
  77. func (i *SImageDownloadProvider) Start() error {
  78. return i.SDownloadProvider.Start(i.prepareDownload, i.onDownloadComplete,
  79. i.downloadFilePath(), i.getHeaders())
  80. }
  81. func (i *SImageDownloadProvider) HandlerHead() error {
  82. headers := i.getHeaders()
  83. if len(i.compressFormat) > 0 {
  84. headers.Set("X-Image-Meta-Checksum", "error")
  85. } else {
  86. checksum, err := fileutils2.MD5(i.fullPath())
  87. if err != nil {
  88. return err
  89. }
  90. headers.Set("X-Image-Meta-Checksum", checksum)
  91. }
  92. for k := range headers {
  93. i.w.Header().Add(k, headers.Get(k))
  94. }
  95. i.w.WriteHeader(200)
  96. return nil
  97. }