Spaces:
Sleeping
Sleeping
const express = require('express'); | |
const app = express(); | |
const bodyParser = require('body-parser'); | |
const fs = require('fs'); | |
const nsfwcheck = require('./lib/isporn'); | |
const PORT = process.env.PORT || 4000; | |
const fetch = require('node-fetch'); | |
const util = require("util"); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
// Upload Image | |
app.get("/api/check", async (req, res) => { | |
const imageUrl = req.query.url; | |
if (imageUrl) { | |
try { | |
const response = await fetch(imageUrl); | |
const buffer = await response.buffer(); | |
const mimeType = response.headers.get('content-type'); | |
/* if (mimeType != "image/png" && mimeType != "image/jpeg") { | |
return res.json({ | |
status: 400, | |
error: "Only PNG and JPEG Image Allowed" | |
}); | |
} */ | |
let check = await nsfwcheck(buffer); | |
res.json({ | |
status: 200, | |
data: check | |
}); | |
} catch (error) { | |
res.json({ | |
status: 500, | |
error: util.format(error || "Unknown Error, Please try another photo") | |
}); | |
console.log(error); | |
} | |
} else { | |
res.status(400).json({ | |
status: 400, | |
error: "Please provide a valid image URL using the ?url parameter" | |
}); | |
} | |
}); | |
app.listen(PORT, () => console.log(`App listen on port ${PORT}`)); |