@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"archive/zip"
5
+ "crypto/sha256"
5
6
"encoding/json"
6
7
"flag"
7
8
"fmt"
@@ -52,7 +53,6 @@ func extractFFmpegExe(zipPath, destDir string) error {
52
53
}
53
54
defer rc .Close ()
54
55
55
- os .MkdirAll (destDir , 0755 )
56
56
outPath := filepath .Join (destDir , "ffmpeg.exe" )
57
57
outFile , err := os .Create (outPath )
58
58
if err != nil {
@@ -167,12 +167,12 @@ func initDownloads() {
167
167
fmt .Println ("Captr requires ffmpeg to record videos. However, the screenshotting functionality is not affected." )
168
168
var i int
169
169
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)" ,
176
176
}, & i , survey .WithValidator (survey .Required ))
177
177
if err != nil {
178
178
fmt .Println ("Action Cancelled" )
@@ -182,10 +182,47 @@ func initDownloads() {
182
182
tasks := progressbar .NewDownloadTasks (progressbar .New ())
183
183
defer tasks .Close ()
184
184
os .MkdirAll (dwnPath , 0755 )
185
-
185
+
186
186
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 ))
187
188
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\n Hash 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
+ }
189
226
extractFFmpegExe (filepath .Join (dwnPath , "ffmpeg_captr.zip" ), dwnPath )
190
227
fmt .Printf ("FFMPEG has been downloaded to %s" , dwnPath )
191
228
} else {
0 commit comments