From 98c0a760785b00cc5c6c74280968f8c4dc1c17e8 Mon Sep 17 00:00:00 2001 From: Pancakes Date: Thu, 14 Nov 2024 00:28:56 -0500 Subject: [PATCH] Add new GetScript endpoint --- api/packages/getscript.go | 53 +++++++++++++++++++++++++++++++++++++++ cloudbox.go | 1 + 2 files changed, 54 insertions(+) create mode 100644 api/packages/getscript.go diff --git a/api/packages/getscript.go b/api/packages/getscript.go new file mode 100644 index 0000000..9394da7 --- /dev/null +++ b/api/packages/getscript.go @@ -0,0 +1,53 @@ +/* + cloudbox - the toybox server emulator + Copyright (C) 2024 patapancakes + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package packages + +import ( + "fmt" + "net/http" + "strconv" + + "github.com/flatgrassdotnet/cloudbox/db" + "github.com/flatgrassdotnet/cloudbox/utils" +) + +func GetScript(w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(r.URL.Query().Get("id")) + if err != nil { + utils.WriteError(w, r, fmt.Sprintf("failed to parse id value: %s", err)) + return + } + + rev, _ := strconv.Atoi(r.URL.Query().Get("rev")) + if rev < 1 { + rev, err = db.FetchPackageLatestRevision(id) + if err != nil { + utils.WriteError(w, r, fmt.Sprintf("failed to fetch package latest revision: %s", err)) + return + } + } + + pkg, err := db.FetchPackage(id, rev) + if err != nil { + utils.WriteError(w, r, fmt.Sprintf("failed to fetch package: %s", err)) + return + } + + w.Write(pkg.Data) +} diff --git a/cloudbox.go b/cloudbox.go index 238353c..2deef6e 100644 --- a/cloudbox.go +++ b/cloudbox.go @@ -57,6 +57,7 @@ func main() { http.HandleFunc("GET api.cl0udb0x.com/auth/getid", auth.GetID) http.HandleFunc("GET api.cl0udb0x.com/packages/list", packages.List) http.HandleFunc("GET api.cl0udb0x.com/packages/get", packages.Get) + http.HandleFunc("GET api.cl0udb0x.com/packages/getscript", packages.GetGMA) http.HandleFunc("GET api.cl0udb0x.com/packages/getgma", packages.GetGMA) http.HandleFunc("GET api.cl0udb0x.com/packages/publishsave", packages.PublishSave) http.HandleFunc("GET api.cl0udb0x.com/content/get", content.Get)