Commit 2987e1d5 authored by Mihkel Putrinš's avatar Mihkel Putrinš
Browse files

refactor big time

parent c8718293
......@@ -9,53 +9,53 @@ var BrowserWindow = require('browser-window')
var windows = {}
var mainWindow
var user_url = 'https://entu.keeleressursid.ee/api2/user'
var auth_url = user_url + '/auth'
var userUrl = 'https://entu.keeleressursid.ee/api2/user'
var authUrl = userUrl + '/auth'
IS_DEV = process.env.DEV ? true : false
ISDEV = process.env.DEV ? true : false
var pjson_path = path.join(__dirname, '..', 'package.json')
var pjson = require(pjson_path)
if (IS_DEV) {
var pjsonPath = path.join(__dirname, '..', 'package.json')
var pjson = require(pjsonPath)
if (ISDEV) {
pjson.build++
fs.writeFileSync(pjson_path, JSON.stringify(pjson, null, 2))
fs.writeFileSync(pjsonPath, JSON.stringify(pjson, null, 2))
}
console.log('----==== ' + pjson.name + ' v.' + pjson.version + ' (build ' + (pjson.build) + ') ====----')
var web_preferences = {
var webPreferences = {
'partition': 'persist:panustaja (build ' + (pjson.build) + ')',
'page-visibility': true, // Prevent throttling DOM timers (app gets less priority while in background)
}
app.on('ready', function() {
windows.authWindow = new BrowserWindow({ width: 900, height: 600, show: true, 'web-preferences': web_preferences })
windows.authWindow = new BrowserWindow({ width: 900, height: 600, show: true, 'web-preferences': webPreferences })
// windows.authWindow.webContents.openDevTools(true)
var title = pjson.name + ' v.' + pjson.version + (pjson.version.indexOf('-') > -1 ? pjson.build : '') + ' | Logi sisse'
windows.authWindow.center()
windows.authWindow.setTitle(title)
windows.authWindow.loadUrl(auth_url)
windows.authWindow.webContents.on('did-get-response-details', function(e, s, new_url) {
windows.authWindow.loadUrl(authUrl)
windows.authWindow.webContents.on('did-get-response-details', function(e, s, newUrl) {
windows.authWindow.setTitle(title)
if (new_url === user_url || new_url === user_url + '#') {
if (newUrl === userUrl || newUrl === userUrl + '#') {
windows.authWindow.hide()
}
})
windows.authWindow.webContents.on('did-finish-load', function() {
windows.authWindow.setTitle(title)
var new_url = windows.authWindow.webContents.getUrl()
if (new_url === user_url || new_url === user_url + '#') {
var newUrl = windows.authWindow.webContents.getUrl()
if (newUrl === userUrl || newUrl === userUrl + '#') {
clipboard.clear()
windows.authWindow.webContents.selectAll()
windows.authWindow.webContents.copy()
setTimeout(function () {
mainWindow = new BrowserWindow({ width: 900, height: 600, show: true, 'web-preferences': web_preferences })
mainWindow = new BrowserWindow({ width: 900, height: 600, show: true, 'web-preferences': webPreferences })
mainWindow.setTitle('Panustaja')
mainWindow.center()
var view_path = path.join(app.getAppPath(), 'code', 'panu_view.html')
mainWindow.webContents.loadUrl('file://' + view_path)
mainWindow.webContents.openDevTools(true)
if (IS_DEV) {
var viewPath = path.join(app.getAppPath(), 'code', 'panuView.html')
mainWindow.webContents.loadUrl('file://' + viewPath)
if (ISDEV) {
mainWindow.webContents.openDevTools(true)
}
windows.authWindow.close()
delete windows.authWindow
......@@ -77,15 +77,15 @@ ipc.on('setTitle', function(event, message) {
mainWindow.setTitle(message)
})
var user_data = false
// console.log('user_data: ' + JSON.stringify(user_data, null, 4))
var userData = false
// console.log('userData: ' + JSON.stringify(userData, null, 4))
ipc.on('setUser', function(event, data) {
user_data = data
// console.log('setUser: ' + JSON.stringify(user_data, null, 4))
userData = data
// console.log('setUser: ' + JSON.stringify(userData, null, 4))
})
ipc.on('getUser', function(event) {
event.returnValue = user_data
// console.log('getUser: ' + JSON.stringify(user_data, null, 4))
event.returnValue = userData
// console.log('getUser: ' + JSON.stringify(userData, null, 4))
})
app.on('window-all-closed', function() {
......
......@@ -11,14 +11,14 @@ var dialog = remote.require('dialog')
var clipboard = remote.require('clipboard')
var pjson = require(path.join(__dirname, '..', 'package.json'))
UPLOADER_VERSION = pjson.name + ' v.' + pjson.version + (pjson.version.indexOf('-') > -1 ? pjson.build : '')
UPLOADERVERSION = pjson.name + ' v.' + pjson.version + (pjson.version.indexOf('-') > -1 ? pjson.build : '')
var ipc = require('ipc')
var b2s = require(path.join(__dirname, 'bytesToSize.js'))
var uploader = require(path.join(__dirname, 'upload.js'))
var user_data = {}
var userData = {}
var data = ipc.sendSync('getUser', null)
function setFormState(state) {
......@@ -79,70 +79,67 @@ function setFormState(state) {
var resource = {}
var resource_stats = {}
var renderer_interval
var resourceStats = {}
var rendererInterval
function renderResource() {
// console.log(JSON.stringify(op.get(resource_stats, 'mime'), null, 2))
// console.log(JSON.stringify(op.get(resourceStats, 'mime'), null, 2))
document.getElementById('resourceStats').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']))
document.getElementById('resourceDirectories').appendChild(document.createTextNode('Katalooge: ' + resourceStats.directories.count))
document.getElementById('resourceFiles').appendChild(document.createTextNode('Faile: ' + resourceStats.files.count + ' | ' + b2s(resourceStats.files.size)))
Object.keys(resourceStats.mime).forEach(function (mimeTypeName) {
var textNode = document.createTextNode(
mimeTypeName
+ ': ' + op.get(resourceStats, ['mime', mimeTypeName, 'count'])
+ ' | ' + b2s(op.get(resourceStats, ['mime', mimeTypeName, 'size']))
)
var li_node = document.createElement('LI')
li_node.appendChild(text_node)
document.getElementById('mimeStats').appendChild(li_node)
var liNode = document.createElement('LI')
liNode.appendChild(textNode)
document.getElementById('mimeStats').appendChild(liNode)
})
}
function resourceLoaded() {
renderResource()
setFormState('loaded')
clearInterval(renderer_interval)
if (document.getElementById('resourceNameInput').value === '') {
document.getElementById('resourceNameInput').focus()
}
clearInterval(rendererInterval)
// ipc.send('data', resource)
document.getElementById('uploadResourceButton').onclick = function uploadResource() {
uploader.upload()
}
}
function registerMime(parent_resource, filepath, filesize, callback) {
resource_stats.files.count++
resource_stats.files.size += filesize
function registerMime(parentResource, filepath, filesize, callback) {
resourceStats.files.count++
resourceStats.files.size += filesize
var mimetype = mime.lookup(filepath)
op.push(parent_resource, 'files', filepath)
op.set(resource_stats, ['mime', mimetype, 'count'], op.get(resource_stats, ['mime', mimetype, 'count'], 0) + 1)
op.set(resource_stats, ['mime', mimetype, 'size'], op.get(resource_stats, ['mime', mimetype, 'size'], 0) + filesize)
op.push(parentResource, 'files', filepath)
op.set(resourceStats, ['mime', mimetype, 'count'], op.get(resourceStats, ['mime', mimetype, 'count'], 0) + 1)
op.set(resourceStats, ['mime', mimetype, 'size'], op.get(resourceStats, ['mime', mimetype, 'size'], 0) + filesize)
callback()
}
function recurseLocal(parent_resource, paths, loadedCB) {
async.each(paths, function iterator(_path, callback) {
fs.stat(_path, function(err, stats) {
function recurseLocal(parentResource, paths, loadedCB) {
async.each(paths, function iterator(myPath, callback) {
fs.stat(myPath, function(err, stats) {
if (err) { return callback() }
if (stats.isFile()) {
registerMime(parent_resource, _path, stats.size, callback)
registerMime(parentResource, myPath, stats.size, callback)
}
else if (stats.isDirectory()) {
resource_stats.directories.count++
var directory = {name: _path}
op.push(parent_resource, 'resources', directory)
fs.readdir(_path, function(err, files) {
resourceStats.directories.count++
var directory = {name: myPath}
op.push(parentResource, 'resources', directory)
fs.readdir(myPath, function(err, files) {
if (err) { return callback(err) }
var _paths = files.map(function(file) {
return path.join(_path, file)
var myPaths = files.map(function(file) {
return path.join(myPath, file)
})
recurseLocal(directory, _paths, callback)
recurseLocal(directory, myPaths, callback)
})
}
})
......@@ -150,7 +147,7 @@ function recurseLocal(parent_resource, paths, loadedCB) {
if( err ) {
console.log('A file failed to process', err)
} else {
// console.log(JSON.stringify(op.get(resource_stats, 'mime'), null, 2))
// console.log(JSON.stringify(op.get(resourceStats, 'mime'), null, 2))
loadedCB()
}
})
......@@ -158,32 +155,24 @@ function recurseLocal(parent_resource, paths, loadedCB) {
document.getElementById('selectLocalButton').onclick = 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)
resourceStats = {files: {count: 0, size: 0}, directories: {count: 0}, mime:{}}
dialog.showOpenDialog({properties:['openFile', 'openDirectory']}, function selectedPath(myPaths) {
if (!myPaths) { return }
rendererInterval = setInterval(function () { renderResource() }, 100)
setFormState('loading')
if (_paths.length === 1) {
var single_file = _paths[0]
op.set(resource, 'name', path.basename(single_file))
document.getElementById('resourceNameInput').value = resource.name
fs.stat(single_file, function(err, stats) {
if (myPaths.length === 1) {
var singleFile = myPaths[0]
op.set(resource, 'name', path.basename(singleFile))
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)
return fullpath
})
recurseLocal(resource, _paths, resourceLoaded)
})
} else {
recurseLocal(resource, _paths, resourceLoaded)
}
})
} else {
recurseLocal(resource, _paths, resourceLoaded)
}
})
}
......@@ -195,13 +184,7 @@ if (!data) {
clipboard.clear()
ipc.send('setUser', data)
}
// console.log('user_data: ' + JSON.stringify(data, null, 4))
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 = UPLOADER_VERSION + ' | ' + user_data.name
ipc.send('setTitle', title)
setFormState('select')
} else {
......
......@@ -13,13 +13,11 @@
Panustaja
</div>
<h1 id="userName">Tere</h1>
<div id="selectLocal" hidden>
<div id="selectLocal" hidden="">
<p id="selectLocalMessage">Palun vali fail(id) ja/või kataloog(id), mida soovid üles laadida.</p>
<button id="selectLocalButton">Vali</button>
</div>
<div id="resourceName" hidden="">
<p id="resourceNameMessage">Muuda ressursi nimi sobivaks.</p>
<input id="resourceNameInput" type="text" placeholder="Ressursi nimi" style="width:100%;">
</div>
<div id="loading" hidden="">
<h3>Loen failisüsteemist ...</h3>
......@@ -49,7 +47,7 @@
<h3>Aitäh!</h3>
<div>
<span>Su ressurss on kohal: </span>
<a id="resource_entu_link">siin</a>
<a id="resourceEntuLink">siin</a>
</div>
</div>
<script src="./panu.js"></script>
......
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
iframe#login_frame(name='disable-x-frame-options' src='https://entu.keeleressursid.ee/api2/user/auth', width='600', height='600')
......@@ -9,51 +9,36 @@ var async = require('async')
var b2s = require(path.join(__dirname, '..', 'code', 'bytesToSize.js'))
var resource_root_eid = 4387
var uploaded_resources_progress
var uploaded_files_progress
ENTU_API_ENTITY = 'https://entu.keeleressursid.ee/api2/entity'
ENTU_API_FILE = 'https://entu.keeleressursid.ee/api2/file'
var resourceRootEid = 4387
var uploadedResourcesProgress
var uploadedFilesProgress
ENTUAPI = 'https://entu.keeleressursid.ee/api2'
ENTUAPIENTITY = 'https://entu.keeleressursid.ee/api2/entity'
ENTUAPIFILE = 'https://entu.keeleressursid.ee/api2/file'
var renderer_interval
var rendererInterval
var file_upload_tasks = []
var fileUploadTasks = []
function openResourceInBrowser() {
require('shell').openExternal('https://entu.keeleressursid.ee/entity/resource/' + resource.eid)
return false
}
function renderProgress() {
// dom_resource_stats.removeAttribute('hidden')
document.getElementById('resourceProgressbarInner').style.width = (uploaded_resources_progress * 100 / (resource_stats.directories.count + 1)) + '%'
document.getElementById('uploadedResources').innerHTML = uploaded_resources_progress
document.getElementById('fileProgressbarInner').style.width = (uploaded_files_progress * 100 / resource_stats.files.size) + '%'
// console.log('== LOGGING: ', uploaded_files_progress, resource_stats.files.size, resource_stats.files.count)
document.getElementById('uploadedSize').innerHTML = b2s(uploaded_files_progress)
// domResourceStats.removeAttribute('hidden')
document.getElementById('resourceProgressbarInner').style.width = (uploadedResourcesProgress * 100 / (resourceStats.directories.count + 1)) + '%'
document.getElementById('uploadedResources').innerHTML = uploadedResourcesProgress
document.getElementById('fileProgressbarInner').style.width = (uploadedFilesProgress * 100 / resourceStats.files.size) + '%'
// console.log('== LOGGING: ', uploadedFilesProgress, resourceStats.files.size, resourceStats.files.count)
document.getElementById('uploadedSize').innerHTML = b2s(uploadedFilesProgress)
}
function resourcesCreated(err) {
if (err) { throw(err) }
async.parallelLimit(file_upload_tasks, 3, function filesUploaded() {
setFormState('uploaded')
document.getElementById('resource_entu_link').setAttribute('href', 'https://entu.keeleressursid.ee/entity/resource/' + resource.eid)
document.getElementById('resource_entu_link').innerHTML = 'https://entu.keeleressursid.ee/entity/resource/' + resource.eid
document.getElementById('resource_entu_link').onclick = openResourceInBrowser
// ipc.send('data', resource)
clearInterval(renderer_interval)
renderProgress()
})
}
function addEntuFile(eid, file_path, callback) {
function addEntuFile(eid, filePath, callback) {
var options = {
url: ENTU_API_FILE,
url: ENTUAPIFILE,
headers: {
'X-Auth-UserId': user_data.user_id,
'X-Auth-Token': user_data.session_key,
'User-Agent': UPLOADER_VERSION
'X-Auth-UserId': userData.userId,
'X-Auth-Token': userData.sessionKey,
'User-Agent': UPLOADERVERSION
}
}
......@@ -71,24 +56,39 @@ function addEntuFile(eid, file_path, callback) {
var form = req.form()
var read_stream = fs.createReadStream(file_path)
form.append('file', read_stream)
var readStream = fs.createReadStream(filePath)
form.append('file', readStream)
form.append('entity', eid)
form.append('property', 'resource-file')
form.append('filename', path.basename(file_path))
read_stream.on('data', function(chunk) {
uploaded_files_progress += chunk.length
console.log('Uploaded: ' + uploaded_files_progress + '(+' + chunk.length + ')')
form.append('filename', path.basename(filePath))
readStream.on('data', function(chunk) {
uploadedFilesProgress += chunk.length
// console.log('Uploaded: ' + uploadedFilesProgress + '(+' + chunk.length + ')')
})
}
function addEntuProperties(eid, data, callback) {
var url_data = Object.keys(data).map(function (ix) {return ix + '=' + data[ix]}).join('&')
// console.log(url_data)
request.put({
url: preparedUrl,
headers: headers,
body: qb,
strictSSL: true,
json: true,
timeout: 60000
}, function(error, response, body) {
if(error) return callback(error)
if(response.statusCode !== 201 || !body.result) return callback(new Error(op.get(body, 'error', body)))
callback(null, op.get(body, 'result.properties.' + property + '.0', null))
})
var urlData = Object.keys(data).map(function (ix) {return ix + '=' + data[ix]}).join('&')
// console.log(urlData)
var xhr = new window.XMLHttpRequest()
xhr.open('PUT', ENTU_API_ENTITY + '-' + eid + '?' + url_data, true)
xhr.setRequestHeader('X-Auth-UserId', user_data.user_id)
xhr.setRequestHeader('X-Auth-Token', user_data.session_key)
xhr.open('PUT', ENTUAPIENTITY + '-' + eid + '?' + urlData, true)
xhr.setRequestHeader('X-Auth-UserId', userData.userId)
xhr.setRequestHeader('X-Auth-Token', userData.sessionKey)
xhr.onload = function () {
// var response = JSON.parse(this.responseText)
// console.log(JSON.stringify({sent:data, got:response}, null, 4))
......@@ -100,79 +100,108 @@ function addEntuProperties(eid, data, callback) {
xhr.send(data)
}
function createEntuResource(parent_eid, resource, callback) {
console.log('create under EID:', parent_eid)
var xhr = new window.XMLHttpRequest()
xhr.open('POST', ENTU_API_ENTITY + '-' + parent_eid, true)
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
xhr.setRequestHeader('X-Auth-UserId', user_data.user_id)
xhr.setRequestHeader('X-Auth-Token', user_data.session_key)
xhr.onload = function () {
// if (err) {
// console.log(err)
// return callback(err)
// }
var new_eid = op.get(JSON.parse(this.responseText), ['result', 'id'], false)
console.log('Looking for new EID:', new_eid)
if (new_eid) {
console.log('onload new EID:', new_eid)
op.set(resource, ['eid'], new_eid)
addEntuProperties(new_eid, {
'resource-name': path.basename(op.get(resource, ['name'], 'nameless resource')),
'resource-uploader-version': UPLOADER_VERSION
}, function(err) {
if (err) { return callback(err) }
uploaded_resources_progress++
op.get(resource, ['files'], []).forEach(function(file_path) {
file_upload_tasks.push(function uploadFile(callback) {
document.getElementById('status').innerHTML = new_eid + ' : ' + file_path
// console.log('F: Uploading file ' + file_path + ' at resource ' + new_eid)
addEntuFile(new_eid, file_path, function fileAddedCB() {
callback()
})
})
// console.log('F: Queued file ' + file_path + ' for upload at resource ' + new_eid)
})
callback(null, new_eid)
})
} else {
callback('ERROR: ', this.responseText)
}
function createEntuResource(parentEid, resource, callback) {
// console.log('create under EID:', parentEid)
var body = {
'definition': 'resource',
'resource-name': path.basename(op.get(resource, ['name'], 'nameless resource')),
'resource-uploader-version': UPLOADERVERSION
}
xhr.onerror = function(err) {
console.log('error:', err)
callback(err)
if (op.get(resource, ['mime-encode'], false)) {
op.set(body, ['resource-mime-encode'], op.get(resource, ['mime-encode']))
}
xhr.send('definition=resource')
if (op.get(resource, ['size'], false)) {
op.set(body, ['resource-size'], op.get(resource, ['size']))
}
var preparedUrl = ENTUAPIENTITY + '-' + parentEid
var headers = {'X-Auth-UserId': userData.userId, 'X-Auth-Token': userData.sessionKey}
// console.log('Try to execute URL ' + preparedUrl, userData, body)
request.post({
url: preparedUrl,
headers: headers,
body: body,
strictSSL: true,
json: true,
timeout: 60000
}, function(error, response, body) {
// console.log('result', body)
if(error) return callback(error)
if(response.statusCode !== 201) return callback(new Error(op.get(body, 'error', body)))
var newEid = body.result.id
uploadedResourcesProgress++
op.get(resource, ['files'], []).forEach(function(filePath) {
fileUploadTasks.push(function uploadFile(callback) {
document.getElementById('status').innerHTML = newEid + ' : ' + filePath
// console.log('F: Uploading file ' + filePath + ' at resource ' + newEid)
addEntuFile(newEid, filePath, function fileAddedCB() {
callback()
})
})
// console.log('F: Queued file ' + filePath + ' for upload at resource ' + newEid)
})
callback(null, newEid)
})
}
function recurseResources(parent_eid, resource, resourcesCreatedCB) {
console.log('Recurse under EID:', parent_eid)
createEntuResource(parent_eid, resource, function resourceCreatedCB(err, new_eid) {
function recurseResources(parentEid, resource, resourcesCreatedCB) {
// console.log('Recurse under EID:', parentEid)
createEntuResource(parentEid, resource, function resourceCreatedCB(err, newEid) {
if (err) { return resourcesCreatedCB(err) }
async.each(op.get(resource, ['resources'], []), function iterator(child_resource, callback) {
recurseResources(new_eid, child_resource, callback)
async.each(op.get(resource, ['resources'], []), function iterator(childResource, callback) {
recurseResources(newEid, childResource, callback)
}, function(err){
if( err ) { return resourcesCreatedCB(err) }
resourcesCreatedCB()
// console.log('Created:', newEid)
resourcesCreatedCB(null, newEid)
})
})
}
function upload() {
op.set(resource, ['name'], document.getElementById('resourceNameInput').value)
uploaded_resources_progress = 0
uploaded_files_progress = 0
console.log(JSON.stringify(resource, null, 4))
document.getElementById('uploadTotalResources').innerHTML = (resource_stats.directories.count + 1)
document.getElementById('uploadTotalSize').innerHTML = b2s(resource_stats.files.size)
renderer_interval = setInterval(function () {
op.set(resource, ['size'], b2s(resourceStats.files.size))
op.set(resource, ['mime-encode'], JSON.stringify(resourceStats.mime, null, 4))
uploadedResourcesProgress = 0
uploadedFilesProgress = 0
// console.log(JSON.stringify(resource, null, 4))
document.getElementById('uploadTotalResources').innerHTML = (resourceStats.directories.count + 1)
document.getElementById('uploadTotalSize').innerHTML = b2s(resourceStats.files.size)
rendererInterval = setInterval(function () {
renderProgress()
}, 250)
setFormState('uploading')
recurseResources(resource_root_eid, resource, resourcesCreated)
async.waterfall([
function runRecurseResources(callback) {
recurseResources(resourceRootEid, resource, function resourcesCreated(err, newEid) {
if (err) { return callback(err) }
callback(null, newEid)
})
},
function runFileUploadTasks(newEid, callback) {
async.parallelLimit(fileUploadTasks, 3, function filesUploaded() { callback(null, newEid) })
},
function updateUI(newEid, callback) {
setFormState('uploaded')
document.getElementById('resourceEntuLink').setAttribute('href', 'https://entu.keeleressursid.ee/entity/resource/' + newEid)
document.getElementById('resourceEntuLink').innerHTML = 'https://entu.keeleressursid.ee/entity/resource/' + newEid
document.getElementById('resourceEntuLink').onclick = function openResourceInBrowser() {
require('shell').open