Skip to content

Commit b32d011

Browse files
Some improvements and checking of the hash of the downloaded ffmpeg zip file.
1 parent 201fc69 commit b32d011

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

main.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"archive/zip"
5+
"crypto/sha256"
56
"encoding/json"
67
"flag"
78
"fmt"
@@ -52,7 +53,6 @@ func extractFFmpegExe(zipPath, destDir string) error {
5253
}
5354
defer rc.Close()
5455

55-
os.MkdirAll(destDir, 0755)
5656
outPath := filepath.Join(destDir, "ffmpeg.exe")
5757
outFile, err := os.Create(outPath)
5858
if err != nil {
@@ -167,12 +167,12 @@ func initDownloads() {
167167
fmt.Println("Captr requires ffmpeg to record videos. However, the screenshotting functionality is not affected.")
168168
var i int
169169
err := survey.AskOne(&survey.Select{
170-
Message: "Choose your action",
171-
Options: []string{
172-
"Download ffmpeg (Download size: ~148MB, Install size: ~132MB)",
173-
"Keep only screenshotting functionality",
174-
},
175-
Default: "Download ffmpeg (Download size: ~148MB, Install size: ~132MB)",
170+
Message: "Choose your action",
171+
Options: []string{
172+
"Download ffmpeg (Download size: ~148MB, Install size: ~132MB)",
173+
"Keep only screenshotting functionality",
174+
},
175+
Default: "Download ffmpeg (Download size: ~148MB, Install size: ~132MB)",
176176
}, &i, survey.WithValidator(survey.Required))
177177
if err != nil {
178178
fmt.Println("Action Cancelled")
@@ -182,10 +182,47 @@ func initDownloads() {
182182
tasks := progressbar.NewDownloadTasks(progressbar.New())
183183
defer tasks.Close()
184184
os.MkdirAll(dwnPath, 0755)
185-
185+
186186
tasks.Add("https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-win64-gpl-7.1.zip", filepath.Join(dwnPath, "ffmpeg_captr.zip"), progressbar.WithBarSpinner(51))
187+
tasks.Add("https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/checksums.sha256", filepath.Join(dwnPath, "checksums.sha256"), progressbar.WithBarSpinner(51))
187188
tasks.Wait()
188-
189+
fmt.Println("Checking sha256 hash of the downloaded file.")
190+
file, err := os.ReadFile(filepath.Join(dwnPath, "checksums.sha256"))
191+
if err != nil {
192+
fmt.Println("Cannot open checksum file for the download. Aborting install...")
193+
os.Remove(filepath.Join(dwnPath, "checksums.sha256"))
194+
os.Remove(filepath.Join(dwnPath, "ffmpeg_captr.zip"))
195+
os.Exit(1)
196+
}
197+
lines := strings.SplitSeq(string(file), "\n")
198+
for line := range lines {
199+
if strings.HasSuffix(line, "ffmpeg-n7.1-latest-win64-gpl-7.1.zip") {
200+
shaHash := strings.Split(line, " ")[0]
201+
f, err := os.Open(filepath.Join(dwnPath, "ffmpeg_captr.zip"))
202+
if err != nil {
203+
fmt.Println(err)
204+
fmt.Println("Cannot match checksum file of the download. Aborting install...")
205+
os.Remove(filepath.Join(dwnPath, "checksums.sha256"))
206+
os.Remove(filepath.Join(dwnPath, "ffmpeg_captr.zip"))
207+
os.Exit(1)
208+
}
209+
defer f.Close()
210+
h := sha256.New()
211+
if _, err := io.Copy(h, f); err != nil {
212+
fmt.Println("Cannot generate sha256 for the download. Aborting install...")
213+
os.Remove(filepath.Join(dwnPath, "checksums.sha256"))
214+
os.Remove(filepath.Join(dwnPath, "ffmpeg_captr.zip"))
215+
os.Exit(1)
216+
}
217+
if shaHash != fmt.Sprintf("%x", h.Sum(nil)) {
218+
fmt.Println("SHA256 hash unmatched for the downloaded file. Install aborted.")
219+
fmt.Printf("Expected hash: %s\nHash got: %x", shaHash, h.Sum(nil))
220+
os.Remove(filepath.Join(dwnPath, "checksums.sha256"))
221+
os.Remove(filepath.Join(dwnPath, "ffmpeg_captr.zip"))
222+
os.Exit(1)
223+
}
224+
}
225+
}
189226
extractFFmpegExe(filepath.Join(dwnPath, "ffmpeg_captr.zip"), dwnPath)
190227
fmt.Printf("FFMPEG has been downloaded to %s", dwnPath)
191228
} else {

0 commit comments

Comments
 (0)