/
storage_gdrive.go
155 lines (129 loc) · 4.93 KB
/
storage_gdrive.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
/*
Copyright (C) 2013 Aaron Lindsay <aaron@aclindsay.com>
*/
package main
import (
"code.google.com/p/goauth2/oauth"
"code.google.com/p/goconf/conf"
"code.google.com/p/google-api-go-client/drive/v2"
"errors"
"fmt"
"io"
"net/http"
)
const GDRIVE_CLIENT_ID = "1006560298028.apps.googleusercontent.com"
const GDRIVE_CLIENT_SECRET = "2iTpEeN76RQK5KKF6ut1TCpV"
type GDriveStorage struct {
cachefile string
directory string
auth_code string
folderid string
transport *oauth.Transport
service *drive.Service
}
func NewGDriveStorage(config *conf.ConfigFile) (*GDriveStorage, error) {
cachefile, err := config.GetString("storage", "cachefile")
if err != nil {
return nil, errors.New("Error: GDriveStorage indicated in config file, but 'cachefile' not specified.")
}
code, err := config.GetString("storage", "oauth_code")
if err != nil {
code = ""
}
directory, err := config.GetString("storage", "directory")
if err != nil {
return nil, errors.New("Error: GDriveStorage indicated in config file, but 'directory' not specified.")
}
oauth_config := &oauth.Config{
ClientId: GDRIVE_CLIENT_ID,
ClientSecret: GDRIVE_CLIENT_SECRET,
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
Scope: "https://www.googleapis.com/auth/drive",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
TokenCache: oauth.CacheFile(cachefile),
}
transport := &oauth.Transport{Config: oauth_config}
token, err := oauth_config.TokenCache.Token()
if err != nil {
//if a code wasn't specified in the config file, ask the user to do that
if code == "" {
url := oauth_config.AuthCodeURL("")
return nil, errors.New(fmt.Sprintf("Visit the following URL and sign in using your Google account to get an authorization code allowing Asink to access your GDrive files. Be sure to add this code to your Asink config file as 'oauth_code = your_code_here' before re-starting Asink:\n%s\n", url))
}
//attempt to fetch a token using the user-supplied code (this
//has the effect of caching the token in the specified cache
//file)
token, err = transport.Exchange(code)
if err != nil {
url := oauth_config.AuthCodeURL("")
return nil, errors.New(fmt.Sprintf("Error exchanging user-supplied GDrive code for an authentication token. Please check your auth code supplied in the Asink config file, or consider obtaining another by visiting %s\n(%s)", url, err.Error()))
}
}
//Now, actually initialize the GDrive part of the API
transport.Token = token
s, err := drive.New(transport.Client())
if err != nil {
return nil, err
}
folderlist, err := s.Files.List().Q("mimeType = 'application/vnd.google-apps.folder' and title = '" + directory + "'").Do()
if len(folderlist.Items) < 1 {
//try to create a folder named 'directory'
f := &drive.File{Title: directory, Description: "Asink client folder", MimeType: "application/vnd.google-apps.folder"}
f, err := s.Files.Insert(f).Do()
folderlist, err = s.Files.List().Q("mimeType = 'application/vnd.google-apps.folder' and title = '" + directory + "'").Do()
if err != nil {
return nil, err
} else if len(folderlist.Items) < 1 {
return nil, errors.New("I was unable to create a new folder in your GDrive, but I'm not sure why")
}
} else if len(folderlist.Items) > 1 {
return nil, errors.New(fmt.Sprintf("Error: Your GDrive has more than one directory named '%s'. You are a barbarian. Fix that and we'll talk. (check your trash if you can't find it)\n", directory))
}
folderid := folderlist.Items[0].Id
gs := new(GDriveStorage)
gs.cachefile = cachefile
gs.directory = directory
gs.auth_code = code
gs.service = s
gs.transport = transport
gs.folderid = folderid
return gs, nil
}
func (gs *GDriveStorage) Put(hash string, done chan error) (w io.WriteCloser, e error) {
//TODO detect duplicates and don't re-upload this file if it already exists
f := &drive.File{Title: hash, Description: hash, MimeType: "application/pgp-encrypted"}
p := &drive.ParentReference{Id: gs.folderid}
f.Parents = []*drive.ParentReference{p}
reader, writer := io.Pipe()
go func() {
_, err := gs.service.Files.Insert(f).Media(reader).Do()
if err != nil {
reader.CloseWithError(err)
}
done <- err
}()
return writer, nil
}
func (gs *GDriveStorage) Get(hash string) (io.ReadCloser, error) {
folderlist, err := gs.service.Files.List().Q("mimeType = 'application/pgp-encrypted' and title = '" + hash + "' and '" + gs.folderid + "' in parents").Do()
if err != nil {
return nil, err
}
if len(folderlist.Items) < 1 {
return nil, errors.New(fmt.Sprintf("Error: '%s' not found", hash))
}
downloadUrl := folderlist.Items[0].DownloadUrl
if downloadUrl == "" {
return nil, errors.New(fmt.Sprintf("Error: content not found for '%s'", hash))
}
request, err := http.NewRequest("GET", downloadUrl, nil)
if err != nil {
return nil, err
}
response, err := gs.transport.RoundTrip(request)
if err != nil {
return nil, err
}
return response.Body, nil
}