Commit 04404871 authored by Mihkel Putrinš's avatar Mihkel Putrinš

build 64. full uploading functionality ok

parent b4f29ef1
......@@ -12,8 +12,6 @@ var user_url = 'https://entu.keeleressursid.ee/api2/user'
var auth_url = user_url + '/auth'
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
var pjson_path = path.join(__dirname, '..', 'package.json')
......@@ -25,36 +23,58 @@ if (IS_DEV) {
}
app.on('ready', function() {
windows['authWindow'] = new BrowserWindow({ width: 350, height: 600, show: true, "web-preferences": {partition: "persist:panustaja (build " + (pjson.build) + ")"} })
var title = pjson.name + ' v.' + pjson.version + (pjson.version.indexOf('-') > -1 ? pjson.build : '') + ' | Logi sisse.'
var home_path = app.getPath('home')
USER_PATH = path.join(home_path, 'user.json')
windows['authWindow'] = new BrowserWindow({ width: 900, height: 600, show: true, "web-preferences": {partition: "persist:panustaja (build " + (pjson.build) + ")"} })
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'].setTitle(title)
if (new_url === user_url || new_url === user_url + '#') {
windows['authWindow'].hide()
}
})
windows['authWindow'].webContents.on('did-finish-load', function() {
windows['authWindow'].setTitle(title)
console.log(windows['authWindow'].webContents.getUrl())
if (windows['authWindow'].webContents.getUrl() !== user_url
&& windows['authWindow'].webContents.getUrl() !== user_url + '#') {
var new_url = windows['authWindow'].webContents.getUrl()
if (new_url === user_url || new_url === user_url + '#') {
mainWindow = new BrowserWindow({ width: 900, height: 600, show: true })
mainWindow.setTitle('Panustaja')
mainWindow.center()
// require('dialog').showMessageBox({type:'info', message:'OK\n"' + windows['authWindow'].webContents.getUrl() + '"'
// + '\n==='
// + '\n"' + user_url + '"'
// , buttons:['ok']
// })
// require('dialog').showMessageBox({type:'info', message:'HOME: ' + home_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 {
var view_path = path.join(__dirname, 'views', 'main.jade')
// require('dialog').showMessageBox({type:'info', message:'peale salvestamist: korras\n'
// + 'Laen lehte: file://' + view_path, buttons:['ok']})
mainWindow.webContents.loadUrl('file://' + view_path)
windows['authWindow'].hide()
if (IS_DEV) {
mainWindow.webContents.openDevTools(true)
}
// require('dialog').showMessageBox({type:'info', message:'fail suletud: ' + USER_PATH, buttons:['ok']})
}
})
} else {
// require('dialog').showMessageBox({type:'info', message:'"' + new_url + '"'
// + '\n!=='
// + '\n"' + user_url + '"'
// , buttons:['ok']
// })
return
}
// 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 {
// 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)
}
// require('dialog').showMessageBox({type:'info', message:'fail suletud: ' + USER_PATH, buttons:['ok']})
}
})
})
})
......
......@@ -9,10 +9,12 @@ var Magic = mmm.Magic
var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING)
var remote = require('remote')
var app = remote.require('app')
var dialog = remote.require('dialog')
// console.log(path.join(__dirname)) ==> panustaja/code/views
var pjson = require(path.join(__dirname, '..', '..', 'package.json'))
UPLOADER_VERSION = pjson.name + ' v.' + pjson.version + (pjson.version.indexOf('-') > -1 ? pjson.build : '')
var ipc = require('ipc')
// ipc.send('userdata-query')
......@@ -29,9 +31,9 @@ var dom_resource_stats = document.getElementById('resourceStats')
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
var home_path = app.getPath('home')
USER_PATH = path.join(home_path, 'user.json')
// dialog.showMessageBox({type:'info', message:'loeme faili: ' + USER_PATH, buttons:['ok']})
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)
......@@ -40,7 +42,7 @@ setTimeout(function () {
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']
var title = UPLOADER_VERSION + ' | ' + user_data['name']
ipc.send('setTitle', title)
} else {
ipc.send('log', 'User data incomplete.')
......@@ -48,7 +50,7 @@ setTimeout(function () {
}
ipc.send('closeAuth')
})
}, 100)
}, 10)
function selectLocal () {
resource = {name: 'root'}
......@@ -93,7 +95,7 @@ function selectLocal () {
var resourceLoaded = function resourceLoaded() {
setFormState('loaded')
clearInterval(renderer_interval)
// ipc.send('data', resource_stats)
// ipc.send('data', resource)
renderResource()
}
......@@ -163,42 +165,8 @@ 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 uploadResource = require(path.join(__dirname, '..', 'upload.js'))
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':
......@@ -239,6 +207,7 @@ var setFormState = function setFormState(state) {
document.getElementById('resourceName').setAttribute('hidden', '')
break
case 'uploaded':
document.getElementById('thankYou').removeAttribute('hidden')
// -------- //
document.getElementById('resourceStats').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
......
#resourceProgressbarOuter {
width: 100%;
height: 10px;
border-radius: 25px;
height: 6px;
background-color: #cdf;
border: 1px solid black;
border: 0px solid black;
}
#resourceProgressbarInner {
width: 10%;
width: 0%;
border-radius: 25px;
height: 100%;
background-color: #79b;
/*border: 1px solid green;*/
/*border: 0px solid green;*/
}
#fileProgressbarOuter {
width: 100%;
height: 10px;
border-radius: 25px;
height: 6px;
background-color: #cdf;
border: 1px solid black;
border: 0px solid black;
}
#fileProgressbarInner {
width: 27%;
width: 0%;
border-radius: 25px;
height: 100%;
background-color: #79b;
/*border: 1px solid green;*/
/*border: 0px solid green;*/
}
var request = require('request')
var fs = require('fs')
var op = require('object-path')
var path = require('path')
var async = require('async')
var b2s = require(path.join(__dirname, '..', 'code', 'bytesToSize.js'))
var resource_root_eid = 4387
var i = 0
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 main = function main() {
uploaded_resources_progress = 0
uploaded_files_progress = 0
setFormState('uploading')
// console.log(JSON.stringify(resource_stats, null, 4))
// 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 () {
renderProgress()
}, 250)
setFormState('uploading')
recurseResources(resource_root_eid, resource, resourcesCreated)
}
var resourcesCreated = function resourcesCreated(err) {
if (err) { throw(err) }
async.parallelLimit(file_upload_tasks, 3, function filesUploaded() {
setFormState('uploaded')
clearInterval(renderer_interval)
renderProgress()
})
}
var recurseResources = function recurseResources(parent_eid, resource, resourcesCreatedCB) {
// console.log('Recurse under EID:', parent_eid)
createEntuResource(parent_eid, resource, function resourceCreatedCB(err, new_eid) {
if (err) { return resourcesCreatedCB(err) }
async.each(op.get(resource, ['resources'], []), function iterator(child_resource, callback) {
recurseResources(new_eid, child_resource, callback)
}, function(err){
if( err ) { return resourcesCreatedCB(err) }
resourcesCreatedCB()
})
})
}
var renderProgress = 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)
}
module.exports = main
var file_upload_tasks = []
var createEntuResource = function createEntuResource(parent_eid, resource, callback) {
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 () {
var new_eid = op.get(JSON.parse(this.responseText), ['result', 'id'], false)
if (new_eid) {
// console.log('onload new 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)
}
}
xhr.onerror = function(err) {
callback(err)
}
xhr.send('definition=resource')
}
var addEntuFile = function addEntuFile(eid, file_path, callback) {
var options = {
url: ENTU_API_FILE,
headers: {
'X-Auth-UserId': user_data['user_id'],
'X-Auth-Token': user_data['session_key'],
'User-Agent': UPLOADER_VERSION
}
}
var req = request.post(options, function (err, resp, body) {
// console.log(err, resp, JSON.parse(body))
if (err) {
callback(err)
// } else if (resp.status.statusCode !== 200) {
// callback(body)
} else {
// console.log('URL: ' + body)
callback()
}
})
var form = req.form()
var read_stream = fs.createReadStream(file_path)
form.append('file', read_stream)
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: " + chunk.length)
})
// setInterval(function () {
// }, 10)
}
var addEntuProperties = function addEntuProperties(eid, data, callback) {
var url_data = Object.keys(data).map(function (ix) {return ix + '=' + data[ix]}).join('&')
// console.log(url_data)
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.onload = function () {
var response = JSON.parse(this.responseText)
// console.log(JSON.stringify({sent:data, got:response}, null, 4))
callback()
}
xhr.onerror = function(err) {
callback('ERROR: ' + err)
}
xhr.send(data)
}
......@@ -8,10 +8,10 @@ html
body
h1#userName(hidden='') Panustaja
h1#userName() Tere
div#selectLocal
p#selectLocalMessage Palun vali fail või kataloog, mida soovid üles laadida.
button#selectLocalButton(onclick='selectLocal()') Vali fail
p#selectLocalMessage Palun vali fail(id) ja/või kataloog(id), mida soovid üles laadida.
button#selectLocalButton(onclick='selectLocal()') Vali
div#resourceName(hidden='')
p#resourceNameMessage Muuda ressursi nimi sobivaks.
input#resourceNameInput(type='text' placeholder='Ressursi nimi' style='width:100%;')
......@@ -38,5 +38,7 @@ html
span#uploadTotalSize 0
div#fileProgressbarOuter
div#fileProgressbarInner
pre#status
div#thankYou(hidden='') Aitäh!
script(src='../main.js')
......@@ -2,7 +2,7 @@
"name": "Panustaja",
"description": "Ressursikataloogi upitamiseks keeleressurside Entu repositooriumisse.",
"version": "1.0.1-d",
"build": 56,
"build": 64,
"main": "./code/app.js",
"license": "CC-SA",
"private": true,
......
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