Skip to content

Commit 724cb62

Browse files
refactoring upload, setting up streaming download
1 parent a33e6a2 commit 724cb62

2 files changed

Lines changed: 40 additions & 40 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,24 @@ await storage.bucket(bucketName).upload(filename, {
2727
});
2828

2929
console.log(`${filename} uploaded to ${bucketName}.`);
30+
31+
/**
32+
* TODO(developer): Uncomment the following lines before running the sample.
33+
*/
34+
// const srcFilename = 'Remote file to download, e.g. file.txt';
35+
// const destFilename = 'Local destination for file, e.g. ./local/path/to/file.txt';
36+
37+
const options = {
38+
// The path to which the file should be downloaded, e.g. "./file.txt"
39+
destination: destFilename,
40+
};
41+
42+
// Downloads the file
43+
await storage
44+
.bucket(bucketName)
45+
.file(srcFilename)
46+
.download(options);
47+
48+
console.log(
49+
`gs://${bucketName}/${srcFilename} downloaded to ${destFilename}.`
50+
);

index.js

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,25 @@ const storage = new Storage({
1212
keyFilename: config.keys.cloud_storage.path
1313
});
1414

15-
function parseURL(urlString) {
16-
try {
17-
return url.parse(urlString);
18-
}
19-
catch(e) {
20-
// TODO
21-
}
22-
}
23-
24-
function checkURLStatus(url) {
15+
/**
16+
* verifyURL - confirm that the URL can be reached.
17+
* @param {string} url
18+
* @return {Promise} Promise resolves to the url or rejects with a JSON object as error.
19+
*
20+
* TODO: Add in more specific error messages for rejection and parsing errors:: {url, error:'invalidURL', message:'URL could not be parsed. Please check that URL is valid'}
21+
*/
22+
function verifyURL(path) {
2523
return new Promise((resolve, reject) => {
26-
request({method: 'HEAD', url}, function (error, response) {
24+
request({method: 'HEAD', url:url.parse(path)}, function (error, response) { // Availability of HEAD method not guaranteed. Refactor.
2725
if(error || response.statusCode !== 200)
2826
reject({url, statusCode:response.statusCode, error:error?error:'inaccessibleURL', message:'Remote URL could not be reached.'});
29-
resolve(url);
27+
resolve(path);
3028
});
3129
});
3230
}
3331

34-
/**
35-
* verifyURL - pre-upload checks to ensure we notify client of failures prior to attempted upload.
36-
**/
37-
function verifyURL(url) {
38-
return new Promise((resolve, reject) => {
39-
if(!parseURL(url))
40-
reject({url, error:'invalidURL', message:'URL could not be parsed. Please check that URL is valid'});
41-
else {
42-
checkURLStatus(url)
43-
.then(resolve)
44-
.catch(reject);
45-
}
46-
});
47-
}
48-
4932
/* // Testing purposes:
5033
async function uploadLocalFile(bucket, fileName) {
51-
5234
await bucket.upload(fileName, { gzip: true });
5335
}
5436
*/
@@ -69,31 +51,28 @@ function upload(url, fileName) {
6951
return uploadByURL(url, storage.bucket(config.bucket_name), fileName);
7052
}
7153

72-
54+
/**
55+
*
56+
* TODO: refactor to adapt based on content-type
57+
*/
7358
function getImage(req, res) {
74-
let message = req.query.message || req.body.message || 'Hello World!';
59+
res.status(200).send();
60+
}
61+
62+
/*
7563
switch (req.get('content-type')) {
7664
case 'application/JSON':
77-
7865
break;
7966
case 'image/jpeg':
80-
8167
break;
8268
case 'image/png':
83-
8469
break;
8570
}
86-
res.status(200).send(message);
87-
}
88-
71+
*/
8972

9073
// Lambdas
91-
9274
/**
9375
* images - /images webhook.
94-
* @param {[type]} req [description]
95-
* @param {[type]} res [description]
96-
* @return {[type]} [description]
9776
*/
9877
exports.images = (req, res) => {
9978
if(!res.body.urls) res.status(400).json({error:'noURLs', message:'No URLs were provided.'});

0 commit comments

Comments
 (0)