Skip to content

Commit

Permalink
Improve download function (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottBrenner authored Sep 6, 2023
1 parent 30257f2 commit a466bd8
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -75,35 +76,37 @@ func unzipDownloadedPack() (err error) {
}
defer archive.Close()
for _, f := range archive.File {
filePath := filepath.Join(dst, f.Name)
fmt.Println("unzipping file ", filePath)

if f.FileInfo().IsDir() {
fmt.Println("creating song folder...")
os.MkdirAll(filePath, os.ModePerm)
continue
}

if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
panic(err)
}

dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
panic(err)
if !strings.Contains(f.Name, "..") {
filePath := filepath.Join(dst, f.Name)
fmt.Println("unzipping file ", filePath)

if f.FileInfo().IsDir() {
fmt.Println("creating song folder...")
os.MkdirAll(filePath, os.ModePerm)
continue
}

if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
panic(err)
}

dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
panic(err)
}

fileInArchive, err := f.Open()
if err != nil {
panic(err)
}

if _, err := io.Copy(dstFile, fileInArchive); err != nil {
panic(err)
}

dstFile.Close()
fileInArchive.Close()
}

fileInArchive, err := f.Open()
if err != nil {
panic(err)
}

if _, err := io.Copy(dstFile, fileInArchive); err != nil {
panic(err)
}

dstFile.Close()
fileInArchive.Close()
}
return nil
}
Expand Down

0 comments on commit a466bd8

Please sign in to comment.