Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
9.2 kB
2
Indexable
Never
const express = require('express');
const bodyParser = require('body-parser');
const pdfP = require('pdf2json');
const app = express();
const crypto = require('crypto');
const multer = require('multer');
const { GridFsStorage } = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const methodOverride = require('method-override');
const mongoose = require('mongoose')
const fs = require('fs')
const getStream = require('get-stream')
const mysql = require('mysql2');
app.use(bodyParser.json());
app.use(bodyParser.raw())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
const path = require('path');
//const db= require("./db");
//const fs= require('fs');
const pdfParse = require("pdf-parse");
const user = require("./models/user");
const uri = 'mongodb+srv://user:dbUser@cluster0.um1b0.mongodb.net/lib?retryWrites=true&w=majority'
const pdfjs = require("pdfjs-dist/legacy/build/pdf")

async function getContent(src, pg) {
  const doc = await pdfjs.getDocument(src).promise
  const page = await doc.getPage(pg)
  return await page.getTextContent()
}
async function getItems(src) {
  const content1 = await getContent(src, 1)
  const items1 = content1.items
  const content2 = await getContent(src, 2)
  const items2 = content2.items
  const content3 = await getContent(src, 3)
  const items3 = content3.items
  const content4 = await getContent(src, 4)
  const items4 = content4.items
  const content5 = await getContent(src, 5)
  const items5 = content5.items
  const content6 = await getContent(src, 6)
  const items6 = content6.items
  const content7 = await getContent(src, 7)
  const items7 = content7.items
  const content8 = await getContent(src, 8)
  const items8 = content8.items
  const content9 = await getContent(src, 9)
  const items9 = content9.items
  var page1 = []
  var page2 = []
  var page3 = []
  var page4 = []
  var page5 = []
  var page6 = []
  var page7 = []
  var page8 = []
  var page9 = []

  for (let i = 0; i < items1.length; i++) {
    if (isThereAnyText(items1[i])) {
      page1.push(items1[i].str)
    }
  }
  for (let i = 0; i < items2.length; i++) {
    if (isThereAnyText(items2[i])) {
      page2.push(items2[i].str)
    }
  }
  for (let i = 0; i < items3.length; i++) {
    if (isThereAnyText(items3[i])) {
      page3.push(items3[i].str)
    }
  }
  for (let i = 0; i < items4.length; i++) {
    if (isThereAnyText(items4[i])) {
      page4.push(items4[i].str)
    }
  }
  for (let i = 0; i < items5.length; i++) {
    if (isThereAnyText(items5[i])) {
      page5.push(items5[i].str)
    }
  }
  for (let i = 0; i < items6.length; i++) {
    if (isThereAnyText(items6[i])) {
      page6.push(items6[i].str)
    }
  }
  for (let i = 0; i < items7.length; i++) {
    if (isThereAnyText(items7[i])) {
      page7.push(items7[i].str)
    }
  }
  for (let i = 0; i < items8.length; i++) {
    if (isThereAnyText(items8[i])) {
      page8.push(items8[i].str)
    }
  }
  for (let i = 0; i < items9.length; i++) {
    if (isThereAnyText(items9[i])) {
      page9.push(items9[i].str)
    }
  }
  let pages = []
  pages.push(page1)
  pages.push(page2)
  pages.push(page3)
  pages.push(page4)
  pages.push(page5)
  pages.push(page6)
  pages.push(page7)
  pages.push(page8)
  pages.push(page9)
  return pages
}
let selam = 'aaa1'
let regex1 = new RegExp(/\d/)
console.log(regex1.test(selam)); // true
console.log("afasfsda")
function isThereAnyText(item) {
  let regex = new RegExp(/[a-z]/i)
  let regex1 = new RegExp(/\d/)
  if (regex.test(item.str) || regex1.test(item.str)) {
    return true
  }
  return false
}
getItems('Ornektez1.pdf').then(pages => getAllInfo(pages))
function getAllInfo(pages) {
  getClassName(pages[0])
  getTerm(pages[1])
  getAuthorInfo(pages[2])
  getProjectSummary(pages[8])
  getProjectTitle(pages[0])
  getConsultantInfo(pages[1])
  getJuryInfo(pages[1])
  getKeywords(pages[8])
}
function getClassName(page1) {
  console.log(page1[3])
  return page1[3]
}
async function getTerm(page2) {
  let x = page2[page2.length - 1]
  let fallOrSpring = ''
  if (x.substring(3, 5) == '01') {
    fallOrSpring = 'Güz'
  }
  else
    fallOrSpring = 'Bahar'
  let year1 = 0
  year1 = parseInt(x.substring(6, 10)) - 1
  let year2 = String(year1)
  console.log(fallOrSpring + " " + year2 + "-" + x.substring(6, 10))
  let term = fallOrSpring + " " + year2 + "-" + x.substring(6, 10)
  return term
}
async function getAuthorInfo(page3) {
  console.log(page3[page3.length - 3])
  console.log(page3[page3.length - 2].substring(13, page3[page3.length - 2].length))
  console.log(page3[page3.length - 3].substring(5, 6))
  let educationType = ''
  if (page3[page3.length - 3].substring(5, 6) == '1') {
    educationType = 'Birinci Öğretim'
  }
  else
    educationType = 'İkinci Öğretim'
  let authorInfo = page3[page3.length - 2].substring(13, page3[page3.length - 2].length) + ' ' + page3[page3.length - 3] + ' ' + educationType
  console.log(authorInfo)
  return authorInfo
}
async function getProjectSummary(page9) {
  console.log(page9)
  let i = 0
  while (page9[i] != 'ÖZET') {
    i++
  }
  console.log(i)
  let summary = ''
  let summary1 = []
  i = i + 1
  while (page9[i] != 'Anahtar ') {
    summary1.push(page9[i])
    i++
  }
  summary = summary1.join(' ')
  console.log(summary)
  return summary
}
async function getProjectTitle(page1) {
  console.log(page1[4])
  return page1[4]
}

