Skip to content

Commit

Permalink
fix(filename): replace some characters in filename that os doesn't like
Browse files Browse the repository at this point in the history
  • Loading branch information
JaskaranSM committed Oct 26, 2022
1 parent a244ff5 commit dd07b6a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions drive/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (G *GoogleDriveClient) Download(nodeId string, localPath string, outputPath
file := G.GetFileMetadata(nodeId)
fmt.Printf("Name: %s, MimeType: %s\n", file.Name, file.MimeType)
if outputPath == "" {
outputPath = file.Name
outputPath = utils.CleanupFilename(file.Name)
}
absPath := path.Join(localPath, outputPath)
if file.MimeType == G.GDRIVE_DIR_MIMETYPE {
Expand All @@ -202,7 +202,7 @@ func (G *GoogleDriveClient) Download(nodeId string, localPath string, outputPath
func (G *GoogleDriveClient) TraverseNodes(nodeId string, localPath string) {
files := G.GetFilesByParentId(nodeId)
for _, file := range files {
absPath := path.Join(localPath, file.Name)
absPath := path.Join(localPath, utils.CleanupFilename(file.Name))
if file.MimeType == G.GDRIVE_DIR_MIMETYPE {
err := os.MkdirAll(absPath, 0755)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path"
"strconv"
"strings"

"github.com/OpenPeeDeeP/xdg"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -52,6 +53,9 @@ func fileExists(filename string) bool {
if os.IsNotExist(err) {
return false
}
if info == nil {
return false
}
return !info.IsDir()
}

Expand Down Expand Up @@ -98,3 +102,10 @@ func StringToInt(str string) (int, error) {
}
return i, nil
}

func CleanupFilename(name string) string {
for _, char := range []string{"\"", "?", "&", "*", "@", "!", "'"} {
name = strings.ReplaceAll(name, char, "")
}
return name
}

0 comments on commit dd07b6a

Please sign in to comment.