我正在建立一个网站,用户可以上传关于阅读小说的见解和评论, 并且用户可以自由获取小说的图像。
如果选择了图像,则发布架构具有image
和imageId
属性,用于Cloudinary上传以及将来的操作,例如从Cloudinary库中更改(更新路由)或删除(销毁路由)。
如果未选择任何图像,则会发生默认图像。
问题是,我设法使default image
函数起作用,但是我不希望将多个相同的默认图像上载到Cloudinary库,因此我将default_image.jpg
放置在本地服务器文件夹(/public/images)
中确切地说),并且该default_image.jpg不应放在Cloudinary库中,这样可以节省很多容量。
但是,对于这些没有选定图片的帖子,我不知道应该为其imageId
属性分配什么。
我尝试了undefined
和null
,当然它们没有用,因为这样,如果它们全部都找不到某些novel.imageId
未定义或为空。
// Schema for data
var fictionSchema = new mongoose.Schema({
...
...
image: String,
// for cloudinary API to track which image to delete or update
imageId: String,
...
...
});
// CREATE route
router.post("/", middleware.isLoggedIn, upload.single('image'), (req, res) => {
if (req.file){
// req.file.path comes from multer npm
cloudinary.v2.uploader.upload(req.file.path, function (err, result) {
if (err) {
...
}
// add cloudinary url for the image to the novel object under image property
req.body.novel.image = result.secure_url;
req.body.novel.imageId = result.public_id;
});
} else {
// setup default image for new novels
req.body.novel.image = "/images/noimage.jpg";
// imageId here should be ... empty? Undefined? Null? A fixed ID but may be delete when accessing Destroy route?
req.body.novel.imageId = undefined;
}
...
...
Novel.create(req.body.novel, function (err, newlyAddedNovel) {
if (err) {
req.flash('error', err.message);
...
} else {
req.flash("success", "Novel added successfully.");
...
}
});
});
// DESTROY route
router.delete("/:id", middleware.checkNovelOwnership, (req, res) => {
// find and delete the correct novel along with the image on cloudinary
Novel.findById(req.params.id, async (err, novel) => {
if (err) {
req.flash("error", err.message);
return res.redirect("back");
}
try {
await cloudinary.v2.uploader.destroy(novel.imageId);
// delete the novel found
novel.remove();
// delete the image from cloudinary
req.flash("success", "Novel deleted successfully.");
...
...
} catch (err) {
..
}
});
});
答案 0 :(得分:0)
好消息,我解决了这个问题!她的光芒!虽然花了我大约2个小时...
所以,最终一切都按我的意愿进行了,如此精致。
代码如下:
•创建路线
对于新帖子,我首先用默认图像设置每本新小说,然后如果提供了图像(要求文件),请对其进行更改并上传。
之后,请记住在mongo数据库中创建该新颖数据。 (在我的情况下为Novel.create()。)
router.post("/", middleware.isLoggedIn, upload.single('image'), async function (req, res) {
// set every new novel with default image first
req.body.novel.image = "https://res.cloudinary.com/dvfkbz6la/image/upload/v1565434656/noimage_ew1uri.jpg";
req.body.novel.imageId = "noimage_ew1uri";
req.body.novel.user = {
id: req.user._id,
username: req.user.username
};
if (req.file) {
// req.file.path comes from multer npm
await cloudinary.v2.uploader.upload(req.file.path, function (err, result) {
if (err) {
req.flash('error', err.message);
return res.redirect('back');
}
// add cloudinary url for the image to the novel object under image property
// add image's public_id (imageId in novel model) to novel object
req.body.novel.image = result.secure_url;
req.body.novel.imageId = result.public_id;
});
}
Novel.create(req.body.novel, function (err, newlyAddedNovel) {
if (err) {
...
} else {
...
}
});
});
•更新路线
在try块中,如果添加了(novel.imageId =“ DEFAULT_IMAGEID”){}否则{}。
// UPDATE route
router.put("/:id", middleware.checkNovelOwnership, upload.single('image'), (req, res) => {
// find the correct novel
Novel.findById(req.params.id, async (err, novel) => {
if (err) {
...
} else {
// if there's a req.file, we know user is trying to upload a new image
if (req.file) {
try {
// if imageId is default, await the result to be uploaded
if (novel.imageId = "noimage_ew1uri") {
var result = await cloudinary.v2.uploader.upload(req.file.path);
} else {
// if not default, find the old image using imageId and delete
await cloudinary.v2.uploader.destroy(novel.imageId);
var result = await cloudinary.v2.uploader.upload(req.file.path);
}
novel.imageId = result.public_id;
novel.image = result.secure_url;
} catch (err) {
...
}
}
novel.title = req.body.title;
novel.author = req.body.author;
novel.price = req.body.price;
novel.description = req.body.description;
// remember to save the changed novel
novel.save();
req.flash("success", "Successfully Updated!");
res.redirect("/novels/" + novel._id);
}
});
});
•毁灭路线
在try块中,如果添加了(novel.imageId!=“ DEFAULT_IMAGEID”){}否则{}。
// DESTROY route
router.delete("/:id", middleware.checkNovelOwnership, (req, res) => {
// find and delete the correct novel along with the image on cloudinary
Novel.findById(req.params.id, async (err, novel) => {
if (err) {
req.flash("error", err.message);
return res.redirect("back");
}
try {
// if novel.imageId isn't default, destroy it from Cloudinary
if (novel.imageId != "noimage_ew1uri") {
await cloudinary.v2.uploader.destroy(novel.imageId);
}
// delete the novel found
novel.remove();
...
} catch (err) {
...
}
});
});
剩下的唯一问题是,我不知道为什么,但是当点击UPDATE路由时, 并且当imageId不是默认值并且用户更改了图像时,
旧映像不会被破坏并保留在Cloudinary库中。真奇怪。
我确定已设置此代码
Novel.findById(req.params.id, async (err, novel) => {
if (err) {
...
} else {
if (req.file) {
try {
// if imageId is default, await the result to be uploaded
if (novel.imageId = "noimage_ew1uri") {
...
} else {
// if not default, find the old image using imageId and delete
await cloudinary.v2.uploader.destroy(novel.imageId);
var result = await cloudinary.v2.uploader.upload(req.file.path);
}
...
...
Dunno为什么要等待cloudinary.v2.uploader.destroy(novel.imageId);不能按预期工作。嗯...
答案 1 :(得分:0)
您网站中已经可用的图像被缓存在一个或多个CDN边缘中。因此,即使您从Cloudinary帐户中删除了映像,缓存的副本仍可能可用。默认情况下,所有交付的图像都会缓存30天。
在将invalidate
参数设置为true
的销毁API时,该图像将被删除并从CDN中清除,从而允许显示新图像。
或者,在更新时,通过在上载新文件时将overwrite
和invalidate
参数设置为true
,可以实现相同的结果(无需调用destroy方法)