main.js 5.27 KB
const { app, BrowserWindow, ipcMain, dialog  } = require('electron')
const path = require('path')
const url = require('url')
const vision = require('@google-cloud/vision');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

function createWindow(){
  //브라우저 창 생성
  win = new BrowserWindow({
    width: 1080,
    minWidth: 680,
    height: 840,
    webPreferences :{
      nodeIntegration : true
    }
  })

  win.loadURL(url.format({
      pathname: path.join(__dirname, 'src/index.html'),
      protocol: 'file:',
      slashes : true
    })
  )

  //win.webContents.openDevTools()

  win.on('closed', ()=>{
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null;
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', ()=>{
  if(process.platform !== 'darwin'){
    app.quit();
  }
});

app.on('activate', ()=>{
    if(win == null){
        createWindow();
    }

})

ipcMain.on("device-name", (e, arg) => {
    win.webContents.send("device", arg);
  });

//photos taken in selected dates
  ipcMain.on("photo", (e, arg) => {
    win.webContents.send("select_result", arg);
  });
  
  //open-file-dialog
  ipcMain.on('open-file-dialog', async (event) => {
  
    const result = await dialog.showOpenDialog({
      properties: ['openFile', 'openDirectory']
    })
      win.webContents.send('selected-directory',result);
  })
 
  //console 출력
  ipcMain.on('print', (e, arg)=>{
    console.log(arg);
  })
  
  //internet connect 확인
  /**
   * const check = require('check-internet-connected');
  
    const config = {
      timeout : 5000,
      retries : 3,
      domain : 'apple.com'
    }
    
    check(config)
    .then(()=>{
      console.log("Connection available");
    }).catch((err)=>{
      console.log("No connection", err);
    });
   */
  //이미지 라벨 
   var label_result = new Array();
  ipcMain.on('image_labels', async (event, list) =>{
    label_result = [];
    await Promise.all(list.map(async (element)=>{
      var path = new String();
      path = element.path;
      var num = path.indexOf("DCIM");
      var folder_path = new String();
      folder_path = "./photos/" + path.substring(num);
      await quickstart(folder_path, element.display_name);
    }))
    //라벨 검출 결과 전송
    win.webContents.send('labels_result',label_result);
    console.log(label_result.length);
  })
  
  async function quickstart(path, name) {
    
      const client = new vision.ImageAnnotatorClient();
    
      // Performs label detection on the image file
      const [result] = await client.labelDetection(path);
      const labels = result.labelAnnotations;
  
      path = '../.'+path;
      var content = {path : path, name : name, labels : labels}
      label_result.push(content);
    }
  

  //detext unsafe image
  let detect_result = new Array();
  
    ipcMain.on("detect", async (e, result)=>{
      detect_result = [];
      await Promise.all(result.map(async (element)=>{
        var path = new String();
        path = element.path;
        var num = path.indexOf("DCIM");
        var folder_path = new String();
        folder_path = "./photos/" + path.substring(num);
        await detect(folder_path, element.display_name);
      }))
      win.webContents.send('detect_result', detect_result);
      console.log("detect_success");
    });
  
    async function detect(path, name){
      //const vision = require('@google-cloud/vision');
      // Creates a client
      const client = new vision.ImageAnnotatorClient();
      // Performs safe search detection on the local file
      const [result] = await client.safeSearchDetection(path);
      const detections = result.safeSearchAnnotation;
  
      var adult_sum = 0;
      var racy_sum = 0;
  
      if(detections.adult == "VERY_LIKELY"){
        adult_sum = 100;
      }
      else if(detections.adult == "LIKELY"){
        adult_sum = 80;
      }
      else if(detections.adult == "POSSIBLE"){
        adult_sum = 60;
      }
      else if(detections.adult == "UNLIKELY"){
        adult_sum = 40;
      }
      else if(detections.adult == "VERY_UNLIKELY"){
        adult_sum = 20;
      }
      else{
        adult_sum = 0;
      }
  
      if(detections.racy == "VERY_LIKELY"){
        racy_sum = 100;
      }
      else if(detections.racy == "LIKELY"){
        racy_sum = 80;
      }
      else if(detections.racy == "POSSIBLE"){
        racy_sum = 60;
      }
      else if(detections.racy == "UNLIKELY"){
        racy_sum = 40;
      }
      else if(detections.racy == "VERY_UNLIKELY"){
        racy_sum = 20;
      }
      else{
        racy_sum = 0;
      }
  
      var score = (adult_sum + racy_sum) / 2;
  
      path = '../.'+path;
  
      if(score>=60){
        var content = {path : path, name : name, score : score};
        detect_result.push(content);
      }
  
    }
    ipcMain.on('open-error-dialog', (event) => {
      dialog.showErrorBox('detected unsafe contents', '당신의 휴대폰에서 유해한 콘텐츠를 발견했습니다!')
    })
//이전에 사용한 photos 폴더 삭제
var rimraf = require("rimraf");
rimraf("./photos", function(){console.log("")});