Skip to content

Commit b272450

Browse files
feat: FileServer
1 parent 935cfbd commit b272450

File tree

5 files changed

+355
-259
lines changed

5 files changed

+355
-259
lines changed

src/fileServer/fileServer.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {FileObject} from "../fileSystem/fileObject";
2+
import {Log} from "../lib/Log";
3+
4+
5+
class FileServer {
6+
private readonly file: FileObject;
7+
private readonly port: number;
8+
private httpServer:Server | undefined;
9+
private readonly log:Log = new Log("fileServer");
10+
11+
constructor(port:number,file:FileObject) {
12+
this.file = file;
13+
this.port = port;
14+
}
15+
16+
start(onSuccess: (msg: any) => void, onError: (err: any) => void,):void{
17+
this.httpServer = CreateServer(this.port,onSuccess,onError)
18+
19+
// @ts-ignore
20+
httpServer.setOnRequestHandler(this.handleRequest.bind(this));
21+
}
22+
23+
private handleRequest(req: { requestId: string; path: string }): void {
24+
this.log.d("Request received:", req);
25+
// handle file serving logic here
26+
this.log.d("Received request:", req.requestId);
27+
this.log.d("Request Path", req.path);
28+
this.sendText("This is a test",req.requestId,null)
29+
this.log.d("Response sent")
30+
}
31+
32+
private sendText(text:string, id:string, mimeType:string | null | undefined) {
33+
this.httpServer?.send(id, {
34+
status: 200,
35+
body: text,
36+
headers: {
37+
"Content-Type": mimeType || "text/html",
38+
},
39+
},()=>{},this.log.e);
40+
}
41+
42+
43+
}

src/fileSystem/DocumentFileWrapper.ts

Lines changed: 0 additions & 248 deletions
This file was deleted.

src/fileSystem/NativeFileWrapper.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
1-
import { FileObject } from "./fileObject";
1+
import {FileObject} from "./fileObject";
22

33
declare var cordova: any;
44

5+
6+
//alternative for internalFs.js
57
export class NativeFileWrapper implements FileObject {
6-
private readonly path: string;
8+
private path: string | undefined;
9+
10+
//always check if fileobject is ready before calling any class function
11+
ready: Promise<void>;
12+
13+
714

8-
constructor(absolutePath: string) {
9-
this.path = absolutePath;
10-
//console.log(`[NativeFileWrapper] Created for path: ${absolutePath}`);
15+
private removePrefix(str: string, prefix: string): string {
16+
return str.startsWith(prefix) ? str.slice(prefix.length) : str;
1117
}
1218

19+
20+
constructor(absolutePathOrUri: string,onReady:(obj:NativeFileWrapper)=>void) {
21+
this.ready = (async () => {
22+
let temp = absolutePathOrUri;
23+
24+
if (absolutePathOrUri.startsWith("cdvfile://")) {
25+
temp = await new Promise<string>((resolve, reject) => {
26+
// @ts-ignore
27+
window.resolveLocalFileSystemURL(
28+
absolutePathOrUri,
29+
(entry: any) => resolve(entry.toURL()),
30+
reject
31+
);
32+
});
33+
}
34+
35+
this.path = this.removePrefix(temp, "file://");
36+
37+
onReady(this)
38+
})();
39+
}
40+
41+
42+
43+
44+
1345
private execPlugin(action: string, args: any[] = []): Promise<any> {
1446
//console.log(`[NativeFileWrapper] execPlugin called: action=${action}, args=${JSON.stringify(args)}`);
1547
return new Promise((resolve, reject) => {
@@ -90,7 +122,7 @@ export class NativeFileWrapper implements FileObject {
90122
const childPath = await this.execPlugin('getChildByName', [name]);
91123
//console.log(`[getChildByName] path=${this.path}, name=${name}, childPath=${childPath}`);
92124
if (childPath && childPath !== "") {
93-
return new NativeFileWrapper(childPath);
125+
return new NativeFileWrapper(childPath,()=>{});
94126
}
95127
return null;
96128
} catch (error) {
@@ -126,7 +158,7 @@ export class NativeFileWrapper implements FileObject {
126158
const parentPath = await this.execPlugin('getParentFile');
127159
//console.log(`[getParentFile] path=${this.path}, parentPath=${parentPath}`);
128160
if (parentPath && parentPath !== "") {
129-
return new NativeFileWrapper(parentPath);
161+
return new NativeFileWrapper(parentPath,()=>{});
130162
}
131163
return null;
132164
} catch (error) {
@@ -194,7 +226,7 @@ export class NativeFileWrapper implements FileObject {
194226
try {
195227
const paths: string[] = await this.execPlugin('listFiles');
196228
//console.log(`[listFiles] path=${this.path}, files=${JSON.stringify(paths)}`);
197-
return paths.map(path => new NativeFileWrapper(path));
229+
return paths.map(path => new NativeFileWrapper(path,()=>{}));
198230
} catch (error) {
199231
console.error(`[listFiles] path=${this.path}, error=${error}`);
200232
return [];
@@ -236,9 +268,8 @@ export class NativeFileWrapper implements FileObject {
236268

237269
async toUri(): Promise<string> {
238270
try {
239-
const uri = await this.execPlugin('toUri');
240271
//console.log(`[toUri] path=${this.path}, uri=${uri}`);
241-
return uri;
272+
return await this.execPlugin('toUri');
242273
} catch (error) {
243274
console.error(`[toUri] path=${this.path}, error=${error}`);
244275
return `file://${this.path}`;
@@ -257,6 +288,6 @@ export class NativeFileWrapper implements FileObject {
257288

258289
getPath(): string {
259290
//console.log(`[getPath] returning path=${this.path}`);
260-
return this.path;
291+
return this.path!!;
261292
}
262293
}

0 commit comments

Comments
 (0)