Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions tests/app/file-system/file-system-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(() => {
Expand Down
5 changes: 5 additions & 0 deletions tns-core-modules/file-system/file-system-access.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions tns-core-modules/file-system/file-system-access.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion tns-core-modules/file-system/file-system-access.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -386,4 +396,4 @@ export class FileSystemAccess {
public joinPaths(paths: string[]): string {
return ios.joinPaths(...paths);
}
}
}
25 changes: 15 additions & 10 deletions tns-core-modules/file-system/file-system.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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;
}
}
}

/**
Expand Down
22 changes: 13 additions & 9 deletions tns-core-modules/file-system/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -304,7 +308,7 @@ export class File extends FileSystemEntity {
onError(error);
}
};

// TODO: Asyncronous
getFileAccess().writeText(this.path, content, localError, encoding);
} finally {
Expand Down Expand Up @@ -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");
Expand All @@ -523,7 +527,7 @@ export module knownFolders {

return _library;
};

let _developer: Folder;
export var developer = function(): Folder {
_checkPlatform("developer");
Expand All @@ -539,7 +543,7 @@ export module knownFolders {

return _developer;
};

let _desktop: Folder;
export var desktop = function(): Folder {
_checkPlatform("desktop");
Expand All @@ -555,7 +559,7 @@ export module knownFolders {

return _desktop;
};

let _downloads: Folder;
export var downloads = function(): Folder {
_checkPlatform("downloads");
Expand All @@ -571,7 +575,7 @@ export module knownFolders {

return _downloads;
};

let _movies: Folder;
export var movies = function(): Folder {
_checkPlatform("movies");
Expand All @@ -587,7 +591,7 @@ export module knownFolders {

return _movies;
};

let _music: Folder;
export var music = function(): Folder {
_checkPlatform("music");
Expand Down