Spaces:
Sleeping
Sleeping
const express = require('express'); | |
const app = express(); | |
const bodyParser = require('body-parser'); | |
const path = require('path'); | |
const multer = require('multer'); | |
const crypto = require('crypto'); | |
const fs = require('fs'); | |
const nsfwcheck = require('./lib/isporn'); | |
const PORT = process.env.PORT || 4000; | |
const tempFolderPath = path.join(__dirname, 'temp'); | |
if (!fs.existsSync(tempFolderPath)) { | |
console.log('Creating Temporary Folder...'); | |
fs.mkdirSync(tempFolderPath); | |
} | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
const storage = multer.diskStorage({ | |
destination: tempFolderPath, | |
filename: function (req, file, cb) { | |
cb(null, crypto.randomBytes(16).toString('hex') + path.extname(file.originalname)); | |
} | |
}); | |
// Limit File Size ( 10 MB ) | |
const upload = multer({ | |
storage: storage, | |
limits: { | |
fileSize: 1048576 * 10 | |
} | |
}); | |
// Upload Image | |
app.post("/api/check", upload.single('image'), async (req, res) => { | |
if (req.file) { | |
if (req.file.mimetype != "image/png" && req.file.mimetype != "image/jpeg") { | |
fs.unlinkSync(req.file.path); | |
return res.json({ | |
status: 400, | |
error: "Only PNG and JPEG Image Allowed" | |
}); | |
} | |
try { | |
let check = await nsfwcheck(fs.readFileSync(req.file.path)); | |
res.json({ | |
status: 200, | |
data: check | |
}); | |
} catch (error) { | |
res.json({ | |
status: 500, | |
error: (error.message || "Unknown Error, Please try another photo") | |
}); | |
console.log(error); | |
} | |
fs.unlinkSync(req.file.path); | |
} else { | |
res.status(400).json({ | |
status: 400, | |
error: "Please Upload some Image" | |
}); | |
} | |
}); | |
app.listen(PORT, () => console.log(`App listen on port ${PORT}`)); |