import { useCtx } from "../ctx"; import { Controller } from "../core/controller"; import { Base64 } from "js-base64"; type GetAssetPathFunc = (ext: string) => string; export class ProjectController extends Controller { RootDir = ""; DataDir = ""; HostURL = ""; _swiftLocal = true; _spu3dFile: any; UserId = "spu3d"; NatsProfile = { apiPort: "", wsPort: "", ip: "" }; async onReady() { // const params = new URLSearchParams(location.search); // const projectPath = params.get("path") as string; // this.RootDir = projectPath; const deviceCtrl = useCtx().deviceCtrl; deviceCtrl.GetAppDataDir().then((dir) => { this.DataDir = dir; this.RootDir = dir; }); // if (!projectPath) { // deviceCtrl.SetMainTitle("spu3d"); // return; // } // deviceCtrl.SetMainTitle("项目:" + projectPath); // this.HostURL = await deviceCtrl.StartHttpServer(this.RootDir); // console.log("host url=>", this.HostURL); this.NatsProfile = await deviceCtrl.GetNatsProfile(); console.log("nats profile=>", this.NatsProfile); } getOutputDir() { return this.RootDir + "/" + "outputs"; } getAppDataDir() { return this.DataDir; } getAppInstallDir() { return this.createPath("installDir"); } getDefaultLogo() { return this.DataDir + "/static/thumbnail.png"; } createPath(fpath: string) { return this.RootDir + "/" + fpath; } createBase64Path(fpath: string) { const str = Base64.encode(this.createPath(fpath)); console.log("str", str); return str; } getSwiftUri(assetPath: string) { if (this._swiftLocal) return this.getLocalAbsoluteUri(assetPath); return this.getHttpAbsoluteUri(assetPath); } getHttpAbsoluteUri(url: string) { if (!url) return ""; if (url.substring(0, 2) == "//") return "http://" + url; if (url.substring(0, 4) == "http") return url; if (url.charAt(0) == "/") return this.HostURL + url.substring(1); return this.HostURL + url; } getLocalAbsoluteUri(url: string) { if (!url) return ""; if (url.substring(0, 2) == "//") return "http://" + url; if (url.substring(0, 4) == "http") return url; if (url.charAt(0) == "/") return this.RootDir + url; return this.RootDir + "/" + url; } getRelativeUri(url: string) { let s = this.HostURL.length; let pre = url.substring(0, s); if (pre == this.HostURL) return url.substring(s + 1); s = this.RootDir.length; pre = url.substring(0, s); if (pre == this.RootDir) return url.substring(s + 1); return ""; } async pickProject() { const deviceCtrl = useCtx().deviceCtrl; const projectDir = await deviceCtrl.SelectDir(); if (await this.isProjectExit(projectDir)) { return projectDir; } else { return Promise.reject(); } } async isProjectExit(dir: string) { const deviceCtrl = useCtx().deviceCtrl; return await deviceCtrl.IsFileExit(`${dir}/project.spu3d`); } isProjectFile(fpath: string) { const r = fpath.substring(0, this.RootDir.length); return r == this.RootDir; } async copy2Project(fpath: string, getAssetPath: GetAssetPathFunc) { if (this.isProjectFile(fpath)) { return fpath.substring(this.RootDir.length + 1); } const ext = fpath.substring(fpath.lastIndexOf(".")); const assetfpath = getAssetPath(ext); await useCtx().deviceCtrl.CopyFile(fpath, this.createPath(assetfpath)); return assetfpath; } }