2020-03-02 19:43:02 +01:00
|
|
|
import * as http from "http"
|
|
|
|
import * as querystring from "querystring"
|
2020-03-16 18:43:32 +01:00
|
|
|
import { Application } from "../../common/api"
|
2020-03-02 19:43:02 +01:00
|
|
|
import { HttpCode, HttpError } from "../../common/http"
|
2020-03-16 18:43:32 +01:00
|
|
|
import { normalize } from "../../common/util"
|
2020-03-02 19:43:02 +01:00
|
|
|
import { HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http"
|
|
|
|
import { ApiHttpProvider } from "./api"
|
|
|
|
import { UpdateHttpProvider } from "./update"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dashboard HTTP provider.
|
|
|
|
*/
|
|
|
|
export class DashboardHttpProvider extends HttpProvider {
|
|
|
|
public constructor(
|
|
|
|
options: HttpProviderOptions,
|
|
|
|
private readonly api: ApiHttpProvider,
|
|
|
|
private readonly update: UpdateHttpProvider,
|
|
|
|
) {
|
|
|
|
super(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
2020-03-02 21:39:12 +01:00
|
|
|
if (route.requestPath !== "/index.html") {
|
|
|
|
throw new HttpError("Not found", HttpCode.NotFound)
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:43:02 +01:00
|
|
|
switch (route.base) {
|
2020-03-16 18:43:32 +01:00
|
|
|
case "/spawn": {
|
2020-03-02 19:43:02 +01:00
|
|
|
this.ensureAuthenticated(request)
|
2020-03-02 21:39:12 +01:00
|
|
|
this.ensureMethod(request, "POST")
|
2020-03-02 19:43:02 +01:00
|
|
|
const data = await this.getData(request)
|
2020-03-16 18:43:32 +01:00
|
|
|
const app = data ? querystring.parse(data) : {}
|
|
|
|
if (app.path) {
|
|
|
|
return { redirect: Array.isArray(app.path) ? app.path[0] : app.path }
|
|
|
|
}
|
|
|
|
if (!app.exec) {
|
|
|
|
throw new Error("No exec was provided")
|
|
|
|
}
|
|
|
|
this.api.spawnProcess(Array.isArray(app.exec) ? app.exec[0] : app.exec)
|
2020-03-02 19:43:02 +01:00
|
|
|
return { redirect: this.options.base }
|
|
|
|
}
|
2020-03-16 18:43:32 +01:00
|
|
|
case "/app":
|
2020-03-02 19:43:02 +01:00
|
|
|
case "/": {
|
|
|
|
this.ensureMethod(request)
|
2020-03-02 21:39:12 +01:00
|
|
|
if (!this.authenticated(request)) {
|
2020-03-02 19:43:02 +01:00
|
|
|
return { redirect: "/login", query: { to: this.options.base } }
|
|
|
|
}
|
2020-03-16 18:43:32 +01:00
|
|
|
return route.base === "/" ? this.getRoot(route) : this.getAppRoot(route)
|
2020-03-02 19:43:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new HttpError("Not found", HttpCode.NotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getRoot(route: Route): Promise<HttpResponse> {
|
|
|
|
const base = this.base(route)
|
|
|
|
const apps = await this.api.installedApplications()
|
|
|
|
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/home.html")
|
|
|
|
response.content = response.content
|
|
|
|
.replace(/{{UPDATE:NAME}}/, await this.getUpdate(base))
|
|
|
|
.replace(
|
|
|
|
/{{APP_LIST:EDITORS}}/,
|
|
|
|
this.getAppRows(
|
|
|
|
base,
|
|
|
|
apps.filter((app) => app.categories && app.categories.includes("Editor")),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
/{{APP_LIST:OTHER}}/,
|
|
|
|
this.getAppRows(
|
|
|
|
base,
|
|
|
|
apps.filter((app) => !app.categories || !app.categories.includes("Editor")),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return this.replaceTemplates(route, response)
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:43:32 +01:00
|
|
|
public async getAppRoot(route: Route): Promise<HttpResponse> {
|
|
|
|
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/app.html")
|
|
|
|
return this.replaceTemplates(route, response)
|
2020-03-02 19:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private getAppRows(base: string, apps: ReadonlyArray<Application>): string {
|
|
|
|
return apps.length > 0
|
|
|
|
? apps.map((app) => this.getAppRow(base, app)).join("\n")
|
2020-03-16 18:43:32 +01:00
|
|
|
: `<div class="none">No applications found.</div>`
|
2020-03-02 19:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private getAppRow(base: string, app: Application): string {
|
2020-03-16 18:43:32 +01:00
|
|
|
return `<form class="block-row${app.exec ? " -x11" : ""}" method="post" action="${normalize(
|
|
|
|
`${base}${this.options.base}/spawn`,
|
|
|
|
)}">
|
|
|
|
<button class="item -row -link">
|
|
|
|
<input type="hidden" name="path" value="${app.path || ""}">
|
|
|
|
<input type="hidden" name="exec" value="${app.exec || ""}">
|
2020-03-02 19:43:02 +01:00
|
|
|
${
|
|
|
|
app.icon
|
|
|
|
? `<img class="icon" src="data:image/png;base64,${app.icon}"></img>`
|
2020-03-16 18:43:32 +01:00
|
|
|
: `<span class="icon -missing"></span>`
|
2020-03-02 19:43:02 +01:00
|
|
|
}
|
2020-03-16 18:43:32 +01:00
|
|
|
<span class="name">${app.name}</span>
|
|
|
|
</button>
|
|
|
|
</form>`
|
2020-03-02 19:43:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getUpdate(base: string): Promise<string> {
|
|
|
|
if (!this.update.enabled) {
|
|
|
|
return `<div class="block-row"><div class="item"><div class="sub">Updates are disabled</div></div></div>`
|
|
|
|
}
|
|
|
|
|
|
|
|
const humanize = (time: number): string => {
|
|
|
|
const d = new Date(time)
|
|
|
|
const pad = (t: number): string => (t < 10 ? "0" : "") + t
|
|
|
|
return (
|
|
|
|
`${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}` +
|
|
|
|
` ${pad(d.getHours())}:${pad(d.getMinutes())}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const update = await this.update.getUpdate()
|
|
|
|
if (this.update.isLatestVersion(update)) {
|
|
|
|
return `<div class="block-row">
|
|
|
|
<div class="item">
|
|
|
|
Latest: ${update.version}
|
|
|
|
<div class="sub">Up to date</div>
|
|
|
|
</div>
|
|
|
|
<div class="item">
|
|
|
|
${humanize(update.checked)}
|
|
|
|
<a class="sub -link" href="${base}/update/check?to=${this.options.base}">Check now</a>
|
|
|
|
</div>
|
|
|
|
<div class="item" >Current: ${this.update.currentVersion}</div>
|
|
|
|
</div>`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<div class="block-row">
|
|
|
|
<div class="item">
|
|
|
|
Latest: ${update.version}
|
|
|
|
<div class="sub">Out of date</div>
|
|
|
|
</div>
|
|
|
|
<div class="item">
|
|
|
|
${humanize(update.checked)}
|
2020-03-02 21:39:12 +01:00
|
|
|
<a class="sub -link" href="${base}/update?to=${this.options.base}">Update now</a>
|
2020-03-02 19:43:02 +01:00
|
|
|
</div>
|
|
|
|
<div class="item" >Current: ${this.update.currentVersion}</div>
|
|
|
|
</div>`
|
|
|
|
}
|
|
|
|
}
|