mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00
IOS Known folders are not created if do not exist
This commit is contained in:
@ -222,18 +222,21 @@ function _testIOSSpecificKnownFolder(knownFolderName: string){
|
||||
let knownFolder: fs.Folder;
|
||||
let createdFile: fs.File;
|
||||
let testFunc = function testFunc(){
|
||||
if (knownFolder) {
|
||||
knownFolder = fs.knownFolders.ios[knownFolderName]();
|
||||
createdFile = knownFolder.getFile("createdFile");
|
||||
createdFile.writeTextSync("some text");
|
||||
}
|
||||
};
|
||||
if (platform.isIOS){
|
||||
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.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.assertEqual(createdFile.readTextSync(), "some text", `The contents of the new file created in the ${knownFolderName} known folder are not as expected.`);
|
||||
}
|
||||
}
|
||||
else {
|
||||
TKUnit.assertThrows(testFunc,
|
||||
`Trying to retrieve the ${knownFolderName} known folder on a platform different from iOS should throw!`,
|
||||
|
@ -107,6 +107,30 @@ export class FileSystemAccess {
|
||||
}
|
||||
}
|
||||
|
||||
public getExistingFolder(path: string, onError?: (error: any) => any): { path: string; name: string } {
|
||||
try {
|
||||
var fileManager = 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) {
|
||||
if (!onEntity) {
|
||||
return;
|
||||
|
@ -512,11 +512,14 @@ export module knownFolders {
|
||||
export var library = function(): Folder {
|
||||
_checkPlatform("library");
|
||||
if (!_library) {
|
||||
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.LibraryDirectory);
|
||||
_library = Folder.fromPath(path);
|
||||
_library[pathProperty] = path;
|
||||
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.LibraryDirectory);
|
||||
|
||||
if (existingFolderInfo) {
|
||||
_library = existingFolderInfo.folder;
|
||||
_library[pathProperty] = existingFolderInfo.path;
|
||||
_library[isKnownProperty] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return _library;
|
||||
};
|
||||
@ -525,11 +528,14 @@ export module knownFolders {
|
||||
export var developer = function(): Folder {
|
||||
_checkPlatform("developer");
|
||||
if (!_developer) {
|
||||
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DeveloperDirectory);
|
||||
_developer = Folder.fromPath(path);
|
||||
_developer[pathProperty] = path;
|
||||
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DeveloperDirectory);
|
||||
|
||||
if (existingFolderInfo) {
|
||||
_developer = existingFolderInfo.folder;
|
||||
_developer[pathProperty] = existingFolderInfo.path;
|
||||
_developer[isKnownProperty] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return _developer;
|
||||
};
|
||||
@ -538,11 +544,14 @@ export module knownFolders {
|
||||
export var desktop = function(): Folder {
|
||||
_checkPlatform("desktop");
|
||||
if (!_desktop) {
|
||||
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DesktopDirectory);
|
||||
_desktop = Folder.fromPath(path);
|
||||
_desktop[pathProperty] = path;
|
||||
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DesktopDirectory);
|
||||
|
||||
if (existingFolderInfo) {
|
||||
_desktop = existingFolderInfo.folder;
|
||||
_desktop[pathProperty] = existingFolderInfo.path;
|
||||
_desktop[isKnownProperty] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return _desktop;
|
||||
};
|
||||
@ -551,11 +560,14 @@ export module knownFolders {
|
||||
export var downloads = function(): Folder {
|
||||
_checkPlatform("downloads");
|
||||
if (!_downloads) {
|
||||
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DownloadsDirectory);
|
||||
_downloads = Folder.fromPath(path);
|
||||
_downloads[pathProperty] = path;
|
||||
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DownloadsDirectory);
|
||||
|
||||
if (existingFolderInfo) {
|
||||
_downloads = existingFolderInfo.folder;
|
||||
_downloads[pathProperty] = existingFolderInfo.path;
|
||||
_downloads[isKnownProperty] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return _downloads;
|
||||
};
|
||||
@ -564,11 +576,14 @@ export module knownFolders {
|
||||
export var movies = function(): Folder {
|
||||
_checkPlatform("movies");
|
||||
if (!_movies) {
|
||||
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.MoviesDirectory);
|
||||
_movies = Folder.fromPath(path);
|
||||
_movies[pathProperty] = path;
|
||||
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.MoviesDirectory);
|
||||
|
||||
if (existingFolderInfo) {
|
||||
_movies = existingFolderInfo.folder;
|
||||
_movies[pathProperty] = existingFolderInfo.path;
|
||||
_movies[isKnownProperty] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return _movies;
|
||||
};
|
||||
@ -577,11 +592,14 @@ export module knownFolders {
|
||||
export var music = function(): Folder {
|
||||
_checkPlatform("music");
|
||||
if (!_music) {
|
||||
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.MusicDirectory);
|
||||
_music = Folder.fromPath(path);
|
||||
_music[pathProperty] = path;
|
||||
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.MusicDirectory);
|
||||
|
||||
if (existingFolderInfo) {
|
||||
_music = existingFolderInfo.folder;
|
||||
_music[pathProperty] = existingFolderInfo.path;
|
||||
_music[isKnownProperty] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return _music;
|
||||
};
|
||||
@ -590,11 +608,14 @@ export module knownFolders {
|
||||
export var pictures = function(): Folder {
|
||||
_checkPlatform("pictures");
|
||||
if (!_pictures) {
|
||||
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.PicturesDirectory);
|
||||
_pictures = Folder.fromPath(path);
|
||||
_pictures[pathProperty] = path;
|
||||
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.PicturesDirectory);
|
||||
|
||||
if (existingFolderInfo) {
|
||||
_pictures = existingFolderInfo.folder;
|
||||
_pictures[pathProperty] = existingFolderInfo.path;
|
||||
_pictures[isKnownProperty] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return _pictures;
|
||||
};
|
||||
@ -603,14 +624,31 @@ export module knownFolders {
|
||||
export var sharedPublic = function(): Folder {
|
||||
_checkPlatform("sharedPublic");
|
||||
if (!_sharedPublic) {
|
||||
var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.SharedPublicDirectory);
|
||||
_sharedPublic = Folder.fromPath(path);
|
||||
_sharedPublic[pathProperty] = path;
|
||||
let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.SharedPublicDirectory);
|
||||
|
||||
if (existingFolderInfo) {
|
||||
_sharedPublic = existingFolderInfo.folder;
|
||||
_sharedPublic[pathProperty] = existingFolderInfo.path;
|
||||
_sharedPublic[isKnownProperty] = true;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user