I am using the code as given in the firebase storage documentation to upload files to my project.
Here is the code:
var metadata = {
contentType: 'image/jpeg'
};
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED:
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING:
console.log('Upload is running');
break;
}
},
(error) => {
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
// ...
case 'storage/unknown':
// Unknown error occurred, inspect error.serverResponse
break;
}
},
() => {
uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log('File available at', downloadURL);
});
}
);
I want to add some functionalities. The first functionality is that I could determine whether the file being uploaded is a document or a photo. The second functionality is that I will only allow pdf and jpeg format files to be uploaded.
In order to achieve these functionalities does firebase storage provides any function or do I need to manually determine the file type from the file name by using the split function?