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

refactor

parent 22d35fe7
......@@ -9,14 +9,14 @@
/*border: 0px solid black;*/
}
#resourceProgressbarInner {
width: 0%;
width: 0;
border-radius: 25px;
height: 100%;
background-color: #92CC6F;
/*border: 0px solid green;*/
}
#fileProgressbarInner {
width: 0%;
width: 0;
border-radius: 25px;
height: 100%;
background-color: #0065CA;
......@@ -26,7 +26,7 @@
#murakas {
text-align: center;
font: caption;
margin: 0px;
margin: 0;
position: fixed;
top: 1em;
right: 1em;
......
......@@ -18,22 +18,21 @@ ENTU_API_FILE = 'https://entu.keeleressursid.ee/api2/file'
var renderer_interval
function upload() {
op.set(resource, ['name'], document.getElementById('resourceNameInput').value)
var file_upload_tasks = []
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 () {
renderProgress()
}, 250)
setFormState('uploading')
recurseResources(resource_root_eid, resource, resourcesCreated)
function openResourceInBrowser() {
require('shell').openExternal('https://entu.keeleressursid.ee/entity/resource/' + resource.eid)
return false
}
var file_upload_tasks = []
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)
}
function resourcesCreated(err) {
if (err) { throw(err) }
......@@ -48,37 +47,60 @@ function resourcesCreated(err) {
})
}
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()
})
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()
}
})
}
function openResourceInBrowser() {
require('shell').openExternal('https://entu.keeleressursid.ee/entity/resource/' + resource.eid)
return false
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: " + uploaded_files_progress + '(+' + chunk.length + ')')
})
}
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)
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)
}
function createEntuResource(parent_eid, resource, callback) {
console.log('create under EID:', parent_eid)
var xhr = new window.XMLHttpRequest()
......@@ -125,59 +147,33 @@ function createEntuResource(parent_eid, resource, callback) {
xhr.send('definition=resource')
}
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()
}
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()
})
})
}
function upload() {
op.set(resource, ['name'], document.getElementById('resourceNameInput').value)
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: " + uploaded_files_progress + '(+' + chunk.length + ')')
})
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 () {
renderProgress()
}, 250)
setFormState('uploading')
recurseResources(resource_root_eid, resource, resourcesCreated)
}
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)
}
module.exports.upload = upload
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