| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -223,16 +223,19 @@ function _testIOSSpecificKnownFolder(knownFolderName: string){ | |||
| 223 | 223 | let createdFile: fs.File; | |
| 224 | 224 | let testFunc = function testFunc(){ | |
| 225 | 225 | knownFolder = fs.knownFolders.ios[knownFolderName](); | |
| 226 | - createdFile = knownFolder.getFile("createdFile"); | ||
| 227 | - createdFile.writeTextSync("some text"); | ||
| 226 | + if (knownFolder) { | ||
| 227 | + createdFile = knownFolder.getFile("createdFile"); | ||
| 228 | + createdFile.writeTextSync("some text"); | ||
| 229 | + } | ||
| 228 | 230 | }; | |
| 229 | 231 | if (platform.isIOS){ | |
| 230 | 232 | testFunc(); | |
| 231 | - TKUnit.assertNotNull(knownFolder, `Could not retrieve the ${knownFolderName} known folder.`); | ||
| 232 | - TKUnit.assertTrue(knownFolder.isKnown, `The ${knownFolderName} folder should have its "isKnown" property set to true.`); | ||
| 233 | - TKUnit.assertNotNull(createdFile, `Could not create a new file in the ${knownFolderName} known folder.`); | ||
| 234 | - TKUnit.assertTrue(fs.File.exists(createdFile.path), `Could not create a new file in the ${knownFolderName} known folder.`); | ||
| 235 | - TKUnit.assertEqual(createdFile.readTextSync(), "some text", `The contents of the new file created in the ${knownFolderName} known folder are not as expected.`); | ||
| 233 | + if (knownFolder) { | ||
| 234 | + TKUnit.assertTrue(knownFolder.isKnown, `The ${knownFolderName} folder should have its "isKnown" property set to true.`); | ||
| 235 | + TKUnit.assertNotNull(createdFile, `Could not create a new file in the ${knownFolderName} known folder.`); | ||
| 236 | + TKUnit.assertTrue(fs.File.exists(createdFile.path), `Could not create a new file in the ${knownFolderName} known folder.`); | ||
| 237 | + TKUnit.assertEqual(createdFile.readTextSync(), "some text", `The contents of the new file created in the ${knownFolderName} known folder are not as expected.`); | ||
| 238 | + } | ||
| 236 | 239 | } | |
| 237 | 240 | else { | |
| 238 | 241 | TKUnit.assertThrows(testFunc, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -107,6 +107,30 @@ export class FileSystemAccess { | |||
| 107 | 107 | } | |
| 108 | 108 | } | |
| 109 | 109 | ||
| 110 | + public getExistingFolder(path: string, onError?: (error: any) => any): { path: string; name: string } { | ||
| 111 | + try { | ||
| 112 | + var fileManager = utils.ios.getter(NSFileManager, NSFileManager.defaultManager); | ||
| 113 | + var exists = this.folderExists(path); | ||
| 114 | + | ||
| 115 | + if (exists) { | ||
| 116 | + var dirName = fileManager.displayNameAtPath(path); | ||
| 117 | + | ||
| 118 | + return { | ||
| 119 | + path: path, | ||
| 120 | + name: dirName | ||
| 121 | + }; | ||
| 122 | + } | ||
| 123 | + return undefined; | ||
| 124 | + } | ||
| 125 | + catch (ex) { | ||
| 126 | + if (onError) { | ||
| 127 | + onError(new Error("Failed to get folder at path '" + path + "'")); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + return undefined; | ||
| 131 | + } | ||
| 132 | + } | ||
| 133 | + | ||
| 110 | 134 | public eachEntity(path: string, onEntity: (file: { path: string; name: string; extension: string }) => any, onError?: (error: any) => any) { | |
| 111 | 135 | if (!onEntity) { | |
| 112 | 136 | return; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -512,10 +512,13 @@ export module knownFolders { | |||
| 512 | 512 | export var library = function(): Folder { | |
| 513 | 513 | _checkPlatform("library"); | |
| 514 | 514 | if (!_library) { | |
| 515 | - var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.LibraryDirectory); | ||
| 516 | - _library = Folder.fromPath(path); | ||
| 517 | - _library[pathProperty] = path; | ||
| 518 | - _library[isKnownProperty] = true; | ||
| 515 | + let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.LibraryDirectory); | ||
| 516 | + | ||
| 517 | + if (existingFolderInfo) { | ||
| 518 | + _library = existingFolderInfo.folder; | ||
| 519 | + _library[pathProperty] = existingFolderInfo.path; | ||
| 520 | + _library[isKnownProperty] = true; | ||
| 521 | + } | ||
| 519 | 522 | } | |
| 520 | 523 | ||
| 521 | 524 | return _library; | |
@@ -525,10 +528,13 @@ export module knownFolders { | |||
| 525 | 528 | export var developer = function(): Folder { | |
| 526 | 529 | _checkPlatform("developer"); | |
| 527 | 530 | if (!_developer) { | |
| 528 | - var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DeveloperDirectory); | ||
| 529 | - _developer = Folder.fromPath(path); | ||
| 530 | - _developer[pathProperty] = path; | ||
| 531 | - _developer[isKnownProperty] = true; | ||
| 531 | + let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DeveloperDirectory); | ||
| 532 | + | ||
| 533 | + if (existingFolderInfo) { | ||
| 534 | + _developer = existingFolderInfo.folder; | ||
| 535 | + _developer[pathProperty] = existingFolderInfo.path; | ||
| 536 | + _developer[isKnownProperty] = true; | ||
| 537 | + } | ||
| 532 | 538 | } | |
| 533 | 539 | ||
| 534 | 540 | return _developer; | |
@@ -538,10 +544,13 @@ export module knownFolders { | |||
| 538 | 544 | export var desktop = function(): Folder { | |
| 539 | 545 | _checkPlatform("desktop"); | |
| 540 | 546 | if (!_desktop) { | |
| 541 | - var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DesktopDirectory); | ||
| 542 | - _desktop = Folder.fromPath(path); | ||
| 543 | - _desktop[pathProperty] = path; | ||
| 544 | - _desktop[isKnownProperty] = true; | ||
| 547 | + let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DesktopDirectory); | ||
| 548 | + | ||
| 549 | + if (existingFolderInfo) { | ||
| 550 | + _desktop = existingFolderInfo.folder; | ||
| 551 | + _desktop[pathProperty] = existingFolderInfo.path; | ||
| 552 | + _desktop[isKnownProperty] = true; | ||
| 553 | + } | ||
| 545 | 554 | } | |
| 546 | 555 | ||
| 547 | 556 | return _desktop; | |
@@ -551,10 +560,13 @@ export module knownFolders { | |||
| 551 | 560 | export var downloads = function(): Folder { | |
| 552 | 561 | _checkPlatform("downloads"); | |
| 553 | 562 | if (!_downloads) { | |
| 554 | - var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.DownloadsDirectory); | ||
| 555 | - _downloads = Folder.fromPath(path); | ||
| 556 | - _downloads[pathProperty] = path; | ||
| 557 | - _downloads[isKnownProperty] = true; | ||
| 563 | + let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.DownloadsDirectory); | ||
| 564 | + | ||
| 565 | + if (existingFolderInfo) { | ||
| 566 | + _downloads = existingFolderInfo.folder; | ||
| 567 | + _downloads[pathProperty] = existingFolderInfo.path; | ||
| 568 | + _downloads[isKnownProperty] = true; | ||
| 569 | + } | ||
| 558 | 570 | } | |
| 559 | 571 | ||
| 560 | 572 | return _downloads; | |
@@ -564,10 +576,13 @@ export module knownFolders { | |||
| 564 | 576 | export var movies = function(): Folder { | |
| 565 | 577 | _checkPlatform("movies"); | |
| 566 | 578 | if (!_movies) { | |
| 567 | - var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.MoviesDirectory); | ||
| 568 | - _movies = Folder.fromPath(path); | ||
| 569 | - _movies[pathProperty] = path; | ||
| 570 | - _movies[isKnownProperty] = true; | ||
| 579 | + let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.MoviesDirectory); | ||
| 580 | + | ||
| 581 | + if (existingFolderInfo) { | ||
| 582 | + _movies = existingFolderInfo.folder; | ||
| 583 | + _movies[pathProperty] = existingFolderInfo.path; | ||
| 584 | + _movies[isKnownProperty] = true; | ||
| 585 | + } | ||
| 571 | 586 | } | |
| 572 | 587 | ||
| 573 | 588 | return _movies; | |
@@ -577,10 +592,13 @@ export module knownFolders { | |||
| 577 | 592 | export var music = function(): Folder { | |
| 578 | 593 | _checkPlatform("music"); | |
| 579 | 594 | if (!_music) { | |
| 580 | - var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.MusicDirectory); | ||
| 581 | - _music = Folder.fromPath(path); | ||
| 582 | - _music[pathProperty] = path; | ||
| 583 | - _music[isKnownProperty] = true; | ||
| 595 | + let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.MusicDirectory); | ||
| 596 | + | ||
| 597 | + if (existingFolderInfo) { | ||
| 598 | + _music = existingFolderInfo.folder; | ||
| 599 | + _music[pathProperty] = existingFolderInfo.path; | ||
| 600 | + _music[isKnownProperty] = true; | ||
| 601 | + } | ||
| 584 | 602 | } | |
| 585 | 603 | ||
| 586 | 604 | return _music; | |
@@ -590,10 +608,13 @@ export module knownFolders { | |||
| 590 | 608 | export var pictures = function(): Folder { | |
| 591 | 609 | _checkPlatform("pictures"); | |
| 592 | 610 | if (!_pictures) { | |
| 593 | - var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.PicturesDirectory); | ||
| 594 | - _pictures = Folder.fromPath(path); | ||
| 595 | - _pictures[pathProperty] = path; | ||
| 596 | - _pictures[isKnownProperty] = true; | ||
| 611 | + let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.PicturesDirectory); | ||
| 612 | + | ||
| 613 | + if (existingFolderInfo) { | ||
| 614 | + _pictures = existingFolderInfo.folder; | ||
| 615 | + _pictures[pathProperty] = existingFolderInfo.path; | ||
| 616 | + _pictures[isKnownProperty] = true; | ||
| 617 | + } | ||
| 597 | 618 | } | |
| 598 | 619 | ||
| 599 | 620 | return _pictures; | |
@@ -603,14 +624,31 @@ export module knownFolders { | |||
| 603 | 624 | export var sharedPublic = function(): Folder { | |
| 604 | 625 | _checkPlatform("sharedPublic"); | |
| 605 | 626 | if (!_sharedPublic) { | |
| 606 | - var path = (<any>getFileAccess()).getKnownPath(NSSearchPathDirectory.SharedPublicDirectory); | ||
| 607 | - _sharedPublic = Folder.fromPath(path); | ||
| 608 | - _sharedPublic[pathProperty] = path; | ||
| 609 | - _sharedPublic[isKnownProperty] = true; | ||
| 627 | + let existingFolderInfo = getExistingFolderInfo(NSSearchPathDirectory.SharedPublicDirectory); | ||
| 628 | + | ||
| 629 | + if (existingFolderInfo) { | ||
| 630 | + _sharedPublic = existingFolderInfo.folder; | ||
| 631 | + _sharedPublic[pathProperty] = existingFolderInfo.path; | ||
| 632 | + _sharedPublic[isKnownProperty] = true; | ||
| 633 | + } | ||
| 610 | 634 | } | |
| 611 | 635 | ||
| 612 | 636 | return _sharedPublic; | |
| 613 | 637 | }; | |
| 638 | + | ||
| 639 | + function getExistingFolderInfo(pathDirectory: NSSearchPathDirectory): { folder: Folder; path: string } { | ||
| 640 | + var fileAccess = (<any>getFileAccess()); | ||
| 641 | + var folderPath = fileAccess.getKnownPath(pathDirectory); | ||
| 642 | + var folderInfo = fileAccess.getExistingFolder(folderPath); | ||
| 643 | + | ||
| 644 | + if (folderInfo) { | ||
| 645 | + return { | ||
| 646 | + folder: createFolder(folderInfo), | ||
| 647 | + path: folderPath | ||
| 648 | + }; | ||
| 649 | + } | ||
| 650 | + return undefined; | ||
| 651 | + } | ||
| 614 | 652 | } | |
| 615 | 653 | } | |
| 616 | 654 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments