sync
This commit is contained in:
@@ -2,17 +2,21 @@ package game
|
||||
|
||||
import (
|
||||
"cloudsave/pkg/tools/id"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type (
|
||||
Metadata struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Version int `json:"version"`
|
||||
}
|
||||
)
|
||||
|
||||
@@ -99,8 +103,66 @@ func Remove(gameID string) error {
|
||||
}
|
||||
|
||||
func Hash(gameID string) (string, error) {
|
||||
content, err := os.ReadFile(filepath.Join(datastorepath, d.Name(), "data.tar.gz"))
|
||||
if err != nil {
|
||||
path := filepath.Join(datastorepath, gameID, "data.tar.gz")
|
||||
|
||||
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
hasher := md5.New()
|
||||
if _, err := io.Copy(hasher, f); err != nil {
|
||||
return "", err
|
||||
}
|
||||
sum := hasher.Sum(nil)
|
||||
return hex.EncodeToString(sum), nil
|
||||
}
|
||||
|
||||
func Version(gameID string) (int, error) {
|
||||
path := filepath.Join(datastorepath, gameID, "metadata.json")
|
||||
|
||||
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var metadata Metadata
|
||||
d := json.NewDecoder(f)
|
||||
err = d.Decode(&metadata)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return metadata.Version, nil
|
||||
}
|
||||
|
||||
func SetVersion(gameID string, version int) error {
|
||||
path := filepath.Join(datastorepath, gameID, "metadata.json")
|
||||
|
||||
f, err := os.OpenFile(path, os.O_RDWR, 0740)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var metadata Metadata
|
||||
d := json.NewDecoder(f)
|
||||
err = d.Decode(&metadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.Seek(0, io.SeekStart)
|
||||
|
||||
metadata.Version = version
|
||||
|
||||
e := json.NewEncoder(f)
|
||||
err = e.Encode(metadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
96
pkg/remote/client/client.go
Normal file
96
pkg/remote/client/client.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"cloudsave/pkg/remote/obj"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type (
|
||||
Client struct {
|
||||
baseURL string
|
||||
username string
|
||||
password string
|
||||
}
|
||||
)
|
||||
|
||||
func New(baseURL, username, password string) *Client {
|
||||
return &Client{
|
||||
baseURL: baseURL,
|
||||
username: username,
|
||||
password: password,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Hash(gameID string) (string, error) {
|
||||
u, err := url.JoinPath(c.baseURL, "api", "v1", "game", gameID, "hash")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
o, err := c.get(u)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if h, ok := (o).(string); ok {
|
||||
return h, nil
|
||||
}
|
||||
|
||||
return "", errors.New("invalid payload sent by the server")
|
||||
}
|
||||
|
||||
func (c *Client) Version(gameID string) (int, error) {
|
||||
u, err := url.JoinPath(c.baseURL, "api", "v1", "game", gameID, "version")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
o, err := c.get(u)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if h, ok := (o).(int); ok {
|
||||
return h, nil
|
||||
}
|
||||
|
||||
return 0, errors.New("invalid payload sent by the server")
|
||||
}
|
||||
|
||||
func (c *Client) get(url string) (any, error) {
|
||||
cli := http.Client{}
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.SetBasicAuth(c.username, c.password)
|
||||
|
||||
res, err := cli.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("server returns an unexpected status code: %d %s", res.StatusCode, res.Status)
|
||||
}
|
||||
|
||||
var httpObject obj.HTTPObject
|
||||
d := json.NewDecoder(res.Body)
|
||||
err = d.Decode(&httpObject)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return httpObject, nil
|
||||
}
|
||||
|
||||
func (c *Client) post() {
|
||||
|
||||
}
|
||||
22
pkg/remote/obj/obj.go
Normal file
22
pkg/remote/obj/obj.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package obj
|
||||
|
||||
import "time"
|
||||
|
||||
type (
|
||||
HTTPCore struct {
|
||||
Status int `json:"status"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
HTTPError struct {
|
||||
HTTPCore
|
||||
Error string `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
HTTPObject struct {
|
||||
HTTPCore
|
||||
Data any `json:"data"`
|
||||
}
|
||||
)
|
||||
26
pkg/tools/prompt/credentials/credentials.go
Normal file
26
pkg/tools/prompt/credentials/credentials.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package credentials
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func Read() (string, string, error) {
|
||||
fmt.Print("Enter username: ")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
username, _ := reader.ReadString('\n')
|
||||
username = strings.TrimSpace(username)
|
||||
|
||||
fmt.Printf("password for %s: ", username)
|
||||
password, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
return username, string(password), nil
|
||||
}
|
||||
Reference in New Issue
Block a user