Commit 3a0b5c98 authored by Mihkel Putrinš's avatar Mihkel Putrinš

recursive read files and detect mime-types

parent 7726d45f
var app = require('app')
var op = require('object-path')
var fs = require('fs')
var ipc = require('ipc')
BrowserWindow = require('browser-window')
var user_url = 'https://entu.keeleressursid.ee/api2/user'
var auth_url = user_url + '/auth'
// var authWindow
// var mainWindow
var user_data = {}
app.on('ready', function() {
authWindow = new BrowserWindow({ width: 300, height: 600, show: true })
authWindow.setTitle('Panustaja - log in')
// authWindow.loadUrl('http://ww.ee')
authWindow.loadUrl(auth_url)
authWindow.webContents.on('did-finish-load', function() {
authWindow.webContents.savePage('./user.json', 'HTMLOnly', function(err) {
if (err) {
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 + '/main.html')
authWindow.close()
mainWindow.webContents.openDevTools(true)
// var dialog = require('dialog')
// dialog.showErrorBox('FOO', 'bar, baz')
// console.log(dialog.showOpenDialog(mainWindow, { properties: [ 'openDirectory' ]}))
})
}
})
})
})
ipc.on('log', function(event, message) {
console.log('message: ' + message)
})
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)
})
<!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>
var request = require('request')
var fs = require('fs')
var op = require('object-path')
var path = require('path')
var async = require('async')
var mime = require('mime-types')
// TODO: WATCH: mmmagic is broken currently: Module version mismatch. Expected 46, got 14
// var Magic = require('mmmagic').Magic
// var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING)
// usage: magic.detectFile(_path, function(err, result) {
var remote = require('remote')
var dialog = remote.require('dialog')
var ipc = require('ipc')
ipc.send('userdata-query')
ipc.on('userdata-reply', function(user_data) {
document.getElementById('user_name').innerHTML = user_data.name
})
var resource = {}
var resource_stats = {}
var dom_resource_name = document.getElementById('resource_name')
dom_resource_name.setAttribute('hidden', '')
function selectLocal () {
resource = {name: 'root'}
resource_stats = {files: 0, directories: 0, mime:{}}
dialog.showOpenDialog({properties:['openFile', 'openDirectory', 'multiSelections']}, function selectedPath(_paths) {
dom_resource_name.removeAttribute('hidden')
dom_resource_name.value = ''
if (_paths.length === 1) {
var single_file = _paths[0]
op.set(resource, 'name', single_file)
dom_resource_name.value = resource.name
fs.stat(single_file, function(err, stats) {
if (err) {
throw (err)
}
if (stats.isDirectory()) {
fs.readdir(single_file, function(err, files) {
if (err) {
throw (err)
}
_paths = files.map(function(file) {
var fullpath = path.join(single_file, file)
console.log('foo1', single_file + '+' + file + '=' + fullpath)
return fullpath
})
console.log('foo1');
recurseLocal(resource, _paths, resourceLoaded)
})
} else {
console.log('foo2');
recurseLocal(resource, _paths, resourceLoaded)
}
})
} else {
console.log('foo3');
recurseLocal(resource, _paths, resourceLoaded)
}
})
}
var resourceLoaded = function resourceLoaded() {
console.log(JSON.stringify(resource, null, 4))
console.log(JSON.stringify(resource_stats, null, 4))
}
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) {
console.log('stats for:', _path)
if (stats.isFile()) {
resource_stats.files ++
op.push(parent_resource, 'files', _path)
var mimetype = mime.lookup(_path) || 'unknown'
op.set(resource_stats, ['mimetypes', mimetype], op.get(resource_stats, ['mimetypes', mimetype], 0) + 1)
console.log('file', _path, mimetype)
callback()
} else if (stats.isDirectory()) {
resource_stats.directories ++
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)
})
}
})
}, function(err){
if( err ) {
console.log('A file failed to process', err)
} else {
loadedCB()
}
})
}
......@@ -2,7 +2,7 @@
"name": "Panustaja",
"description": "Ressursikataloogi upitamiseks keeleressurside Entu repositooriumisse.",
"version": "1.0.1",
"main": "./main/main.js",
"main": "./code/app.js",
"license": "CC-SA",
"private": true,
"repository": {
......@@ -10,8 +10,11 @@
"url": "git@gitlab.keeleressursid.ee:michelek/panustaja.git"
},
"dependencies": {
"marked": "0.3.3",
"moment": "2.9.0",
"async": "^1.5.0",
"marked": "^0.3.5",
"mime-types": "^2.1.7",
"moment": "^2.10.6",
"object-path": "^0.9.2",
"request": "^2.65.0"
}
}
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