mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(file-system): add ability to retrieve file size (#5710)
This commit is contained in:
committed by
Manol Donev
parent
0e1f19a76a
commit
6bdb5b553f
@@ -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(() => {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
25
tns-core-modules/file-system/file-system.d.ts
vendored
25
tns-core-modules/file-system/file-system.d.ts
vendored
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user