From 0a3e6e5b8e5c83fa08dbaa73be1995d66d5da3fe Mon Sep 17 00:00:00 2001 From: itzaname Date: Mon, 7 Sep 2020 22:45:07 -0400 Subject: [PATCH] Add asset publishing/downloading --- asset.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ 2 files changed, 93 insertions(+) create mode 100644 asset.go create mode 100644 go.mod diff --git a/asset.go b/asset.go new file mode 100644 index 0000000..4d6df6b --- /dev/null +++ b/asset.go @@ -0,0 +1,90 @@ +package roblox + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "strconv" +) + +type AssetUploadOptions struct { + Name string + Description string + Public bool + Comments bool + Group int +} + +type AssetUploadResponse struct { + AssetID int64 `json:"AssetId"` + AssetVersionID int64 `json:"AssetVersionId"` +} + +func (s *Session) CreateAsset(options *AssetUploadOptions, f io.Reader) (AssetUploadResponse, error) { + var aresp AssetUploadResponse + + endpoint, err := url.Parse("https://data.roblox.com/Data/Upload.ashx?json=1&assetid=0&type=Model&genreTypeId=1") + if err != nil { + return aresp, err + } + + query := endpoint.Query() + query.Set("name", options.Name) + query.Set("description", options.Description) + + // Comments + if options.Comments { + query.Set("allowComments", "true") + } else { + query.Set("allowComments", "false") + } + + // Public + if options.Public { + query.Set("ispublic", "true") + } else { + query.Set("ispublic", "false") + } + + // Group + if options.Group > 0 { + query.Set("groupId", strconv.Itoa(options.Group)) + } + + endpoint.RawQuery = query.Encode() + + req, err := http.NewRequest("POST", endpoint.String(), f) + req.Header.Set("user-agent", "Roblox") + + // Perform request + resp, err := s.client.Do(req) + if err != nil { + return aresp, err + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return aresp, fmt.Errorf(resp.Status) + } + + if err := json.NewDecoder(resp.Body).Decode(&aresp); err != nil { + return aresp, err + } + + return aresp, nil +} + +func (s *Session) Download(id int) (io.Reader, error) { + resp, err := s.client.Get("https://assetgame.roblox.com/Asset/?id=" + strconv.Itoa(id)) + if err != nil { + return nil, err + } + + if resp.StatusCode != 200 { + return nil, fmt.Errorf(resp.Status) + } + + return resp.Body, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1134bc8 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module ci.itzana.me/itzaname/go-roblox + +go 1.15