This question is a follow up to this previous question I asked earlier today Unique File Upload with Hashing on Firebase
I am trying to generate hash keys for the images I wish to upload to firebase storage.
Before upload, If the files haven't been uploaded before, I store the generated hash keys and image file name in firestore for future comparison and referencing with storage bucket.
if the file has been uploaded before, I intend to not proceed with the upload.
These hash keys will let me know if the file has been previously uploaded because I check the server to see if their names(associated with the corresponding hash value) exist.
The code used to generate hash key for the image:
// Initializing the file reader
const fr = new FileReader();
let hashKey = '';
// Listening to when the file has been read.
fr.onload = async () => {
// Get Hash Key
hashKey = await sha512(fr.result);
}
// Reading the image file.
fr.readAsText(image);
The code below is used to check if the hash key generated locally matches the one stored in the server. If it matches, don't proceed with file upload. If it doesn't exist, proceed with the upload
const existingImageQuery = query(existingImageRef, where('hashKey', '==', hashKey));
const existingImageSnap = await getDoc(existingImageQuery);
if (existingImageSnap.exists()) {
console.log("Document data:", existingImageSnap.data());
// Don't upload photo
} else {
// doc.data() will be undefined in this case
// Add image name and hash key to "images" collection
await addDoc(collection(db, "images"), {
name: image.name,
hashKey: hashKey,
});
// Upload Photo to Image Bucket
const imgRef = ref(
storage,
`images/${image.name}`
);
const snap = await uploadBytes(imgRef, image);
const downloadUrl = await getDownloadURL(ref(storage, snap.ref.fullPath));
url = downloadUrl;
}
THe issue now is that the codes above doesn't work. I have tried a lot of methods to make it work but I can't seem to find a better way to check if the locally generated hash key matches the hash key stored on firestore.
If it matches, I intend to get the file name that corresponds with the hash key stored on firestore to get the specific image on firebase storage. I also do not know how to that.
How do I go about it all?
Thank you in advance.
CodePudding user response:
Based on your code above, you're using the getDoc()
method instead of the getDocs()
method. The query you executed may contain one or more documents which will break your code. See sample code below for your reference:
import { getFirestore, query, getDocs, collection, where, addDoc } from "firebase/firestore";
import { getStorage, ref, getDownloadURL, uploadBytes } from "firebase/storage";
// Collection Reference
const existingImageRef = collection(db, "images");
const existingImageQuery = query(existingImageRef, where('hashKey', '==', hashKey));
// `existingImageQuery` can consist one or more documents making it impossible to use `getDoc`
// Use `getDocs()` instead when using simple or compound queries.
const existingImageSnap = await getDocs(existingImageQuery);
// Check if the snapshot/result is empty.
if (existingImageSnap.empty) {
// Execute needed functions.
await addDoc(existingImageRef, {
name: image.name,
hashKey: hashKey,
});
// Upload Photo to Image Bucket
const imgRef = ref(
storage,
`images/${image.name}`
);
const snap = await uploadBytes(imgRef, image);
const downloadUrl = await getDownloadURL(ref(storage, snap.ref.fullPath));
const url = downloadUrl;
console.log(url);
}
For more information, you may check how to properly Execute a query.