Skip to content

Commit 328c8d9

Browse files
committed
Stream export downloads to file in node-webkit. Set proper filename.
1 parent ce91b46 commit 328c8d9

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

codepulse/src/main/resources/toserve/common/Downloader.js

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
.css('display', 'none')
3636
.appendTo('body')
3737

38+
function getAbsoluteUrl(relative) {
39+
var resolver = document.createElement('a');
40+
resolver.href = relative;
41+
return resolver.href;
42+
}
43+
3844
function pickFilename(callback){ filenamePicker
3945
.val('')
4046
.one('change', function(){
@@ -45,12 +51,24 @@
4551
.click()
4652
}
4753

54+
function doDownload(url){
55+
var http = require('http'), fs = require('fs');
56+
57+
var request = http.get(getAbsoluteUrl(url), function(response) {
58+
if (response.statusCode == '200') {
59+
pickFilename(function(path) {
60+
var file = fs.createWriteStream(path);
61+
response.pipe(file);
62+
file.on('finish', function() { file.close(); });
63+
});
64+
} else
65+
alert('Error exporting: statusCode = ' + response.statusCode);
66+
}).on('error', function(e) { alert('Error exporting: ' + e); });
67+
}
68+
4869
// clicking the download link triggers the filenamePicker's dialog
4970
$elem.click(function(e){
50-
pickFilename(function(path){
51-
console.log('picked filename: ', path)
52-
doDownload(url, path)
53-
})
71+
doDownload(url)
5472
})
5573

5674
this.destroy = function(){
@@ -69,32 +87,6 @@
6987

7088
}
7189

72-
function doDownload(url, savePath){
73-
var xhr = new XMLHttpRequest()
74-
xhr.open('GET', url, true)
75-
xhr.responseType = 'arraybuffer'
76-
77-
xhr.onload = function(e){
78-
var uint8array = new Uint8Array(this.response)
79-
var arrayBuffer = new Buffer(uint8array)
80-
81-
console.log('loaded raw data from server; about to save it to ' + savePath, arrayBuffer)
82-
83-
var fs = require('fs')
84-
85-
fs.writeFile(savePath, arrayBuffer, function(err){
86-
if(err) {
87-
console.error('failed to save the array data:', err)
88-
alert('Failed to save ' + savePath + '\nError Message: ' + err.message)
89-
} else {
90-
console.log('saved the array file!')
91-
}
92-
})
93-
}
94-
95-
xhr.send()
96-
}
97-
9890
$.fn.traceDownloader = function(method){
9991
if(method == 'create'){
10092
var old = this.data('traceDownloader')

codepulse/src/main/resources/toserve/pages/TraceList/TraceList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
// set up the export link
6161
$exportLink
6262
.attr('data-downloader', data.exportHref)
63-
.attr('data-filename', 'export.zip')
63+
.attr('data-filename', data.name + '.pulse')
6464
.traceDownloader('create')
6565

6666
// set up the delete link

codepulse/src/main/scala/com/secdec/codepulse/tracer/snippet/TraceWidgetry.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class TraceWidgetry(manager: TraceManager, target: TracingTarget) extends Dispat
7171
},
7272
"exportlink" -> { (xml: NodeSeq) =>
7373
val href = tracer.traceAPIServer.Paths.Export.toHref(target)
74-
<a data-downloader={ href } data-filename="export.zip">{ runBinding(xml) }</a>
74+
<a data-downloader={ href } data-filename={ s"${data.metadata.name}.pulse" }>{ runBinding(xml) }</a>
7575
})
7676

7777
cometTemplate +: runBinding(template)

0 commit comments

Comments
 (0)