├── .firebaserc ├── .gitignore ├── firebase.json └── functions ├── index.js ├── package-lock.json └── package.json /.firebaserc: -------------------------------------------------------------------------------- 1 | { 2 | "projects": { 3 | "default": "fb-cloud-functions-demo" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | fb-cloud-functions-demo-firebase-adminsdk-km39q-405896eddb.json 3 | -------------------------------------------------------------------------------- /firebase.json: -------------------------------------------------------------------------------- 1 | {} 2 | -------------------------------------------------------------------------------- /functions/index.js: -------------------------------------------------------------------------------- 1 | const functions = require("firebase-functions"); 2 | const os = require("os"); 3 | const path = require("path"); 4 | const spawn = require("child-process-promise").spawn; 5 | const cors = require("cors")({ origin: true }); 6 | const Busboy = require("busboy"); 7 | const fs = require("fs"); 8 | 9 | const gcconfig = { 10 | projectId: "fb-cloud-functions-demo", 11 | keyFilename: "fb-cloud-functions-demo-firebase-adminsdk-km39q-405896eddb.json" 12 | }; 13 | 14 | const gcs = require("@google-cloud/storage")(gcconfig); 15 | // // Create and Deploy Your First Cloud Functions 16 | // // https://firebase.google.com/docs/functions/write-firebase-functions 17 | // 18 | exports.onFileChange = functions.storage.object().onChange(event => { 19 | const object = event.data; 20 | const bucket = object.bucket; 21 | const contentType = object.contentType; 22 | const filePath = object.name; 23 | console.log("File change detected, function execution started"); 24 | 25 | if (object.resourceState === "not_exists") { 26 | console.log("We deleted a file, exit..."); 27 | return; 28 | } 29 | 30 | if (path.basename(filePath).startsWith("resized-")) { 31 | console.log("We already renamed that file!"); 32 | return; 33 | } 34 | 35 | const destBucket = gcs.bucket(bucket); 36 | const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath)); 37 | const metadata = { contentType: contentType }; 38 | return destBucket 39 | .file(filePath) 40 | .download({ 41 | destination: tmpFilePath 42 | }) 43 | .then(() => { 44 | return spawn("convert", [tmpFilePath, "-resize", "500x500", tmpFilePath]); 45 | }) 46 | .then(() => { 47 | return destBucket.upload(tmpFilePath, { 48 | destination: "resized-" + path.basename(filePath), 49 | metadata: metadata 50 | }); 51 | }); 52 | }); 53 | 54 | exports.uploadFile = functions.https.onRequest((req, res) => { 55 | cors(req, res, () => { 56 | if (req.method !== "POST") { 57 | return res.status(500).json({ 58 | message: "Not allowed" 59 | }); 60 | } 61 | const busboy = new Busboy({ headers: req.headers }); 62 | let uploadData = null; 63 | 64 | busboy.on("file", (fieldname, file, filename, encoding, mimetype) => { 65 | const filepath = path.join(os.tmpdir(), filename); 66 | uploadData = { file: filepath, type: mimetype }; 67 | file.pipe(fs.createWriteStream(filepath)); 68 | }); 69 | 70 | busboy.on("finish", () => { 71 | const bucket = gcs.bucket("fb-cloud-functions-demo.appspot.com"); 72 | bucket 73 | .upload(uploadData.file, { 74 | uploadType: "media", 75 | metadata: { 76 | metadata: { 77 | contentType: uploadData.type 78 | } 79 | } 80 | }) 81 | .then(() => { 82 | res.status(200).json({ 83 | message: "It worked!" 84 | }); 85 | }) 86 | .catch(err => { 87 | res.status(500).json({ 88 | error: err 89 | }); 90 | }); 91 | }); 92 | busboy.end(req.rawBody); 93 | }); 94 | }); 95 | 96 | exports.onDataAdded = functions.database.ref('/message/{id}').onCreate(event => { 97 | const data = event.data.val(); 98 | const newData = { 99 | msg: event.params.id + '-' + data.msg.toUpperCase() 100 | }; 101 | return event.data.ref.child('copiedData').set(newData); 102 | }); 103 | -------------------------------------------------------------------------------- /functions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "functions", 3 | "description": "Cloud Functions for Firebase", 4 | "scripts": { 5 | "serve": "firebase serve --only functions", 6 | "shell": "firebase experimental:functions:shell", 7 | "start": "npm run shell", 8 | "deploy": "firebase deploy --only functions", 9 | "logs": "firebase functions:log" 10 | }, 11 | "dependencies": { 12 | "@google-cloud/storage": "^1.5.1", 13 | "busboy": "^0.2.14", 14 | "child-process-promise": "^2.2.1", 15 | "cors": "^2.8.4", 16 | "firebase-admin": "~5.4.2", 17 | "firebase-functions": "^0.7.1" 18 | }, 19 | "private": true 20 | } 21 | --------------------------------------------------------------------------------