2023-10-11 04:16:52 +02:00
|
|
|
import { Utils } from "../../Utils"
|
|
|
|
import Constants from "../../Models/Constants"
|
|
|
|
import { LayerConfigJson } from "../../Models/ThemeConfig/Json/LayerConfigJson"
|
|
|
|
|
|
|
|
export default class StudioServer {
|
2023-10-13 18:46:56 +02:00
|
|
|
private readonly url: string
|
2023-10-11 04:16:52 +02:00
|
|
|
|
|
|
|
constructor(url: string) {
|
2023-10-13 18:46:56 +02:00
|
|
|
this.url = url
|
2023-10-11 04:16:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async fetchLayerOverview(): Promise<Set<string>> {
|
|
|
|
const { allFiles } = <{ allFiles: string[] }>(
|
2023-10-13 18:46:56 +02:00
|
|
|
await Utils.downloadJson(this.url + "/overview")
|
2023-10-11 04:16:52 +02:00
|
|
|
)
|
|
|
|
const layers = allFiles
|
|
|
|
.filter((f) => f.startsWith("layers/"))
|
|
|
|
.map((l) => l.substring(l.lastIndexOf("/") + 1, l.length - ".json".length))
|
|
|
|
.filter((layerId) => Constants.priviliged_layers.indexOf(<any>layerId) < 0)
|
|
|
|
return new Set<string>(layers)
|
|
|
|
}
|
|
|
|
|
2023-10-13 18:46:56 +02:00
|
|
|
async fetchLayer(layerId: string): Promise<LayerConfigJson> {
|
2023-10-11 04:16:52 +02:00
|
|
|
try {
|
2023-10-16 15:06:50 +02:00
|
|
|
return await Utils.downloadJson(this.layerUrl(layerId))
|
2023-10-11 04:16:52 +02:00
|
|
|
} catch (e) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
2023-10-13 18:46:56 +02:00
|
|
|
|
|
|
|
async updateLayer(config: LayerConfigJson) {
|
|
|
|
const id = config.id
|
|
|
|
if (id === undefined || id === "") {
|
|
|
|
return
|
|
|
|
}
|
2023-10-16 15:06:50 +02:00
|
|
|
await fetch(this.layerUrl(id), {
|
2023-10-13 18:46:56 +02:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json;charset=utf-8",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(config, null, " "),
|
|
|
|
})
|
|
|
|
}
|
2023-10-16 15:06:50 +02:00
|
|
|
|
|
|
|
public layerUrl(id: string) {
|
|
|
|
return `${this.url}/layers/${id}/${id}.json`
|
|
|
|
}
|
2023-10-11 04:16:52 +02:00
|
|
|
}
|