所以我需要将文档保存到我的 Firestore,每个文档都有一个文件链接(下载链接)。
为了实现这一目标,我:
将文件上传到 Firebase 存储
上传成功后获取文件的下载链接。
上传带有链接到存储文件的 download_url 字段的文档。
您可以在代码中看到这一点。
这有以下问题:
我的 javascript 都是客户端(我使用 Github Pages),所以有人可以简单地更改下载链接并将其放入文档上传中。
我可以添加 firestore 规则来检查下载链接是否以https://firebasestorage.googleapis.com/v0/b/my_application.appspot.com/o/info开头,但这真的是最好的方法吗?
如果上传的文件合法,但文件不合法,文件将保存到我的存储中,但文件将被拒绝。没有参考该文件,除非我手动将其删除,否则它将始终保留在我的存储中。
function uploadFile(my_file) {
// Create a root reference
var storageRef = firebase.storage().ref();
var uploadTask = storageRef.child('info/' + my_file.name).put(my_file);
uploadTask.on('state_changed', function (snapshot) {
}, function (error) {
console.error("Error uploading file: ", error);
}, function () {
// Handle successful uploads on complete
uploadTask.snapshot.ref.getDownloadURL().then(function (downloadURL) {
uploadDocument(downloadURL);
});
});
}
function uploadDocument(downloadUrl) {
var name = document.forms["infoForm"]["name"].value;
var author = document.forms["infoForm"]["author"].value;
var documentObject = {
name: name,
author: author,
download_url: downloadUrl
};
db.collection("info").add(documentObject)
.then(function (docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function (error) {
console.error("Error adding document: ", error);
});
}
我不知道处理这个问题的最佳方法是什么。任何帮助将不胜感激:)
慕尼黑8549860
相关分类