-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
letrananhvu
committed
Oct 7, 2020
0 parents
commit b33eb51
Showing
7 changed files
with
2,394 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"projects": { | ||
"default": "cloud-fnc" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
Oops, something went wrong.