Skip to content

Commit a42d157

Browse files
committed
updates
1 parent 5d7be2d commit a42d157

31 files changed

+2622
-1984
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
declare var com;
2+
3+
export class FileManager {
4+
5+
public static writeFile(bytes: any, path: string, callback: (...args) => void) {
6+
const listener = new com.github.triniwiz.async.Async2.FileManager.Callback({
7+
onError(param0: string, param1: java.lang.Exception): void {
8+
callback(param0, null);
9+
},
10+
onComplete(param0: any): void {
11+
callback(null, param0);
12+
}
13+
});
14+
if (bytes instanceof java.nio.ByteBuffer) {
15+
com.github.triniwiz.async.Async2.FileManager.writeFile(bytes.array(), path, listener);
16+
} else if (bytes instanceof ArrayBuffer) {
17+
if ((bytes as any).nativeObject) {
18+
com.github.triniwiz.async.Async2.FileManager.writeFile((bytes as any).nativeObject.array(), path, listener);
19+
}
20+
} else {
21+
com.github.triniwiz.async.Async2.FileManager.writeFile(bytes, path, listener);
22+
}
23+
24+
}
25+
26+
public static readFile(path: string, options: Options = {asStream: false}, callback: (...args) => void) {
27+
//const opts = new com.github.triniwiz.async.Async2.FileManager.Options();
28+
//opts.asStream = options.asStream;
29+
com.github.triniwiz.async.Async2.FileManager.readFile(path, null, new com.github.triniwiz.async.Async2.FileManager.Callback({
30+
onError(param0: string, param1: java.lang.Exception): void {
31+
callback(param0, null);
32+
},
33+
onComplete(param0: any): void {
34+
callback(null, param0);
35+
}
36+
}));
37+
}
38+
39+
public static deleteFile(path: string, options: Options = {asStream: false}, callback: (...args) => void) {
40+
// TODO
41+
callback(null, true);
42+
}
43+
}
44+
45+
export interface Options {
46+
asStream?: boolean;
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface Options {
2+
asStream?: boolean;
3+
}
4+
5+
export class FileManager {
6+
public static writeFile(bytes: any, path: string, callback: (...args) => void);
7+
8+
public static readFile(path: string, options: Options, callback: (...args) => void);
9+
10+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const background_queue = dispatch_get_global_queue(qos_class_t.QOS_CLASS_DEFAULT, 0);
2+
const main_queue = dispatch_get_current_queue();
3+
export class FileManager {
4+
5+
public static writeFile(bytes: any, path: string, callback: (...args) => void) {
6+
dispatch_async(background_queue, () => {
7+
try {
8+
if (bytes instanceof NSData) {
9+
bytes.writeToFileAtomically(path, true);
10+
} else if (bytes instanceof ArrayBuffer) {
11+
NSData.dataWithData(bytes as any).writeToFileAtomically(path, true);
12+
}
13+
dispatch_async(main_queue, ()=>{
14+
callback(null, path);
15+
});
16+
} catch (e) {
17+
dispatch_async(main_queue, ()=>{
18+
callback(e, null);
19+
});
20+
}
21+
});
22+
}
23+
24+
public static readFile(path: string, options: Options = {asStream: false}, callback: (...args) => void) {
25+
dispatch_async(background_queue, () => {
26+
try {
27+
const data = NSData.dataWithContentsOfFile(path);
28+
dispatch_async(main_queue, ()=>{
29+
callback(null, data);
30+
});
31+
} catch (e) {
32+
dispatch_async(main_queue, ()=>{
33+
callback(e, null);
34+
});
35+
}
36+
});
37+
}
38+
39+
public static deleteFile(path: string, options: Options = {asStream: false}, callback: (...args) => void) {
40+
dispatch_async(background_queue, () => {
41+
try {
42+
NSFileManager.defaultManager.removeItemAtPathError(path);
43+
dispatch_async(main_queue, ()=>{
44+
callback(null, true);
45+
});
46+
} catch (e) {
47+
dispatch_async(main_queue, ()=>{
48+
callback(e, false);
49+
});
50+
}
51+
});
52+
}
53+
}
54+
55+
export interface Options {
56+
asStream?: boolean;
57+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
export interface ISaveImageSettings {
2+
numberOfRequests?: number;
3+
removeAfterDays?: number;
4+
storageKey?: string;
5+
}
6+
7+
export const SaveImageStorageKey = 'http.saved-images';
8+
export function isImageUrl(url: string) {
9+
return url && /(http(s?):)([/|.|\w|\s|-])*\.(?:jpg|gif|png)/ig.test(url);
10+
}
11+
export function fileNameFromPath(fullPath: string) {
12+
let filename = '';
13+
if (fullPath) {
14+
filename = fullPath.replace(/^.*[\\\/]/ig, '');
15+
}
16+
return filename;
17+
}
18+
19+
export class TNSHttpSettings {
20+
static debug: boolean = false;
21+
static saveImage: ISaveImageSettings = {};
22+
static currentlySavedImages: { [url: string]: {
23+
date: number;
24+
requests: number;
25+
localPath?: string;
26+
}
27+
} = {}
28+
}
29+
30+
31+
export class ProgressEvent {
32+
private _type: string;
33+
private _lengthComputable: boolean;
34+
private _loaded: number;
35+
private _total: number;
36+
private _target: any;
37+
38+
constructor(
39+
type: string,
40+
data: { lengthComputable: boolean; loaded: number; total: number, target: any } = {
41+
lengthComputable: false,
42+
loaded: 0,
43+
total: 0,
44+
target: {}
45+
}
46+
) {
47+
this._type = type;
48+
this._lengthComputable = data.lengthComputable;
49+
this._loaded = data.loaded;
50+
this._total = data.total;
51+
this._target = data.target;
52+
}
53+
54+
get lengthComputable(): boolean {
55+
return this._lengthComputable;
56+
}
57+
58+
get loaded(): number {
59+
return this._loaded;
60+
}
61+
62+
get total(): number {
63+
return this._total;
64+
}
65+
66+
get type(): string {
67+
return this._type;
68+
}
69+
70+
get target(): any {
71+
return this._target;
72+
}
73+
}
74+
75+
export type Headers = { [key: string]: string | string[] } | Map<string, string>;
76+
77+
export enum HttpError {
78+
Error,
79+
Timeout,
80+
Cancelled
81+
}
82+
83+
export interface HttpRequestOptions {
84+
method: string;
85+
url: string;
86+
headers?: Headers;
87+
content?: any;
88+
timeout?: number;
89+
onProgress?: (event: any) => void;
90+
onHeaders?: (...args) => void;
91+
onLoading?: () => void;
92+
}
93+
94+
export interface HttpDownloadRequestOptions {
95+
url: string;
96+
headers?: Headers;
97+
filePath?: any;
98+
timeout?: number;
99+
onProgress?: (event: any) => void;
100+
onHeaders?: (...args) => void;
101+
onLoading?: () => void;
102+
}
103+
104+
export enum HttpResponseEncoding {
105+
UTF8,
106+
GBK
107+
}
108+
109+
export interface HttpResponse {
110+
statusCode: number;
111+
content: any;
112+
headers: Headers;
113+
url: string;
114+
}
115+
116+
export interface HttpDownloadResponse {
117+
statusCode: number;
118+
filePath: string;
119+
headers: Headers;
120+
url: string;
121+
}
122+

0 commit comments

Comments
 (0)