Download tasks/codeql_javascript_benchmark_top30_v1/morefixes-0e82cb4ad70fc2c896ef/patch.diff from Benchanything/checkerbench: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/datasets/Benchanything/checkerbench/resolve/main/tasks/codeql_javascript_benchmark_top30_v1/morefixes-0e82cb4ad70fc2c896ef/patch.diff
- Command line
-
hf download hf://datasets/Benchanything/checkerbench/tasks/codeql_javascript_benchmark_top30_v1/morefixes-0e82cb4ad70fc2c896ef/patch.diff
-
curl -L -o patch.diff https://huggingface.co/datasets/Benchanything/checkerbench/resolve/main/tasks/codeql_javascript_benchmark_top30_v1/morefixes-0e82cb4ad70fc2c896ef/patch.diff
4.23 kB
| diff --git a/lib/common.js b/lib/common.js | |
| index 7c4841f..13b193f 100644 | |
| --- a/lib/common.js | |
| +++ b/lib/common.js | |
| const restrictedRoutes = [ | |
| {route: '/admin/file/delete', response: 'json'} | |
| ]; | |
| +// Allowed mime types for product images | |
| +exports.allowedMimeType = [ | |
| + 'image/jpeg', | |
| + 'image/png', | |
| + 'image/gif', | |
| + 'image/bmp', | |
| + 'image/webp' | |
| +]; | |
| + | |
| +exports.fileSizeLimit = 10485760; | |
| + | |
| // common functions | |
| exports.restrict = (req, res, next) => { | |
| exports.checkLogin(req, res, next); | |
| diff --git a/routes/admin.js b/routes/admin.js | |
| index 2199d8a..9ef5673 100644 | |
| --- a/routes/admin.js | |
| +++ b/routes/admin.js | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const multer = require('multer'); | |
| const glob = require('glob'); | |
| +const mime = require('mime-type/with-db'); | |
| const router = express.Router(); | |
| // Admin section | |
| router.post('/admin/file/upload', common.restrict, common.checkAccess, upload.si | |
| const db = req.app.db; | |
| if(req.file){ | |
| - // check for upload select | |
| - let uploadDir = path.join('public/uploads', req.body.directory); | |
| - | |
| - // Check directory and create (if needed) | |
| - common.checkDirectorySync(uploadDir); | |
| - | |
| let file = req.file; | |
| - let source = fs.createReadStream(file.path); | |
| - let dest = fs.createWriteStream(path.join(uploadDir, file.originalname.replace(/ /g, '_'))); | |
| - // save the new file | |
| - source.pipe(dest); | |
| - source.on('end', () => { }); | |
| + // Get the mime type of the file | |
| + const mimeType = mime.lookup(file.originalname); | |
| + | |
| + // Check for allowed mime type and file size | |
| + if(!common.allowedMimeType.includes(mimeType) || file.size > common.fileSizeLimit){ | |
| + // Remove temp file | |
| + fs.unlinkSync(file.path); | |
| - // delete the temp file. | |
| - fs.unlink(file.path, (err) => { | |
| - if(err){ | |
| - console.info(err.stack); | |
| - } | |
| - }); | |
| + // Redirect to error | |
| + req.session.message = 'File type not allowed or too large. Please try again.'; | |
| + req.session.messageType = 'danger'; | |
| + res.redirect('/admin/product/edit/' + req.body.productId); | |
| + return; | |
| + } | |
| // get the product form the DB | |
| db.products.findOne({_id: common.getId(req.body.productId)}, (err, product) => { | |
| if(err){ | |
| console.info(err.stack); | |
| + // delete the temp file. | |
| + fs.unlinkSync(file.path); | |
| + | |
| + // Redirect to error | |
| + req.session.message = 'File upload error. Please try again.'; | |
| + req.session.messageType = 'danger'; | |
| + res.redirect('/admin/product/edit/' + req.body.productId); | |
| + return; | |
| } | |
| - let imagePath = path.join('/uploads', req.body.directory, file.originalname.replace(/ /g, '_')); | |
| + | |
| + const productPath = product.productPermalink; | |
| + let uploadDir = path.join('public/uploads', productPath); | |
| + | |
| + // Check directory and create (if needed) | |
| + common.checkDirectorySync(uploadDir); | |
| + | |
| + let source = fs.createReadStream(file.path); | |
| + let dest = fs.createWriteStream(path.join(uploadDir, file.originalname.replace(/ /g, '_'))); | |
| + | |
| + // save the new file | |
| + source.pipe(dest); | |
| + source.on('end', () => { }); | |
| + | |
| + // delete the temp file. | |
| + fs.unlinkSync(file.path); | |
| + | |
| + let imagePath = path.join('/uploads', productPath, file.originalname.replace(/ /g, '_')); | |
| // if there isn't a product featured image, set this one | |
| if(!product.productImage){ | |
| router.post('/admin/file/upload', common.restrict, common.checkAccess, upload.si | |
| } | |
| }); | |
| }else{ | |
| + // delete the temp file. | |
| + fs.unlinkSync(file.path); | |
| + | |
| + // Redirect to error | |
| req.session.message = 'File upload error. Please select a file.'; | |
| req.session.messageType = 'danger'; | |
| res.redirect('/admin/product/edit/' + req.body.productId); | |