Skip to content

Commit

Permalink
nulink upload
Browse files Browse the repository at this point in the history
  • Loading branch information
bailicangdu committed Jul 21, 2018
1 parent f482689 commit dafba45
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions prototype/baseComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,19 @@ export default class BaseComponent {
img_id = await this.getId('img_id');
}catch(err){
console.log('获取图片id失败');
fs.unlink(files.file.path);
reject('获取图片id失败')
fs.unlinkSync(files.file.path);
reject('获取图片id失败');
}
const hashName = (new Date().getTime() + Math.ceil(Math.random()*10000)).toString(16) + img_id;
const extname = path.extname(files.file.name);
if (!['.jpg', '.jpeg', '.png'].includes(extname)) {
fs.unlink(files.file.path);
fs.unlinkSync(files.file.path);
res.send({
status: 0,
type: 'ERROR_EXTNAME',
message: '文件格式错误'
})
reject('上传失败');
return
}
const fullName = hashName + extname;
Expand All @@ -136,9 +137,9 @@ export default class BaseComponent {
}catch(err){
console.log('保存图片失败', err);
if (fs.existsSync(repath)) {
fs.unlink(repath);
fs.unlinkSync(repath);
} else {
fs.unlink(files.file.path);
fs.unlinkSync(files.file.path);
}
reject('保存图片失败')
}
Expand All @@ -156,7 +157,7 @@ export default class BaseComponent {
img_id = await this.getId('img_id');
}catch(err){
console.log('获取图片id失败');
fs.unlink(files.file.path);
fs.unlinkSync(files.file.path);
reject('获取图片id失败')
}
const hashName = (new Date().getTime() + Math.ceil(Math.random()*10000)).toString(16) + img_id;
Expand All @@ -167,11 +168,11 @@ export default class BaseComponent {
await fs.rename(files.file.path, repath);
const token = this.uptoken('node-elm', key);
const qiniuImg = await this.uploadFile(token.toString(), key, repath);
fs.unlink(repath);
fs.unlinkSync(repath);
resolve(qiniuImg)
}catch(err){
console.log('保存至七牛失败', err);
fs.unlink(files.file.path)
fs.unlinkSync(files.file.path)
reject('保存至七牛失败')
}
});
Expand Down

0 comments on commit dafba45

Please sign in to comment.