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 7efe112
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 106 deletions.
20 changes: 13 additions & 7 deletions src/filesystem/impls/filer/ArchiveUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ 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);
}
Expand Down Expand Up @@ -108,6 +108,10 @@ define(function (require, exports, module) {
function decompress(path, callback) {
var basedir = Path.dirname(path.absPath);

function writeFile() {
FilerUtils.writeFileAsBinary(path.absPath, path.data, callback);
}

if(path.isDirectory) {
fs.mkdirp(path.absPath, callback);
} else {
Expand All @@ -124,10 +128,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 +142,7 @@ define(function (require, exports, module) {
return callback(err);
}

_refreshFilesystem(function(err) {
_refreshFileTree(function(err) {
if(err) {
return callback(err);
}
Expand All @@ -147,7 +151,8 @@ define(function (require, exports, module) {
DefaultDialogs.DIALOG_ID_INFO,
Strings.DND_SUCCESS_UNZIP_TITLE
).getPromise().then(function() {
callback(null);
// We don't bother sending a list of files to open for archives.
callback(null, []);
}, callback);
});
});
Expand Down Expand Up @@ -272,7 +277,7 @@ define(function (require, exports, module) {
untarWorker.terminate();
untarWorker = null;

_refreshFilesystem(function(err) {
_refreshFileTree(function(err) {
if(err) {
return callback(err);
}
Expand All @@ -281,7 +286,8 @@ define(function (require, exports, module) {
DefaultDialogs.DIALOG_ID_INFO,
Strings.DND_SUCCESS_UNTAR_TITLE
).getPromise().then(function() {
callback(null);
// We don't bother sending a list of files to open for archives.
callback(null, []);
}, 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
17 changes: 14 additions & 3 deletions src/filesystem/impls/filer/lib/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ define(function (require, exports, module) {
}

module.exports = {
mimeFromExt: function(ext) {
return _lookupMimeType(ext);
},

// Whether or not we can provide a useful editor for this, and should try to open.
isEditable: function(ext) {
return this.isUTF8Encoded(ext) ||
this.isImage(ext) ||
this.isAudio(ext) ||
this.isVideo(ext) ||
this.isFont(ext) ||
this.isPDF(ext);
},

isImage: function(ext) {
var info = new FileInfo(ext);
return info.type === "image";
Expand Down Expand Up @@ -193,9 +207,6 @@ define(function (require, exports, module) {
}
},

mimeFromExt: function(ext) {
return _lookupMimeType(ext);
},

// Whether or not this is a text/* mime type
isTextType: function(mime) {
Expand Down
Loading

0 comments on commit 7efe112

Please sign in to comment.