From 6bd6cdfdc54231c50e17df79330533fe8760ddd1 Mon Sep 17 00:00:00 2001 From: Bobby Bones <159389674+saariuslystoned@users.noreply.github.com> Date: Sun, 8 Nov 2026 07:54:41 -0500 Subject: [PATCH 1/2] fix(drive): download the target of a shortcut drive download treated every application/vnd.google-apps.* type as a Workspace document, so a shortcut ID went to files.export, which only exports Docs Editors files. Read shortcutDetails.targetId and download or export the target instead. An unreadable target keeps Drive's error, so a deleted target still exits not_found. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 + internal/cmd/drive_download.go | 27 ++++- internal/cmd/drive_download_shortcut_test.go | 112 +++++++++++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 internal/cmd/drive_download_shortcut_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 861521fad..15b293635 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`). — 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. diff --git a/internal/cmd/drive_download.go b/internal/cmd/drive_download.go index ea15f6500..1b9f33a8d 100644 --- a/internal/cmd/drive_download.go +++ b/internal/cmd/drive_download.go @@ -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") } @@ -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)) diff --git a/internal/cmd/drive_download_shortcut_test.go b/internal/cmd/drive_download_shortcut_test.go new file mode 100644 index 000000000..cd8a3f628 --- /dev/null +++ b/internal/cmd/drive_download_shortcut_test.go @@ -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) + } +} From 6d9d4aae41b374785469b267b9fcd4826f95b8a9 Mon Sep 17 00:00:00 2001 From: Bobby Bones <159389674+saariuslystoned@users.noreply.github.com> Date: Sun, 8 Nov 2026 14:01:18 -0500 Subject: [PATCH 2/2] docs(changelog): reference #1315 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15b293635..9c24822c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 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`). — thanks @saariuslystoned. +- 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