Commit 0f518d4c authored by Mihkel Putrinš's avatar Mihkel Putrinš
Browse files

dont transport user-data thru IPC

parent 63ade4dc
var app = require('app')
var op = require('object-path')
var path = require('path')
var fs = require('fs')
var ipc = require('ipc')
BrowserWindow = require('browser-window')
var j = require('electron-jade')({pretty: true}, {})
var BrowserWindow = require('browser-window')
var windows = {}
var user_url = 'https://entu.keeleressursid.ee/api2/user'
var auth_url = user_url + '/auth'
// var authWindow
// var mainWindow
var user_data = {}
var home_path = process.env.HOME ? process.env.HOME : process.env.HOMEPATH
USER_PATH = path.join(home_path, 'user.json')
IS_DEV = process.env.DEV ? true : false
if (IS_DEV) {
var pjson_path = path.join(__dirname, '..', 'package.json')
var pjson = require(pjson_path)
console.log('----==== ' + pjson.name + ' v.' + pjson.version + ' (build ' + (++pjson.build) + ') ====----')
fs.writeFileSync(pjson_path, JSON.stringify(pjson, null, 4))
}
app.on('ready', function() {
authWindow = new BrowserWindow({ width: 300, height: 600, show: true })
authWindow.setTitle('Panustaja - log in')
authWindow.loadUrl(auth_url)
authWindow.webContents.on('did-finish-load', function() {
authWindow.webContents.savePage('./user.json', 'HTMLOnly', function(err) {
windows['authWindow'] = new BrowserWindow({ width: 300, height: 600, show: true })
windows['authWindow'].setTitle('Panustaja - log in')
windows['authWindow'].loadUrl(auth_url)
windows['authWindow'].webContents.on('did-finish-load', function() {
// require('dialog').showMessageBox({type:'info', message:'enne salvestamist: ' + USER_PATH, buttons:['ok']})
windows['authWindow'].webContents.savePage(USER_PATH, 'HTMLOnly', function(err) {
if (err) {
require('dialog').showMessageBox({type:'info', message:'peale salvestamist: katki' + err, buttons:['ok']})
console.log("Error:", err)
process.exit()
} else {
fs.readFile('./user.json', 'utf8', function(err, data) {
data = JSON.parse(data)
if (op.get(data, 'result.user_id', false)) {
user_data['user_id'] = op.get(data, 'result.user_id')
user_data['session_key'] = op.get(data, 'result.session_key')
user_data['name'] = op.get(data, 'result.name')
// console.log(JSON.stringify(data, null, 4))
} else {
console.log('User data incomplete.')
console.log(JSON.stringify(data, null, 4))
}
fs.unlink('./user.json', function (err) {
if (err) {
console.log("Error:", err)
process.exit()
}
})
mainWindow = new BrowserWindow({ width: 900, height: 600, show: true })
mainWindow.setTitle('Panustaja - ' + user_data['name'])
mainWindow.center()
mainWindow.loadUrl('file://' + __dirname + '/views/main.jade')
authWindow.close()
// require('dialog').showMessageBox({type:'info', message:'peale salvestamist: korras', buttons:['ok']})
mainWindow = new BrowserWindow({ width: 900, height: 600, show: true })
mainWindow.setTitle('Panustaja')
mainWindow.center()
mainWindow.loadUrl('file://' + __dirname + '/views/main.jade')
// windows['authWindow'].hide()
if (IS_DEV) {
mainWindow.webContents.openDevTools(true)
// var dialog = require('dialog')
// dialog.showErrorBox('FOO', 'bar, baz')
// console.log(dialog.showOpenDialog(mainWindow, { properties: [ 'openDirectory' ]}))
})
}
// require('dialog').showMessageBox({type:'info', message:'fail suletud: ' + USER_PATH, buttons:['ok']})
}
})
})
......@@ -62,7 +57,17 @@ ipc.on('data', function(event, message) {
console.log('data: ' + JSON.stringify(message, null, 4))
})
ipc.on('userdata-query', function(event) {
console.log('Sent user data', JSON.stringify(user_data, null, 4))
event.sender.send('userdata-reply', user_data)
ipc.on('setTitle', function(event, message) {
mainWindow.setTitle(message)
})
ipc.on('closeAuth', function(event) {
if (windows['authWindow']) {
windows['authWindow'].close()
delete windows['authWindow']
}
})
app.on('window-all-closed', function() {
app.quit()
})
......@@ -11,29 +11,59 @@ var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING)
var remote = require('remote')
var dialog = remote.require('dialog')
// console.log(path.join(__dirname)) ==> panustaja/code/views
var pjson = require(path.join(__dirname, '..', '..', 'package.json'))
var ipc = require('ipc')
ipc.send('userdata-query')
ipc.on('userdata-reply', function(user_data) {
document.getElementById('userName').innerHTML = user_data.name
})
// ipc.send('userdata-query')
// ipc.on('userdata-reply', function(user_data) {
// })
var b2s = require(path.join(__dirname, '..', 'bytesToSize.js'))
var user_data = {}
var resource = {}
var resource_stats = {}
var dom_resource_name = document.getElementById('resourceName')
var dom_resource_name = document.getElementById('resourceNameInput')
var dom_resource_stats = document.getElementById('resourceStats')
dom_resource_name.setAttribute('hidden', '')
var renderer_interval
setTimeout(function () {
// dialog.showMessageBox({type:'info', message:'loeme faili: ' + USER_PATH, buttons:['ok']})
var home_path = process.env.HOME ? process.env.HOME : process.env.HOMEPATH
USER_PATH = path.join(home_path, 'user.json')
fs.readFile(USER_PATH, 'utf8', function(err, data_json) {
// dialog.showMessageBox({type:'info', message:'fail avatud: ' + USER_PATH, buttons:['ok']})
var data = JSON.parse(data_json)
if (op.get(data, 'result.user_id', false)) {
user_data['user_id'] = op.get(data, 'result.user_id')
user_data['session_key'] = op.get(data, 'result.session_key')
user_data['name'] = op.get(data, 'result.name')
document.getElementById('userName').innerHTML = user_data.name
var title = pjson.name + ' v.' + pjson.version + (pjson.version.indexOf('-') > -1 ? pjson.build : '') + ' | ' + user_data['name']
ipc.send('setTitle', title)
} else {
ipc.send('log', 'User data incomplete.')
ipc.send('data', data)
}
ipc.send('closeAuth')
})
}, 100)
function selectLocal () {
resource = {name: 'root'}
resource_stats = {files: {count: 0, size: 0}, directories: {count: 0}, mime:{}}
dialog.showOpenDialog({properties:['openFile', 'openDirectory', 'multiSelections']}, function selectedPath(_paths) {
if (!_paths) {
return
}
renderer_interval = setInterval(function () {
renderResource()
}, 100)
setFormState('loading')
dom_resource_name.value = ''
if (_paths.length === 1) {
var single_file = _paths[0]
op.set(resource, 'name', single_file)
op.set(resource, 'name', path.basename(single_file))
dom_resource_name.value = resource.name
fs.stat(single_file, function(err, stats) {
if (err) {
......@@ -46,7 +76,6 @@ function selectLocal () {
}
_paths = files.map(function(file) {
var fullpath = path.join(single_file, file)
// console.log('foo1', single_file + '+' + file + '=' + fullpath)
return fullpath
})
recurseLocal(resource, _paths, resourceLoaded)
......@@ -62,10 +91,9 @@ function selectLocal () {
}
var resourceLoaded = function resourceLoaded() {
setFormState('select')
console.log(JSON.stringify(b2s(resource_stats.files.size), null, 4))
console.log(resource_stats.files.size)
ipc.send('data', resource_stats)
setFormState('loaded')
clearInterval(renderer_interval)
// ipc.send('data', resource_stats)
renderResource()
}
......@@ -75,12 +103,12 @@ var renderResource = function renderResource() {
document.getElementById('resourceFiles').innerHTML = ''
document.getElementById('mimeStats').innerHTML = ''
document.getElementById('resourceDirectories').appendChild(document.createTextNode('Katalooge: ' + resource_stats.directories.count))
document.getElementById('resourceFiles').appendChild(document.createTextNode('Faile: ' + resource_stats.files.count + ' / ' + b2s(resource_stats.files.size)))
document.getElementById('resourceFiles').appendChild(document.createTextNode('Faile: ' + resource_stats.files.count + ' | ' + b2s(resource_stats.files.size)))
Object.keys(resource_stats.mime).forEach(function (mime_type_name) {
var text_node = document.createTextNode(
mime_type_name
+ ': ' + op.get(resource_stats, ['mime', mime_type_name, 'count'])
+ ' / ' + b2s(op.get(resource_stats, ['mime', mime_type_name, 'size']))
+ ' | ' + b2s(op.get(resource_stats, ['mime', mime_type_name, 'size']))
)
var li_node = document.createElement('LI')
li_node.appendChild(text_node)
......@@ -89,17 +117,14 @@ var renderResource = function renderResource() {
}
var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) {
// console.log(JSON.stringify(paths, null, 4))
async.each(paths, function iterator(_path, callback) {
fs.stat(_path, function(err, stats) {
if (err) {
// console.log(err)
return callback()
}
if (stats.isFile()) {
magic.detectFile(_path, function(err, result) {
if (err) {
// console.log(err)
return callback()
}
resource_stats.files.count++
......@@ -117,14 +142,12 @@ var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) {
resource_stats.directories.count++
var directory = {name: _path}
op.push(parent_resource, 'resources', directory)
// console.log('dir: ' + JSON.stringify(_path, null, 4))
fs.readdir(_path, function(err, files) {
if (err) {
return callback(err)
}
var _paths = files.map(function(file) {
var fullpath = path.join(_path, file)
// console.log(_path + '+' + file + '=' + fullpath)
return fullpath
})
recurseLocal(directory, _paths, callback)
......@@ -140,20 +163,91 @@ var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) {
})
}
var uploadResource = function uploadResource() {
setFormState('uploading')
document.getElementById('uploadTotalResources').innerHTML = (resource_stats.directories.count + 1)
document.getElementById('uploadTotalSize').innerHTML = b2s(resource_stats.files.size)
renderer_interval = setInterval(function () {
renderResource()
}, 100)
setFormState('uploading')
}
var resourceUploaded = function resourceUploaded() {
setFormState('uploaded')
clearInterval(renderer_interval)
// ipc.send('data', resource_stats)
renderProgress()
}
var renderProgress = function renderProgress() {
// dom_resource_stats.removeAttribute('hidden')
document.getElementById('resourceDirectories').innerHTML = ''
document.getElementById('resourceFiles').innerHTML = ''
document.getElementById('mimeStats').innerHTML = ''
document.getElementById('resourceDirectories').appendChild(document.createTextNode('Katalooge: ' + resource_stats.directories.count))
document.getElementById('resourceFiles').appendChild(document.createTextNode('Faile: ' + resource_stats.files.count + ' | ' + b2s(resource_stats.files.size)))
Object.keys(resource_stats.mime).forEach(function (mime_type_name) {
var text_node = document.createTextNode(
mime_type_name
+ ': ' + op.get(resource_stats, ['mime', mime_type_name, 'count'])
+ ' | ' + b2s(op.get(resource_stats, ['mime', mime_type_name, 'size']))
)
var li_node = document.createElement('LI')
li_node.appendChild(text_node)
document.getElementById('mimeStats').appendChild(li_node)
})
}
var setFormState = function setFormState(state) {
switch(state) {
case 'select':
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('resourceStats').removeAttribute('hidden')
document.getElementById('selectLocal').removeAttribute('hidden')
document.getElementById('selectLocalMessage').removeAttribute('hidden')
// ---
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('resourceStats').setAttribute('hidden', '')
document.getElementById('uploading').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('resourceName').setAttribute('hidden', '')
break
case 'loading':
dom_resource_name.removeAttribute('hidden')
document.getElementById('loading').removeAttribute('hidden')
document.getElementById('resourceStats').removeAttribute('hidden')
// -------- //
document.getElementById('uploading').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('resourceName').setAttribute('hidden', '')
break
case 'loaded':
document.getElementById('resourceStats').removeAttribute('hidden')
document.getElementById('uploadResource').removeAttribute('hidden')
document.getElementById('selectLocal').removeAttribute('hidden')
document.getElementById('selectLocalButton').innerHTML = 'Vali uuesti'
document.getElementById('resourceName').removeAttribute('hidden')
// -------- //
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('uploading').setAttribute('hidden', '')
break
case 'uploading':
document.getElementById('uploading').removeAttribute('hidden')
// -------- //
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('resourceStats').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('selectLocalMessage').setAttribute('hidden', '')
document.getElementById('resourceName').setAttribute('hidden', '')
break
case 'uploaded':
// -------- //
document.getElementById('resourceStats').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('selectLocalButton').innerHTML = 'Vali uuesti'
document.getElementById('resourceName').setAttribute('hidden', '')
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('uploading').setAttribute('hidden', '')
break
}
}
setFormState('select')
#resourceProgressbarOuter {
width: 100%;
height: 10px;
background-color: #cdf;
border: 1px solid black;
}
#resourceProgressbarInner {
width: 10%;
height: 100%;
background-color: #79b;
/*border: 1px solid green;*/
}
#fileProgressbarOuter {
width: 100%;
height: 10px;
background-color: #cdf;
border: 1px solid black;
}
#fileProgressbarInner {
width: 27%;
height: 100%;
background-color: #79b;
/*border: 1px solid green;*/
}
......@@ -8,15 +8,35 @@ html
body
h1#userName Panustaja
div#selectLocalMessage Palun vali fail või kataloog, mida soovid üles laadida.
button#selectLocal(onclick='selectLocal()') Vali fail
input#resourceName(type='text' placeholder='Ressursi nimi' style='width:100%;' hidden='')
div#contents
h3#loading(hidden='') Loading...
ul#resourceStats(hidden='')
h1#userName(hidden='') Panustaja
div#selectLocal
p#selectLocalMessage Palun vali fail või kataloog, mida soovid üles laadida.
button#selectLocalButton(onclick='selectLocal()') Vali fail
div#resourceName(hidden='')
p#resourceNameMessage Muuda ressursi nimi sobivaks.
input#resourceNameInput(type='text' placeholder='Ressursi nimi' style='width:100%;')
div#loading(hidden='')
h3 Loen failisüsteemist ...
div#resourceStats(hidden='')
ul#resourceStatsList
li#resourceDirectories
li#resourceFiles
ul#mimeStats
div#uploadResource(hidden='')
p#uploadResourceMessage Ressurss on üleslaadimiseks valmis.
button#uploadResourceButton(onclick='uploadResource()') Panusta!
div#uploading(hidden='')
span= 'Kataloogid: '
span#uploadedResources 0
span /
span#uploadTotalResources 0
div#resourceProgressbarOuter
div#resourceProgressbarInner
span= 'Failid: '
span#uploadedSize 0
span /
span#uploadTotalSize 0
div#fileProgressbarOuter
div#fileProgressbarInner
script(src='../main.js')
{
"name": "Panustaja",
"description": "Ressursikataloogi upitamiseks keeleressurside Entu repositooriumisse.",
"version": "1.0.1",
"main": "./code/app.js",
"license": "CC-SA",
"private": true,
"repository": {
"type": "git",
"url": "git@gitlab.keeleressursid.ee:michelek/panustaja.git"
},
"dependencies": {
"async": "^1.5.0",
"electron-jade": "^1.3.0",
"marked": "^0.3.5",
"mmmagic": "^0.4.1",
"moment": "^2.10.6",
"object-path": "^0.9.2",
"request": "^2.65.0"
}
}
"name": "Panustaja",
"description": "Ressursikataloogi upitamiseks keeleressurside Entu repositooriumisse.",
"version": "1.0.1-d",
"build": 41,
"main": "./code/app.js",
"license": "CC-SA",
"private": true,
"repository": {
"type": "git",
"url": "git@gitlab.keeleressursid.ee:michelek/panustaja.git"
},
"dependencies": {
"async": "^1.5.0",
"electron-jade": "^1.3.0",
"marked": "^0.3.5",
"mmmagic": "^0.4.1",
"moment": "^2.10.6",
"object-path": "^0.9.2",
"request": "^2.65.0"
},
"devDependencies": {
"electron-packager": "^5.1.1"
}
}
\ No newline at end of file
#! /bin/bash
rm -r Panustaja*
electron-packager . Panustaja --platform=all --arch=x64 --version=0.34.1 --ignore Panustaja
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment