Merge pull request #2699 from NativeScript/ios-folders

IOS Known folders will not be created if do not exist
This commit is contained in:
Maya Zhecheva
2016-09-15 18:38:13 +03:00
committed by GitHub
3 changed files with 104 additions and 39 deletions

View File

@ -223,16 +223,19 @@ function _testIOSSpecificKnownFolder(knownFolderName: string){
let createdFile: fs.File; let createdFile: fs.File;
let testFunc = function testFunc(){ let testFunc = function testFunc(){
knownFolder = fs.knownFolders.ios[knownFolderName](); knownFolder = fs.knownFolders.ios[knownFolderName]();
createdFile = knownFolder.getFile("createdFile"); if (knownFolder) {
createdFile.writeTextSync("some text"); createdFile = knownFolder.getFile("createdFile");
createdFile.writeTextSync("some text");
}
}; };
if (platform.isIOS){ if (platform.isIOS){
testFunc(); testFunc();
TKUnit.assertNotNull(knownFolder, `Could not retrieve the ${knownFolderName} known folder.`); if (knownFolder) {
TKUnit.assertTrue(knownFolder.isKnown, `The ${knownFolderName} folder should have its "isKnown" property set to true.`); TKUnit.assertTrue(knownFolder.isKnown, `The ${knownFolderName} folder should have its "isKnown" property set to true.`);
TKUnit.assertNotNull(createdFile, `Could not create a new file in the ${knownFolderName} known folder.`); TKUnit.assertNotNull(createdFile, `Could not create a new file in the ${knownFolderName} known folder.`);
TKUnit.assertTrue(fs.File.exists(createdFile.path), `Could not create a new file in the ${knownFolderName} known folder.`); TKUnit.assertTrue(fs.File.exists(createdFile.path), `Could not create a new file in the ${knownFolderName} known folder.`);
TKUnit.assertEqual(createdFile.readTextSync(), "some text", `The contents of the new file created in the ${knownFolderName} known folder are not as expected.`); TKUnit.assertEqual(createdFile.readTextSync(), "some text", `The contents of the new file created in the ${knownFolderName} known folder are not as expected.`);
}
} }
else { else {
TKUnit.assertThrows(testFunc, TKUnit.assertThrows(testFunc,

View File

@ -107,6 +107,30 @@ export class FileSystemAccess {
} }
} }
public getExistingFolder(path: string, onError?: (error: any) => any): { path: string; name: string } {
try {
var fileManager = utils.ios.getter(NSFileManager, NSFileManager.defaultManager);
var exists = this.folderExists(path);
if (exists) {
var dirName = fileManager.displayNameAtPath(path);
return {
path: path,
name: dirName
};
}
return undefined;
}
catch (ex) {
if (onError) {
onError(new Error("Failed to get folder at path '" + path + "'"));
}
return undefined;
}
}
public eachEntity(path: string, onEntity: (file: { path: string; name: string; extension: string }) => any, onError?: (error: any) => any) { public eachEntity(path: string, onEntity: (file: { path: string; name: string; extension: string }) => any, onError?: (error: any) => any) {
if (!onEntity) { if (!onEntity) {
return; return;

View File

@ -512,10 +512,13 @@ export module knownFolders {
export var library = function(): Folder { export var library = function(): Folder {
_checkPlatform("library"); _checkPlatform("library");
if (!_library) { if (!_library) {
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.LibraryDirectory); let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.LibraryDirectory);
_library = Folder.fromPath(path);
_library[pathProperty] = path; if (existingFolderInfo) {
_library[isKnownProperty] = true; _library = existingFolderInfo.folder;
_library[pathProperty] = existingFolderInfo.path;
_library[isKnownProperty] = true;
}
} }
return _library; return _library;
@ -525,10 +528,13 @@ export module knownFolders {
export var developer = function(): Folder { export var developer = function(): Folder {
_checkPlatform("developer"); _checkPlatform("developer");
if (!_developer) { if (!_developer) {
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DeveloperDirectory); let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DeveloperDirectory);
_developer = Folder.fromPath(path);
_developer[pathProperty] = path; if (existingFolderInfo) {
_developer[isKnownProperty] = true; _developer = existingFolderInfo.folder;
_developer[pathProperty] = existingFolderInfo.path;
_developer[isKnownProperty] = true;
}
} }
return _developer; return _developer;
@ -538,10 +544,13 @@ export module knownFolders {
export var desktop = function(): Folder { export var desktop = function(): Folder {
_checkPlatform("desktop"); _checkPlatform("desktop");
if (!_desktop) { if (!_desktop) {
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DesktopDirectory); let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DesktopDirectory);
_desktop = Folder.fromPath(path);
_desktop[pathProperty] = path; if (existingFolderInfo) {
_desktop[isKnownProperty] = true; _desktop = existingFolderInfo.folder;
_desktop[pathProperty] = existingFolderInfo.path;
_desktop[isKnownProperty] = true;
}
} }
return _desktop; return _desktop;
@ -551,10 +560,13 @@ export module knownFolders {
export var downloads = function(): Folder { export var downloads = function(): Folder {
_checkPlatform("downloads"); _checkPlatform("downloads");
if (!_downloads) { if (!_downloads) {
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DownloadsDirectory); let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DownloadsDirectory);
_downloads = Folder.fromPath(path);
_downloads[pathProperty] = path; if (existingFolderInfo) {
_downloads[isKnownProperty] = true; _downloads = existingFolderInfo.folder;
_downloads[pathProperty] = existingFolderInfo.path;
_downloads[isKnownProperty] = true;
}
} }
return _downloads; return _downloads;
@ -564,10 +576,13 @@ export module knownFolders {
export var movies = function(): Folder { export var movies = function(): Folder {
_checkPlatform("movies"); _checkPlatform("movies");
if (!_movies) { if (!_movies) {
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.MoviesDirectory); let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.MoviesDirectory);
_movies = Folder.fromPath(path);
_movies[pathProperty] = path; if (existingFolderInfo) {
_movies[isKnownProperty] = true; _movies = existingFolderInfo.folder;
_movies[pathProperty] = existingFolderInfo.path;
_movies[isKnownProperty] = true;
}
} }
return _movies; return _movies;
@ -577,10 +592,13 @@ export module knownFolders {
export var music = function(): Folder { export var music = function(): Folder {
_checkPlatform("music"); _checkPlatform("music");
if (!_music) { if (!_music) {
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.MusicDirectory); let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.MusicDirectory);
_music = Folder.fromPath(path);
_music[pathProperty] = path; if (existingFolderInfo) {
_music[isKnownProperty] = true; _music = existingFolderInfo.folder;
_music[pathProperty] = existingFolderInfo.path;
_music[isKnownProperty] = true;
}
} }
return _music; return _music;
@ -590,10 +608,13 @@ export module knownFolders {
export var pictures = function(): Folder { export var pictures = function(): Folder {
_checkPlatform("pictures"); _checkPlatform("pictures");
if (!_pictures) { if (!_pictures) {
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.PicturesDirectory); let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.PicturesDirectory);
_pictures = Folder.fromPath(path);
_pictures[pathProperty] = path; if (existingFolderInfo) {
_pictures[isKnownProperty] = true; _pictures = existingFolderInfo.folder;
_pictures[pathProperty] = existingFolderInfo.path;
_pictures[isKnownProperty] = true;
}
} }
return _pictures; return _pictures;
@ -603,14 +624,31 @@ export module knownFolders {
export var sharedPublic = function(): Folder { export var sharedPublic = function(): Folder {
_checkPlatform("sharedPublic"); _checkPlatform("sharedPublic");
if (!_sharedPublic) { if (!_sharedPublic) {
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.SharedPublicDirectory); let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.SharedPublicDirectory);
_sharedPublic = Folder.fromPath(path);
_sharedPublic[pathProperty] = path; if (existingFolderInfo) {
_sharedPublic[isKnownProperty] = true; _sharedPublic = existingFolderInfo.folder;
_sharedPublic[pathProperty] = existingFolderInfo.path;
_sharedPublic[isKnownProperty] = true;
}
} }
return _sharedPublic; return _sharedPublic;
}; };
function getExistingFolderInfo(pathDirectory: NSSearchPathDirectory): { folder: Folder; path: string } {
var fileAccess = (<any>getFileAccess());
var folderPath = fileAccess.getKnownPath(pathDirectory);
var folderInfo = fileAccess.getExistingFolder(folderPath);
if (folderInfo) {
return {
folder: createFolder(folderInfo),
path: folderPath
};
}
return undefined;
}
} }
} }