Skip to content

Commit

Permalink
fix copying files without an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Oct 20, 2021
1 parent 2318d4d commit 44ad106
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions dirfs/dirfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ func Unzip(name string) error {

// CopyFile copies a file given a name.
func CopyFile(name string) error {
var splitName []string
var output string

srcFile, err := os.Open(name)
if err != nil {
return err
Expand All @@ -326,8 +329,20 @@ func CopyFile(name string) error {
err = srcFile.Close()
}()

splitName := strings.Split(name, ".")
output := fmt.Sprintf("%s_%d.%s", splitName[0], time.Now().Unix(), splitName[1])
fileExtension := filepath.Ext(name)
switch {
case strings.HasPrefix(name, ".") && fileExtension != "" && fileExtension == name:
output = fmt.Sprintf("%s_%d", name, time.Now().Unix())
case strings.HasPrefix(name, ".") && fileExtension != "" && fileExtension != name:
splitName = strings.Split(name, ".")
output = fmt.Sprintf(".%s_%d.%s", splitName[1], time.Now().Unix(), splitName[2])
case fileExtension != "":
splitName = strings.Split(name, ".")
output = fmt.Sprintf("%s_%d.%s", splitName[0], time.Now().Unix(), splitName[1])
default:
output = fmt.Sprintf("%s_%d", name, time.Now().Unix())
}

destFile, err := os.Create(output)
if err != nil {
return err
Expand Down

0 comments on commit 44ad106

Please sign in to comment.