async function getConsultantInfo(page2) {
  console.log(page2[6])
}
async function getJuryInfo(page2) {
  console.log(page2[10])
  console.log(page2[15])
}
async function getKeywords(page9) {
  let i = 0
  while (page9[i] != 'Kelimeler: ') {
    i++
  }
  i++
  let keywords = ''
  for (i; i < page9.length; i++) {
    keywords += page9[i]
  }
  console.log(keywords)
  let keywords1 = []
  keywords1 = keywords.split(', ')
  keywords1[keywords1.length - 1] = keywords1[keywords1.length - 1].slice(0, -1)
  console.log(keywords1[keywords1.length - 1])
  console.log(keywords1)
  return keywords1
}
mongoose.connect(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,



}).then(() => console.log('db is connected')).catch((err) => console.error('Error'));
const a = mongoose.connection;


/*   var connection = mysql.createPool({
    connectionLimit:10,
    host: 'localhost',
    user: 'root',
    password: '3005',
    database: 'emlak_takip'
  });
  connection.getConnection((err,connection)=>{
    if(err) throw err;
    console.log("connection established")
  }); */

const upload = multer({ dest: "models/files" });





app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'));
});


app.get('/register', (req, res) => {
  res.sendFile(path.join(__dirname, 'pass.html'));
});
app.get('/user', (req, res) => {
  res.sendFile(path.join(__dirname, 'user.html'));
});
app.get('/admin', (req, res) => {
  res.sendFile(path.join(__dirname, 'admin.html'));
});

app.post("/user", upload.single('file'), (req, res, next) => {

  pdfParse(req.file.path).then(function (data) {
    console.log(data.numpages)

  })
    .catch(function (error) {
      console.log(err);
    })
  getItems(req.file.path)
});



app.post("/submit", (req, res, next) => {

  if (req.body.password == " " && req.body.name == " ") {
    return res.status(500).json({
      message: "Make sure there isn't any empty field"
    });
  }
  else if (req.body.password == "" || req.body.name == "") {
    return res.status(500).json({
      message: "Make sure there isn't any empty field"
    });
  }
  else {
    user.find({ type: req.body.type, name: req.body.name, pass: req.body.password }).exec()
      .then(user => {
        if (user.length > 0 && req.body.type == 'admin') {
          res.redirect('/admin')

        }
        else if (user.length > 0 && req.body.type == 'user') {

          res.redirect('/user')

        }
        else {

          res.sendFile(path.join(__dirname, 'msg.html'))
        }
      })
      .catch(err => {
        console.log(err);
        res.status(500).json({
          error: err
        });
      });
  }
});

app.post("/changepassword", (req, res, next) => {

  if (req.body.oldP == "" || req.body.username == "" || req.body.email == "" || req.body.newP == "") {
    return res.status(500).json({
      message: "Make sure there isn't any empty field"
    });
  }
  else {
    user.findOneAndUpdate({ type: req.body.type, email: req.body.email, name: req.body.username, pass: req.body.oldP }, { pass: req.body.newP }).exec()
      .then(doc => {
        if (!doc) {
          return res.status(500).json({
            message: "The information entered is not correct!",
          });
        }
        return res.status(500).json({
          message: "Password changed successfully!",
        });
      }).catch(err => {
        console.log(err);
        res.status(500).json({
          error: err
        });
      });
  }
});

app.listen(3000)