Compare commits
5 Commits
f16da548b6
...
d84bfb7eba
Author | SHA1 | Date | |
---|---|---|---|
d84bfb7eba | |||
|
3574e3d70d | ||
a09b6e34a3 | |||
41119b5966 | |||
9652a2630e |
38
asset.go
38
asset.go
@ -1,16 +1,18 @@
|
||||
package roblox
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AssetUploadOptions struct {
|
||||
Name string
|
||||
AssetID int
|
||||
Description string
|
||||
Public bool
|
||||
Comments bool
|
||||
@ -22,17 +24,16 @@ type AssetUploadResponse struct {
|
||||
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")
|
||||
func (s *Session) CreateAsset(options *AssetUploadOptions, f io.Reader) (int, error) {
|
||||
endpoint, err := url.Parse("https://data.roblox.com/Data/Upload.ashx?json=1&type=Model&genreTypeId=1")
|
||||
if err != nil {
|
||||
return aresp, err
|
||||
return -1, 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 {
|
||||
@ -55,25 +56,38 @@ func (s *Session) CreateAsset(options *AssetUploadOptions, f io.Reader) (AssetUp
|
||||
|
||||
endpoint.RawQuery = query.Encode()
|
||||
|
||||
req, err := http.NewRequest("POST", endpoint.String(), f)
|
||||
req, err := http.NewRequest("POST", endpoint.String(), nil)
|
||||
req.Header.Set("user-agent", "Roblox")
|
||||
|
||||
// Perform request
|
||||
resp, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
return aresp, err
|
||||
return -1, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == 403 && resp.Header.Get("X-Csrf-Token") != "" {
|
||||
req, err := http.NewRequest("POST", endpoint.String(), f)
|
||||
req.Header.Set("user-agent", "Roblox")
|
||||
req.Header.Set("x-csrf-token", strings.Trim(resp.Header["X-Csrf-Token"][0], " "))
|
||||
// Perform request
|
||||
resp, err = s.client.Do(req)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return aresp, fmt.Errorf(resp.Status)
|
||||
return -1, fmt.Errorf(resp.Status)
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&aresp); err != nil {
|
||||
return aresp, err
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return aresp, nil
|
||||
return strconv.Atoi(string(body))
|
||||
}
|
||||
|
||||
func (s *Session) Download(id int) (io.Reader, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user