diff --git a/db/db.go b/db/db.go index 3dc4ac6..b23b3e5 100644 --- a/db/db.go +++ b/db/db.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "log" "path" + "github.com/prologic/bitcask" ) @@ -12,6 +13,7 @@ const ( DB_NAME string = "drivedl-go-db" CREDENTIALS string = "credentials" TOKEN string = "token" + DL_DIR string = "dl_dir" ) func getDb() *bitcask.Bitcask { @@ -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 +} diff --git a/drive/drive.go b/drive/drive.go index 987167e..d0ab2bf 100644 --- a/drive/drive.go +++ b/drive/drive.go @@ -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) @@ -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) diff --git a/main.go b/main.go index d9626fe..ed95826 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "log" "net/url" "os" + "path" "regexp" "github.com/urfave/cli" @@ -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() { @@ -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{ @@ -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)