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 app = require('app')
var op = require('object-path') var op = require('object-path')
var path = require('path')
var fs = require('fs') var fs = require('fs')
var ipc = require('ipc') var ipc = require('ipc')
BrowserWindow = require('browser-window')
var j = require('electron-jade')({pretty: true}, {}) var j = require('electron-jade')({pretty: true}, {})
var BrowserWindow = require('browser-window')
var windows = {}
var user_url = 'https://entu.keeleressursid.ee/api2/user' var user_url = 'https://entu.keeleressursid.ee/api2/user'
var auth_url = user_url + '/auth' 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() { app.on('ready', function() {
authWindow = new BrowserWindow({ width: 300, height: 600, show: true }) windows['authWindow'] = new BrowserWindow({ width: 300, height: 600, show: true })
authWindow.setTitle('Panustaja - log in') windows['authWindow'].setTitle('Panustaja - log in')
authWindow.loadUrl(auth_url) windows['authWindow'].loadUrl(auth_url)
authWindow.webContents.on('did-finish-load', function() { windows['authWindow'].webContents.on('did-finish-load', function() {
authWindow.webContents.savePage('./user.json', 'HTMLOnly', function(err) { // require('dialog').showMessageBox({type:'info', message:'enne salvestamist: ' + USER_PATH, buttons:['ok']})
windows['authWindow'].webContents.savePage(USER_PATH, 'HTMLOnly', function(err) {
if (err) { if (err) {
require('dialog').showMessageBox({type:'info', message:'peale salvestamist: katki' + err, buttons:['ok']})
console.log("Error:", err) console.log("Error:", err)
process.exit() process.exit()
} else { } else {
fs.readFile('./user.json', 'utf8', function(err, data) { // require('dialog').showMessageBox({type:'info', message:'peale salvestamist: korras', buttons:['ok']})
data = JSON.parse(data) mainWindow = new BrowserWindow({ width: 900, height: 600, show: true })
if (op.get(data, 'result.user_id', false)) { mainWindow.setTitle('Panustaja')
user_data['user_id'] = op.get(data, 'result.user_id') mainWindow.center()
user_data['session_key'] = op.get(data, 'result.session_key') mainWindow.loadUrl('file://' + __dirname + '/views/main.jade')
user_data['name'] = op.get(data, 'result.name') // windows['authWindow'].hide()
// console.log(JSON.stringify(data, null, 4)) if (IS_DEV) {
} 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()
mainWindow.webContents.openDevTools(true) mainWindow.webContents.openDevTools(true)
}
// var dialog = require('dialog') // require('dialog').showMessageBox({type:'info', message:'fail suletud: ' + USER_PATH, buttons:['ok']})
// dialog.showErrorBox('FOO', 'bar, baz')
// console.log(dialog.showOpenDialog(mainWindow, { properties: [ 'openDirectory' ]}))
})
} }
}) })
}) })
...@@ -62,7 +57,17 @@ ipc.on('data', function(event, message) { ...@@ -62,7 +57,17 @@ ipc.on('data', function(event, message) {
console.log('data: ' + JSON.stringify(message, null, 4)) console.log('data: ' + JSON.stringify(message, null, 4))
}) })
ipc.on('userdata-query', function(event) { ipc.on('setTitle', function(event, message) {
console.log('Sent user data', JSON.stringify(user_data, null, 4)) mainWindow.setTitle(message)
event.sender.send('userdata-reply', user_data) })
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) ...@@ -11,29 +11,59 @@ var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING)
var remote = require('remote') var remote = require('remote')
var dialog = remote.require('dialog') 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') var ipc = require('ipc')
ipc.send('userdata-query') // ipc.send('userdata-query')
ipc.on('userdata-reply', function(user_data) { // ipc.on('userdata-reply', function(user_data) {
document.getElementById('userName').innerHTML = user_data.name // })
})
var b2s = require(path.join(__dirname, '..', 'bytesToSize.js')) var b2s = require(path.join(__dirname, '..', 'bytesToSize.js'))
var user_data = {}
var resource = {} var resource = {}
var resource_stats = {} var resource_stats = {}
var dom_resource_name = document.getElementById('resourceName') var dom_resource_name = document.getElementById('resourceNameInput')
var dom_resource_stats = document.getElementById('resourceStats') 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 () { function selectLocal () {
resource = {name: 'root'} resource = {name: 'root'}
resource_stats = {files: {count: 0, size: 0}, directories: {count: 0}, mime:{}} resource_stats = {files: {count: 0, size: 0}, directories: {count: 0}, mime:{}}
dialog.showOpenDialog({properties:['openFile', 'openDirectory', 'multiSelections']}, function selectedPath(_paths) { dialog.showOpenDialog({properties:['openFile', 'openDirectory', 'multiSelections']}, function selectedPath(_paths) {
if (!_paths) {
return
}
renderer_interval = setInterval(function () {
renderResource()
}, 100)
setFormState('loading') setFormState('loading')
dom_resource_name.value = ''
if (_paths.length === 1) { if (_paths.length === 1) {
var single_file = _paths[0] 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 dom_resource_name.value = resource.name
fs.stat(single_file, function(err, stats) { fs.stat(single_file, function(err, stats) {
if (err) { if (err) {
...@@ -46,7 +76,6 @@ function selectLocal () { ...@@ -46,7 +76,6 @@ function selectLocal () {
} }
_paths = files.map(function(file) { _paths = files.map(function(file) {
var fullpath = path.join(single_file, file) var fullpath = path.join(single_file, file)
// console.log('foo1', single_file + '+' + file + '=' + fullpath)
return fullpath return fullpath
}) })
recurseLocal(resource, _paths, resourceLoaded) recurseLocal(resource, _paths, resourceLoaded)
...@@ -62,10 +91,9 @@ function selectLocal () { ...@@ -62,10 +91,9 @@ function selectLocal () {
} }
var resourceLoaded = function resourceLoaded() { var resourceLoaded = function resourceLoaded() {
setFormState('select') setFormState('loaded')
console.log(JSON.stringify(b2s(resource_stats.files.size), null, 4)) clearInterval(renderer_interval)
console.log(resource_stats.files.size) // ipc.send('data', resource_stats)
ipc.send('data', resource_stats)
renderResource() renderResource()
} }
...@@ -75,12 +103,12 @@ var renderResource = function renderResource() { ...@@ -75,12 +103,12 @@ var renderResource = function renderResource() {
document.getElementById('resourceFiles').innerHTML = '' document.getElementById('resourceFiles').innerHTML = ''
document.getElementById('mimeStats').innerHTML = '' document.getElementById('mimeStats').innerHTML = ''
document.getElementById('resourceDirectories').appendChild(document.createTextNode('Katalooge: ' + resource_stats.directories.count)) 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) { Object.keys(resource_stats.mime).forEach(function (mime_type_name) {
var text_node = document.createTextNode( var text_node = document.createTextNode(
mime_type_name mime_type_name
+ ': ' + op.get(resource_stats, ['mime', mime_type_name, 'count']) + ': ' + 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') var li_node = document.createElement('LI')
li_node.appendChild(text_node) li_node.appendChild(text_node)
...@@ -89,17 +117,14 @@ var renderResource = function renderResource() { ...@@ -89,17 +117,14 @@ var renderResource = function renderResource() {
} }
var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) { var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) {
// console.log(JSON.stringify(paths, null, 4))
async.each(paths, function iterator(_path, callback) { async.each(paths, function iterator(_path, callback) {
fs.stat(_path, function(err, stats) { fs.stat(_path, function(err, stats) {
if (err) { if (err) {
// console.log(err)
return callback() return callback()
} }
if (stats.isFile()) { if (stats.isFile()) {
magic.detectFile(_path, function(err, result) { magic.detectFile(_path, function(err, result) {
if (err) { if (err) {
// console.log(err)
return callback() return callback()
} }
resource_stats.files.count++ resource_stats.files.count++
...@@ -117,14 +142,12 @@ var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) { ...@@ -117,14 +142,12 @@ var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) {
resource_stats.directories.count++ resource_stats.directories.count++
var directory = {name: _path} var directory = {name: _path}
op.push(parent_resource, 'resources', directory) op.push(parent_resource, 'resources', directory)
// console.log('dir: ' + JSON.stringify(_path, null, 4))
fs.readdir(_path, function(err, files) { fs.readdir(_path, function(err, files) {
if (err) { if (err) {
return callback(err) return callback(err)
} }
var _paths = files.map(function(file) { var _paths = files.map(function(file) {
var fullpath = path.join(_path, file) var fullpath = path.join(_path, file)
// console.log(_path + '+' + file + '=' + fullpath)
return fullpath return fullpath
}) })
recurseLocal(directory, _paths, callback) recurseLocal(directory, _paths, callback)
...@@ -140,20 +163,91 @@ var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) { ...@@ -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) { var setFormState = function setFormState(state) {
switch(state) { switch(state) {
case 'select': case 'select':
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('resourceStats').removeAttribute('hidden')
document.getElementById('selectLocal').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 break
case 'loading': case 'loading':
dom_resource_name.removeAttribute('hidden')
document.getElementById('loading').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('resourceStats').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').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 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 ...@@ -8,15 +8,35 @@ html
body body
h1#userName Panustaja h1#userName(hidden='') Panustaja
div#selectLocalMessage Palun vali fail või kataloog, mida soovid üles laadida. div#selectLocal
button#selectLocal(onclick='selectLocal()') Vali fail p#selectLocalMessage Palun vali fail või kataloog, mida soovid üles laadida.
input#resourceName(type='text' placeholder='Ressursi nimi' style='width:100%;' hidden='') button#selectLocalButton(onclick='selectLocal()') Vali fail
div#contents div#resourceName(hidden='')
h3#loading(hidden='') Loading... p#resourceNameMessage Muuda ressursi nimi sobivaks.
ul#resourceStats(hidden='') 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#resourceDirectories
li#resourceFiles li#resourceFiles
ul#mimeStats 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') script(src='../main.js')
{ {
"name": "Panustaja", "name": "Panustaja",
"description": "Ressursikataloogi upitamiseks keeleressurside Entu repositooriumisse.", "description": "Ressursikataloogi upitamiseks keeleressurside Entu repositooriumisse.",
"version": "1.0.1", "version": "1.0.1-d",
"main": "./code/app.js", "build": 41,
"license": "CC-SA", "main": "./code/app.js",
"private": true, "license": "CC-SA",
"repository": { "private": true,
"type": "git", "repository": {
"url": "git@gitlab.keeleressursid.ee:michelek/panustaja.git" "type": "git",
}, "url": "git@gitlab.keeleressursid.ee:michelek/panustaja.git"
"dependencies": { },
"async": "^1.5.0", "dependencies": {
"electron-jade": "^1.3.0", "async": "^1.5.0",
"marked": "^0.3.5", "electron-jade": "^1.3.0",
"mmmagic": "^0.4.1", "marked": "^0.3.5",
"moment": "^2.10.6", "mmmagic": "^0.4.1",
"object-path": "^0.9.2", "moment": "^2.10.6",
"request": "^2.65.0" "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