From 893dfff49213df3ea40500490253030527c10c75 Mon Sep 17 00:00:00 2001 From: namhq1989 Date: Thu, 1 Dec 2022 16:41:10 +0700 Subject: [PATCH] return filename in download google file --- google_drive.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/google_drive.go b/google_drive.go index 7c2c417..36f3102 100644 --- a/google_drive.go +++ b/google_drive.go @@ -86,7 +86,7 @@ func (g GoogleDrive) GetFilesByFolderID(folderID string, mimeType []string) ([]G } // DownloadByFileID download file into specific path -func (g GoogleDrive) DownloadByFileID(id, path string) (err error) { +func (g GoogleDrive) DownloadByFileID(id, path string) (filename string, err error) { // trim "/" in the end of path path = strings.TrimSuffix(path, "/") @@ -96,21 +96,21 @@ func (g GoogleDrive) DownloadByFileID(id, path string) (err error) { // Get the data resp, err := http.Get(url) if err != nil { - return err + return "", err } defer resp.Body.Close() // Check server response if resp.StatusCode != http.StatusOK { - return fmt.Errorf("bad status: %s", resp.Status) + return "", fmt.Errorf("bad status: %s", resp.Status) } // get file name - filename := getFileNameFromHeaderContentDisposition(resp.Header.Get("Content-Disposition")) + filename = getFileNameFromHeaderContentDisposition(resp.Header.Get("Content-Disposition")) // return err if not found if filename == "" { - return errors.New("file is not existed or is in private mode, please check again") + return "", errors.New("file is not existed or is in private mode, please check again") } // assign location with field name @@ -119,7 +119,7 @@ func (g GoogleDrive) DownloadByFileID(id, path string) (err error) { // Create the file out, err := os.Create(location) if err != nil { - return err + return "", err } defer out.Close() @@ -129,8 +129,8 @@ func (g GoogleDrive) DownloadByFileID(id, path string) (err error) { // Writer the body to file _, err = io.Copy(out, resp.Body) if err != nil { - return err + return "", err } - return nil + return filename, nil }