Skip to content

Commit 6bdb5b5

Browse files
EddyVerbruggenmanoldonev
authored andcommitted
feat(file-system): add ability to retrieve file size (NativeScript#5710)
1 parent 0e1f19a commit 6bdb5b5

6 files changed

Lines changed: 71 additions & 31 deletions

File tree

tests/app/file-system/file-system-tests.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export var testFileReadWriteBinary = function () {
186186
var source = sourceFile.readSync(e=> { error = e; });
187187

188188
destinationFile.writeSync(source, e=> { error = e; });
189-
189+
190190
// >> (hide)
191191
var destination = destinationFile.readSync(e=> { error = e; });
192192
TKUnit.assertNull(error);
@@ -238,22 +238,22 @@ function _testIOSSpecificKnownFolder(knownFolderName: string){
238238
}
239239
}
240240
else {
241-
TKUnit.assertThrows(testFunc,
241+
TKUnit.assertThrows(testFunc,
242242
`Trying to retrieve the ${knownFolderName} known folder on a platform different from iOS should throw!`,
243243
`The "${knownFolderName}" known folder is available on iOS only!`);
244244
}
245245
}
246246

247247
export var testIOSSpecificKnownFolders = function () {
248-
_testIOSSpecificKnownFolder("library");
249-
_testIOSSpecificKnownFolder("developer");
250-
_testIOSSpecificKnownFolder("desktop");
251-
_testIOSSpecificKnownFolder("downloads");
252-
_testIOSSpecificKnownFolder("movies");
253-
_testIOSSpecificKnownFolder("music");
254-
_testIOSSpecificKnownFolder("pictures");
255-
_testIOSSpecificKnownFolder("sharedPublic");
256-
}
248+
_testIOSSpecificKnownFolder("library");
249+
_testIOSSpecificKnownFolder("developer");
250+
_testIOSSpecificKnownFolder("desktop");
251+
_testIOSSpecificKnownFolder("downloads");
252+
_testIOSSpecificKnownFolder("movies");
253+
_testIOSSpecificKnownFolder("music");
254+
_testIOSSpecificKnownFolder("pictures");
255+
_testIOSSpecificKnownFolder("sharedPublic");
256+
};
257257

