Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.48.1 - Unreleased

- Drive: `drive download` of a shortcut now downloads or exports the file the shortcut points to, instead of asking Drive to export the shortcut itself, which always failed. A shortcut whose target is gone or unreadable reports that target with the target's error, so a deleted target exits `not_found` (`5`). (#1315) — thanks @saariuslystoned.

## 0.48.0 - 2026-11-05

**Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates.
Expand Down
27 changes: 26 additions & 1 deletion internal/cmd/drive_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,18 @@ func (c *DriveDownloadCmd) Run(ctx context.Context, flags *RootFlags) error {

meta, err := svc.Files.Get(fileID).
SupportsAllDrives(true).
Fields("id, name, mimeType").
Fields("id, name, mimeType, shortcutDetails(targetId)").
Context(ctx).
Do()
if err != nil {
return err
}
if meta.MimeType == driveMimeShortcut {
meta, err = driveShortcutTarget(ctx, svc, meta)
if err != nil {
return err
}
}
if meta.Name == "" {
return errors.New("file has no name")
}
Expand Down Expand Up @@ -130,6 +136,25 @@ func (c *DriveDownloadCmd) Run(ctx context.Context, flags *RootFlags) error {
return nil
}

// driveShortcutTarget returns the metadata of the file a shortcut points to.
// A shortcut has no content of its own, and Drive exports only Docs Editors
// files, so downloading the shortcut itself cannot succeed.
func driveShortcutTarget(ctx context.Context, svc *drive.Service, shortcut *drive.File) (*drive.File, error) {
if shortcut.ShortcutDetails == nil || strings.TrimSpace(shortcut.ShortcutDetails.TargetId) == "" {
return nil, fmt.Errorf("shortcut %s has no target", shortcut.Id)
}
targetID := strings.TrimSpace(shortcut.ShortcutDetails.TargetId)
target, err := svc.Files.Get(targetID).
SupportsAllDrives(true).
Fields("id, name, mimeType").
Context(ctx).
Do()
if err != nil {
return nil, fmt.Errorf("shortcut %s points to %s, which could not be read: %w", shortcut.Id, targetID, err)
}
return target, nil
}

func downloadDriveFile(ctx context.Context, svc *drive.Service, meta *drive.File, destPath string, format string, overwrite bool) (string, int64, error) {
isGoogleDoc := strings.HasPrefix(meta.MimeType, "application/vnd.google-apps.")
normalizedFormat := strings.ToLower(strings.TrimSpace(format))
Expand Down
112 changes: 112 additions & 0 deletions internal/cmd/drive_download_shortcut_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package cmd

import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"

"google.golang.org/api/drive/v3"
)

type driveShortcutDownloadCalls struct {
downloads []string
exports []string
}

// runDriveShortcutDownload serves metadata for shortcut sc1, which points at
// target, and records which file IDs gog downloads or exports.
func runDriveShortcutDownload(t *testing.T, target map[string]any, args ...string) (driveShortcutDownloadCalls, string, error) {
t.Helper()
var calls driveShortcutDownloadCalls
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
switch {
case strings.HasSuffix(r.URL.Path, "/files/sc1"):
_ = json.NewEncoder(w).Encode(map[string]any{
"id": "sc1",
"name": "Shortcut to report",
"mimeType": driveMimeShortcut,
"shortcutDetails": map[string]any{
"targetId": "t1",
"targetMimeType": target["mimeType"],
},
})
case strings.HasSuffix(r.URL.Path, "/files/t1") && target != nil:
_ = json.NewEncoder(w).Encode(target)
default:
w.WriteHeader(http.StatusNotFound)
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"code": 404, "message": "File not found: t1.", "errors": []map[string]any{{"reason": "notFound"}}}})
}
})
svc, cleanup := newDriveTestService(t, handler)
t.Cleanup(cleanup)

download := func(_ context.Context, _ *drive.Service, fileID string) (*http.Response, error) {
calls.downloads = append(calls.downloads, fileID)
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("target-bytes"))}, nil
}
export := func(_ context.Context, _ *drive.Service, fileID, _ string) (*http.Response, error) {
calls.exports = append(calls.exports, fileID)
if fileID != "t1" {
// Drive exports only Docs Editors files; a shortcut is not one.
return &http.Response{StatusCode: http.StatusForbidden, Status: "403 Forbidden", Body: io.NopCloser(strings.NewReader(`{"error":{"code":403,"message":"Export only supports Docs Editors files."}}`))}, nil
}
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("exported-pdf"))}, nil
}

dir := t.TempDir()
ctx := withDriveTestOperations(newCmdRuntimeJSONOutputContext(t, io.Discard, io.Discard), svc, download, export)
err := runKong(t, &DriveDownloadCmd{}, append([]string{"sc1", "--out", dir + string(os.PathSeparator)}, args...), ctx, &RootFlags{Account: "a@b.com"})
return calls, dir, err
}

func TestDriveDownloadCmd_ShortcutDownloadsTargetFile(t *testing.T) {
calls, dir, err := runDriveShortcutDownload(t, map[string]any{"id": "t1", "name": "report.pdf", "mimeType": "application/pdf"})
if err != nil {
t.Fatalf("download: %v", err)
}
if len(calls.exports) != 0 || len(calls.downloads) != 1 || calls.downloads[0] != "t1" {
t.Fatalf("calls = %#v, want one download of target t1", calls)
}
data, readErr := os.ReadFile(filepath.Join(dir, "t1_report.pdf"))
if readErr != nil || string(data) != "target-bytes" {
t.Fatalf("t1_report.pdf = %q, %v", data, readErr)
}
}

func TestDriveDownloadCmd_ShortcutExportsTargetDoc(t *testing.T) {
calls, dir, err := runDriveShortcutDownload(t, map[string]any{"id": "t1", "name": "Plan", "mimeType": driveMimeGoogleDoc})
if err != nil {
t.Fatalf("download: %v", err)
}
if len(calls.downloads) != 0 || len(calls.exports) != 1 || calls.exports[0] != "t1" {
t.Fatalf("calls = %#v, want one export of target t1", calls)
}
if data, readErr := os.ReadFile(filepath.Join(dir, "t1_Plan.pdf")); readErr != nil || string(data) != "exported-pdf" {
t.Fatalf("t1_Plan.pdf = %q, %v", data, readErr)
}
}

func TestDriveDownloadCmd_BrokenShortcutIsNotFound(t *testing.T) {
calls, _, err := runDriveShortcutDownload(t, nil)
var exitErr *ExitError
if !errors.As(stableExitCode(err), &exitErr) || exitErr.Code != 5 {
t.Fatalf("err = %v, want not_found exit 5", err)
}
if !strings.Contains(err.Error(), "shortcut") {
t.Fatalf("err = %v, want it to say the shortcut target is missing", err)
}
if len(calls.downloads) != 0 || len(calls.exports) != 0 {
t.Fatalf("calls = %#v, want no content request", calls)
}
}
Loading