|
2 | 2 | const url = require('url'); |
3 | 3 | //const uuid = require('uuidv4'); |
4 | 4 | const request = require('request'); |
| 5 | +const path = require('path'); |
5 | 6 | //const rp = require('request-promise'); |
6 | 7 |
|
7 | 8 | const config = require('./config.json'); |
@@ -46,52 +47,64 @@ function uploadByURL(url, bucket, fileName) { |
46 | 47 | .catch((error) => { return { error }; }); // TODO: Add more detailed error handling. - statusCode etc. |
47 | 48 | } |
48 | 49 |
|
49 | | -function upload(url, fileName) { |
50 | | - return uploadByURL(url, storage.bucket(config.bucket_name), fileName); |
| 50 | +function upload(url) { |
| 51 | + return uploadByURL(url, storage.bucket(config.bucket_name), path.basename(url)); |
51 | 52 | } |
52 | 53 |
|
| 54 | + |
53 | 55 | /** |
54 | | - * |
55 | | - * TODO: refactor to adapt based on content-type |
| 56 | + * Returns a Promise which resolves to a Signed URL to access the uploaded file |
| 57 | + * TODO: set expiry date to match the bucket's expiry rules for files |
56 | 58 | */ |
57 | | -function getImage(req, res) { |
58 | | - |
59 | | - res.status(200).send(); |
| 59 | +function getURL(fileName, bucket) { |
| 60 | + let file = bucket.file(fileName); |
| 61 | + return new Promise((resolve, reject) => { |
| 62 | + file.getSignedUrl({action:'read', expires:'01-01-2020'}, function(err, url) { |
| 63 | + if (err) reject(err); |
| 64 | + else resolve(url); |
| 65 | + }); |
| 66 | + }); |
60 | 67 | } |
61 | 68 |
|
62 | | -/* |
63 | | - switch (req.get('content-type')) { |
| 69 | +function getImage(fileName, contentType) { |
| 70 | + switch (contentType) { |
64 | 71 | case 'application/JSON': |
65 | | - break; |
66 | | - case 'image/jpeg': |
| 72 | + return getURL(fileName, storage.bucket(config.bucket_name)); |
| 73 | + /*case 'image/jpeg': |
67 | 74 | break; |
68 | 75 | case 'image/png': |
69 | | - break; |
| 76 | + break;*/ |
70 | 77 | } |
71 | | - */ |
| 78 | +} |
72 | 79 |
|
73 | | -// Lambdas |
74 | 80 | /** |
75 | 81 | * images - /images webhook. |
76 | 82 | */ |
77 | 83 | exports.images = (req, res) => { |
78 | | - if(!res.body.urls) res.status(400).json({error:'noURLs', message:'No URLs were provided.'}); |
79 | | - let urls = res.body.urls; |
| 84 | + if(req.method == 'PUT' && !req.body.urls) res.status(400).json({error:'noURLs', message:'No URLs were provided.'}); |
| 85 | + if(req.method == 'GET' && !req.body.filename) res.status(400).json({error:'noImageName', message:'No Image Name was provided'}); |
| 86 | + let urls = req.body.urls; |
| 87 | + let fileName = req.body.filename; |
| 88 | + |
80 | 89 | switch(req.method) { |
81 | | - case 'POST': |
| 90 | + case 'PUT': |
82 | 91 | // For each URL, verify, upload it, then aggregate the results to return as JSON object. |
83 | 92 | Promise.all( |
84 | 93 | urls.map((url) => { |
85 | 94 | return verifyURL(url) |
86 | | - .then((url) => { upload(url, url); }); // naming convention will likely need to change for multi-users |
| 95 | + // TODO: Check pre-existence of file |
| 96 | + .then((url) => { upload(url); }); |
87 | 97 | }) |
88 | 98 | ) |
89 | 99 | // TODO: Change status IDs depending on error/s from Promises. |
| 100 | + // Promise failure here is rudimentary - need to handle mixed results. |
90 | 101 | .then(res.status(202).json) |
91 | 102 | .catch(res.status(400).json); |
92 | 103 | break; |
93 | 104 | case 'GET': |
94 | | - getImage(req, res); |
| 105 | + getImage(fileName, req.get('content-type')) |
| 106 | + .then(res.status(202).json) |
| 107 | + .catch(res.status(400).json); |
95 | 108 | break; |
96 | 109 | } |
97 | 110 | }; |
0 commit comments