go-roblox/asset.go
2021-03-17 08:41:51 -04:00

95 lines
1.9 KiB
Go

package roblox
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
)
type AssetUploadOptions struct {
Name string
AssetID int
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&type=Model&genreTypeId=1")
if err != nil {
return aresp, err
}
query := endpoint.Query()
query.Set("name", options.Name)
query.Set("description", options.Description)
query.Set("assetid", strconv.Itoa(options.AssetID))
// 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/WinInet")
req.Header.Set("content-type", "application/xml")
req.Header.Set("accept", "application/json")
// 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
}