Skip to content

Commit

Permalink
Always open dropped files if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Dec 19, 2017
1 parent 21e91f1 commit a74e06e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 94 deletions.
34 changes: 26 additions & 8 deletions src/filesystem/impls/filer/ArchiveUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ define(function (require, exports, module) {
return false;
}

function _refreshFilesystem(callback) {
function _refreshFileTree(callback) {
// Update the file tree to show the new files
CommandManager.execute(Commands.FILE_REFRESH).always(callback);
}

// zipfile can be a path (string) to a zipfile, or raw binary data.
function unzip(zipfile, options, callback) {
var projectPrefix = StartupState.project("zipFilenamePrefix").replace(/\/?$/, "/");
var pathsToOpen = [];

if(typeof options === 'function') {
callback = options;
Expand Down Expand Up @@ -108,6 +109,16 @@ define(function (require, exports, module) {
function decompress(path, callback) {
var basedir = Path.dirname(path.absPath);

function writeFile() {
FilerUtils
.writeFileAsBinary(path.absPath, path.data)
.done(function() {
pathsToOpen.push(path.absPath);
callback();
})
.fail(callback);
}

if(path.isDirectory) {
fs.mkdirp(path.absPath, callback);
} else {
Expand All @@ -124,10 +135,10 @@ define(function (require, exports, module) {
return callback(err);
}

FilerUtils.writeFileAsBinary(path.absPath, path.data, callback);
writeFile();
});
} else {
FilerUtils.writeFileAsBinary(path.absPath, path.data, callback);
writeFile();
}
});
}
Expand All @@ -138,7 +149,7 @@ define(function (require, exports, module) {
return callback(err);
}

_refreshFilesystem(function(err) {
_refreshFileTree(function(err) {
if(err) {
return callback(err);
}
Expand All @@ -147,7 +158,7 @@ define(function (require, exports, module) {
DefaultDialogs.DIALOG_ID_INFO,
Strings.DND_SUCCESS_UNZIP_TITLE
).getPromise().then(function() {
callback(null);
callback(null, pathsToOpen);
}, callback);
});
});
Expand Down Expand Up @@ -247,6 +258,7 @@ define(function (require, exports, module) {
options = options || {};
callback = callback || function(){};

var pathsToOpen = [];
var untarWorker = new Worker("thirdparty/bitjs/bitjs-untar.min.js");
var root = options.root || StartupState.project("root");
var pending = null;
Expand All @@ -264,15 +276,21 @@ define(function (require, exports, module) {
return callback(err);
}

FilerUtils.writeFileAsBinary(path, new Buffer(data), callback);
FilerUtils
.writeFileAsBinary(path, new Buffer(data))
.done(function() {
pathsToOpen.push(path.absPath);
callback();
})
.fail(callback);
});
}

function finish(err) {
untarWorker.terminate();
untarWorker = null;

_refreshFilesystem(function(err) {
_refreshFileTree(function(err) {
if(err) {
return callback(err);
}
Expand All @@ -281,7 +299,7 @@ define(function (require, exports, module) {
DefaultDialogs.DIALOG_ID_INFO,
Strings.DND_SUCCESS_UNTAR_TITLE
).getPromise().then(function() {
callback(null);
callback(null, pathsToOpen);
}, callback);
});
}
Expand Down
65 changes: 25 additions & 40 deletions src/filesystem/impls/filer/lib/LegacyFileImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ define(function (require, exports, module) {
FileSystem = require("filesystem/FileSystem"),
FileUtils = require("file/FileUtils"),
Strings = require("strings"),
Filer = require("filesystem/impls/filer/BracketsFiler"),
Path = Filer.Path,
FilerUtils = require("filesystem/impls/filer/FilerUtils"),
Buffer = FilerUtils.Buffer,
Path = FilerUtils.Path,
Content = require("filesystem/impls/filer/lib/content"),
ArchiveUtils = require("filesystem/impls/filer/ArchiveUtils");

Expand All @@ -43,64 +44,58 @@ define(function (require, exports, module) {
// We want event.dataTransfer.files for legacy browsers.
LegacyFileImport.prototype.import = function(source, parentPath, callback) {
var files = source instanceof DataTransfer ? source.files : source;
var pathList = [];
var pathsToOpen = [];
var errorList = [];

if (!(files && files.length)) {
return callback();
return callback(null, []);
}

function shouldOpenFile(filename, encoding) {
return Content.isImage(Path.extname(filename)) || encoding === "utf8";
}

function handleRegularFile(deferred, file, filename, buffer, encoding) {
function handleRegularFile(deferred, filename, buffer) {
// Don't write thing like .DS_Store, thumbs.db, etc.
if(ArchiveUtils.skipFile(filename)) {
deferred.resolve();
return;
}

file.write(buffer, {encoding: encoding}, function(err) {
if (err) {
errorList.push({path: filename, error: "unable to write file: " + err.message || ""});
FilerUtils
.writeFileAsBinary(filename, buffer)
.done(function() {
pathsToOpen.push(filename);
deferred.resolve();
})
.fail(function(err) {
errorList.push({path: filename, error: err.message || "unable to write file" });
deferred.reject(err);
return;
}

// See if this file is worth trying to open in the editor or not
if(shouldOpenFile(filename, encoding)) {
pathList.push(filename);
}

deferred.resolve();
});
});
}

function handleZipFile(deferred, file, filename, buffer, encoding) {
var basename = Path.basename(filename);

ArchiveUtils.unzip(buffer, { root: parentPath }, function(err) {
ArchiveUtils.unzip(buffer, { root: parentPath }, function(err, unzippedPaths) {
if (err) {
errorList.push({path: filename, error: Strings.DND_ERROR_UNZIP});
deferred.reject(err);
return;
}

pathsToOpen = pathsToOpen.concat(unzippedPaths);
deferred.resolve();
});
}

function handleTarFile(deferred, file, filename, buffer, encoding) {
var basename = Path.basename(filename);

ArchiveUtils.untar(buffer, { root: parentPath }, function(err) {
ArchiveUtils.untar(buffer, { root: parentPath }, function(err, untarredPaths) {
if (err) {
errorList.push({path: filename, error: Strings.DND_ERROR_UNTAR});
deferred.reject(err);
return;
}

pathsToOpen = pathsToOpen.concat(untarredPaths);
deferred.resolve();
});
}
Expand Down Expand Up @@ -156,26 +151,16 @@ define(function (require, exports, module) {
delete reader.onload;

var filename = Path.join(parentPath, item.name);
var file = FileSystem.getFileForPath(filename);
var ext = Path.extname(filename).toLowerCase();

// Create a Filer Buffer, and determine the proper encoding. We
// use the extension, and also the OS provided mime type for clues.
var buffer = new Filer.Buffer(e.target.result);
var utf8FromExt = Content.isUTF8Encoded(ext);
var utf8FromOS = Content.isTextType(item.type);
var encoding = utf8FromExt || utf8FromOS ? 'utf8' : null;
if(encoding === 'utf8') {
buffer = buffer.toString();
}
var ext = FilerUtils.normalizeExtension(Path.extname(filename));
var buffer = new Buffer(e.target.result);

// Special-case .zip files, so we can offer to extract the contents
if(ext === ".zip") {
handleZipFile(deferred, file, filename, buffer, encoding);
handleZipFile(deferred, filename, buffer);
} else if(ext === ".tar") {
handleTarFile(deferred, file, filename, buffer, encoding);
handleTarFile(deferred, filename, buffer);
} else {
handleRegularFile(deferred, file, filename, buffer, encoding);
handleRegularFile(deferred, filename, buffer);
}
};

Expand All @@ -193,7 +178,7 @@ define(function (require, exports, module) {

Async.doSequentially(prepareDropPaths(files), maybeImportFile, false)
.done(function() {
callback(null, pathList);
callback(null, pathsToOpen);
})
.fail(function() {
callback(errorList);
Expand Down
65 changes: 26 additions & 39 deletions src/filesystem/impls/filer/lib/WebKitFileImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ define(function (require, exports, module) {
Async = require("utils/Async"),
FileSystem = require("filesystem/FileSystem"),
FileUtils = require("file/FileUtils"),
FilerUtils = require("filesystem/impls/filer/FilerUtils"),
Path = FilerUtils.Path,
Buffer = FilerUtils.Buffer,
Strings = require("strings"),
Filer = require("filesystem/impls/filer/BracketsFiler"),
Path = Filer.Path,
Content = require("filesystem/impls/filer/lib/content"),
ArchiveUtils = require("filesystem/impls/filer/ArchiveUtils");

Expand All @@ -43,25 +44,21 @@ define(function (require, exports, module) {
// We want event.dataTransfer.items for WebKit style browsers
WebKitFileImport.prototype.import = function(source, parentPath, callback) {
var items = source instanceof DataTransfer ? source.items : source;
var pathList = [];
var pathsToOpen = [];
var errorList = [];
var started = 0;
var completed = 0;

if (!(items && items.length)) {
return callback();
}

function shouldOpenFile(filename, encoding) {
return Content.isImage(Path.extname(filename)) || encoding === "utf8";
return callback(null, []);
}

function checkDone() {
if(started === completed) {
if(errorList.length) {
callback(errorList);
} else {
callback(null, pathList);
callback(null, pathsToOpen);
}
}
}
Expand Down Expand Up @@ -123,44 +120,42 @@ define(function (require, exports, module) {
});
}

function handleRegularFile(deferred, file, filename, buffer, encoding) {
file.write(buffer, {encoding: encoding}, function(err) {
if (err) {
function handleRegularFile(deferred, filename, buffer) {
FilerUtils
.writeFileAsBinary(filename, buffer)
.done(function() {
pathsToOpen.push(filename);
onSuccess(deferred);
})
.fail(function(err) {
onError(deferred, filename, err);
return;
}

// See if this file is worth trying to open in the editor or not
if(shouldOpenFile(filename, encoding)) {
pathList.push(filename);
}

onSuccess(deferred);
});
});
}

function handleZipFile(deferred, file, filename, buffer, encoding) {
function handleZipFile(deferred, filename, buffer) {
var basename = Path.basename(filename);

ArchiveUtils.unzip(buffer, { root: parentPath }, function(err) {
ArchiveUtils.unzip(buffer, { root: parentPath }, function(err, unzippedPaths) {
if (err) {
onError(deferred, filename, new Error(Strings.DND_ERROR_UNZIP));
return;
}

pathsToOpen = pathsToOpen.concat(unzippedPaths);
onSuccess(deferred);
});
}

function handleTarFile(deferred, file, filename, buffer, encoding) {
function handleTarFile(deferred, filename, buffer) {
var basename = Path.basename(filename);

ArchiveUtils.untar(buffer, { root: parentPath }, function(err) {
ArchiveUtils.untar(buffer, { root: parentPath }, function(err, untarredPaths) {
if (err) {
onError(deferred, filename, new Error(Strings.DND_ERROR_UNTAR));
return;
}

pathsToOpen = pathsToOpen.concat(untarredPaths);
onSuccess(deferred);
});
}
Expand All @@ -184,16 +179,8 @@ define(function (require, exports, module) {
delete reader.onload;

var filename = Path.join(parentPath, entry.name);
var file = FileSystem.getFileForPath(filename);
var ext = Path.extname(filename).toLowerCase();

// Create a Filer Buffer, and determine the proper encoding.
var buffer = new Filer.Buffer(e.target.result);
var utf8FromExt = Content.isUTF8Encoded(ext);
var encoding = utf8FromExt ? "utf8" : null;
if(utf8FromExt) {
buffer = buffer.toString();
}
var ext = FilerUtils.normalizeExtension(Path.extname(filename));
var buffer = new Buffer(e.target.result);

// Don't bother writing things like .DS_Store, thumbs.db, etc.
if(ArchiveUtils.skipFile(filename)) {
Expand All @@ -210,11 +197,11 @@ define(function (require, exports, module) {

// Special-case .zip files, so we can offer to extract the contents
if(ext === ".zip") {
handleZipFile(deferred, file, filename, buffer, encoding);
handleZipFile(deferred, filename, buffer);
} else if(ext === ".tar") {
handleTarFile(deferred, file, filename, buffer, encoding);
handleTarFile(deferred, filename, buffer);
} else {
handleRegularFile(deferred, file, filename, buffer, encoding);
handleRegularFile(deferred, filename, buffer);
}
};

Expand Down
Loading

0 comments on commit a74e06e

Please sign in to comment.