Last active
November 30, 2022 16:18
-
-
Save Charlesmendez/c8f24275b66bae89b7dadeeee1bb18e1 to your computer and use it in GitHub Desktop.
TimeOut firbase error
This file contains hidden or 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
import {QueryDocumentSnapshot} from "@google-cloud/firestore"; | |
import * as functions from "firebase-functions"; | |
import {EventContext} from "firebase-functions"; | |
import {Storage} from "@google-cloud/storage"; | |
export const shareToAnotherGraph = async (snap: QueryDocumentSnapshot, _context: EventContext, db: FirebaseFirestore.Firestore) => { | |
const data = snap.data(); | |
// The ID of your GCP project | |
const projectId = "xx"; | |
// The ID of the bucket the original object is in | |
const sourceBucket = "xx"; | |
// The ID of the GCS object to copy | |
const sourceFile = data.sourcePath; | |
functions.logger.log("sourceFile: ", sourceFile); | |
// The ID of the bucket to copy the object to | |
const destinationBucket = "xx"; | |
// get the name of the source graph | |
const sourceGraphName = String(snap.data().sourceGraphString); | |
functions.logger.log("sourceGraphName: ", sourceGraphName); | |
// get the name of the destination graph | |
const destinationGraphName = String(snap.data().destinationGraphString); | |
functions.logger.log("destinationGraphName: ", destinationGraphName); | |
// get the name of the node to copy | |
const sourceNodeString = String(snap.data().sourceNodeString); | |
functions.logger.log("sourceFileName: ", sourceNodeString); | |
// get the node of the destination | |
const destinationNodeString = String(snap.data().destinationNodeString); | |
functions.logger.log("destinationPathName: ", destinationNodeString); | |
const storage = new Storage({ | |
projectId: projectId, | |
}); | |
const [Files] = await storage.bucket(sourceBucket).getFiles({ | |
prefix: sourceFile, | |
}); | |
// create a regex to use the replace function | |
const regex = new RegExp(sourceNodeString, "g"); | |
// create regex to use for the source graph | |
const regexSourceGraph = new RegExp(sourceGraphName, "g"); | |
// create a bool variable with the value of false | |
let fileExists = false; | |
for (const file of Files) { | |
const fileName = file.name; | |
if (fileName.includes(sourceNodeString) || fileName.includes(sourceGraphName)) { | |
// const destination = storage.bucket(destinationBucket).file(fileName.replace(regex, destinationNodeString)); | |
const destination = storage.bucket(destinationBucket).file(fileName.replace(regex, destinationNodeString).replace(regexSourceGraph, destinationGraphName)); | |
functions.logger.log("Destination file.name: ", file.name); | |
await file.copy(destination); | |
// check if the file was correctly copied | |
const [exists] = await destination.exists(); | |
if (exists) { | |
fileExists = true; | |
} else { | |
fileExists = false; | |
} | |
} | |
} | |
if (fileExists) { | |
// delete the document from the shareToAnotherGraph collection | |
await db.collection("shareToAnotherGraph").doc(snap.id).delete(); | |
} | |
return; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment