-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
183 lines (156 loc) · 5.39 KB
/
index.js
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
var url = require('url')
var request = require('request').defaults({json: true})
var extend = require('xtend')
var through = require('through2')
var duplex = require('duplexify')
var mime = require('mime')
var concat = require('concat-stream')
var debug = require('debug')('google-cloud-storage')
module.exports = Client
function Client(opts) {
if (!(this instanceof Client)) return new Client(opts)
if (!opts) opts = {}
if (!opts.token) throw new Error('Must specify token')
this.baseURL = opts.baseURL || "https://www.googleapis.com/storage/v1"
this.uploadBaseURL = opts.uploadBaseURL || "https://www.googleapis.com/upload/storage/v1"
this.defaults = {
'Authorization': "Bearer " + opts.token
}
this.bucket = opts.bucket
}
Client.prototype.request = function(opts, cb) {
var reqOpts = extend({}, opts)
reqOpts.headers = extend({}, this.defaults, opts.headers)
if (process.env['DEBUG']) debug('request', JSON.stringify(reqOpts))
if (!cb) return request(reqOpts)
return request(reqOpts, function(err, resp, body) {
if (process.env['DEBUG']) debug('response', err, resp.statusCode, body)
cb(err, resp, body)
})
}
Client.prototype.exists = function(options, cb) {
var self = this
var bucket = options.bucket || this.bucket
if (!bucket) return cb(new Error('must specify bucket'))
if (!options.name) return cb(new Error('must specify object name'))
var url = this.baseURL + '/b/' + bucket + '/o/' + encodeURIComponent(options.name)
self.request({url: url, json: true}, function(err, resp, data) {
if (err) return cb(err)
if (resp.statusCode === 404) return cb(null, false)
cb(null, !!data)
})
}
Client.prototype.remove = function(options, cb) {
var self = this
var bucket = options.bucket || this.bucket
if (!bucket) return cb(new Error('must specify bucket'))
if (!options.name) return cb(new Error('must specify object name'))
var url = this.baseURL + '/b/' + bucket + '/o/' + encodeURIComponent(options.name)
self.request({url: url, json: true, method: 'DELETE'}, function(err, resp, data) {
if (err) return cb(err)
if (resp.statusCode > 299) return cb(new Error(data))
cb(null)
})
}
Client.prototype.createReadStream = function(options) {
var self = this
var proxy = duplex()
var bucket = options.bucket || this.bucket
if (!bucket) return proxy.destroy(new Error('must specify bucket'))
if (!options.key) return proxy.destroy(new Error('must specify object key'))
// alt=media makes it stream the file instead of the metadata
var url = this.baseURL + '/b/' + bucket + '/o/' + encodeURIComponent(options.key) + '?alt=media'
var read = self.request({url: url})
proxy.setReadable(read)
read.on('response', function(resp) {
if (resp.statusCode > 299) proxy.destroy(new Error(resp.statusCode))
})
read.on('error', function(err) {
proxy.destroy(err)
})
return proxy
}
Client.prototype.createWriteStream = function(options, cb) {
var self = this
var proxy = duplex()
var bucket = options.bucket || this.bucket
if (!bucket) {
var err = new Error('must specify bucket')
cb(err)
proxy.destroy(err)
return proxy
}
var objectURL = this.uploadBaseURL + '/b/' + encodeURIComponent(bucket) + '/o'
var newSession = {
method: "POST",
url: objectURL + '?uploadType=resumable',
json: {
name: options.name
},
headers: {
'X-Upload-Content-Type': mime.lookup(options.name)
}
}
self.request(newSession, function(err, resp, session) {
if (err) {
proxy.destroy(err)
return cb(err)
}
if (resp.statusCode > 299) {
var error = new Error(JSON.stringify({code: resp.statusCode, error: session}))
proxy.destroy(error)
return cb(error)
}
var parsed = url.parse(resp.headers.location, true)
var session = parsed.query.upload_id
var uploadURL = objectURL + '?uploadType=resumable&upload_id=' + session
var upload = self.request({url: uploadURL, method: 'PUT'})
proxy.setWritable(upload)
upload.on('response', function(resp) {
resp.pipe(concat(function(body) {
var meta = JSON.parse(body)
cb(null, self.normalizeMetadata(meta))
}))
})
upload.on('error', function(err) {
proxy.destroy(err)
cb(err)
})
})
return proxy
}
// normalize googles return metadata with what abstract-blob store expects
Client.prototype.normalizeMetadata = function(object) {
object.hash = new Buffer(object.md5Hash, 'base64').toString('hex')
object.size = +object.size
object.key = object.name
return object
}
Client.prototype.createBucketReadStream = function(bucket) {
var self = this
if (!bucket) bucket = this.bucket
importBucket()
var stream = through.obj()
return stream
function importBucket(next) {
debug('importing', next ? next : '')
var url = self.baseURL + '/b/' + encodeURIComponent(bucket) + '/o'
if (next) url += '?pageToken=' + next
self.request({url: url}, function(err, resp, body) {
if (err || (body && body.error) ) {
return console.error(JSON.stringify(err || body))
stream.end()
}
body.items.map(function(i) {
stream.push(self.normalizeMetadata(i))
})
if (body.nextPageToken) {
importBucket(body.nextPageToken)
} else {
if (resp.statusCode < 299) debug('finished import')
else debug(resp.statusCode, body)
stream.end()
}
})
}
}