internal.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package updates
  2. import (
  3. "archive/tar"
  4. "compress/gzip"
  5. "crypto/sha1"
  6. "errors"
  7. "io"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. )
  14. func getFileSize(filename string) int64 {
  15. fi, err := os.Stat(filename)
  16. if err != nil {
  17. return -1
  18. }
  19. // get the size
  20. return fi.Size()
  21. }
  22. func getDownloadFileSize(url string) (int, error) {
  23. headResp, err := http.Head(url)
  24. if err != nil {
  25. return -1, err
  26. }
  27. defer headResp.Body.Close()
  28. return strconv.Atoi(headResp.Header.Get("Content-Length"))
  29. }
  30. func downloadFile(url string, dest string) error {
  31. out, err := os.Create(dest)
  32. if err != nil {
  33. return err
  34. }
  35. defer out.Close()
  36. if err != nil {
  37. return err
  38. }
  39. resp, err := http.Get(url)
  40. if err != nil {
  41. return err
  42. }
  43. defer resp.Body.Close()
  44. _, err = io.Copy(out, resp.Body)
  45. if err != nil {
  46. return err
  47. }
  48. return nil
  49. }
  50. func extractTarGz(gzipStream io.Reader, unzipPath string, progressUpdateFunction func(int, float64, string)) error {
  51. uncompressedStream, err := gzip.NewReader(gzipStream)
  52. if err != nil {
  53. return err
  54. }
  55. tarReader := tar.NewReader(uncompressedStream)
  56. for {
  57. header, err := tarReader.Next()
  58. if err == io.EOF {
  59. break
  60. }
  61. if err != nil {
  62. return err
  63. }
  64. switch header.Typeflag {
  65. case tar.TypeDir:
  66. if err := os.Mkdir(filepath.Join(unzipPath, header.Name), 0755); err != nil {
  67. return err
  68. }
  69. case tar.TypeReg:
  70. progressUpdateFunction(2, 100, "Extracting: "+header.Name)
  71. outFile, err := os.Create(filepath.Join(unzipPath, header.Name))
  72. if err != nil {
  73. return err
  74. }
  75. if _, err := io.Copy(outFile, tarReader); err != nil {
  76. return err
  77. }
  78. outFile.Close()
  79. default:
  80. return errors.New("Unable to decode .tar.gz")
  81. }
  82. }
  83. return nil
  84. }
  85. func getSHA1Hash(filename string) (string, error) {
  86. f, err := os.Open(filename)
  87. if err != nil {
  88. return "", err
  89. }
  90. defer f.Close()
  91. h := sha1.New()
  92. if _, err := io.Copy(h, f); err != nil {
  93. return "", err
  94. }
  95. return string(h.Sum(nil)), nil
  96. }
  97. func readCheckSumFile(fileContent string, filename string, checksum string) bool {
  98. checkSumFromFile := strings.Split(fileContent, "\r\n")
  99. for _, line := range checkSumFromFile {
  100. checkSumLine := strings.Split(line, " *")
  101. if checkSumLine[1] == filename {
  102. return checkSumLine[0] == checksum
  103. }
  104. }
  105. return false
  106. }