Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mozilla/thimble.mozilla.org#1564 - Check for existing file with name before creating a new file #566

Merged
merged 2 commits into from
Nov 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/document/DocumentCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,14 +572,23 @@ define(function (require, exports, module) {
* @private
* Ensures the suggested file name doesn't already exit.
* @param {Directory} dir The directory to use
* @param {string} baseFileName The base to start with, "-n" will get appended to make unique
* @param {object} fileNameOptions Properties about the filename that should be used. The object should contain these properties:
* baseFileName: The base to start with, "-n" will get appended to make unique
* extension: Optional extension to be used in the filename
* @param {boolean} isFolder True if the suggestion is for a folder name
* @return {$.Promise} a jQuery promise that will be resolved with a unique name starting with
* the given base name
*/
function _getUntitledFileSuggestion(dir, baseFileName, isFolder) {
var suggestedName = baseFileName + "-" + _nextUntitledIndexToUse++,
deferred = $.Deferred();
function _getUntitledFileSuggestion(dir, fileNameOptions, isFolder) {
var baseFileName = fileNameOptions.baseFileName,
extension = fileNameOptions.extension ? fileNameOptions.extension.replace(/^\.?/, ".") : "";

if(isFolder) {
extension = "";
}

var suggestedName = baseFileName + "-" + (_nextUntitledIndexToUse++) + extension,
deferred = $.Deferred();

if (_nextUntitledIndexToUse > 9999) {
//we've tried this enough
Expand All @@ -591,7 +600,7 @@ define(function (require, exports, module) {

entry.exists(function (err, exists) {
if (err || exists) {
_getUntitledFileSuggestion(dir, baseFileName, isFolder)
_getUntitledFileSuggestion(dir, fileNameOptions, isFolder)
.then(deferred.resolve, deferred.reject);
} else {
deferred.resolve(suggestedName);
Expand Down Expand Up @@ -649,7 +658,7 @@ define(function (require, exports, module) {
.always(function () { fileNewInProgress = false; });
}

return _getUntitledFileSuggestion(baseDirEntry, Strings.UNTITLED, isFolder)
return _getUntitledFileSuggestion(baseDirEntry, { baseFileName: Strings.UNTITLED }, isFolder)
.then(createWithSuggestedName, createWithSuggestedName.bind(undefined, Strings.UNTITLED));
}

Expand Down Expand Up @@ -714,9 +723,12 @@ define(function (require, exports, module) {
options.ext = options.ext ? options.ext.replace(/^\.?/, ".") : "";
options.basenamePrefix = options.basenamePrefix || Strings.UNTITLED;

_getUntitledFileSuggestion({fullPath: root + "/"}, options.basenamePrefix, false)
_getUntitledFileSuggestion(
{ fullPath: root + "/" },
{ baseFileName: options.basenamePrefix, extension: options.ext },
false
)
.then(function(filename) {
filename = filename + options.ext;
writeContents(filename, options.contents);
});
}
Expand Down