0
点赞
收藏
分享

微信扫一扫

快速搭建图片服务器

洛茄 2022-02-08 阅读 81


快速搭建图片服务器_自定义

​​参看文章​​

​​备份链接​​

代码中有些地方需要改进,比如上传的位置​​/var/nodeimageupload/Resize.js​​,

const sharp = require('sharp');
const uuidv4 = require('uuid/v4');
const path = require('path');

class Resize {
constructor(folder) {
this.folder = folder;
}
async save(buffer) {
const filename = Resize.filename();
const filepath = this.filepath(filename);

await sharp(buffer)
.resize({
fit: sharp.fit.outside,
withoutEnlargement: true
})
.toFile(filepath);

return filename;
}
static filename() {
return `${uuidv4()}.png`;
}
filepath(filename) {
return path.resolve(`/var/www/html/${filename}`)
}
}
module.exports = Resize;

这里我们可以自定义位置

return path.resolve(`/var/www/html/${filename}`)

为了隐藏我们的upload路径,我们可以将默认的upload改成随机字符串,这样可以避免恶意用户使劲上传文件,导致服务器崩溃

把这两个地方的​​uplyaiusfrhgr978oad​​替换成你自己的随机字符串即可

nodeimageupload\server.js (1 hit)
Line 14: app.use('/uplyaiusfrhgr978oad', router);
nodeimageupload\views\index.ejs (1 hit)
Line 9: <form method="post" action="uplyaiusfrhgr978oad/post" enctype="multipart/form-data">

​​完整代码​​

效果如下:



举报

相关推荐

0 条评论