Created
May 5, 2016 22:25
-
-
Save HaxThePlanet/a7b9b081df1e1baf976fffcab7e70577 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deleteTempFile(uri) { | |
fs.unlink(uri, function(err) { | |
if (err) return console.log('error deleting file ' + err); | |
console.log('file deleted successfully'); | |
}); | |
} | |
function downloadFile(uri, filename, callback) { | |
request.head(uri, function(err, res, body) { | |
try { | |
var r = request(uri).pipe(fs.createWriteStream(filename)); | |
r.on('close', callback); | |
} catch (e) { | |
} | |
}); | |
}; | |
function checkLocalFile(filepath){ | |
console.log('check filename =' + filepath); | |
let flag = true; | |
try { | |
fs.accessSync(filepath, fs.F_OK); | |
} catch(e) { | |
flag = false; | |
} | |
return flag; | |
} | |
function checkRemoteURL(remotePath, res) { | |
let flag = true; | |
try { | |
//valid url? | |
if (remotePath == '' || !req.query.imageurl || !validUrl.isUri(req.query.imageurl)) { | |
//no | |
res.status(400).send('error, please specify a valid image url'); | |
flag = false; | |
} else { | |
//yes, check ext | |
console.log('File path = ' + path.extname(removeQueryString(remotePath))); | |
//valid file ext? | |
if (path.extname(removeQueryString(remotePath)) != '.jpg' && path.extname(removeQueryString(remotePath)) != '.png') { | |
res.status(400).send('error, invalid file extension, not supported'); | |
flag = false; | |
} else { | |
flag = true; | |
} | |
} | |
} catch (e) { | |
flag = false; | |
} | |
return flag; | |
} | |
function removeQueryString(mUrl) { | |
var obj = url.parse(mUrl); | |
obj.search = obj.query = ""; | |
return url.format(obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment