return filename in download google file

This commit is contained in:
namhq1989 2022-12-01 16:41:10 +07:00
parent 1917722b3a
commit 893dfff492
1 changed files with 8 additions and 8 deletions

View File

@ -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
}