2023-10-11 04:16:52 +02:00
|
|
|
import { Utils } from "../../Utils"
|
|
|
|
import Constants from "../../Models/Constants"
|
|
|
|
import { LayerConfigJson } from "../../Models/ThemeConfig/Json/LayerConfigJson"
|
2023-10-24 22:01:10 +02:00
|
|
|
import { Store } from "../../Logic/UIEventSource"
|
2024-01-19 17:31:35 +01:00
|
|
|
import { LayoutConfigJson } from "../../Models/ThemeConfig/Json/LayoutConfigJson"
|
2023-10-11 04:16:52 +02:00
|
|
|
|
|
|
|
export default class StudioServer {
|
2023-10-13 18:46:56 +02:00
|
|
|
private readonly url: string
|
2023-10-24 22:01:10 +02:00
|
|
|
private readonly _userId: Store<number>
|
2023-10-11 04:16:52 +02:00
|
|
|
|
2023-10-24 22:01:10 +02:00
|
|
|
constructor(url: string, userId: Store<number>) {
|
2023-10-13 18:46:56 +02:00
|
|
|
this.url = url
|
2023-10-24 22:01:10 +02:00
|
|
|
this._userId = userId
|
2023-10-11 04:16:52 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 13:58:45 +02:00
|
|
|
public async fetchOverview(): Promise<
|
2023-10-24 22:01:10 +02:00
|
|
|
{
|
|
|
|
id: string
|
|
|
|
owner: number
|
2023-10-26 13:58:45 +02:00
|
|
|
category: "layers" | "themes"
|
2023-10-24 22:01:10 +02:00
|
|
|
}[]
|
|
|
|
> {
|
2023-10-11 04:16:52 +02:00
|
|
|
const { allFiles } = <{ allFiles: string[] }>(
|
2023-10-30 13:45:44 +01:00
|
|
|
await Utils.downloadJson(this.url + "/overview")
|
2023-10-11 04:16:52 +02:00
|
|
|
)
|
2023-10-24 22:01:10 +02:00
|
|
|
const layerOverview: {
|
|
|
|
id: string
|
|
|
|
owner: number | undefined
|
2023-10-26 13:58:45 +02:00
|
|
|
category: "layers" | "themes"
|
2023-10-24 22:01:10 +02:00
|
|
|
}[] = []
|
|
|
|
for (let file of allFiles) {
|
2023-10-30 13:45:44 +01:00
|
|
|
let parts = file.split("/")
|
|
|
|
let owner = Number(parts[0])
|
|
|
|
if (!isNaN(owner)) {
|
|
|
|
parts.splice(0, 1)
|
2023-10-24 22:01:10 +02:00
|
|
|
file = file.substring(file.indexOf("/") + 1)
|
2023-10-30 13:45:44 +01:00
|
|
|
} else {
|
|
|
|
owner = undefined
|
2023-10-24 22:01:10 +02:00
|
|
|
}
|
2023-10-30 13:45:44 +01:00
|
|
|
const category = <"layers" | "themes">parts[0]
|
2023-10-24 22:01:10 +02:00
|
|
|
const id = file.substring(file.lastIndexOf("/") + 1, file.length - ".json".length)
|
|
|
|
if (Constants.priviliged_layers.indexOf(<any>id) > 0) {
|
|
|
|
continue
|
|
|
|
}
|
2023-10-26 13:58:45 +02:00
|
|
|
layerOverview.push({ id, owner, category })
|
2023-10-24 22:01:10 +02:00
|
|
|
}
|
|
|
|
return layerOverview
|
2023-10-11 04:16:52 +02:00
|
|
|
}
|
|
|
|
|
2024-01-19 17:31:35 +01:00
|
|
|
async fetch(layerId: string, category: "layers", uid?: number): Promise<LayerConfigJson>
|
|
|
|
async fetch(layerId: string, category: "themes", uid?: number): Promise<LayoutConfigJson>
|
2023-10-30 13:45:44 +01:00
|
|
|
async fetch(
|
|
|
|
layerId: string,
|
|
|
|
category: "layers" | "themes",
|
|
|
|
uid?: number
|
2024-01-19 17:31:35 +01:00
|
|
|
): Promise<LayerConfigJson | LayoutConfigJson> {
|
2023-10-11 04:16:52 +02:00
|
|
|
try {
|
2023-10-30 13:45:44 +01:00
|
|
|
return await Utils.downloadJson(this.urlFor(layerId, category, uid))
|
2023-10-11 04:16:52 +02:00
|
|
|
} catch (e) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
2023-12-02 00:24:55 +01:00
|
|
|
async delete(id: string, category: "layers" | "themes") {
|
|
|
|
if (id === undefined || id === "") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await fetch(this.urlFor(id, category), {
|
2023-12-19 22:08:00 +01:00
|
|
|
method: "DELETE",
|
2023-12-02 00:24:55 +01:00
|
|
|
})
|
|
|
|
}
|
2023-10-26 13:58:45 +02:00
|
|
|
async update(id: string, config: string, category: "layers" | "themes") {
|
2023-10-13 18:46:56 +02:00
|
|
|
if (id === undefined || id === "") {
|
|
|
|
return
|
|
|
|
}
|
2023-10-26 13:58:45 +02:00
|
|
|
await fetch(this.urlFor(id, category), {
|
2023-10-13 18:46:56 +02:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json;charset=utf-8",
|
|
|
|
},
|
2023-10-24 22:01:10 +02:00
|
|
|
body: config,
|
2023-10-13 18:46:56 +02:00
|
|
|
})
|
|
|
|
}
|
2023-10-16 15:06:50 +02:00
|
|
|
|
|
|
|
public layerUrl(id: string) {
|
2023-10-26 13:58:45 +02:00
|
|
|
return this.urlFor(id, "layers")
|
|
|
|
}
|
|
|
|
|
2023-10-30 13:45:44 +01:00
|
|
|
public urlFor(id: string, category: "layers" | "themes", uid?: number) {
|
|
|
|
uid ??= this._userId.data
|
2023-10-24 22:01:10 +02:00
|
|
|
const uidStr = uid !== undefined ? "/" + uid : ""
|
2023-10-26 13:58:45 +02:00
|
|
|
return `${this.url}${uidStr}/${category}/${id}/${id}.json`
|
2023-10-16 15:06:50 +02:00
|
|
|
}
|
2023-10-11 04:16:52 +02:00
|
|
|
}
|