From c36f40c5237c69524259016699a83e88dfb5458e Mon Sep 17 00:00:00 2001 From: Levi-Hope <117010292@link.cuhk.edu.cn> Date: Sun, 27 Jun 2021 12:03:04 +0800 Subject: [PATCH] googleDrive: add direct link support for link cmd --- backend/drive/drive.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/drive/drive.go b/backend/drive/drive.go index 240e6a189..18befa3e3 100755 --- a/backend/drive/drive.go +++ b/backend/drive/drive.go @@ -55,6 +55,7 @@ import ( const ( rcloneClientID = "202264815644.apps.googleusercontent.com" rcloneEncryptedClientSecret = "eX8GpZTVx3vxMWVkuuBdDWmAUE6rGhTwVrvG9GhllYccSdj2-mvHVg" + rcloneAPIKey = "AIzaSyB7Qf0eRsyvLZoCMtYnHMc1U5CjvX0pEdQ" // FIXME Should use APIKey of rcloneDev driveFolderType = "application/vnd.google-apps.folder" shortcutMimeType = "application/vnd.google-apps.shortcut" shortcutMimeTypeDangling = "application/vnd.google-apps.shortcut.dangling" // synthetic mime type for internal use @@ -2575,10 +2576,12 @@ func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object, // PublicLink adds a "readable by anyone with link" permission on the given file or folder. func (f *Fs) PublicLink(ctx context.Context, remote string, expire fs.Duration, unlink bool) (link string, err error) { + isFolder := false id, err := f.dirCache.FindDir(ctx, remote, false) if err == nil { fs.Debugf(f, "attempting to share directory '%s'", remote) id = shortcutID(id) + isFolder = true } else { fs.Debugf(f, "attempting to share single file '%s'", remote) o, err := f.NewObject(ctx, remote) @@ -2606,7 +2609,16 @@ func (f *Fs) PublicLink(ctx context.Context, remote string, expire fs.Duration, if err != nil { return "", err } - return fmt.Sprintf("https://drive.google.com/open?id=%s", id), nil + + // Convert share link to direct download link if target is not a folder + if isFolder { + fs.Debugf(nil, "Can't convert share link for folder to direct link - returning the link as is") + return fmt.Sprintf("https://drive.google.com/open?id=%s", id), nil + } + + // Do not use "https://drive.google.com/uc?id={id}&export=download" method because of the 100 MB file size limit. + // The current "https://www.googleapis.com/drive/v3/..." method will rename the downloaded file to {id}.{suffix} + return fmt.Sprintf("https://www.googleapis.com/drive/v3/files/%s?alt=media&key=%s", id, rcloneAPIKey), nil } // DirMove moves src, srcRemote to this remote at dstRemote