Skip to content

Commit

Permalink
BUGFIX: Log the output's filename correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa-asg committed Apr 2, 2021
1 parent ee7efbc commit b2928e6
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ func getFilenameAndExt(fileName string) (string, string) {
return strings.TrimSuffix(fileName, ext), ext
}

func getFullOutputPath(outputDir, filenameWithExt string) string {
// Add a number to the filename if file already exist
// For instance, if filename `hello.pdf` already exist
// it returns hello(1).pdf
func renameFilenameIfNecessary(outputDir, filenameWithExt string) string {
fullOutputPath := path.Join(outputDir, filenameWithExt)

// Add a number to the filename if file already exist
// For instance, if filename `hello.pdf` already exist
// it returns hello(1).pdf
if _, err := os.Stat(fullOutputPath); err == nil {
counter := 1
filename, ext := getFilenameAndExt(filenameWithExt)

for err == nil {
log.Printf("File %s alread exist", filenameWithExt)
log.Printf("File %s already exist", filenameWithExt)
filenameWithExt = fmt.Sprintf("%s(%d)%s", filename, counter, ext)
fullOutputPath = path.Join(outputDir, filenameWithExt)
_, err = os.Stat(fullOutputPath)
counter += 1
}
}

return fullOutputPath
return filenameWithExt
}

type downloader struct {
Expand Down Expand Up @@ -89,8 +89,10 @@ func NewFromConfig(config *Config) (*downloader, error) {
if config.CopyBufferSize == 0 {
config.CopyBufferSize = 32 * 1024
}
config.fullOutputPath = getFullOutputPath(config.OutputDir, config.Filename)
// rename file if such file already exist
config.Filename = renameFilenameIfNecessary(config.OutputDir, config.Filename)
log.Printf("Output file: %s", config.Filename)
config.fullOutputPath = path.Join(config.OutputDir, config.Filename)

return &downloader{config: config}, nil
}
Expand Down

0 comments on commit b2928e6

Please sign in to comment.