258258
export var testGetEntities = function () {
259259
// >> file-system-folders-content
@@ -559,12 +559,22 @@ export function test_FSEntity_Properties() {
559559
TKUnit.assert(file.extension === ".txt", "FileEntity.extension not working.");
560560
TKUnit.assert(file.isLocked === false, "FileEntity.isLocked not working.");
561561
TKUnit.assert(file.lastModified instanceof Date, "FileEntity.lastModified not working.");
562+
TKUnit.assert(file.size === 0, "FileEntity.size not working.");
562563
TKUnit.assert(file.name === "Test_File.txt", "FileEntity.name not working.");
563564
TKUnit.assert(file.parent === documents, "FileEntity.parent not working.");
564565

565566
file.remove();
566567
}
567568

569+
export function test_FileSize(done) {
570+
var file = fs.knownFolders.documents().getFile("Test_File_Size.txt");
571+
file.writeText("Hello World!").then(() => {
572+
TKUnit.assert(file.size === "Hello World!".length);
573+
return file.remove();
574+
}).then(() => done())
575+
.catch(done);
576+
}
577+
568578
export function test_UnlockAfterWrite(done) {
569579
var file = fs.knownFolders.documents().getFile("Test_File_Lock.txt");
570580
file.writeText("Hello World!").then(() => {

tns-core-modules/file-system/file-system-access.android.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ export class FileSystemAccess {
1818
return new Date(javaFile.lastModified());
1919
}
2020

21+
public getFileSize(path: string): number {
22+
const javaFile = new java.io.File(path);
23+
return javaFile.length();
24+
}
25+
2126
public getParent(path: string, onError?: (error: any) => any): { path: string; name: string } {
2227
try {
2328
var javaFile = new java.io.File(path);

tns-core-modules/file-system/file-system-access.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export class FileSystemAccess {
1212
*/
1313
getLastModified(path: string): Date;
1414

15+
/**
16+
* Gets the size in bytes of a file with a given path.
17+
* @param path Path to the file.
18+
*/
19+
getFileSize(path: string): number;
20+
1521
/**
1622
* Gets the parent folder of a file with a given path.
1723
* @param path Path to the file.

tns-core-modules/file-system/file-system-access.ios.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ export class FileSystemAccess {
1616
}
1717
}
1818

19+
public getFileSize(path: string): number {
20+
const fileManager = ios.getter(NSFileManager, NSFileManager.defaultManager);
21+
const attributes = fileManager.attributesOfItemAtPathError(path);
22+
if (attributes) {
23+
return attributes.objectForKey("NSFileSize");
24+
} else {
25+
return 0;
26+
}
27+
}
28+
1929
public getParent(path: string, onError?: (error: any) => any): { path: string; name: string } {
2030
try {
2131
const fileManager = ios.getter(NSFileManager, NSFileManager.defaultManager);
@@ -386,4 +396,4 @@ export class FileSystemAccess {
386396
public joinPaths(paths: string[]): string {
387397
return ios.joinPaths(...paths);
388398
}
389-
}
399+
}

tns-core-modules/file-system/file-system.d.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class FileSystemEntity {
2323
path: string;
2424

2525
/**
26-
* Gets the Folder object representing the parent of this entity.
26+
* Gets the Folder object representing the parent of this entity.
2727
* Will be null for a root folder like Documents or Temporary.
2828
* This property is readonly.
2929
*/
@@ -67,6 +67,11 @@ export class File extends FileSystemEntity {
6767
*/
6868
extension: string;
6969

70+
/**
71+
* Gets the size in bytes of the file.
72+
*/
73+
size: number;
74+
7075
/**
7176
* Gets a value indicating whether the file is currently locked, meaning a background operation associated with this file is running.
7277
*/
@@ -208,7 +213,7 @@ export module knownFolders {
208213
* iOS - this folder is read-only and contains the app and all its resources.
209214
*/
210215
export function currentApp(): Folder;
211-
216+
212217
/**
213218
* Contains iOS-specific known folders.
214219
*/
@@ -217,42 +222,42 @@ export module knownFolders {
217222
* Gets the NSLibraryDirectory.
218223
*/
219224
export function library(): Folder;
220-
225+
221226
/**
222227
* Gets the NSDeveloperDirectory.
223228
*/
224229
export function developer(): Folder;
225-
230+
226231
/**
227232
* Gets the NSDesktopDirectory.
228233
*/
229234
export function desktop(): Folder;
230-
235+
231236
/**
232237
* Gets the NSDownloadsDirectory.
233238
*/
234239
export function downloads(): Folder;
235-
240+
236241
/**
237242
* Gets the NSMoviesDirectory.
238243
*/
239244
export function movies(): Folder;
240-
245+
241246
/**
242247
* Gets the NSMusicDirectory.
243248
*/
244249
export function music(): Folder;
245-
250+
246251
/**
247252
* Gets the NSPicturesDirectory.
248253
*/
249254
export function pictures(): Folder;
250-
255+
251256
/**
252257
* Gets the NSSharedPublicDirectory.
253258
*/
254259
export function sharedPublic(): Folder;
255-
}
260+
}
256261
}
257262

258263
/**

tns-core-modules/file-system/file-system.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ export class File extends FileSystemEntity {
200200
return !!this._locked;
201201
}
202202

203+
get size(): number {
204+
return getFileAccess().getFileSize(this.path);
205+
}
206+
203207
public readSync(onError?: (error: any) => any): any {
204208
this.checkAccess();
205209

@@ -304,7 +308,7 @@ export class File extends FileSystemEntity {
304308
onError(error);
305309
}
306310
};
307-
311+
308312
// TODO: Asyncronous
309313
getFileAccess().writeText(this.path, content, localError, encoding);
310314
} finally {
@@ -499,15 +503,15 @@ export module knownFolders {
499503

500504
return _app;
501505
};
502-
506+
503507
export module ios {
504508
function _checkPlatform(knownFolderName: string){
505509
ensurePlatform();
506510
if (!platform.isIOS){
507511
throw new Error(`The "${knownFolderName}" known folder is available on iOS only!`);
508-
}
512+
}
509513
}
510-
514+
511515
let _library: Folder;
512516
export var library = function(): Folder {
513517
_checkPlatform("library");
@@ -523,7 +527,7 @@ export module knownFolders {
523527

524528
return _library;
525529
};
526-
530+
527531
let _developer: Folder;
528532
export var developer = function(): Folder {
529533
_checkPlatform("developer");
@@ -539,7 +543,7 @@ export module knownFolders {
539543

540544
return _developer;
541545
};
542-
546+
543547
let _desktop: Folder;
544548
export var desktop = function(): Folder {
545549
_checkPlatform("desktop");
@@ -555,7 +559,7 @@ export module knownFolders {
555559

556560
return _desktop;
557561
};
558-
562+
559563
let _downloads: Folder;
560564
export var downloads = function(): Folder {
561565
_checkPlatform("downloads");
@@ -571,7 +575,7 @@ export module knownFolders {
571575

572576
return _downloads;
573577
};
574-
578+
575579
let _movies: Folder;
576580
export var movies = function(): Folder {
577581
_checkPlatform("movies");
@@ -587,7 +591,7 @@ export module knownFolders {
587591

588592
return _movies;
589593
};
590-
594+
591595
let _music: Folder;
592596
export var music = function(): Folder {
593597
_checkPlatform("music");

0 commit comments

Comments
 (0)