Fix: Do not close the file before uploads finish

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-04-02 13:19:36 +02:00
parent 7bac228330
commit 2b991407d7
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E

41
main.go
View file

@ -86,7 +86,23 @@ func main() {
logrus.Fatal("Usage: publish-vod <filename>") logrus.Fatal("Usage: publish-vod <filename>")
} }
if err = startUploads(ctx, rconfig.Args()[1], configFile, wg, barPool, longestPrefix); err != nil { fileName := rconfig.Args()[1]
f, err := os.Open(fileName) //#nosec G304 // Intended to open and upload arbitrary file
if err != nil {
logrus.WithError(err).Fatal("opening VoD")
}
defer f.Close() //nolint:errcheck // File is closed by process exit
stat, err := f.Stat()
if err != nil {
logrus.WithError(err).Fatal("getting VoD stat")
}
if err = startUploads(
ctx, wg, barPool, longestPrefix, configFile,
fileName, f, stat.Size(),
); err != nil {
logrus.WithError(err).Fatal("starting uploads") logrus.WithError(err).Fatal("starting uploads")
} }
@ -135,23 +151,14 @@ func preflight(ctx context.Context, configFile config.File) (longestPrefix int,
func startUploads( func startUploads(
ctx context.Context, ctx context.Context,
fileName string,
configFile config.File,
wg *sync.WaitGroup, wg *sync.WaitGroup,
barPool *pb.Pool, barPool *pb.Pool,
longestPrefix int, longestPrefix int,
configFile config.File,
fileName string,
fileReader io.ReaderAt,
fileSize int64,
) (err error) { ) (err error) {
f, err := os.Open(fileName) //#nosec G304 // Intended to open and upload arbitrary file
if err != nil {
return fmt.Errorf("opening VoD: %w", err)
}
defer f.Close() //nolint:errcheck // File is closed by process exit
stat, err := f.Stat()
if err != nil {
return fmt.Errorf("getting VoD stat: %w", err)
}
for id := range configFile.Uploaders { for id := range configFile.Uploaders {
if cfg.Limit != nil && !str.StringInSlice(id, cfg.Limit) { if cfg.Limit != nil && !str.StringInSlice(id, cfg.Limit) {
continue continue
@ -161,7 +168,7 @@ func startUploads(
wg.Add(1) wg.Add(1)
bar := pb.New64(stat.Size()) bar := pb.New64(fileSize)
bar.SetTemplate(pb.Full) bar.SetTemplate(pb.Full)
barPool.Add(bar) barPool.Add(bar)
@ -175,8 +182,8 @@ func startUploads(
Config: c.Settings, Config: c.Settings,
Filename: fileName, Filename: fileName,
Content: io.NewSectionReader(f, 0, stat.Size()), Content: io.NewSectionReader(fileReader, 0, fileSize),
Size: stat.Size(), Size: fileSize,
ProgressBar: bar, ProgressBar: bar,
FinalMessage: func(format string, opts ...any) { FinalMessage: func(format string, opts ...any) {