mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 20:11:24 +08:00
namespaces removed (except Application)
This commit is contained in:
254
FileSystem/file_system.d.ts
vendored
254
FileSystem/file_system.d.ts
vendored
@ -1,138 +1,134 @@
|
||||
export declare module tk {
|
||||
export module io {
|
||||
export class FileSystemEntity {
|
||||
public readonly: boolean;
|
||||
public lastModified: Date;
|
||||
/**
|
||||
* Gets the name of the entity.
|
||||
*/
|
||||
public name: string;
|
||||
/**
|
||||
* Gets the fully-qualified path (including the extension for a File) of the entity.
|
||||
*/
|
||||
public path: string;
|
||||
/**
|
||||
* Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary.
|
||||
*/
|
||||
public getParent(onSuccess: (folder: Folder) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Deletes the current Entity from the file system.
|
||||
*/
|
||||
public delete(onSuccess?: () => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Renames the current entity using the specified name.
|
||||
*/
|
||||
public rename(newName: string, onSuccess?: Function, onError?: Function);
|
||||
}
|
||||
export declare class FileSystemEntity {
|
||||
public readonly: boolean;
|
||||
public lastModified: Date;
|
||||
/**
|
||||
* Gets the name of the entity.
|
||||
*/
|
||||
public name: string;
|
||||
/**
|
||||
* Gets the fully-qualified path (including the extension for a File) of the entity.
|
||||
*/
|
||||
public path: string;
|
||||
/**
|
||||
* Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary.
|
||||
*/
|
||||
public getParent(onSuccess: (folder: Folder) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Deletes the current Entity from the file system.
|
||||
*/
|
||||
public delete(onSuccess?: () => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Renames the current entity using the specified name.
|
||||
*/
|
||||
public rename(newName: string, onSuccess?: Function, onError?: Function);
|
||||
}
|
||||
|
||||
export class File extends FileSystemEntity {
|
||||
/**
|
||||
* Gets the extension of the file.
|
||||
*/
|
||||
public extension: string;
|
||||
/**
|
||||
* Gets a value indicating whether the file is currently locked, meaning a background operation associated with this file is running.
|
||||
*/
|
||||
public isLocked: boolean;
|
||||
/**
|
||||
* Gets or creates a File entity at the specified path.
|
||||
*/
|
||||
public static fromPath(path: string, onSuccess: (file: File) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Checks whether a File with the specified path already exists.
|
||||
*/
|
||||
public static exists(path: string): boolean;
|
||||
/**
|
||||
* Creates a FileReader object over this file and locks the file until the reader is released.
|
||||
*/
|
||||
public openRead(): FileReader;
|
||||
/**
|
||||
* Creates a FileWriter object over this file and locks the file until the writer is released.
|
||||
*/
|
||||
public openWrite(): FileWriter;
|
||||
}
|
||||
export declare class File extends FileSystemEntity {
|
||||
/**
|
||||
* Gets the extension of the file.
|
||||
*/
|
||||
public extension: string;
|
||||
/**
|
||||
* Gets a value indicating whether the file is currently locked, meaning a background operation associated with this file is running.
|
||||
*/
|
||||
public isLocked: boolean;
|
||||
/**
|
||||
* Gets or creates a File entity at the specified path.
|
||||
*/
|
||||
public static fromPath(path: string, onSuccess: (file: File) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Checks whether a File with the specified path already exists.
|
||||
*/
|
||||
public static exists(path: string): boolean;
|
||||
/**
|
||||
* Creates a FileReader object over this file and locks the file until the reader is released.
|
||||
*/
|
||||
public openRead(): FileReader;
|
||||
/**
|
||||
* Creates a FileWriter object over this file and locks the file until the writer is released.
|
||||
*/
|
||||
public openWrite(): FileWriter;
|
||||
}
|
||||
|
||||
export class Folder extends FileSystemEntity {
|
||||
/**
|
||||
* Determines whether this instance is a KnownFolder (accessed through the KnownFolders object).
|
||||
*/
|
||||
public isKnown: boolean;
|
||||
export declare class Folder extends FileSystemEntity {
|
||||
/**
|
||||
* Determines whether this instance is a KnownFolder (accessed through the KnownFolders object).
|
||||
*/
|
||||
public isKnown: boolean;
|
||||
|
||||
/**
|
||||
* Gets or creates a Folder entity at the specified path.
|
||||
*/
|
||||
public static fromPath(path: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Checks whether a Folder with the specified path already exists.
|
||||
*/
|
||||
public static exists(path: string): boolean;
|
||||
/**
|
||||
* Checks whether this Folder contains a file with the specified name.
|
||||
*/
|
||||
public containsFile(name: string): boolean;
|
||||
/**
|
||||
* Deletes all the files and folders (recursively), contained within this Folder.
|
||||
*/
|
||||
public empty(onSuccess?: () => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Gets or creates a File entity with the specified name within this Folder.
|
||||
*/
|
||||
public getFile(name: string, onSuccess: (file: File) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Gets or creates a Folder entity with the specified name within this Folder.
|
||||
*/
|
||||
public getFolder(name: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Gets all the top-level files residing within this Folder.
|
||||
*/
|
||||
public enumFiles(onSuccess: (files: Array<File>) => any, onError?: (error: any) => any);
|
||||
}
|
||||
/**
|
||||
* Gets or creates a Folder entity at the specified path.
|
||||
*/
|
||||
public static fromPath(path: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Checks whether a Folder with the specified path already exists.
|
||||
*/
|
||||
public static exists(path: string): boolean;
|
||||
/**
|
||||
* Checks whether this Folder contains a file with the specified name.
|
||||
*/
|
||||
public containsFile(name: string): boolean;
|
||||
/**
|
||||
* Deletes all the files and folders (recursively), contained within this Folder.
|
||||
*/
|
||||
public empty(onSuccess?: () => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Gets or creates a File entity with the specified name within this Folder.
|
||||
*/
|
||||
public getFile(name: string, onSuccess: (file: File) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Gets or creates a Folder entity with the specified name within this Folder.
|
||||
*/
|
||||
public getFolder(name: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any);
|
||||
/**
|
||||
* Gets all the top-level files residing within this Folder.
|
||||
*/
|
||||
public enumFiles(onSuccess: (files: Array<File>) => any, onError?: (error: any) => any);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to the top-level Folders instances that are accessible from the application. Use these as entry points to access the FileSystem.
|
||||
*/
|
||||
export class KnownFolders {
|
||||
/**
|
||||
* Gets the Documents folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
|
||||
*/
|
||||
public static Documents(): Folder;
|
||||
/**
|
||||
* Provides access to the top-level Folders instances that are accessible from the application. Use these as entry points to access the FileSystem.
|
||||
*/
|
||||
export declare class KnownFolders {
|
||||
/**
|
||||
* Gets the Documents folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
|
||||
*/
|
||||
public static Documents(): Folder;
|
||||
|
||||
/**
|
||||
* Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
|
||||
*/
|
||||
public static Temporary(): Folder;
|
||||
}
|
||||
/**
|
||||
* Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
|
||||
*/
|
||||
public static Temporary(): Folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for FileReader and FileWriter APIs.
|
||||
*/
|
||||
export class FileAccess {
|
||||
constructor(file: File);
|
||||
/**
|
||||
* Unlocks the file and allows other operations over it.
|
||||
*/
|
||||
public release();
|
||||
/**
|
||||
* Gets the underlying File instance.
|
||||
*/
|
||||
file: File;
|
||||
}
|
||||
/**
|
||||
* Base class for FileReader and FileWriter APIs.
|
||||
*/
|
||||
export declare class FileAccess {
|
||||
constructor(file: File);
|
||||
/**
|
||||
* Unlocks the file and allows other operations over it.
|
||||
*/
|
||||
public release();
|
||||
/**
|
||||
* Gets the underlying File instance.
|
||||
*/
|
||||
file: File;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables reading the content of a File entity.
|
||||
*/
|
||||
export class FileReader extends FileAccess {
|
||||
/**
|
||||
* Reads the content of the underlying File as a UTF8 encoded string.
|
||||
*/
|
||||
public readText(onSuccess: (content: string) => any, onError?: (error: any) => any);
|
||||
}
|
||||
/**
|
||||
* Enables reading the content of a File entity.
|
||||
*/
|
||||
export declare class FileReader extends FileAccess {
|
||||
/**
|
||||
* Reads the content of the underlying File as a UTF8 encoded string.
|
||||
*/
|
||||
public readText(onSuccess: (content: string) => any, onError?: (error: any) => any);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables saving data to a File entity.
|
||||
*/
|
||||
export class FileWriter extends FileAccess {
|
||||
public writeText(content: string, onSuccess?: () => any, onError?: (error: any) => any);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Enables saving data to a File entity.
|
||||
*/
|
||||
export declare class FileWriter extends FileAccess {
|
||||
public writeText(content: string, onSuccess?: () => any, onError?: (error: any) => any);
|
||||
}
|
@ -1,364 +1,360 @@
|
||||
import file_access_module = require("FileSystem/file_system_access");
|
||||
|
||||
export module tk {
|
||||
export module io {
|
||||
// The FileSystemAccess implementation, used through all the APIs.
|
||||
var fileAccess;
|
||||
var getFileAccess = function (): file_access_module.tk.io.FileSystemAccess {
|
||||
if (!fileAccess) {
|
||||
fileAccess = new file_access_module.tk.io.FileSystemAccess();
|
||||
}
|
||||
// The FileSystemAccess implementation, used through all the APIs.
|
||||
var fileAccess;
|
||||
var getFileAccess = function (): file_access_module.FileSystemAccess {
|
||||
if (!fileAccess) {
|
||||
fileAccess = new file_access_module.FileSystemAccess();
|
||||
}
|
||||
|
||||
return fileAccess;
|
||||
}
|
||||
return fileAccess;
|
||||
}
|
||||
|
||||
// we are defining these as private variables within the IO scope and will use them to access the corresponding properties for each FSEntity instance.
|
||||
// this allows us to encapsulate (hide) the explicit property setters and force the users go through the exposed APIs to receive FSEntity instances.
|
||||
var nameProperty = "_name";
|
||||
var pathProperty = "_path";
|
||||
var isKnownProperty = "_isKnown";
|
||||
var fileLockedProperty = "_locked";
|
||||
var extensionProperty = "_extension";
|
||||
var readonlyProperty = "_readonly";
|
||||
var lastModifiedProperty = "_lastModified";
|
||||
var pathProperty = "_path";
|
||||
var isKnownProperty = "_isKnown";
|
||||
var fileLockedProperty = "_locked";
|
||||
var extensionProperty = "_extension";
|
||||
var readonlyProperty = "_readonly";
|
||||
var lastModifiedProperty = "_lastModified";
|
||||
|
||||
/**
|
||||
* Represents the basic file system entity - a File or a Folder.
|
||||
*/
|
||||
export class FileSystemEntity {
|
||||
/**
|
||||
* Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary.
|
||||
*/
|
||||
public getParent(onSuccess: (parent: Folder) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (path: string) {
|
||||
var folder = new Folder();
|
||||
folder[pathProperty] = path;
|
||||
/**
|
||||
* Represents the basic file system entity - a File or a Folder.
|
||||
*/
|
||||
export class FileSystemEntity {
|
||||
/**
|
||||
* Gets the Folder object representing the parent of this entity. Will be null for a root folder like Documents or Temporary.
|
||||
*/
|
||||
public getParent(onSuccess: (parent: Folder) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (path: string) {
|
||||
var folder = new Folder();
|
||||
folder[pathProperty] = path;
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(folder);
|
||||
}
|
||||
}
|
||||
if (onSuccess) {
|
||||
onSuccess(folder);
|
||||
}
|
||||
}
|
||||
getFileAccess().getParent(this.path, localSuccess, onError);
|
||||
}
|
||||
/**
|
||||
* Deletes the current entity from the file system.
|
||||
*/
|
||||
public delete(onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
if (this instanceof File) {
|
||||
getFileAccess().deleteFile(this.path, onSuccess, onError);
|
||||
} else if (this instanceof Folder) {
|
||||
getFileAccess().deleteFolder(this.path, this[isKnownProperty], onSuccess, onError);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Renames the current entity using the specified name.
|
||||
*/
|
||||
public rename(newName: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: No implementation
|
||||
}
|
||||
/**
|
||||
* Gets the name of the entity.
|
||||
*/
|
||||
get name(): string {
|
||||
return this[nameProperty];
|
||||
}
|
||||
/**
|
||||
* Gets the fully-qualified path (including the extension for a File) of the entity.
|
||||
*/
|
||||
get path(): string {
|
||||
return this[pathProperty];
|
||||
}
|
||||
/**
|
||||
* Gets a value indicating whether this entity is read-only (no write persmissions).
|
||||
*/
|
||||
get readonly(): boolean {
|
||||
var value = this[readonlyProperty];
|
||||
if (this[readonlyProperty] === undefined) {
|
||||
value = this[readonlyProperty] = getFileAccess().getReadonly(this.path);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
/**
|
||||
* Gets the fully-qualified path (including the extension for a File) of the entity.
|
||||
*/
|
||||
get lastModified(): Date {
|
||||
var value = this[lastModifiedProperty];
|
||||
if (this[lastModifiedProperty] === undefined) {
|
||||
value = this[lastModifiedProperty] = getFileAccess().getLastModified(this.path);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a File entity.
|
||||
*/
|
||||
export class File extends FileSystemEntity {
|
||||
/**
|
||||
* Gets the File instance associated with the specified path.
|
||||
*/
|
||||
public static fromPath(path: string, onSuccess: (file: File) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (path: string) {
|
||||
var file = new File();
|
||||
file[pathProperty] = path;
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(file);
|
||||
}
|
||||
}
|
||||
getFileAccess().getFile(path, localSuccess, onError);
|
||||
}
|
||||
/**
|
||||
* Checks whether a File with the specified path already exists.
|
||||
*/
|
||||
public static exists(path: string): boolean {
|
||||
return getFileAccess().fileExists(path);
|
||||
}
|
||||
/**
|
||||
* Deletes the current File from the file system.
|
||||
*/
|
||||
public delete(onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
getFileAccess().deleteFile(this.path, onSuccess, onError);
|
||||
}
|
||||
/**
|
||||
* Creates a FileReader object over this file and locks the file until the reader is released.
|
||||
*/
|
||||
public openRead(): FileReader {
|
||||
this.checkAccess();
|
||||
return new FileReader(this);
|
||||
}
|
||||
/**
|
||||
* Creates a FileWriter object over this file and locks the file until the writer is released.
|
||||
*/
|
||||
public openWrite(): FileWriter {
|
||||
this.checkAccess();
|
||||
return new FileWriter(this);
|
||||
}
|
||||
/**
|
||||
* Gets the extension of the entity.
|
||||
*/
|
||||
get extension(): string {
|
||||
return this[extensionProperty];
|
||||
}
|
||||
/**
|
||||
* Gets a value indicating whether the file is currently locked, meaning a background operation associated with this file is running.
|
||||
*/
|
||||
get isLocked(): boolean {
|
||||
return this[fileLockedProperty];
|
||||
}
|
||||
|
||||
private checkAccess() {
|
||||
if (this.isLocked) {
|
||||
throw {
|
||||
message: "Cannot access a locked file."
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a Folder entity.
|
||||
*/
|
||||
export class Folder extends FileSystemEntity {
|
||||
/**
|
||||
* Attempts to access a Folder at the specified path and creates a new Folder if there is no existing one.
|
||||
*/
|
||||
public static fromPath(path: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (path: string) {
|
||||
var folder = new Folder();
|
||||
folder[pathProperty] = path;
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(folder);
|
||||
}
|
||||
}
|
||||
getFileAccess().getFolder(path, localSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a Folder with the specified path already exists.
|
||||
*/
|
||||
public static exists(path: string): boolean {
|
||||
return getFileAccess().folderExists(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this Folder contains a file with the specified name.
|
||||
*/
|
||||
public containsFile(name: string): boolean {
|
||||
var fileAccess = getFileAccess();
|
||||
var path = fileAccess.concatPath(this.path, name);
|
||||
return fileAccess.fileExists(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the current Folder (recursively) from the file system.
|
||||
*/
|
||||
public delete(onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
getFileAccess().deleteFolder(this.path, this.isKnown, onSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all the files and folders (recursively), contained within this Folder.
|
||||
*/
|
||||
public empty(onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
getFileAccess().emptyFolder(this.path, onSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether this instance is a KnownFolder (accessed through the KnownFolders object).
|
||||
*/
|
||||
get isKnown(): boolean {
|
||||
return this[isKnownProperty];
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to open a File with the specified name within this Folder and optionally creates a new File if there is no existing one.
|
||||
*/
|
||||
public getFile(name: string, onSuccess: (file: File) => any, onError?: (error: any) => any, createIfNonExisting?: boolean) {
|
||||
var localSuccess = function (filePath: string) {
|
||||
var newFile = new File();
|
||||
|
||||
newFile[pathProperty] = filePath;
|
||||
newFile[nameProperty] = name;
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(newFile);
|
||||
}
|
||||
}
|
||||
var fileAccess = getFileAccess();
|
||||
var path = fileAccess.concatPath(this.path, name);
|
||||
fileAccess.getFile(path, localSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to open a Folder with the specified name within this Folder and optionally creates a new Folder if there is no existing one.
|
||||
*/
|
||||
public getFolder(name: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (filePath: string) {
|
||||
var newFolder = new Folder();
|
||||
|
||||
newFolder[pathProperty] = filePath;
|
||||
newFolder[nameProperty] = name;
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(newFolder);
|
||||
}
|
||||
}
|
||||
|
||||
var fileAccess = getFileAccess();
|
||||
var path = fileAccess.concatPath(this.path, name);
|
||||
fileAccess.getFolder(path, localSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the top-level files residing within this Folder.
|
||||
*/
|
||||
public enumFiles(onSuccess: (files: Array<File>) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (paths: Array<string>) {
|
||||
if (onSuccess) {
|
||||
var files = new Array<File>();
|
||||
var i,
|
||||
path: string,
|
||||
file: File;
|
||||
|
||||
for (i = 0; i < files.length; i++) {
|
||||
file = new File();
|
||||
file[pathProperty] = files[i];
|
||||
files.push(file);
|
||||
}
|
||||
|
||||
onSuccess(files);
|
||||
}
|
||||
}
|
||||
getFileAccess().enumFiles(this.path, localSuccess, onError);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to the top-level Folders instances that are accessible from the application. Use these as entry points to access the FileSystem.
|
||||
*/
|
||||
export class KnownFolders {
|
||||
private static _documents: Folder;
|
||||
private static _temp: Folder;
|
||||
|
||||
/**
|
||||
* Gets the Documents folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
|
||||
*/
|
||||
public static Documents(): Folder {
|
||||
if (!KnownFolders._documents) {
|
||||
var path = getFileAccess().getDocumentsFolderPath();
|
||||
KnownFolders._documents = new Folder();
|
||||
KnownFolders._documents[pathProperty] = path;
|
||||
KnownFolders._documents[isKnownProperty] = true;
|
||||
}
|
||||
|
||||
return KnownFolders._documents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
|
||||
*/
|
||||
public static Temporary(): Folder {
|
||||
if (!KnownFolders._temp) {
|
||||
var path = getFileAccess().getTempFolderPath();
|
||||
KnownFolders._temp = new Folder();
|
||||
KnownFolders._temp[pathProperty] = path;
|
||||
KnownFolders._temp[isKnownProperty] = true;
|
||||
}
|
||||
|
||||
return KnownFolders._temp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for FileReader and FileWriter APIs.
|
||||
*/
|
||||
export class FileAccess {
|
||||
private _file;
|
||||
|
||||
constructor(file: File) {
|
||||
this._file = file;
|
||||
this._file[fileLockedProperty] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks the file and allows other operations over it.
|
||||
*/
|
||||
public release() {
|
||||
this._file[fileLockedProperty] = false;
|
||||
this._file = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the underlying File instance.
|
||||
*/
|
||||
get file(): File {
|
||||
return this._file;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables reading the content of a File entity.
|
||||
*/
|
||||
export class FileReader extends FileAccess {
|
||||
/**
|
||||
* Reads the content of the underlying File as a UTF8 encoded string.
|
||||
*/
|
||||
public readText(onSuccess: (content: string) => any, onError?: (error: any) => any) {
|
||||
getFileAccess().readText(this.file.path, onSuccess, onError);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables saving data to a File entity.
|
||||
*/
|
||||
export class FileWriter extends FileAccess {
|
||||
public writeText(content: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
getFileAccess().writeText(this.file.path, content, onSuccess, onError);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Deletes the current entity from the file system.
|
||||
*/
|
||||
public delete(onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
if (this instanceof File) {
|
||||
getFileAccess().deleteFile(this.path, onSuccess, onError);
|
||||
} else if (this instanceof Folder) {
|
||||
getFileAccess().deleteFolder(this.path, this[isKnownProperty], onSuccess, onError);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Renames the current entity using the specified name.
|
||||
*/
|
||||
public rename(newName: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: No implementation
|
||||
}
|
||||
/**
|
||||
* Gets the name of the entity.
|
||||
*/
|
||||
get name(): string {
|
||||
return this[nameProperty];
|
||||
}
|
||||
/**
|
||||
* Gets the fully-qualified path (including the extension for a File) of the entity.
|
||||
*/
|
||||
get path(): string {
|
||||
return this[pathProperty];
|
||||
}
|
||||
/**
|
||||
* Gets a value indicating whether this entity is read-only (no write persmissions).
|
||||
*/
|
||||
get readonly(): boolean {
|
||||
var value = this[readonlyProperty];
|
||||
if (this[readonlyProperty] === undefined) {
|
||||
value = this[readonlyProperty] = getFileAccess().getReadonly(this.path);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
/**
|
||||
* Gets the fully-qualified path (including the extension for a File) of the entity.
|
||||
*/
|
||||
get lastModified(): Date {
|
||||
var value = this[lastModifiedProperty];
|
||||
if (this[lastModifiedProperty] === undefined) {
|
||||
value = this[lastModifiedProperty] = getFileAccess().getLastModified(this.path);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a File entity.
|
||||
*/
|
||||
export class File extends FileSystemEntity {
|
||||
/**
|
||||
* Gets the File instance associated with the specified path.
|
||||
*/
|
||||
public static fromPath(path: string, onSuccess: (file: File) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (path: string) {
|
||||
var file = new File();
|
||||
file[pathProperty] = path;
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(file);
|
||||
}
|
||||
}
|
||||
getFileAccess().getFile(path, localSuccess, onError);
|
||||
}
|
||||
/**
|
||||
* Checks whether a File with the specified path already exists.
|
||||
*/
|
||||
public static exists(path: string): boolean {
|
||||
return getFileAccess().fileExists(path);
|
||||
}
|
||||
/**
|
||||
* Deletes the current File from the file system.
|
||||
*/
|
||||
public delete(onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
getFileAccess().deleteFile(this.path, onSuccess, onError);
|
||||
}
|
||||
/**
|
||||
* Creates a FileReader object over this file and locks the file until the reader is released.
|
||||
*/
|
||||
public openRead(): FileReader {
|
||||
this.checkAccess();
|
||||
return new FileReader(this);
|
||||
}
|
||||
/**
|
||||
* Creates a FileWriter object over this file and locks the file until the writer is released.
|
||||
*/
|
||||
public openWrite(): FileWriter {
|
||||
this.checkAccess();
|
||||
return new FileWriter(this);
|
||||
}
|
||||
/**
|
||||
* Gets the extension of the entity.
|
||||
*/
|
||||
get extension(): string {
|
||||
return this[extensionProperty];
|
||||
}
|
||||
/**
|
||||
* Gets a value indicating whether the file is currently locked, meaning a background operation associated with this file is running.
|
||||
*/
|
||||
get isLocked(): boolean {
|
||||
return this[fileLockedProperty];
|
||||
}
|
||||
|
||||
private checkAccess() {
|
||||
if (this.isLocked) {
|
||||
throw {
|
||||
message: "Cannot access a locked file."
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a Folder entity.
|
||||
*/
|
||||
export class Folder extends FileSystemEntity {
|
||||
/**
|
||||
* Attempts to access a Folder at the specified path and creates a new Folder if there is no existing one.
|
||||
*/
|
||||
public static fromPath(path: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (path: string) {
|
||||
var folder = new Folder();
|
||||
folder[pathProperty] = path;
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(folder);
|
||||
}
|
||||
}
|
||||
getFileAccess().getFolder(path, localSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a Folder with the specified path already exists.
|
||||
*/
|
||||
public static exists(path: string): boolean {
|
||||
return getFileAccess().folderExists(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether this Folder contains a file with the specified name.
|
||||
*/
|
||||
public containsFile(name: string): boolean {
|
||||
var fileAccess = getFileAccess();
|
||||
var path = fileAccess.concatPath(this.path, name);
|
||||
return fileAccess.fileExists(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the current Folder (recursively) from the file system.
|
||||
*/
|
||||
public delete(onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
getFileAccess().deleteFolder(this.path, this.isKnown, onSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all the files and folders (recursively), contained within this Folder.
|
||||
*/
|
||||
public empty(onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
getFileAccess().emptyFolder(this.path, onSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether this instance is a KnownFolder (accessed through the KnownFolders object).
|
||||
*/
|
||||
get isKnown(): boolean {
|
||||
return this[isKnownProperty];
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to open a File with the specified name within this Folder and optionally creates a new File if there is no existing one.
|
||||
*/
|
||||
public getFile(name: string, onSuccess: (file: File) => any, onError?: (error: any) => any, createIfNonExisting?: boolean) {
|
||||
var localSuccess = function (filePath: string) {
|
||||
var newFile = new File();
|
||||
|
||||
newFile[pathProperty] = filePath;
|
||||
newFile[nameProperty] = name;
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(newFile);
|
||||
}
|
||||
}
|
||||
var fileAccess = getFileAccess();
|
||||
var path = fileAccess.concatPath(this.path, name);
|
||||
fileAccess.getFile(path, localSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to open a Folder with the specified name within this Folder and optionally creates a new Folder if there is no existing one.
|
||||
*/
|
||||
public getFolder(name: string, onSuccess: (folder: Folder) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (filePath: string) {
|
||||
var newFolder = new Folder();
|
||||
|
||||
newFolder[pathProperty] = filePath;
|
||||
newFolder[nameProperty] = name;
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(newFolder);
|
||||
}
|
||||
}
|
||||
|
||||
var fileAccess = getFileAccess();
|
||||
var path = fileAccess.concatPath(this.path, name);
|
||||
fileAccess.getFolder(path, localSuccess, onError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the top-level files residing within this Folder.
|
||||
*/
|
||||
public enumFiles(onSuccess: (files: Array<File>) => any, onError?: (error: any) => any) {
|
||||
var localSuccess = function (paths: Array<string>) {
|
||||
if (onSuccess) {
|
||||
var files = new Array<File>();
|
||||
var i,
|
||||
path: string,
|
||||
file: File;
|
||||
|
||||
for (i = 0; i < files.length; i++) {
|
||||
file = new File();
|
||||
file[pathProperty] = files[i];
|
||||
files.push(file);
|
||||
}
|
||||
|
||||
onSuccess(files);
|
||||
}
|
||||
}
|
||||
getFileAccess().enumFiles(this.path, localSuccess, onError);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to the top-level Folders instances that are accessible from the application. Use these as entry points to access the FileSystem.
|
||||
*/
|
||||
export class KnownFolders {
|
||||
private static _documents: Folder;
|
||||
private static _temp: Folder;
|
||||
|
||||
/**
|
||||
* Gets the Documents folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
|
||||
*/
|
||||
public static Documents(): Folder {
|
||||
if (!KnownFolders._documents) {
|
||||
var path = getFileAccess().getDocumentsFolderPath();
|
||||
KnownFolders._documents = new Folder();
|
||||
KnownFolders._documents[pathProperty] = path;
|
||||
KnownFolders._documents[isKnownProperty] = true;
|
||||
}
|
||||
|
||||
return KnownFolders._documents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Temporary (Caches) folder available for the current application. This Folder is private for the application and not accessible from Users/External apps.
|
||||
*/
|
||||
public static Temporary(): Folder {
|
||||
if (!KnownFolders._temp) {
|
||||
var path = getFileAccess().getTempFolderPath();
|
||||
KnownFolders._temp = new Folder();
|
||||
KnownFolders._temp[pathProperty] = path;
|
||||
KnownFolders._temp[isKnownProperty] = true;
|
||||
}
|
||||
|
||||
return KnownFolders._temp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for FileReader and FileWriter APIs.
|
||||
*/
|
||||
export class FileAccess {
|
||||
private _file;
|
||||
|
||||
constructor(file: File) {
|
||||
this._file = file;
|
||||
this._file[fileLockedProperty] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks the file and allows other operations over it.
|
||||
*/
|
||||
public release() {
|
||||
this._file[fileLockedProperty] = false;
|
||||
this._file = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the underlying File instance.
|
||||
*/
|
||||
get file(): File {
|
||||
return this._file;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables reading the content of a File entity.
|
||||
*/
|
||||
export class FileReader extends FileAccess {
|
||||
/**
|
||||
* Reads the content of the underlying File as a UTF8 encoded string.
|
||||
*/
|
||||
public readText(onSuccess: (content: string) => any, onError?: (error: any) => any) {
|
||||
getFileAccess().readText(this.file.path, onSuccess, onError);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables saving data to a File entity.
|
||||
*/
|
||||
export class FileWriter extends FileAccess {
|
||||
public writeText(content: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
getFileAccess().writeText(this.file.path, content, onSuccess, onError);
|
||||
}
|
||||
}
|
@ -1,305 +1,301 @@
|
||||
import app_module = require("Application/application");
|
||||
|
||||
export module tk {
|
||||
export module io {
|
||||
export class FileSystemAccess {
|
||||
private _encoding = "UTF-8";
|
||||
private _pathSeparator = "/";
|
||||
export class FileSystemAccess {
|
||||
private _encoding = "UTF-8";
|
||||
private _pathSeparator = "/";
|
||||
|
||||
public getReadonly(path: string): boolean {
|
||||
var javaFile = new java.io.File(path);
|
||||
return javaFile.exists() && !javaFile.canWrite();
|
||||
public getReadonly(path: string): boolean {
|
||||
var javaFile = new java.io.File(path);
|
||||
return javaFile.exists() && !javaFile.canWrite();
|
||||
}
|
||||
|
||||
public getLastModified(path: string): Date {
|
||||
var javaFile = new java.io.File(path);
|
||||
return new Date(javaFile.lastModified());
|
||||
}
|
||||
|
||||
public getParent(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (onSuccess) {
|
||||
var parent = javaFile.getParentFile();
|
||||
onSuccess(parent.getPath());
|
||||
}
|
||||
} catch (exception) {
|
||||
// TODO: unified approach for error messages
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public getFile(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
this.ensureFile(new java.io.File(path), onSuccess, onError);
|
||||
}
|
||||
|
||||
public getFolder(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (javaFile.exists() && !javaFile.isDirectory()) {
|
||||
if (onError) {
|
||||
onError("The path " + path + "exists and is not a directory");
|
||||
}
|
||||
} else {
|
||||
this.ensureFile(javaFile, onSuccess, onError);
|
||||
}
|
||||
}
|
||||
|
||||
public enumFiles(path: string, onSuccess: (files: Array<string>) => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (!javaFile.isDirectory()) {
|
||||
if (onError) {
|
||||
onError("There is no folder existing at path " + path);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
public getLastModified(path: string): Date {
|
||||
var javaFile = new java.io.File(path);
|
||||
return new Date(javaFile.lastModified());
|
||||
}
|
||||
if (javaFile.isDirectory()) {
|
||||
var filesList: Array<java.io.File> = javaFile.listFiles();
|
||||
var filePaths = new Array<string>();
|
||||
|
||||
public getParent(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (onSuccess) {
|
||||
var parent = javaFile.getParentFile();
|
||||
onSuccess(parent.getPath());
|
||||
}
|
||||
} catch (exception) {
|
||||
// TODO: unified approach for error messages
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
var length = filesList.length,
|
||||
i,
|
||||
filePath;
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
javaFile = filesList[i];
|
||||
|
||||
if (javaFile.isFile()) {
|
||||
filePath = javaFile.getPath();
|
||||
filesList.push(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(filePaths);
|
||||
}
|
||||
}
|
||||
|
||||
public getFile(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
this.ensureFile(new java.io.File(path), onSuccess, onError);
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public fileExists(path: string): boolean {
|
||||
var file = new java.io.File(path);
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
public folderExists(path: string): boolean {
|
||||
var file = new java.io.File(path);
|
||||
return file.exists() && file.isDirectory();
|
||||
}
|
||||
|
||||
public concatPath(left: string, right: string): string {
|
||||
return left + this._pathSeparator + right;
|
||||
}
|
||||
|
||||
public deleteFile(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (!javaFile.isFile()) {
|
||||
if (onError) {
|
||||
onError({ message: "The specified parameter is not a File entity." });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
public getFolder(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (javaFile.exists() && !javaFile.isDirectory()) {
|
||||
if (onError) {
|
||||
onError("The path " + path + "exists and is not a directory");
|
||||
if (!javaFile.delete()) {
|
||||
if (onError) {
|
||||
onError({ message: "File deletion failed" });
|
||||
}
|
||||
} else if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public deleteFolder(path: string, isKnown?: boolean, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (!javaFile.isDirectory()) {
|
||||
if (onError) {
|
||||
onError({ message: "The specified parameter is not a Folder entity." });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Asynchronous
|
||||
this.deleteFolderContent(javaFile);
|
||||
|
||||
if (!isKnown) {
|
||||
if (javaFile.delete()) {
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} else {
|
||||
this.ensureFile(javaFile, onSuccess, onError);
|
||||
}
|
||||
}
|
||||
|
||||
public enumFiles(path: string, onSuccess: (files: Array<string>) => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (!javaFile.isDirectory()) {
|
||||
if (onError) {
|
||||
onError("There is no folder existing at path " + path);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (javaFile.isDirectory()) {
|
||||
var filesList: Array<java.io.File> = javaFile.listFiles();
|
||||
var filePaths = new Array<string>();
|
||||
|
||||
var length = filesList.length,
|
||||
i,
|
||||
filePath;
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
javaFile = filesList[i];
|
||||
|
||||
if (javaFile.isFile()) {
|
||||
filePath = javaFile.getPath();
|
||||
filesList.push(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(filePaths);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
onError({ message: "Folder deletion failed." });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// TODO: Notify error?
|
||||
}
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public emptyFolder(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (!javaFile.isDirectory()) {
|
||||
if (onError) {
|
||||
onError({ message: "The specified parameter is not a Folder entity." });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Asynchronous
|
||||
this.deleteFolderContent(javaFile);
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public rename(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: No implementation
|
||||
}
|
||||
|
||||
public getDocumentsFolderPath(): string {
|
||||
var context = app_module.tk.ui.Application.current.android.context;
|
||||
var dir: java.io.File = context.getFilesDir();
|
||||
return dir.getPath();
|
||||
}
|
||||
|
||||
public getTempFolderPath(): string {
|
||||
var context = app_module.tk.ui.Application.current.android.context;
|
||||
var dir: java.io.File = context.getCacheDir();
|
||||
return dir.getPath();
|
||||
}
|
||||
|
||||
public readText(path: string, onSuccess: (content: string) => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
var stream = new java.io.FileInputStream(javaFile);
|
||||
var reader = new java.io.InputStreamReader(stream, this._encoding);
|
||||
var bufferedReader = new java.io.BufferedReader(reader);
|
||||
|
||||
// TODO: We will need to read the entire file to a CharBuffer instead of reading it line by line
|
||||
// TODO: bufferedReader.read(CharBuffer) does not currently work
|
||||
var line = undefined;
|
||||
var result = "";
|
||||
while (true) {
|
||||
line = bufferedReader.readLine();
|
||||
if (!line) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (result.length > 0) {
|
||||
// add the new line manually to the result
|
||||
// TODO: Try with CharBuffer at a later stage, when the Bridge allows it
|
||||
// result += "\n";
|
||||
}
|
||||
result += line;
|
||||
}
|
||||
|
||||
bufferedReader.close();
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(result);
|
||||
}
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public writeText(path: string, content: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
var stream = new java.io.FileOutputStream(javaFile);
|
||||
var writer = new java.io.OutputStreamWriter(stream, this._encoding);
|
||||
var bufferedWriter = new java.io.BufferedWriter(writer);
|
||||
|
||||
bufferedWriter.write(content);
|
||||
bufferedWriter.close();
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private deleteFolderContent(file: java.io.File): boolean {
|
||||
var filesList = file.listFiles();
|
||||
|
||||
var i,
|
||||
childFile: java.io.File,
|
||||
success: boolean = false;
|
||||
|
||||
for (i = 0; i < filesList.length; i++) {
|
||||
childFile = filesList[i];
|
||||
if (childFile.isDirectory()) {
|
||||
success = this.deleteFolderContent(childFile);
|
||||
if (!success) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public fileExists(path: string): boolean {
|
||||
var file = new java.io.File(path);
|
||||
return file.exists();
|
||||
}
|
||||
success = childFile.delete();
|
||||
}
|
||||
|
||||
public folderExists(path: string): boolean {
|
||||
var file = new java.io.File(path);
|
||||
return file.exists() && file.isDirectory();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
public concatPath(left: string, right: string): string {
|
||||
return left + this._pathSeparator + right;
|
||||
}
|
||||
|
||||
public deleteFile(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (!javaFile.isFile()) {
|
||||
if (onError) {
|
||||
onError({ message: "The specified parameter is not a File entity." });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!javaFile.delete()) {
|
||||
if (onError) {
|
||||
onError({ message: "File deletion failed" });
|
||||
}
|
||||
} else if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public deleteFolder(path: string, isKnown?: boolean, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (!javaFile.isDirectory()) {
|
||||
if (onError) {
|
||||
onError({ message: "The specified parameter is not a Folder entity." });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Asynchronous
|
||||
this.deleteFolderContent(javaFile);
|
||||
|
||||
if (!isKnown) {
|
||||
if (javaFile.delete()) {
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} else {
|
||||
if (onError) {
|
||||
onError({ message: "Folder deletion failed." });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// TODO: Notify error?
|
||||
}
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public emptyFolder(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
if (!javaFile.isDirectory()) {
|
||||
if (onError) {
|
||||
onError({ message: "The specified parameter is not a Folder entity." });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Asynchronous
|
||||
this.deleteFolderContent(javaFile);
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public rename(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: No implementation
|
||||
}
|
||||
|
||||
public getDocumentsFolderPath(): string {
|
||||
var context = app_module.tk.ui.Application.current.android.context;
|
||||
var dir: java.io.File = context.getFilesDir();
|
||||
return dir.getPath();
|
||||
}
|
||||
|
||||
public getTempFolderPath(): string {
|
||||
var context = app_module.tk.ui.Application.current.android.context;
|
||||
var dir: java.io.File = context.getCacheDir();
|
||||
return dir.getPath();
|
||||
}
|
||||
|
||||
public readText(path: string, onSuccess: (content: string) => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
var stream = new java.io.FileInputStream(javaFile);
|
||||
var reader = new java.io.InputStreamReader(stream, this._encoding);
|
||||
var bufferedReader = new java.io.BufferedReader(reader);
|
||||
|
||||
// TODO: We will need to read the entire file to a CharBuffer instead of reading it line by line
|
||||
// TODO: bufferedReader.read(CharBuffer) does not currently work
|
||||
var line = undefined;
|
||||
var result = "";
|
||||
while (true) {
|
||||
line = bufferedReader.readLine();
|
||||
if (!line) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (result.length > 0) {
|
||||
// add the new line manually to the result
|
||||
// TODO: Try with CharBuffer at a later stage, when the Bridge allows it
|
||||
// result += "\n";
|
||||
}
|
||||
result += line;
|
||||
}
|
||||
|
||||
bufferedReader.close();
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(result);
|
||||
}
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public writeText(path: string, content: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
var javaFile = new java.io.File(path);
|
||||
var stream = new java.io.FileOutputStream(javaFile);
|
||||
var writer = new java.io.OutputStreamWriter(stream, this._encoding);
|
||||
var bufferedWriter = new java.io.BufferedWriter(writer);
|
||||
|
||||
bufferedWriter.write(content);
|
||||
bufferedWriter.close();
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} catch (exception) {
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private deleteFolderContent(file: java.io.File): boolean {
|
||||
var filesList = file.listFiles();
|
||||
|
||||
var i,
|
||||
childFile: java.io.File,
|
||||
success: boolean = false;
|
||||
|
||||
for (i = 0; i < filesList.length; i++) {
|
||||
childFile = filesList[i];
|
||||
if (childFile.isDirectory()) {
|
||||
success = this.deleteFolderContent(childFile);
|
||||
if (!success) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
success = childFile.delete();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
private ensureFile(javaFile: java.io.File, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
if (!javaFile.exists()) {
|
||||
if (!javaFile.createNewFile()) {
|
||||
// TODO: unified approach for error messages
|
||||
if (onError) {
|
||||
onError("Failed to create new file for path " + javaFile.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(javaFile.getPath());
|
||||
}
|
||||
} catch (exception) {
|
||||
private ensureFile(javaFile: java.io.File, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
try {
|
||||
if (!javaFile.exists()) {
|
||||
if (!javaFile.createNewFile()) {
|
||||
// TODO: unified approach for error messages
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
onError("Failed to create new file for path " + javaFile.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(javaFile.getPath());
|
||||
}
|
||||
} catch (exception) {
|
||||
// TODO: unified approach for error messages
|
||||
if (onError) {
|
||||
onError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
42
FileSystem/file_system_access.d.ts
vendored
42
FileSystem/file_system_access.d.ts
vendored
@ -1,26 +1,22 @@
|
||||
// TODO: Implement "hidden" notation so that such declarations are not included in the d.ts file we will provide for the users.
|
||||
//@hidden
|
||||
export declare module tk {
|
||||
export module io {
|
||||
export class FileSystemAccess {
|
||||
getReadonly(path: string): boolean;
|
||||
getLastModified(path: string): Date;
|
||||
export declare class FileSystemAccess {
|
||||
getReadonly(path: string): boolean;
|
||||
getLastModified(path: string): Date;
|
||||
|
||||
getParent(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any);
|
||||
getFile(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any);
|
||||
getFolder(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any);
|
||||
enumFiles(path: string, onSuccess: (files: Array<string>) => any, onError?: (error: any) => any);
|
||||
fileExists(path: string): boolean;
|
||||
folderExists(path: string): boolean;
|
||||
concatPath(left: string, right: string): string;
|
||||
deleteFile(path: string, onSuccess?: () => any, onError?: (error: any) => any);
|
||||
deleteFolder(path: string, isKnown: boolean, onSuccess?: () => any, onError?: (error: any) => any);
|
||||
emptyFolder(path: string, onSuccess?: () => any, onError?: (error: any) => any): void;
|
||||
rename(path: string, onSuccess?: () => any, onError?: (error: any) => any): void;
|
||||
getDocumentsFolderPath(): string;
|
||||
getTempFolderPath(): string;
|
||||
readText(path: string, onSuccess: (content: string) => any, onError?: (error: any) => any);
|
||||
writeText(path: string, content: string, onSuccess?: () => any, onError?: (error: any) => any);
|
||||
}
|
||||
}
|
||||
}
|
||||
getParent(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any);
|
||||
getFile(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any);
|
||||
getFolder(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any);
|
||||
enumFiles(path: string, onSuccess: (files: Array<string>) => any, onError?: (error: any) => any);
|
||||
fileExists(path: string): boolean;
|
||||
folderExists(path: string): boolean;
|
||||
concatPath(left: string, right: string): string;
|
||||
deleteFile(path: string, onSuccess?: () => any, onError?: (error: any) => any);
|
||||
deleteFolder(path: string, isKnown: boolean, onSuccess?: () => any, onError?: (error: any) => any);
|
||||
emptyFolder(path: string, onSuccess?: () => any, onError?: (error: any) => any): void;
|
||||
rename(path: string, onSuccess?: () => any, onError?: (error: any) => any): void;
|
||||
getDocumentsFolderPath(): string;
|
||||
getTempFolderPath(): string;
|
||||
readText(path: string, onSuccess: (content: string) => any, onError?: (error: any) => any);
|
||||
writeText(path: string, content: string, onSuccess?: () => any, onError?: (error: any) => any);
|
||||
}
|
@ -1,93 +1,89 @@
|
||||
import app_module = require("Application/application");
|
||||
|
||||
export module tk {
|
||||
export module io {
|
||||
export class FileSystemAccess {
|
||||
private keyFileType = "NSFileType";
|
||||
private keyModificationTime = "NSFileModificationDate";
|
||||
private keyReadonly = "NSFileImmutable";
|
||||
export class FileSystemAccess {
|
||||
private keyFileType = "NSFileType";
|
||||
private keyModificationTime = "NSFileModificationDate";
|
||||
private keyReadonly = "NSFileImmutable";
|
||||
|
||||
public getReadonly(path: string): boolean {
|
||||
// TODO: Not implemented
|
||||
return false;
|
||||
}
|
||||
|
||||
public getLastModified(path: string): Date {
|
||||
// TODO: Not implemented
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public getParent(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public getFile(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public getFolder(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public enumFiles(path: string, onSuccess: (files: Array<string>) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public fileExists(path: string): boolean {
|
||||
// TODO: Not implemented
|
||||
return false;
|
||||
}
|
||||
|
||||
public folderExists(path: string): boolean {
|
||||
// TODO: Not implemented
|
||||
return false;
|
||||
}
|
||||
|
||||
public concatPath(left: string, right: string): string {
|
||||
// TODO: Not implemented
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public deleteFile(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public deleteFolder(path: string, isKnown?: boolean, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public emptyFolder(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public rename(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: No implementation
|
||||
}
|
||||
|
||||
public getDocumentsFolderPath(): string {
|
||||
// TODO: Not implemented
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public getTempFolderPath(): string {
|
||||
// TODO: Not implemented
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public readText(path: string, onSuccess: (content: string) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public writeText(path: string, content: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
//private getKnownPath(folderType: number): string {
|
||||
// var fileManager = Foundation.NSFileManager.defaultManager();
|
||||
|
||||
|
||||
// return folder;
|
||||
//}
|
||||
}
|
||||
public getReadonly(path: string): boolean {
|
||||
// TODO: Not implemented
|
||||
return false;
|
||||
}
|
||||
|
||||
public getLastModified(path: string): Date {
|
||||
// TODO: Not implemented
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public getParent(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public getFile(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public getFolder(path: string, onSuccess: (path: string) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public enumFiles(path: string, onSuccess: (files: Array<string>) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public fileExists(path: string): boolean {
|
||||
// TODO: Not implemented
|
||||
return false;
|
||||
}
|
||||
|
||||
public folderExists(path: string): boolean {
|
||||
// TODO: Not implemented
|
||||
return false;
|
||||
}
|
||||
|
||||
public concatPath(left: string, right: string): string {
|
||||
// TODO: Not implemented
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public deleteFile(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public deleteFolder(path: string, isKnown?: boolean, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public emptyFolder(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public rename(path: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: No implementation
|
||||
}
|
||||
|
||||
public getDocumentsFolderPath(): string {
|
||||
// TODO: Not implemented
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public getTempFolderPath(): string {
|
||||
// TODO: Not implemented
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public readText(path: string, onSuccess: (content: string) => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
public writeText(path: string, content: string, onSuccess?: () => any, onError?: (error: any) => any) {
|
||||
// TODO: Not implemented
|
||||
}
|
||||
|
||||
//private getKnownPath(folderType: number): string {
|
||||
// var fileManager = Foundation.NSFileManager.defaultManager();
|
||||
|
||||
|
||||
// return folder;
|
||||
//}
|
||||
}
|
Reference in New Issue
Block a user