Skip to content

Commit

Permalink
worked
Browse files Browse the repository at this point in the history
  • Loading branch information
letrananhvu committed Oct 7, 2020
0 parents commit b33eb51
Show file tree
Hide file tree
Showing 7 changed files with 2,394 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "cloud-fnc"
}
}
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*

# Firebase cache
.firebase/

# Firebase config

# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
# .firebaserc

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
1 change: 1 addition & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
72 changes: 72 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const functions = require('firebase-functions');
const { v4: uuidv4 } = require('uuid');

const path = require('path');
const os = require('os');
const fs = require('fs');


const admin = require('firebase-admin');
admin.initializeApp();

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.onFileChange = functions.storage.object().onFinalize(async (object) => {
const bucket = object.bucket;
const contentType = object.contentType;
const filePath = object.name;
const fileName = path.basename(filePath);
const destBucket = admin.storage().bucket(bucket);
const prefix = 'Renamed-';

if (!contentType.startsWith('image/')) {
return console.log('This is not an image.');
}


if (fileName.startsWith(prefix)) {
// Get the Signed URLs for the thumbnail and original image.
const config = {
action: 'read',
expires: '03-01-2500',
};

const renamedFile = destBucket.file(fileName);
console.log('renamedFile:', renamedFile);

await renamedFile.getSignedUrl(config);

return console.log('skip this file');
}

console.log('File change detected, function execution started');

const tmpFilePath = path.join(os.tmpdir(), filePath);


await destBucket.file(filePath).download({destination: tmpFilePath});

const uuid = uuidv4();
const metadata = {
contentType: contentType,
metadata: {
firebaseStorageDownloadTokens: uuid
}

};
await destBucket.upload(tmpFilePath, {
destination: prefix + fileName,
metadata: metadata,
public: true,
predefinedAcl: 'publicRead'
});

// remove the temporary file
fs.unlinkSync(tmpFilePath);




return console.log('done');
});
Loading

0 comments on commit b33eb51

Please sign in to comment.