Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
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
172 changes: 121 additions & 51 deletions src/NativeFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,86 @@ window.NativeFileSystem = {
* @param {function} resultCallback
* @constructor
*/
showOpenDialog: function ( allowMultipleSelection,
showOpenDialog: function ( allowMultipleSelection,
chooseDirectories,
title,
initialPath,
fileTypes,
resultCallback ) {
if( !resultCallback )
return null;
successCallback,
errorCallback ) {
if( !successCallback )
return;

var files = brackets.file.showOpenDialog( allowMultipleSelection,
chooseDirectories,
title,
initialPath,
fileTypes,
resultCallback );
var files = brackets.fs.showOpenDialog( allowMultipleSelection,
chooseDirectories,
title,
initialPath,
fileTypes,
function( err, data ){
if( ! err )
successCallback( data );
else if (errorCallback)
errorCallback(NativeFileSystem._nativeToFileError(err));
});

},


/** requestNativeFileSystem
*
* @param {string} path
* @param {function} successCallback
* @param {function} errorCallback
*/
requestNativeFileSystem: function( path, successCallback, errorCallback ){

// TODO: assumes path is a directory right now. Need to error check
// TODO: don't actually need to get the listing here, but should verify the directory exists
var entryList = brackets.file.getDirectoryListing(path);
if (entryList) {
var files = JSON.parse(entryList);
var root = new DirectoryEntry( path );
return root;
}
else {
return null;
brackets.fs.stat(path, function( err, data ){
if( !err ){
var root = new DirectoryEntry( path );
successCallback( root );
}
else if (errorCallback) {
errorCallback(NativeFileSystem._nativeToFileError(err));
}
});
},

_nativeToFileError: function(nativeErr) {
// The HTML file spec says SECURITY_ERR is a catch-all to be used in situations
// not covered by other error codes.
var error = FileError.SECURITY_ERR;

switch (nativeErr) {
// We map ERR_UNKNOWN and ERR_INVALID_PARAMS to SECURITY_ERR,
// since there aren't specific mappings for these.
case brackets.fs.ERR_UNKNOWN:
case brackets.fs.ERR_INVALID_PARAMS:
error = FileError.SECURITY_ERR;
break;

case brackets.fs.ERR_NOT_FOUND:
error = FileError.NOT_FOUND_ERR;
break;
case brackets.fs.ERR_CANT_READ:
error = FileError.NOT_READABLE_ERR;
break;

// It might seem like you should use FileError.ENCODING_ERR for this,
// but according to the spec that's for malformed URLs.
case brackets.fs.ERR_UNSUPPORTED_ENCODING:
error = FileError.SECURITY_ERR;
break;

case brackets.fs.ERR_CANT_WRITE:
error = FileError.NO_MODIFICATION_ALLOWED_ERR;
break;
case brackets.fs.ERR_OUT_OF_SPACE:
error = FileError.QUOTA_EXCEEDED_ERR;
break;
}
}

return new FileError(error);
}
};


/** class: Entry
*
* @param {string} name
Expand All @@ -73,7 +111,7 @@ Entry = function( fullPath, isDirectory) {
this.name = pathParts.pop();
}

// IMPLEMENT LATERvar filesystem;
// IMPLEMENT LATER var filesystem;
// IMPLEMENT LATER void moveTo (DirectoryEntry parent, optional DOMString newName, optional EntryCallback successCallback, optional ErrorCallback errorCallback);
// IMPLEMENT LATER void copyTo (DirectoryEntry parent, optional DOMString newName, optional EntryCallback successCallback, optional ErrorCallback errorCallback);
// IMPLEMENT LATER DOMString toURL (optional DOMString mimeType);
Expand Down Expand Up @@ -131,39 +169,71 @@ DirectoryReader = function() {
};


/** readEntires
/** readEntries
*
* @param {function} successCallback
* @param {function} errorCallback
* @returns {Entry[]}
*/
DirectoryReader.prototype.readEntries = function( successCallback, errorCallback ){
var rootPath = this._directory.fullPath;
var jsonList = brackets.file.getDirectoryListing( rootPath );
var nameList = JSON.parse(jsonList);

// Create entries for each name
var entries = [];
nameList.forEach(function(item){
// Ignore names starting with "."
if (item.indexOf(".") != 0) {
var itemFullPath = rootPath + "/" + item;

if( brackets.file.isDirectory( itemFullPath ) ) {
entries.push( new DirectoryEntry( itemFullPath ) );
}
else {
entries.push( new FileEntry( itemFullPath ) );
}
var jsonList = brackets.fs.readdir( rootPath, function( err, filelist ) {
if( ! err ){
// Create entries for each name
var entries = [];
filelist.forEach(function(item){
var itemFullPath = rootPath + "/" + item;

brackets.fs.stat( itemFullPath, function( err, statData) {

if( !err ){
if( statData.isDirectory( itemFullPath ) )
entries.push( new DirectoryEntry( itemFullPath ) );
else if( statData.isFile( itemFullPath ) )
entries.push( new FileEntry( itemFullPath ) );
}
else if (errorCallback) {
errorCallback(NativeFileSystem._nativeToFileError(err));
}

})
});

successCallback( entries );
}
});



successCallback( entries );

// TODO: error handling
else if (errorCallback) {
errorCallback(NativeFileSystem._nativeToFileError(err));
}
});
};

/** class: FileError
*
* Implementation of HTML file API error code return class. Note that the
* various HTML file API specs are not consistent in their definition of
* some error code values like ABORT_ERR; I'm using the definitions from
* the Directories and System spec since it seems to be the most
* comprehensive.
*
* @constructor
* @param {number} code The error code to return with this FileError. Must be
* one of the codes defined in the FileError class.
*/
FileError = function(code) {
this.code = code || 0;
};


$.extend(FileError, {
NOT_FOUND_ERR: 1,
SECURITY_ERR: 2,
ABORT_ERR: 3,
NOT_READABLE_ERR: 4,
ENCODING_ERR: 5,
NO_MODIFICATION_ALLOWED_ERR: 6,
INVALID_STATE_ERR: 7,
SYNTAX_ERR: 8,
INVALID_MODIFICATION_ERR: 9,
QUOTA_EXCEEDED_ERR: 10,
TYPE_MISMATCH_ERR: 11,
PATH_EXISTS_ERR: 12
});
61 changes: 58 additions & 3 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,50 @@ $(document).ready(function() {
// Temporary button to test file directory traversa;
$("#menu-file-open").click(function(){
if (!inBrowser) {
window.NativeFileSystem.showOpenDialog(false, true, "Choose a folder", null, null, showOpenDialogCallback);
window.NativeFileSystem.showOpenDialog( false, true, "Choose a folder",
null, null,
showOpenDialogSuccessCallback,
showOpenDialogErrorCallback);

/*
// TEST CODE
var reader = new FileReader();
reader.onerror = errorHandler;

reader.onabort = function(e) {
alert('File read cancelled');
};

reader.onloadstart = function(e) {
console.log( "loading" );
};


// Read in the image file as a binary string.
reader.readAsText(file);


function errorHandler(evt) {
switch(evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
alert('File Not Found!');
break;
case evt.target.error.NOT_READABLE_ERR:
alert('File is not readable');
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
alert('An error occurred reading this file.');
};
}

*/

}
});


// Implements the 'Run Tests' menu to bring up the Jasmine unit test window
var testWindow = null;
$("#menu-runtests").click(function(){
Expand All @@ -34,21 +74,36 @@ $(document).ready(function() {
}
});

function showOpenDialogCallback( files ) {
function showOpenDialogErrorCallback( err ){
console.log( err )
}

function showOpenDialogSuccessCallback( files ) {
var folderName = files instanceof Array ? files[0] : files;
var nestingLevel = 0;

if (folderName != "") {
var rootEntry = window.NativeFileSystem.requestNativeFileSystem( folderName, null, null ); // TODO: add callbacks
window.NativeFileSystem.requestNativeFileSystem( folderName,
requestNativeFileSystemSuccessCB, requestNativeFileSystemErrorCB ); // TODO: add callbacks


}

function requestNativeFileSystemSuccessCB( rootEntry ){
var nestingLevel = 0;

if( rootEntry && rootEntry.isDirectory )
readDirectory( rootEntry );
}

function requestNativeFileSystemErrorCB( err){
console.log( err );
}


// Test directory traversal
function readDirectory( entry ){

var reader = entry.createReader();
reader.readEntries( dirReaderSuccessCB, dirReaderErrorCB);
}
Expand Down
2 changes: 1 addition & 1 deletion test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<script type="text/javascript" src="lib/jasmine-jquery-1.3.1.js"></script>

<!-- include spec files here... -->
<script type="text/javascript" src="spec/SampleTest.js"></script>
<script type="text/javascript" src="spec/Editor-test.js"></script>
<script type="text/javascript" src="spec/NativeFileSystem-test.js"></script>

<!-- include source files here... -->
Expand Down
20 changes: 2 additions & 18 deletions test/spec/SampleTest.js → test/spec/Editor-test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
describe("Brackets", function(){
var content = 'Brackets is going to be awesome!"\n';

it("should be awesome", function() {
this.addMatchers({
toBeAwesome: function(expected) {
return true;
}
});

expect("Brackets").toBeAwesome();
});
describe("Editor", function(){
var content = 'Brackets is going to be awesome!\n';

describe("CodeMirror", function() {
var myCodeMirror;
Expand All @@ -33,9 +23,3 @@ describe("Brackets", function(){
});
});
});

describe("Failure", function() {
it("is expected sometimes", function() {
expect("failure").toEqual("sometimes");
});
});
Loading