MCPcopy Create free account
hub / github.com/bailicangdu/node-elm / getPath

Method getPath

prototype/baseComponent.js:98–148  ·  view source on GitHub ↗
(req, res)

Source from the content-addressed store, hash-verified

96 }
97
98 async getPath(req, res){
99 return new Promise((resolve, reject) => {
100 const form = formidable.IncomingForm();
101 form.uploadDir = './public/img';
102 form.parse(req, async (err, fields, files) => {
103 let img_id;
104 try{
105 img_id = await this.getId('img_id');
106 }catch(err){
107 console.log('获取图片id失败');
108 fs.unlinkSync(files.file.path);
109 reject('获取图片id失败');
110 }
111 const hashName = (new Date().getTime() + Math.ceil(Math.random()*10000)).toString(16) + img_id;
112 const extname = path.extname(files.file.name);
113 if (!['.jpg', '.jpeg', '.png'].includes(extname)) {
114 fs.unlinkSync(files.file.path);
115 res.send({
116 status: 0,
117 type: 'ERROR_EXTNAME',
118 message: '文件格式错误'
119 })
120 reject('上传失败');
121 return
122 }
123 const fullName = hashName + extname;
124 const repath = './public/img/' + fullName;
125 try{
126 fs.renameSync(files.file.path, repath);
127 gm(repath)
128 .resize(200, 200, "!")
129 .write(repath, async (err) => {
130 // if(err){
131 // console.log('裁切图片失败');
132 // reject('裁切图片失败');
133 // return
134 // }
135 resolve(fullName)
136 })
137 }catch(err){
138 console.log('保存图片失败', err);
139 if (fs.existsSync(repath)) {
140 fs.unlinkSync(repath);
141 } else {
142 fs.unlinkSync(files.file.path);
143 }
144 reject('保存图片失败')
145 }
146 });
147 })
148 }
149
150 async qiniu(req, type = 'default'){
151 return new Promise((resolve, reject) => {

Callers 3

uploadImgMethod · 0.95
updateAvatarMethod · 0.80
updateAvatarMethod · 0.80

Calls 1

getIdMethod · 0.95

Tested by

no test coverage detected