Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.use(koaRoute.post('/:repository/:pullRequestNumber', function* loadPullRequest(repository, pullRequestNumber) {
const parts = busboyParse(this, {
checkField: (name, value) => {
if (name === 'githubAccessToken' && !value) {
return new Error(401);
}
},
checkFile: (fieldname, file, filename) => {
if (path.extname(filename) !== '.jpg') {
return new Error(400, 'Invalid jpg image');
}
},
});
let imageAsBuffer;
let pullrequest;
let part;
while ((part = yield parts)) { // eslint-disable-line no-cond-assign
app.context.multipart = async function(opts?: MulOpts) {
// multipart/form-data
if (!this.is('multipart')) {
throw new Error('Content-Type must be multipart/*');
}
// field指表单中的非文件
const parts = parse(this, { autoFields: opts && opts.autoFields });
let part;
let totalSize = 0;
const files: any[] = [];
// tslint:disable-next-line:no-conditional-assignment
while ((part = await parts()) != null) {
if (part.length) {
// arrays are busboy fields
} else {
if (!part.filename) {
// user click `upload` before choose a file,
// `part` will be file stream, but `part.filename` is empty
// must handler this, such as log error.
await sendToWormhole(part);
continue;
}
// otherwise, it's a stream
var upload = function * (next) {
var parts = coParse(this)
var part
while (part = yield parts) {
var url = '/upload/' + part.filename
var stream = fs.createWriteStream(path.join(__dirname, '../public', url))
part.pipe(stream)
console.log('uploading %s -> %s', part.filename, stream.path)
}
this.body = {
status: 'success',
url: url
}
}
return new Promise((resolve, reject) =>
{
const file_name = Math.random().toString().slice(2)
fs.existsAsync(path.join(folder, file_name)).then(exists =>
{
resolve(file_name)
},
error =>
{
reject(error)
})
})
}
const files = busboy(this)
const file_names = []
let file
while (file = yield files)
{
if (log)
{
log.debug(`Uploading: ${file.filename}`)
}
if (!multiple_files && file_names.not_empty())
{
throw new Error(`Multiple files are being uploaded to a single file upload endpoint`)
}