updates.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package updates
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. )
  8. //Download updates from given URL, return real time progress of stage (int), progress (int) and status text (string)
  9. func DownloadUpdatesFromURL(binaryURL string, webpackURL string, progressUpdateFunction func(int, float64, string)) error {
  10. //Create the update download folder
  11. os.RemoveAll("./updates")
  12. os.MkdirAll("./updates", 0755)
  13. //Get the total size of download expected
  14. binarySize, webpackSize, err := GetUpdateSizes(binaryURL, webpackURL)
  15. if err != nil {
  16. return errors.New("Unable to access given URL")
  17. }
  18. //Generate the download position
  19. binaryDownloadTarget := "./updates/" + filepath.Base(binaryURL)
  20. webpackDownloadTarget := "./updates/" + filepath.Base(webpackURL)
  21. //Start the download of binary
  22. //0 = false, 1 = true, 2 = error
  23. binaryDownloadComplete := 0
  24. webpackDownloadComplete := 0
  25. errorMessage := ""
  26. go func() {
  27. for binaryDownloadComplete == 0 {
  28. binaryFileSize := getFileSize(binaryDownloadTarget)
  29. progress := (float64(binaryFileSize) / float64(binarySize) * 100)
  30. progressUpdateFunction(0, progress, "Downloading binary")
  31. time.Sleep(100 * time.Millisecond)
  32. }
  33. if binaryDownloadComplete == 1 {
  34. progressUpdateFunction(0, 100, "Binary Download Completed")
  35. } else {
  36. progressUpdateFunction(0, 100, "Error: "+errorMessage)
  37. }
  38. }()
  39. err = downloadFile(binaryURL, binaryDownloadTarget)
  40. if err != nil {
  41. errorMessage = err.Error()
  42. binaryDownloadComplete = 2
  43. return err
  44. }
  45. binaryDownloadComplete = 1
  46. //Downlaod webpack
  47. go func() {
  48. for webpackDownloadComplete == 0 {
  49. webpackFileSize := getFileSize(webpackDownloadTarget)
  50. progress := (float64(webpackFileSize) / float64(webpackSize) * 100)
  51. progressUpdateFunction(1, progress, "Downloading webpack")
  52. time.Sleep(100 * time.Millisecond)
  53. }
  54. if webpackDownloadComplete == 1 {
  55. progressUpdateFunction(1, 100, "Webpack Download Completed")
  56. } else {
  57. progressUpdateFunction(1, 100, "Error: "+errorMessage)
  58. }
  59. }()
  60. err = downloadFile(webpackURL, webpackDownloadTarget)
  61. if err != nil {
  62. errorMessage = err.Error()
  63. webpackDownloadComplete = 2
  64. return err
  65. }
  66. webpackDownloadComplete = 1
  67. //Download completed. Try unzip webpack
  68. gzipstrean, err := os.Open(webpackDownloadTarget)
  69. if err != nil {
  70. return err
  71. }
  72. progressUpdateFunction(2, 0, "Extracting webpack")
  73. err = extractTarGz(gzipstrean, "./updates/", progressUpdateFunction)
  74. if err != nil {
  75. return err
  76. }
  77. gzipstrean.Close()
  78. //Remove the webpack compressed file
  79. os.Remove(webpackDownloadTarget)
  80. progressUpdateFunction(3, 100, "Updates Downloaded")
  81. return nil
  82. }
  83. //Get the update sizes, return binary size, webpack size and error if any
  84. func GetUpdateSizes(binaryURL string, webpackURL string) (int, int, error) {
  85. bps, err := getDownloadFileSize(binaryURL)
  86. if err != nil {
  87. return -1, -1, err
  88. }
  89. wps, err := getDownloadFileSize(webpackURL)
  90. if err != nil {
  91. return -1, -1, err
  92. }
  93. return bps, wps, nil
  94. }