How to check if file exists in Firebase web storage?

1

I'm doing a web application with firebase, in this application I have an image register (optional), similar to the idea of a profile (the user may or may not have a registered image if a default image is not loaded), I would like how do I check if a file exists for that user?

Note: To "relate" the database (realtime database) to the storage (storage) I register the images using the user's key as a name, for example user registration with an xyz key, if it registers an image , it will be in firebase.storage().ref('xyz')

If the way I use it is wrong tell me the right way to do it.

I thought first of creating a chave: valor with the path to the image in the database, but I found occupying unneeded space, in addition it would have to somehow check the file name not to repeat

To show the data I call the function mostrar() within the reading forEach :

function ler() {
database.ref(referencia_database).once('value').then(function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            var chave = childSnapshot.key
            var obj = childSnapshot.val()
            var urlImagem;

            storage.ref(chave).getDownloadURL().then(function(url) {
                // A imagem existe
                urlImagem = url;
                console.log(urlImagem);
            }).catch(function(error) {
                switch (error.code) {
                    case 'storage/object_not_found':
                        // A imagem não existe
                        urlImagem = './image/default.png';
                        console.log(urlImagem);
                        break;

                    default:
                        // Ocorreu um erro desconhecido
                        break;
                }
            });

            mostrar(chave, obj.nome, urlImagem)
        })
    })
}

function mostrar(chave, nome, url) {
    div.innerHTML +=    '<a href="#container">' + 
                                '<div class="col s12 m4 l3" onclick="selecionar(\'' + chave + '\')">' +
                                    '<div class="card">' + 
                                        '<div class="card-image">' +
                                            '<img src="' + url + '">' +
                                            '<span class="card-title">' + nome + '</span>' +
                                        '</div>' +
                                    '</div>' +
                                '</div>' + 
                            '</a>'
}
    
asked by anonymous 17.02.2018 / 04:13

1 answer

1

You can try to get the download URL for the image. If error object_not_found occurs because it does not exist.

var ref = firebase.storage().ref('xyz');
var urlImagem;
ref.getDownloadURL().then(function(url) {
  // A imagem existe
  urlImagem = url;
  console.log(urlImagem);
  mostrar(chave, obj.nome, urlImagem)
}).catch(function(error) {
  switch (error.code) {
    case 'storage/object_not_found':
      // A imagem não existe
      urlImagem = './image/default.png';
      console.log(urlImagem);
      mostrar(chave, obj.nome, urlImagem)
      break;

    default:
      // Ocorreu um erro desconhecido
      break;
  }
});
    
17.02.2018 / 23:06