11package check
22
33import (
4+ "bytes"
45 "context"
56 "encoding/hex"
67 "encoding/json"
@@ -356,23 +357,22 @@ func (c *Checkrr) checkFile(path string) {
356357 ctx := context .Background ()
357358
358359 // This seems like an insane number, but it's only 33KB and will allow detection of all file types via the filetype library
359- f , _ := os .Open (path )
360- defer func (f * os.File ) {
361- err := f .Close ()
362- if err != nil {
363- message := c .Localizer .MustLocalize (& i18n.LocalizeConfig {
364- MessageID : "CheckErrorClosing" ,
365- TemplateData : map [string ]interface {}{
366- "Path" : path ,
367- "Error" : err .Error (),
368- },
369- })
370- c .Logger .WithFields (log.Fields {"fileopen" : true }).Warn (message )
371- }
372- }(f )
360+ f , err := os .Open (path )
361+ if err != nil {
362+ message := c .Localizer .MustLocalize (& i18n.LocalizeConfig {
363+ MessageID : "CheckErrorReading" ,
364+ TemplateData : map [string ]interface {}{
365+ "Path" : path ,
366+ "Error" : err .Error (),
367+ },
368+ })
369+ c .Logger .WithFields (log.Fields {"fileopen" : true }).Warn (message )
370+ return
371+ }
373372
374373 buf := make ([]byte , 33000 )
375- _ , err := f .Read (buf )
374+ _ , err = f .Read (buf )
375+ closeErr := f .Close ()
376376 if err != nil {
377377 message := c .Localizer .MustLocalize (& i18n.LocalizeConfig {
378378 MessageID : "CheckErrorReading" ,
@@ -384,6 +384,17 @@ func (c *Checkrr) checkFile(path string) {
384384 c .Logger .WithFields (log.Fields {"fileopen" : true }).Warn (message )
385385 return
386386 }
387+ if closeErr != nil {
388+ message := c .Localizer .MustLocalize (& i18n.LocalizeConfig {
389+ MessageID : "CheckErrorClosing" ,
390+ TemplateData : map [string ]interface {}{
391+ "Path" : path ,
392+ "Error" : closeErr .Error (),
393+ },
394+ })
395+ c .Logger .WithFields (log.Fields {"fileopen" : true }).Warn (message )
396+ return
397+ }
387398 var detectedFileType string
388399 var formatLong string
389400
@@ -399,7 +410,9 @@ func (c *Checkrr) checkFile(path string) {
399410 }
400411 // FFProbe checks
401412 if c .ffProbe {
402- data , err := ffprobe .ProbeURL (ctx , path )
413+ probeCtx , probeCancel := context .WithTimeout (ctx , 30 * time .Second )
414+ data , err := ffprobe .ProbeURL (probeCtx , path )
415+ probeCancel ()
403416 if err != nil {
404417 message := c .Localizer .MustLocalize (& i18n.LocalizeConfig {
405418 MessageID : "CheckErrorReading" ,
@@ -759,30 +772,19 @@ func (c *Checkrr) recordBadFile(path string, fileType string, reason string) {
759772 c .Logger .WithFields (log.Fields {"DB Update" : "Failure" }).Warn (message )
760773 }
761774 if len (c .config .String ("csvfile" )) > 0 {
762- log .Debug ("writting bad file to csv" )
775+ log .Debug ("writing bad file to csv" )
763776 c .csv .Write (path , fileType )
764777 }
765778}
766779
767780func runFFmpeg (ctx context.Context , args []string ) (string , error ) {
768781 cmd := exec .CommandContext (ctx , "ffmpeg" , args ... )
782+ cmd .Stdout = io .Discard
783+ var stderr bytes.Buffer
784+ cmd .Stderr = & stderr
769785
770- stderrPipe , err := cmd .StderrPipe ()
771- if err != nil {
772- return "" , err
773- }
774-
775- if err := cmd .Start (); err != nil {
776- return "" , err
777- }
778-
779- stderrBytes , readErr := io .ReadAll (stderrPipe )
780-
781- waitErr := cmd .Wait ()
782-
783- if readErr != nil {
784- return "" , readErr
785- }
786+ runErr := cmd .Run ()
787+ stderrBytes := stderr .Bytes ()
786788
787789 var firstLine string
788790 if len (stderrBytes ) > 0 {
@@ -794,7 +796,7 @@ func runFFmpeg(ctx context.Context, args []string) (string, error) {
794796 return firstLine , ctx .Err ()
795797 }
796798
797- return firstLine , waitErr
799+ return firstLine , runErr
798800}
799801
800802type BadFile struct {
0 commit comments