Skip to content

Commit

Permalink
Merge pull request #736 from joemirizio/fix-invalid-export-path-bug
Browse files Browse the repository at this point in the history
Prevent data loss when exporting a file as an existing directory
  • Loading branch information
prikhi committed Sep 21, 2015
2 parents 93cb178 + ce2de4b commit b1021aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
39 changes: 27 additions & 12 deletions app/content/pencil/exportWizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ ExportWizard.setup = function () {
Dom.empty(ExportWizard.exporterRadioGroup);

var selectedExporterRadioItem = null;

var selectedExporter = null;
if (ExportWizard.dialogData.forcedExporterId) {
selectedExporter = ExportWizard.Pencil.getDocumentExporterById(ExportWizard.dialogData.forcedExporterId);
} else if (lastSelection.exporterId) {
selectedExporter = ExportWizard.Pencil.getDocumentExporterById(lastSelection.exporterId);
}

if (!selectedExporter) selectedExporter = ExportWizard.Pencil.defaultDocumentExporter;

for (var i = 0; i < exporters.length; i ++) {
Expand Down Expand Up @@ -81,7 +81,7 @@ ExportWizard.setup = function () {
window.setTimeout(ExportWizard.onPageSelectionChanged, 10);

//Setup templates and other options
if (ExportWizard.getSelectedExporter().supportTemplating() &&
if (ExportWizard.getSelectedExporter().supportTemplating() &&
lastSelection.templateId) {
ExportWizard.templateMenu.value = lastSelection.templateId;
}
Expand All @@ -94,7 +94,7 @@ ExportWizard.setup = function () {

ExportWizard._setupFormatPageDone = true;
window.sizeToContent();

if (ExportWizard.dialogData.forcedExporterId) {
document.documentElement.advance();
}
Expand Down Expand Up @@ -238,12 +238,12 @@ ExportWizard.browseTargetFile = function () {
fp.appendFilter(Util.getMessage("filepicker.all.files"), "*");

if (fp.show() == nsIFilePicker.returnCancel) return;

var path = fp.file.path;
if (isChoosingFile && defaultExt && path.indexOf(".") < 0) {
path = path + defaultExt;
}

ExportWizard.targetFilePathText.value = path;
};
ExportWizard.validatePageSelection = function () {
Expand All @@ -264,22 +264,38 @@ ExportWizard.validatePageSelection = function () {
};
ExportWizard.validateOptions = function () {
var exporter = ExportWizard.getSelectedExporter();
var outputType = exporter.getOutputType();

if (exporter.getOutputType() != BaseExporter.OUTPUT_TYPE_NONE) {
if (outputType != BaseExporter.OUTPUT_TYPE_NONE) {
// Display an error message if the given output file/directory is empty
if (!ExportWizard.targetFilePathText.value) {
Util.error(Util.getMessage("error.title"), Util.getMessage("please.select.the.target.directory"), Util.getMessage("button.close.label"));
var errorMessage = (outputType === BaseExporter.OUTPUT_TYPE_FILE) ?
"please.select.the.target.file" :
"please.select.the.target.directory";
Util.error(Util.getMessage("error.title"),
Util.getMessage(errorMessage),
Util.getMessage("button.close.label"));
ExportWizard.targetFilePathText.focus();
return false;
}

// Create file instance to test properties
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(ExportWizard.targetFilePathText.value);

if (!file.parent.exists()) {
Util.error(Util.getMessage("error.title"), Util.getMessage("the.specified.path.does.not.exists"), Util.getMessage("button.close.label"));
// Ensure the given path is valid
var isValidPath = file.parent.exists();
if (file.exists()) {
// Ensure the file/directory is the correct type if it exists
isValidPath &= (outputType === BaseExporter.OUTPUT_TYPE_FILE) ?
file.isFile() : file.isDirectory();
}
if (!isValidPath) {
Util.error(Util.getMessage("error.title"),
Util.getMessage("the.specified.path.is.invalid"),
Util.getMessage("button.close.label"));
ExportWizard.targetFilePathText.focus();

return false;
}
}
Expand Down Expand Up @@ -330,4 +346,3 @@ window.onload = function () {
window.onerror = function (msg, url, lineNumber) {
error([msg, url, lineNumber]);
};

3 changes: 2 additions & 1 deletion app/locale/en-US/pencil.properties
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ select.output.file=Select output file
select.destination=Select destination
please.select.at.least.one.page.to.export=Please select at least one page to export
please.select.the.target.directory=Please select the target directory
the.specified.path.does.not.exists=The specified path does not exists.
please.select.the.target.file=Please select the target file
the.specified.path.is.invalid=The specified path is invalid

#external editor
unsupported.type=Unsupported type: %S
Expand Down

0 comments on commit b1021aa

Please sign in to comment.