Commit 63ade4dc authored by Mihkel Putrinš's avatar Mihkel Putrinš
Browse files

render resource stats

parent 3a0b5c98
bin bin
node_modules node_modules
old
...@@ -3,6 +3,7 @@ var op = require('object-path') ...@@ -3,6 +3,7 @@ var op = require('object-path')
var fs = require('fs') var fs = require('fs')
var ipc = require('ipc') var ipc = require('ipc')
BrowserWindow = require('browser-window') BrowserWindow = require('browser-window')
var j = require('electron-jade')({pretty: true}, {})
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'
...@@ -13,7 +14,6 @@ var user_data = {} ...@@ -13,7 +14,6 @@ var user_data = {}
app.on('ready', function() { app.on('ready', function() {
authWindow = new BrowserWindow({ width: 300, height: 600, show: true }) authWindow = new BrowserWindow({ width: 300, height: 600, show: true })
authWindow.setTitle('Panustaja - log in') authWindow.setTitle('Panustaja - log in')
// authWindow.loadUrl('http://ww.ee')
authWindow.loadUrl(auth_url) authWindow.loadUrl(auth_url)
authWindow.webContents.on('did-finish-load', function() { authWindow.webContents.on('did-finish-load', function() {
authWindow.webContents.savePage('./user.json', 'HTMLOnly', function(err) { authWindow.webContents.savePage('./user.json', 'HTMLOnly', function(err) {
...@@ -41,7 +41,7 @@ app.on('ready', function() { ...@@ -41,7 +41,7 @@ app.on('ready', function() {
mainWindow = new BrowserWindow({ width: 900, height: 600, show: true }) mainWindow = new BrowserWindow({ width: 900, height: 600, show: true })
mainWindow.setTitle('Panustaja - ' + user_data['name']) mainWindow.setTitle('Panustaja - ' + user_data['name'])
mainWindow.center() mainWindow.center()
mainWindow.loadUrl('file://' + __dirname + '/main.html') mainWindow.loadUrl('file://' + __dirname + '/views/main.jade')
authWindow.close() authWindow.close()
mainWindow.webContents.openDevTools(true) mainWindow.webContents.openDevTools(true)
...@@ -56,7 +56,7 @@ app.on('ready', function() { ...@@ -56,7 +56,7 @@ app.on('ready', function() {
}) })
ipc.on('log', function(event, message) { ipc.on('log', function(event, message) {
console.log('message: ' + message) console.log('message: ', message)
}) })
ipc.on('data', function(event, message) { ipc.on('data', function(event, message) {
console.log('data: ' + JSON.stringify(message, null, 4)) console.log('data: ' + JSON.stringify(message, null, 4))
......
var bytesToSize = function bytesToSize(bytes) { var bytesToSize = function bytesToSize(bytes) {
if (bytes === undefined) bytes = 0 if (bytes === undefined) bytes = 0
var sizes = ['B', 'KB', 'MB', 'GB', 'TB'] var sizes = ['B', 'KB', 'MB', 'GB', 'TB']
......
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css"> -->
</head>
<body>
<h1 id="user_name">Panustaja</h1>
<input id="resource_name" type="text" placeholder="Ressursi nimi" style="width:100%;" hidden></input>
<div id="contents">
<div>Palun vali fail või kataloog, mida soovid üles laadida.</div>
<button id="selectLocal" onclick="selectLocal();">Vali fail</button>
</div>
<script src='./main.js'></script>
</body>
</html>
...@@ -3,12 +3,10 @@ var fs = require('fs') ...@@ -3,12 +3,10 @@ var fs = require('fs')
var op = require('object-path') var op = require('object-path')
var path = require('path') var path = require('path')
var async = require('async') var async = require('async')
var mime = require('mime-types')
// TODO: WATCH: mmmagic is broken currently: Module version mismatch. Expected 46, got 14 var mmm = require('mmmagic')
// var Magic = require('mmmagic').Magic var Magic = mmm.Magic
// var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING) var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING)
// usage: magic.detectFile(_path, function(err, result) {
var remote = require('remote') var remote = require('remote')
var dialog = remote.require('dialog') var dialog = remote.require('dialog')
...@@ -16,19 +14,22 @@ var dialog = remote.require('dialog') ...@@ -16,19 +14,22 @@ var dialog = remote.require('dialog')
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('user_name').innerHTML = user_data.name document.getElementById('userName').innerHTML = user_data.name
}) })
var b2s = require(path.join(__dirname, '..', 'bytesToSize.js'))
var resource = {} var resource = {}
var resource_stats = {} var resource_stats = {}
var dom_resource_name = document.getElementById('resource_name') var dom_resource_name = document.getElementById('resourceName')
var dom_resource_stats = document.getElementById('resourceStats')
dom_resource_name.setAttribute('hidden', '') dom_resource_name.setAttribute('hidden', '')
function selectLocal () { function selectLocal () {
resource = {name: 'root'} resource = {name: 'root'}
resource_stats = {files: 0, directories: 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) {
dom_resource_name.removeAttribute('hidden') setFormState('loading')
dom_resource_name.value = '' dom_resource_name.value = ''
if (_paths.length === 1) { if (_paths.length === 1) {
var single_file = _paths[0] var single_file = _paths[0]
...@@ -45,53 +46,85 @@ function selectLocal () { ...@@ -45,53 +46,85 @@ 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) // console.log('foo1', single_file + '+' + file + '=' + fullpath)
return fullpath return fullpath
}) })
console.log('foo1');
recurseLocal(resource, _paths, resourceLoaded) recurseLocal(resource, _paths, resourceLoaded)
}) })
} else { } else {
console.log('foo2');
recurseLocal(resource, _paths, resourceLoaded) recurseLocal(resource, _paths, resourceLoaded)
} }
}) })
} else { } else {
console.log('foo3');
recurseLocal(resource, _paths, resourceLoaded) recurseLocal(resource, _paths, resourceLoaded)
} }
}) })
} }
var resourceLoaded = function resourceLoaded() { var resourceLoaded = function resourceLoaded() {
console.log(JSON.stringify(resource, null, 4)) setFormState('select')
console.log(JSON.stringify(resource_stats, null, 4)) console.log(JSON.stringify(b2s(resource_stats.files.size), null, 4))
console.log(resource_stats.files.size)
ipc.send('data', resource_stats)
renderResource()
}
var renderResource = function renderResource() {
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 recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) { var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) {
console.log(JSON.stringify(paths, null, 4)) // 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) {
console.log('stats for:', _path) if (err) {
// console.log(err)
return callback()
}
if (stats.isFile()) { if (stats.isFile()) {
resource_stats.files ++ magic.detectFile(_path, function(err, result) {
op.push(parent_resource, 'files', _path) if (err) {
var mimetype = mime.lookup(_path) || 'unknown' // console.log(err)
op.set(resource_stats, ['mimetypes', mimetype], op.get(resource_stats, ['mimetypes', mimetype], 0) + 1) return callback()
console.log('file', _path, mimetype) }
callback() resource_stats.files.count++
resource_stats.files.size += stats.size
op.push(parent_resource, 'files', _path)
var mime = result.split(';')[0]
var charset = result.split(';')[1].split('=')[1]
op.set(resource_stats, ['mime', mime, 'count'], op.get(resource_stats, ['mime', mime, 'count'], 0) + 1)
op.set(resource_stats, ['mime', mime, 'size'], op.get(resource_stats, ['mime', mime, 'size'], 0) + stats.size)
op.set(resource_stats, ['mime', mime, 'charsets', charset, 'count'], op.get(resource_stats, ['mime', mime, 'charsets', charset, 'count'], 0) + 1)
op.set(resource_stats, ['mime', mime, 'charsets', charset, 'size'], op.get(resource_stats, ['mime', mime, 'charsets', charset, 'size'], 0) + stats.size)
callback()
})
} else if (stats.isDirectory()) { } else if (stats.isDirectory()) {
resource_stats.directories ++ 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)) // 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) // console.log(_path + '+' + file + '=' + fullpath)
return fullpath return fullpath
}) })
recurseLocal(directory, _paths, callback) recurseLocal(directory, _paths, callback)
...@@ -106,3 +139,21 @@ var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) { ...@@ -106,3 +139,21 @@ var recurseLocal = function recurseLocal(parent_resource, paths, loadedCB) {
} }
}) })
} }
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')
break
case 'loading':
dom_resource_name.removeAttribute('hidden')
document.getElementById('loading').removeAttribute('hidden')
document.getElementById('resourceStats').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('selectLocalMessage').setAttribute('hidden', '')
break
}
}
doctype html
html
head
meta(charset='utf-8')
link(rel='stylesheet', type='text/css', href='../style/style.css')
link(rel='icon' type='image/png', href='/favicon-96x96.png' sizes='96x96')
link(rel='shortcut icon', href='/favicon.ico')
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='')
li#resourceDirectories
li#resourceFiles
ul#mimeStats
script(src='../main.js')
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