Skip to content
This repository has been archived by the owner on Dec 3, 2018. It is now read-only.

Commit

Permalink
Clean up saveAs/export flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
yhahn committed Mar 22, 2015
1 parent 91a25e1 commit 01ba27a
Showing 1 changed file with 47 additions and 37 deletions.
84 changes: 47 additions & 37 deletions app/atom.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,49 @@ $(document).ready(function() {
}
var typeExtension = (uri.pathname || '').split('.').pop().toLowerCase();
var typeLabel = fileTypes[typeExtension];
if (typeLabel) {
// HOME is undefined on windows
if (process.platform === 'win32') process.env.HOME = process.env.USERPROFILE;
var defaultPath = path.join(process.env.HOME,'Untitled ' + typeLabel + '.' + typeExtension);
dialog.showSaveDialog({
title: 'Save ' + typeLabel,
defaultPath: defaultPath,
filters: [{ name: typeExtension.toUpperCase(), extensions: [typeExtension]}]
}, function(filePath){
if (!filePath) return error(new Error('No filepath specified'));

window.Modal.show('atomexporting');
// Passthrough for all other extensions.
if (!typeLabel) return undefined;

try {
var writeStream = fs.createWriteStream(filePath);
} catch(err) {
return error(err);
}
// HOME is undefined on windows
if (process.platform === 'win32') process.env.HOME = process.env.USERPROFILE;

uri.method = 'GET';
var req = http.request(uri, function(res) {
if (res.statusCode !== 200) return error(new Error('Got HTTP code ' + res.statusCode));
res.on('error', error);
writeStream.on('error', error);
res.pipe(writeStream).on('finish', function() {
window.Modal.close();
});
});
req.on('error', error);
req.end();
// Show save dialog and make a GET request, piping
// the response into the specified filePath.
var defaultPath = path.join(process.env.HOME,'Untitled ' + typeLabel + '.' + typeExtension);
dialog.showSaveDialog({
title: 'Save ' + typeLabel,
defaultPath: defaultPath,
filters: [{ name: typeExtension.toUpperCase(), extensions: [typeExtension]}]
}, function(filePath){
if (!filePath) return false;

function error(err) {
window.Modal.close();
window.Modal.show('err', err);
return false;
}
window.Modal.show('atomexporting');

return false;
});
return false;
}
// Passthrough everything else.
function error(err) {
window.Modal.close();
window.Modal.show('error', err);
}

function finish() {
window.Modal.close();
window.Modal.show('atomcomplete');
}

try {
var writeStream = fs.createWriteStream(filePath);
} catch(err) {
return error(err);
}

http.get(uri, function(res) {
if (res.statusCode !== 200) return error(new Error('Got HTTP code ' + res.statusCode));
res.on('error', error);
writeStream.on('error', error);
res.pipe(writeStream).on('finish', finish);
}).on('error', error);
});
return false;
});

if (window.Modal) {
Expand All @@ -85,5 +86,14 @@ $(document).ready(function() {
<div class='row2 loading contain'></div>\
</div>";
};
window.Modal.options.templates.modalatomcomplete = function() {
return "\
<div id='atom-loading' class='modal-body contain round col6 space-bottom4 dark fill-dark'>\
<h3 class='center pad1y pad2x keyline-bottom'>Exporting</h3>\
<div class='row2 pad2 contain clearfix'>\
<a href='#' class='js-close margin3 col6 button icon check'>Export complete</a>\
</div>\
</div>";
};
}
});

0 comments on commit 01ba27a

Please sign in to comment.