Skip to content

Commit 308516c

Browse files
reorganising components
1 parent e100759 commit 308516c

11 files changed

Lines changed: 589 additions & 5 deletions

File tree

package-lock.json

Lines changed: 522 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"devDependencies": {
3030
"express": "^4.16.4",
31-
"helmet": "^3.15.0"
31+
"helmet": "^3.15.0",
32+
"sharp": "^0.21.1"
3233
}
3334
}

server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ const bodyParser = require('body-parser');
1515
//require('dotenv').config({silent:true}) // Switch to Env loading of config?
1616

1717
// Require Route Modules
18-
const index = require(__dirname + '/index.js');
18+
const index = require(__dirname + '/experiments/gcp/index.js');
19+
//const index = require(__dirname + '/experiments/bluemix/index.js');
20+
//const index = require(__dirname + '/experiments/gcp/index.js');
21+
//const index = require(__dirname + '/experiments/gcp/index.js');
1922

2023
const PORT = 8080;
2124

File renamed without changes.
File renamed without changes.
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ const storage = new Storage({
88
keyFilename: './keys/cloudStorage.json'
99
});
1010

11+
/**
12+
* downloadID
13+
* @param {string} fileName
14+
* @param {Storage.bucket} bucket
15+
* @return {Promise} Promise that resolves to the download stream.
16+
*/
1117
function downloadByID(fileName, bucket) {
1218
let file = bucket.file(fileName);
1319
return new Promise((resolve, reject) => {
1420
file.createReadStream()
1521
.on('response', (response) => { response.pause(); resolve(response); })
16-
.on('error', (reject))
17-
.pipe(); // localCache
22+
.on('error', (reject));
23+
// .pipe(); // localCache
1824
})
1925
.catch((error) => { return { error }; }); // TODO: Add more detailed error handling. - statusCode etc.
2026
}

src/gcp/experiments/resize.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const {Storage} = require('@google-cloud/storage');
4+
import * as sharp from 'sharp';
5+
6+
import * as download from './download.js';
7+
const get = './get.js';
8+
const config = require('./../../config.json');
9+
const storage = new Storage({
10+
projectId: config.project_id,
11+
keyFilename: './keys/cloudStorage.json'
12+
});
13+
14+
//TODO: Update download/get/upload files to include both named and default exports.
15+
16+
let resizeToStream = exports.resizeToStream = function (id, bucket, size) {
17+
let transform = sharp().resize(size.width, size.height);
18+
19+
return download.downloadByID(id, bucket)
20+
.then((stream) => { stream.pipe(transform); stream.pause(); return stream; });
21+
};
22+
23+
let resizeInPlace = exports.resizeInPlace = function (id, bucket, size) {
24+
let fileName = `resize@${size.width}x${size.height}_${fileName}`;
25+
let file = bucket.file(fileName);
26+
27+
return resizeToStream(id, bucket, size)
28+
.then((stream) => { return stream.pipe(file.createWriteStream({ gzip: true })); })
29+
.then(() => { get.getURL(fileName, bucket); })
30+
.catch((error) => { return { error }; }); // TODO: Add more detailed error handling. - statusCode etc
31+
};
32+
33+
// TODO: Refactor away from Firebase syntax to raw Storage listener
34+
// https://angularfirebase.com/lessons/image-thumbnail-resizer-cloud-function/
35+
exports resizeCRON = functions.storage
36+
.object()
37+
38+
39+
export default function (req, res) {
40+
// TODO: pre-string parsing / error validation and checking.
41+
let width = parseInt(req.query.width);
42+
let height = parseInt(req.query.height);
43+
let id = req.params.imageID;
44+
//const format = req.query.format;
45+
46+
return resizeToStream(id, {width, height})
47+
.then((stream) => {
48+
//res.type(''); //TODO
49+
return stream.pipe(res);
50+
});
51+
//return uploadByURL(url, storage.bucket(config.bucket_name), path.basename(url));
52+
}

0 commit comments

Comments
 (0)