File size: 1,847 Bytes
eaada9a
 
 
 
 
 
 
 
 
9ecf8b5
eaada9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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}`));