internal.go 2.3 KB

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