Fix: Do not close the file before uploads finish
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
7bac228330
commit
2b991407d7
1 changed files with 24 additions and 17 deletions
41
main.go
41
main.go
|
@ -86,7 +86,23 @@ func main() {
|
|||
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")
|
||||
}
|
||||
|
||||
|
@ -135,23 +151,14 @@ func preflight(ctx context.Context, configFile config.File) (longestPrefix int,
|
|||
|
||||
func startUploads(
|
||||
ctx context.Context,
|
||||
fileName string,
|
||||
configFile config.File,
|
||||
wg *sync.WaitGroup,
|
||||
barPool *pb.Pool,
|
||||
longestPrefix int,
|
||||
configFile config.File,
|
||||
fileName string,
|
||||
fileReader io.ReaderAt,
|
||||
fileSize int64,
|
||||
) (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 {
|
||||
if cfg.Limit != nil && !str.StringInSlice(id, cfg.Limit) {
|
||||
continue
|
||||
|
@ -161,7 +168,7 @@ func startUploads(
|
|||
|
||||
wg.Add(1)
|
||||
|
||||
bar := pb.New64(stat.Size())
|
||||
bar := pb.New64(fileSize)
|
||||
bar.SetTemplate(pb.Full)
|
||||
barPool.Add(bar)
|
||||
|
||||
|
@ -175,8 +182,8 @@ func startUploads(
|
|||
Config: c.Settings,
|
||||
|
||||
Filename: fileName,
|
||||
Content: io.NewSectionReader(f, 0, stat.Size()),
|
||||
Size: stat.Size(),
|
||||
Content: io.NewSectionReader(fileReader, 0, fileSize),
|
||||
Size: fileSize,
|
||||
|
||||
ProgressBar: bar,
|
||||
FinalMessage: func(format string, opts ...any) {
|
||||
|
|
Loading…
Reference in a new issue