From cd3ff2cd4c35b91874ed787a5a47a52a1236937b Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Thu, 19 Apr 2018 21:30:05 +0200 Subject: [PATCH] Implements #3864 --- tests/app/file-system/file-system-tests.ts | 32 ++++++++++++------- .../file-system/file-system-access.android.ts | 5 +++ .../file-system/file-system-access.d.ts | 6 ++++ .../file-system/file-system-access.ios.ts | 12 ++++++- tns-core-modules/file-system/file-system.d.ts | 25 +++++++++------ tns-core-modules/file-system/file-system.ts | 22 +++++++------ 6 files changed, 71 insertions(+), 31 deletions(-) diff --git a/tests/app/file-system/file-system-tests.ts b/tests/app/file-system/file-system-tests.ts index 9087a71510..3146c0bbbd 100644 --- a/tests/app/file-system/file-system-tests.ts +++ b/tests/app/file-system/file-system-tests.ts @@ -186,7 +186,7 @@ export var testFileReadWriteBinary = function () { var source = sourceFile.readSync(e=> { error = e; }); destinationFile.writeSync(source, e=> { error = e; }); - + // >> (hide) var destination = destinationFile.readSync(e=> { error = e; }); TKUnit.assertNull(error); @@ -238,22 +238,22 @@ function _testIOSSpecificKnownFolder(knownFolderName: string){ } } else { - TKUnit.assertThrows(testFunc, + TKUnit.assertThrows(testFunc, `Trying to retrieve the ${knownFolderName} known folder on a platform different from iOS should throw!`, `The "${knownFolderName}" known folder is available on iOS only!`); } } export var testIOSSpecificKnownFolders = function () { - _testIOSSpecificKnownFolder("library"); - _testIOSSpecificKnownFolder("developer"); - _testIOSSpecificKnownFolder("desktop"); - _testIOSSpecificKnownFolder("downloads"); - _testIOSSpecificKnownFolder("movies"); - _testIOSSpecificKnownFolder("music"); - _testIOSSpecificKnownFolder("pictures"); - _testIOSSpecificKnownFolder("sharedPublic"); -} + _testIOSSpecificKnownFolder("library"); + _testIOSSpecificKnownFolder("developer"); + _testIOSSpecificKnownFolder("desktop"); + _testIOSSpecificKnownFolder("downloads"); + _testIOSSpecificKnownFolder("movies"); + _testIOSSpecificKnownFolder("music"); + _testIOSSpecificKnownFolder("pictures"); + _testIOSSpecificKnownFolder("sharedPublic"); +}; export var testGetEntities = function () { // >> file-system-folders-content @@ -559,12 +559,22 @@ export function test_FSEntity_Properties() { TKUnit.assert(file.extension === ".txt", "FileEntity.extension not working."); TKUnit.assert(file.isLocked === false, "FileEntity.isLocked not working."); TKUnit.assert(file.lastModified instanceof Date, "FileEntity.lastModified not working."); + TKUnit.assert(file.size === 0, "FileEntity.size not working."); TKUnit.assert(file.name === "Test_File.txt", "FileEntity.name not working."); TKUnit.assert(file.parent === documents, "FileEntity.parent not working."); file.remove(); } +export function test_FileSize(done) { + var file = fs.knownFolders.documents().getFile("Test_File_Size.txt"); + file.writeText("Hello World!").then(() => { + TKUnit.assert(file.size === "Hello World!".length); + return file.remove(); + }).then(() => done()) + .catch(done); +} + export function test_UnlockAfterWrite(done) { var file = fs.knownFolders.documents().getFile("Test_File_Lock.txt"); file.writeText("Hello World!").then(() => { diff --git a/tns-core-modules/file-system/file-system-access.android.ts b/tns-core-modules/file-system/file-system-access.android.ts index 4778367142..9caa6fcfbc 100644 --- a/tns-core-modules/file-system/file-system-access.android.ts +++ b/tns-core-modules/file-system/file-system-access.android.ts @@ -18,6 +18,11 @@ export class FileSystemAccess { return new Date(javaFile.lastModified()); } + public getFileSize(path: string): number { + const javaFile = new java.io.File(path); + return javaFile.length(); + } + public getParent(path: string, onError?: (error: any) => any): { path: string; name: string } { try { var javaFile = new java.io.File(path); diff --git a/tns-core-modules/file-system/file-system-access.d.ts b/tns-core-modules/file-system/file-system-access.d.ts index c9048d50ae..e89e3dca81 100644 --- a/tns-core-modules/file-system/file-system-access.d.ts +++ b/tns-core-modules/file-system/file-system-access.d.ts @@ -12,6 +12,12 @@ export class FileSystemAccess { */ getLastModified(path: string): Date; + /** + * Gets the size in bytes of a file with a given path. + * @param path Path to the file. + */ + getFileSize(path: string): number; + /** * Gets the parent folder of a file with a given path. * @param path Path to the file. diff --git a/tns-core-modules/file-system/file-system-access.ios.ts b/tns-core-modules/file-system/file-system-access.ios.ts index fbac869538..33efcf15c6 100644 --- a/tns-core-modules/file-system/file-system-access.ios.ts +++ b/tns-core-modules/file-system/file-system-access.ios.ts @@ -16,6 +16,16 @@ export class FileSystemAccess { } } + public getFileSize(path: string): number { + const fileManager = ios.getter(NSFileManager, NSFileManager.defaultManager); + const attributes = fileManager.attributesOfItemAtPathError(path); + if (attributes) { + return attributes.objectForKey("NSFileSize"); + } else { + return 0; + } + } + public getParent(path: string, onError?: (error: any) => any): { path: string; name: string } { try { const fileManager = ios.getter(NSFileManager, NSFileManager.defaultManager); @@ -386,4 +396,4 @@ export class FileSystemAccess { public joinPaths(paths: string[]): string { return ios.joinPaths(...paths); } -} \ No newline at end of file +} diff --git a/tns-core-modules/file-system/file-system.d.ts b/tns-core-modules/file-system/file-system.d.ts index b3b4f06765..30268aad46 100644 --- a/tns-core-modules/file-system/file-system.d.ts +++ b/tns-core-modules/file-system/file-system.d.ts @@ -23,7 +23,7 @@ export class FileSystemEntity { path: string; /** - * Gets the Folder object representing the parent of this entity. + * Gets the Folder object representing the parent of this entity. * Will be null for a root folder like Documents or Temporary. * This property is readonly. */ @@ -67,6 +67,11 @@ export class File extends FileSystemEntity { */ extension: string; + /** + * Gets the size in bytes of the file. + */ + size: number; + /** * Gets a value indicating whether the file is currently locked, meaning a background operation associated with this file is running. */ @@ -208,7 +213,7 @@ export module knownFolders { * iOS - this folder is read-only and contains the app and all its resources. */ export function currentApp(): Folder; - + /** * Contains iOS-specific known folders. */ @@ -217,42 +222,42 @@ export module knownFolders { * Gets the NSLibraryDirectory. */ export function library(): Folder; - + /** * Gets the NSDeveloperDirectory. */ export function developer(): Folder; - + /** * Gets the NSDesktopDirectory. */ export function desktop(): Folder; - + /** * Gets the NSDownloadsDirectory. */ export function downloads(): Folder; - + /** * Gets the NSMoviesDirectory. */ export function movies(): Folder; - + /** * Gets the NSMusicDirectory. */ export function music(): Folder; - + /** * Gets the NSPicturesDirectory. */ export function pictures(): Folder; - + /** * Gets the NSSharedPublicDirectory. */ export function sharedPublic(): Folder; - } + } } /** diff --git a/tns-core-modules/file-system/file-system.ts b/tns-core-modules/file-system/file-system.ts index 830d51817f..fc7f83b64f 100644 --- a/tns-core-modules/file-system/file-system.ts +++ b/tns-core-modules/file-system/file-system.ts @@ -200,6 +200,10 @@ export class File extends FileSystemEntity { return !!this._locked; } + get size(): number { + return getFileAccess().getFileSize(this.path); + } + public readSync(onError?: (error: any) => any): any { this.checkAccess(); @@ -304,7 +308,7 @@ export class File extends FileSystemEntity { onError(error); } }; - + // TODO: Asyncronous getFileAccess().writeText(this.path, content, localError, encoding); } finally { @@ -499,15 +503,15 @@ export module knownFolders { return _app; }; - + export module ios { function _checkPlatform(knownFolderName: string){ ensurePlatform(); if (!platform.isIOS){ throw new Error(`The "${knownFolderName}" known folder is available on iOS only!`); - } + } } - + let _library: Folder; export var library = function(): Folder { _checkPlatform("library"); @@ -523,7 +527,7 @@ export module knownFolders { return _library; }; - + let _developer: Folder; export var developer = function(): Folder { _checkPlatform("developer"); @@ -539,7 +543,7 @@ export module knownFolders { return _developer; }; - + let _desktop: Folder; export var desktop = function(): Folder { _checkPlatform("desktop"); @@ -555,7 +559,7 @@ export module knownFolders { return _desktop; }; - + let _downloads: Folder; export var downloads = function(): Folder { _checkPlatform("downloads"); @@ -571,7 +575,7 @@ export module knownFolders { return _downloads; }; - + let _movies: Folder; export var movies = function(): Folder { _checkPlatform("movies"); @@ -587,7 +591,7 @@ export module knownFolders { return _movies; }; - + let _music: Folder; export var music = function(): Folder { _checkPlatform("music");