This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 506
/
remote.go
203 lines (180 loc) · 5.4 KB
/
remote.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package drive
import (
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"code.google.com/p/goauth2/oauth"
drive "code.google.com/p/google-api-go-client/drive/v2"
"github.com/rakyll/drive/config"
)
const (
// Google OAuth 2.0 service URLs
GoogleOAuth2AuthURL = "https://accounts.google.com/o/oauth2/auth"
GoogleOAuth2TokenURL = "https://accounts.google.com/o/oauth2/token"
// OAuth 2.0 OOB redirect URL for authorization.
RedirectURL = "urn:ietf:wg:oauth:2.0:oob"
// OAuth 2.0 full Drive scope used for authorization.
DriveScope = "https://www.googleapis.com/auth/drive"
// OAuth 2.0 access type for offline/refresh access.
AccessType = "offline"
)
var (
ErrPathNotExists = errors.New("remote path doesn't exist")
)
type Remote struct {
transport *oauth.Transport
service *drive.Service
}
func NewRemoteContext(context *config.Context) *Remote {
transport := newTransport(context)
service, _ := drive.New(transport.Client())
return &Remote{service: service, transport: transport}
}
func RetrieveRefreshToken(context *config.Context) (string, error) {
transport := newTransport(context)
url := transport.Config.AuthCodeURL("")
fmt.Println("Visit this URL to get an authorization code")
fmt.Println(url)
fmt.Print("Paste the authorization code: ")
var code string
fmt.Scanln(&code)
token, err := transport.Exchange(code)
if err != nil {
return "", err
}
return token.RefreshToken, nil
}
func (r *Remote) FindById(id string) (file *File, err error) {
req := r.service.Files.Get(id)
var f *drive.File
if f, err = req.Do(); err != nil {
return
}
return NewRemoteFile(f), nil
}
func (r *Remote) FindByPath(p string) (file *File, err error) {
if p == "/" {
return r.FindById("root")
}
parts := strings.Split(p, "/") // TODO: use path.Split instead
return r.findByPathRecv("root", parts[1:])
}
func (r *Remote) FindByParentId(parentId string) (files []*File, err error) {
req := r.service.Files.List()
// TODO: use field selectors
req.Q(fmt.Sprintf("'%s' in parents and trashed=false", parentId))
results, err := req.Do()
// TODO: handle paging
if err != nil {
return
}
for _, f := range results.Items {
if !strings.HasPrefix(f.Title, ".") { // ignore hidden files
files = append(files, NewRemoteFile(f))
}
}
return
}
func (r *Remote) Trash(id string) error {
_, err := r.service.Files.Trash(id).Do()
return err
}
func (r *Remote) Unpublish(id string) error {
return r.service.Permissions.Delete(id, "anyone").Do()
}
func (r *Remote) Publish(id string) (string, error) {
perm := &drive.Permission{Type: "anyone", Role: "reader"}
_, err := r.service.Permissions.Insert(id, perm).Do()
if err != nil {
return "", err
}
return "https://googledrive.com/host/" + id, nil
}
func (r *Remote) Download(id string) (io.ReadCloser, error) {
resp, err := r.transport.Client().Get("https://googledrive.com/host/" + id)
if err != nil || resp.StatusCode < 200 || resp.StatusCode > 299 {
return resp.Body, err
}
return resp.Body, nil
}
func (r *Remote) Upsert(parentId string, file *File, body io.Reader) (f *File, err error) {
uploaded := &drive.File{
Title: file.Name,
Parents: []*drive.ParentReference{&drive.ParentReference{Id: parentId}},
}
if file.IsDir {
uploaded.MimeType = "application/vnd.google-apps.folder"
}
if file.Id == "" {
req := r.service.Files.Insert(uploaded)
if !file.IsDir && body != nil {
req = req.Media(body)
}
if uploaded, err = req.Do(); err != nil {
return
}
return NewRemoteFile(uploaded), nil
}
// update the existing
req := r.service.Files.Update(file.Id, uploaded)
if !file.IsDir && body != nil {
req = req.Media(body)
}
if uploaded, err = req.Do(); err != nil {
return
}
return NewRemoteFile(uploaded), nil
}
func (r *Remote) findByPathRecv(parentId string, p []string) (file *File, err error) {
// find the file or directory under parentId and titled with p[0]
req := r.service.Files.List()
// TODO: use field selectors
req.Q(fmt.Sprintf("'%s' in parents and title = '%s' and trashed=false", parentId, p[0]))
files, err := req.Do()
if err != nil || len(files.Items) < 1 {
// TODO: make sure only 404s are handled here
return nil, ErrPathNotExists
}
file = NewRemoteFile(files.Items[0])
if len(p) == 1 {
return file, nil
}
return r.findByPathRecv(file.Id, p[1:])
}
func newAuthConfig(context *config.Context) *oauth.Config {
return &oauth.Config{
ClientId: context.ClientId,
ClientSecret: context.ClientSecret,
AuthURL: GoogleOAuth2AuthURL,
TokenURL: GoogleOAuth2TokenURL,
RedirectURL: RedirectURL,
AccessType: AccessType,
Scope: DriveScope,
}
}
func newTransport(context *config.Context) *oauth.Transport {
return &oauth.Transport{
Config: newAuthConfig(context),
Transport: http.DefaultTransport,
Token: &oauth.Token{
RefreshToken: context.RefreshToken,
Expiry: time.Now(),
},
}
}