Skip to content

Commit

Permalink
Added ability to set custom default directory
Browse files Browse the repository at this point in the history
  • Loading branch information
JaskaranSM committed Mar 5, 2021
1 parent 6a78b98 commit d2da7ac
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
32 changes: 32 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"io/ioutil"
"log"
"path"

"github.com/prologic/bitcask"
)

const (
DB_NAME string = "drivedl-go-db"
CREDENTIALS string = "credentials"
TOKEN string = "token"
DL_DIR string = "dl_dir"
)

func getDb() *bitcask.Bitcask {
Expand Down Expand Up @@ -97,3 +99,33 @@ func RemoveTokenDb() (bool, error) {
}
return true, nil
}

func AddDLDirDb(dir_path string) (bool, error) {
db := getDb()
defer db.Close()
err := db.Put([]byte(DL_DIR), []byte(dir_path))
if err != nil {
return false, err
}
return true, nil
}

func GetDLDirDb() (string, error) {
db := getDb()
defer db.Close()
data, err := db.Get([]byte(DL_DIR))
if err != nil {
return ".", err
}
return string(data), nil
}

func RemoveDLDirDb() (bool, error) {
db := getDb()
defer db.Close()
err := db.Delete([]byte(DL_DIR))
if err != nil {
return false, err
}
return true, nil
}
15 changes: 12 additions & 3 deletions drive/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,19 @@ func (G *GoogleDriveClient) Download(nodeId string, localPath string) {
fmt.Printf("Name: %s, MimeType: %s\n", file.Name, file.MimeType)
absPath := path.Join(localPath, file.Name)
if file.MimeType == G.GDRIVE_DIR_MIMETYPE {
os.MkdirAll(absPath, 0755)
err := os.MkdirAll(absPath, 0755)
if err != nil {
log.Println("Error while creating directory: ", err.Error())
return
}
G.TraverseNodes(file.Id, absPath)

} else {
os.MkdirAll(localPath, 0755)
err := os.MkdirAll(localPath, 0755)
if err != nil {
log.Println("Error while creating directory: ", err.Error())
return
}
exists, bytesDled, err := utils.CheckLocalFile(absPath, file.Md5Checksum)
if err != nil {
log.Printf("[FileCheckError]: %v\n", err)
Expand Down Expand Up @@ -248,7 +257,7 @@ func (G *GoogleDriveClient) DownloadFile(file *drive.File, localPath string, sta
time.Sleep(5 * time.Second)
return G.DownloadFile(file, localPath, startByteIndex)
}
log.Printf("[API-files:get]: (%s) %v", file.Id, err)
log.Printf("[API-files:get]: (%s) %v\n", file.Id, err)
return false
}
bar := G.GetProgressBar(file.Name, file.Size-startByteIndex)
Expand Down
54 changes: 51 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/url"
"os"
"path"
"regexp"

"github.com/urfave/cli"
Expand Down Expand Up @@ -52,14 +53,22 @@ func downloadCallback(c *cli.Context) error {
GD.Authorize()
GD.SetConcurrency(c.Int("conn"))
GD.SetAbusiveFileDownload(c.Bool("acknowledge-abuse"))
GD.Download(fileId, c.String("path"))
cus_path, err := db.GetDLDirDb()
if err == nil {
if c.String("path") == "." {
path.Join(cus_path, c.String("path"))
} else {
cus_path = c.String("path")
}
}
GD.Download(fileId, cus_path)
return nil
}

func setCredsCallback(c *cli.Context) error {
arg := c.Args().Get(0)
if arg == "" {
return errors.New("Provide a Proper credentials.json file path.")
return errors.New("Provide a proper credentials.json file path.")
}
fmt.Printf("Detected credentials.json Path: %s\n", arg)
if !db.IsCredentialsInDb() {
Expand All @@ -85,6 +94,35 @@ func rmCredsCallback(c *cli.Context) error {
return nil
}

func setDLDirCallback(c *cli.Context) error {
arg := c.Args().Get(0)
if arg == "" {
return errors.New("Provide a proper download directory path.")
}
fmt.Printf("Detected download directory path: %s\n", arg)
_, err := db.GetDLDirDb()
if err == nil {
db.RemoveDLDirDb()
}
_, err = db.AddDLDirDb(arg)
return err
}

func rmDLDirCallback(c *cli.Context) error {
_, err := db.GetDLDirDb()
if err != nil {
fmt.Println("DB doesnt contain default directory path, try --help.")
} else {
_, err = db.RemoveDLDirDb()
if err == nil {
fmt.Println("Default directory removed successfully, now application will download in current working directory.")
} else {
fmt.Println("Error while removing default directory: ", err.Error())
}
}
return nil
}

func main() {
dlFlags := []cli.Flag{
&cli.StringFlag{
Expand Down Expand Up @@ -122,8 +160,18 @@ func main() {
Usage: "remove credentials from database",
Action: rmCredsCallback,
},
{
Name: "setdldir",
Usage: "set default download directory",
Action: setDLDirCallback,
},
{
Name: "rmdldir",
Usage: "remove default download directory and set the application to download in current folder.",
Action: rmDLDirCallback,
},
}
app.Version = "1.4"
app.Version = "1.5"
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit d2da7ac

Please sign in to comment.