lancherPrjCtrl.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { useCtx } from "../ctx";
  2. import { Controller } from "../core/controller";
  3. import { Base64 } from "js-base64";
  4. type GetAssetPathFunc = (ext: string) => string;
  5. export class ProjectController extends Controller {
  6. RootDir = "";
  7. DataDir = "";
  8. HostURL = "";
  9. _swiftLocal = true;
  10. _spu3dFile: any;
  11. UserId = "spu3d";
  12. NatsProfile = { apiPort: "", wsPort: "", ip: "" };
  13. async onReady() {
  14. // const params = new URLSearchParams(location.search);
  15. // const projectPath = params.get("path") as string;
  16. // this.RootDir = projectPath;
  17. const deviceCtrl = useCtx().deviceCtrl;
  18. deviceCtrl.GetAppDataDir().then((dir) => {
  19. this.DataDir = dir;
  20. this.RootDir = dir;
  21. });
  22. // if (!projectPath) {
  23. // deviceCtrl.SetMainTitle("spu3d");
  24. // return;
  25. // }
  26. // deviceCtrl.SetMainTitle("项目:" + projectPath);
  27. // this.HostURL = await deviceCtrl.StartHttpServer(this.RootDir);
  28. // console.log("host url=>", this.HostURL);
  29. this.NatsProfile = await deviceCtrl.GetNatsProfile();
  30. console.log("nats profile=>", this.NatsProfile);
  31. }
  32. getOutputDir() {
  33. return this.RootDir + "/" + "outputs";
  34. }
  35. getAppDataDir() {
  36. return this.DataDir;
  37. }
  38. getAppInstallDir() {
  39. return this.createPath("installDir");
  40. }
  41. getDefaultLogo() {
  42. return this.DataDir + "/static/thumbnail.png";
  43. }
  44. createPath(fpath: string) {
  45. return this.RootDir + "/" + fpath;
  46. }
  47. createBase64Path(fpath: string) {
  48. const str = Base64.encode(this.createPath(fpath));
  49. console.log("str", str);
  50. return str;
  51. }
  52. getSwiftUri(assetPath: string) {
  53. if (this._swiftLocal) return this.getLocalAbsoluteUri(assetPath);
  54. return this.getHttpAbsoluteUri(assetPath);
  55. }
  56. getHttpAbsoluteUri(url: string) {
  57. if (!url) return "";
  58. if (url.substring(0, 2) == "//") return "http://" + url;
  59. if (url.substring(0, 4) == "http") return url;
  60. if (url.charAt(0) == "/") return this.HostURL + url.substring(1);
  61. return this.HostURL + url;
  62. }
  63. getLocalAbsoluteUri(url: string) {
  64. if (!url) return "";
  65. if (url.substring(0, 2) == "//") return "http://" + url;
  66. if (url.substring(0, 4) == "http") return url;
  67. if (url.charAt(0) == "/") return this.RootDir + url;
  68. return this.RootDir + "/" + url;
  69. }
  70. getRelativeUri(url: string) {
  71. let s = this.HostURL.length;
  72. let pre = url.substring(0, s);
  73. if (pre == this.HostURL) return url.substring(s + 1);
  74. s = this.RootDir.length;
  75. pre = url.substring(0, s);
  76. if (pre == this.RootDir) return url.substring(s + 1);
  77. return "";
  78. }
  79. async pickProject() {
  80. const deviceCtrl = useCtx().deviceCtrl;
  81. const projectDir = await deviceCtrl.SelectDir();
  82. if (await this.isProjectExit(projectDir)) {
  83. return projectDir;
  84. } else {
  85. return Promise.reject();
  86. }
  87. }
  88. async isProjectExit(dir: string) {
  89. const deviceCtrl = useCtx().deviceCtrl;
  90. return await deviceCtrl.IsFileExit(`${dir}/project.spu3d`);
  91. }
  92. isProjectFile(fpath: string) {
  93. const r = fpath.substring(0, this.RootDir.length);
  94. return r == this.RootDir;
  95. }
  96. async copy2Project(fpath: string, getAssetPath: GetAssetPathFunc) {
  97. if (this.isProjectFile(fpath)) {
  98. return fpath.substring(this.RootDir.length + 1);
  99. }
  100. const ext = fpath.substring(fpath.lastIndexOf("."));
  101. const assetfpath = getAssetPath(ext);
  102. await useCtx().deviceCtrl.CopyFile(fpath, this.createPath(assetfpath));
  103. return assetfpath;
  104. }
  105. }