Reputation: 31
I have a requirement where my app will allow users to upload images. Is there a functionality in firebase storage where I can prevent duplicates and use the old image url when the user/some other user is trying to load the same image.
Upvotes: 3
Views: 2207
Reputation: 15963
Yes. Hash the image and store the file using that name. Write a rule that doesn't allow overwriting of a file.
// Upload the file on the client to a file named after a hash of the data
var string = 'Hello, world!'
firebase.storage().ref().child('images/' + hash(string)).putString(string);
var hash = function(string) { /* SHA1, MD5, etc. (note: collisions exist for certain algorithms ) */ }
// Rules
service firebase.storage {
match /b/{bucket}/o {
match /images/{imageHash} {
// only allow create and delete, not overwrite
allow write: if resource == null || request.resource == null
}
}
}
Upvotes: 2