aboutsummaryrefslogtreecommitdiff
path: root/internal/drive/drive.go
blob: 677880bc4d19e032ea0b310020d82ef85cd2482d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Package drive provides a thin wrapper around the Google Drive API v3 for
// operations relevant to Colab notebooks.
package drive

import (
	"context"
	"fmt"
	"net/http"

	gdrive "google.golang.org/api/drive/v3"
	"google.golang.org/api/googleapi"
	"google.golang.org/api/option"
)

const colabMIMEType = "application/vnd.google.colaboratory"

// Notebook is a minimal representation of a Colab notebook file in Drive.
type Notebook struct {
	ID       string
	Name     string
	Modified string // RFC 3339 string
	URL      string
}

// Client wraps the Drive API service.
type Client struct {
	svc *gdrive.Service
}

// New creates a Drive client using the provided authenticated HTTP client.
func New(ctx context.Context, httpClient *http.Client) (*Client, error) {
	svc, err := gdrive.NewService(ctx, option.WithHTTPClient(httpClient))
	if err != nil {
		return nil, fmt.Errorf("create drive service: %w", err)
	}
	return &Client{svc: svc}, nil
}

// ListNotebooks returns all Colab notebooks (.ipynb files with the Colab MIME
// type) that are owned by or shared with the authenticated user.
func (c *Client) ListNotebooks(ctx context.Context) ([]*Notebook, error) {
	var notebooks []*Notebook
	pageToken := ""
	fields := googleapi.Field("nextPageToken, files(id, name, modifiedTime, webViewLink)")
	query := fmt.Sprintf("mimeType='%s' and trashed=false", colabMIMEType)

	for {
		call := c.svc.Files.List().
			Q(query).
			Fields(fields).
			OrderBy("modifiedTime desc").
			PageSize(100).
			Context(ctx)
		if pageToken != "" {
			call = call.PageToken(pageToken)
		}

		resp, err := call.Do()
		if err != nil {
			return nil, fmt.Errorf("list drive files: %w", err)
		}
		for _, f := range resp.Files {
			notebooks = append(notebooks, &Notebook{
				ID:       f.Id,
				Name:     f.Name,
				Modified: f.ModifiedTime,
				URL:      f.WebViewLink,
			})
		}
		if resp.NextPageToken == "" {
			break
		}
		pageToken = resp.NextPageToken
	}
	return notebooks, nil
}

// GetNotebook fetches the raw JSON bytes of a notebook from Drive by its file
// ID. The content is the raw .ipynb JSON.
func (c *Client) GetNotebook(ctx context.Context, fileID string) ([]byte, error) {
	resp, err := c.svc.Files.Export(fileID, "application/json").Context(ctx).Download()
	if err != nil {
		// Colab files are not Google Docs, so Export will not work.
		// Use the plain media download instead.
		resp2, err2 := c.svc.Files.Get(fileID).Context(ctx).Download()
		if err2 != nil {
			return nil, fmt.Errorf("download notebook (file %s): %w", fileID, err)
		}
		defer resp2.Body.Close()
		buf := make([]byte, 0, 512*1024)
		tmp := make([]byte, 32*1024)
		for {
			n, readErr := resp2.Body.Read(tmp)
			if n > 0 {
				buf = append(buf, tmp[:n]...)
			}
			if readErr != nil {
				break
			}
		}
		return buf, nil
	}
	defer resp.Body.Close()
	buf := make([]byte, 0, 512*1024)
	tmp := make([]byte, 32*1024)
	for {
		n, readErr := resp.Body.Read(tmp)
		if n > 0 {
			buf = append(buf, tmp[:n]...)
		}
		if readErr != nil {
			break
		}
	}
	return buf, nil
}