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,17 +223,20 @@ 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]();
if (knownFolder) {
createdFile = knownFolder.getFile("createdFile"); createdFile = knownFolder.getFile("createdFile");
createdFile.writeTextSync("some text"); 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,
`Trying to retrieve the ${knownFolderName} known folder on a platform different from iOS should throw!`, `Trying to retrieve the ${knownFolderName} known folder on a platform different from iOS should throw!`,

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,11 +512,14 @@ 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 = existingFolderInfo.folder;
_library[pathProperty] = existingFolderInfo.path;
_library[isKnownProperty] = true; _library[isKnownProperty] = true;
} }
}
return _library; return _library;
}; };
@ -525,11 +528,14 @@ 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 = existingFolderInfo.folder;
_developer[pathProperty] = existingFolderInfo.path;
_developer[isKnownProperty] = true; _developer[isKnownProperty] = true;
} }
}
return _developer; return _developer;
}; };
@ -538,11 +544,14 @@ 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 = existingFolderInfo.folder;
_desktop[pathProperty] = existingFolderInfo.path;
_desktop[isKnownProperty] = true; _desktop[isKnownProperty] = true;
} }
}
return _desktop; return _desktop;
}; };
@ -551,11 +560,14 @@ 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 = existingFolderInfo.folder;
_downloads[pathProperty] = existingFolderInfo.path;
_downloads[isKnownProperty] = true; _downloads[isKnownProperty] = true;
} }
}
return _downloads; return _downloads;
}; };
@ -564,11 +576,14 @@ 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 = existingFolderInfo.folder;
_movies[pathProperty] = existingFolderInfo.path;
_movies[isKnownProperty] = true; _movies[isKnownProperty] = true;
} }
}
return _movies; return _movies;
}; };
@ -577,11 +592,14 @@ 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 = existingFolderInfo.folder;
_music[pathProperty] = existingFolderInfo.path;
_music[isKnownProperty] = true; _music[isKnownProperty] = true;
} }
}
return _music; return _music;
}; };
@ -590,11 +608,14 @@ 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 = existingFolderInfo.folder;
_pictures[pathProperty] = existingFolderInfo.path;
_pictures[isKnownProperty] = true; _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 = existingFolderInfo.folder;
_sharedPublic[pathProperty] = existingFolderInfo.path;
_sharedPublic[isKnownProperty] = true; _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;
}
} }
} }