multiple fix again

This commit is contained in:
2025-08-18 20:52:06 +02:00
parent 2ff191fecf
commit 0d92b6b8a0
4 changed files with 36 additions and 40 deletions

View File

@@ -94,13 +94,6 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
pg.Describe(fmt.Sprintf("[%s] Fetching metadata...", g.Name))
hremote, err := cli.Hash(r.GameID)
if err != nil {
destroyPg()
fmt.Fprintln(os.Stderr, "error: failed to get the file hash from the remote:", err)
continue
}
remoteMetadata, err := cli.Metadata(r.GameID)
if err != nil {
destroyPg()
@@ -118,7 +111,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
slog.Warn("failed to push backup files", "err", err)
}
if g.MD5 == hremote {
if g.MD5 == remoteMetadata.MD5 {
destroyPg()
if g.Version != remoteMetadata.Version {
slog.Debug("version is not the same, but the hash is equal. Updating local database")

View File

@@ -60,7 +60,6 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
gamesRouter.Group(func(saveRouter chi.Router) {
saveRouter.Post("/{id}/data", s.upload)
saveRouter.Get("/{id}/data", s.download)
saveRouter.Get("/{id}/hash", s.hash)
saveRouter.Get("/{id}/metadata", s.metadata)
saveRouter.Get("/{id}/hist", s.allHist)
@@ -272,22 +271,6 @@ func (s HTTPServer) histExists(w http.ResponseWriter, r *http.Request) {
ok(finfo, w, r)
}
func (s HTTPServer) hash(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
m, err := s.Service.One(id)
if err != nil {
if errors.Is(err, repository.ErrNotFound) {
notFound("not found", w, r)
return
}
fmt.Fprintln(os.Stderr, "error: an error occured while calculating the hash:", err)
internalServerError(w, r)
return
}
ok(m.MD5, w, r)
}
func (s HTTPServer) metadata(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")

View File

@@ -49,7 +49,7 @@ func New(baseURL, username, password string) *Client {
}
func (c *Client) Exists(gameID string) (bool, error) {
u, err := url.JoinPath(c.baseURL, "api", "v1", "games", gameID, "hash")
u, err := url.JoinPath(c.baseURL, "api", "v1", "games", gameID, "metadata")
if err != nil {
return false, err
}
@@ -104,22 +104,13 @@ func (c *Client) Version() (Information, error) {
return Information{}, errors.New("invalid payload sent by the server")
}
// Deprecated: use c.Metadata instead
func (c *Client) Hash(gameID string) (string, error) {
u, err := url.JoinPath(c.baseURL, "api", "v1", "games", gameID, "hash")
m, err := c.Metadata(gameID)
if err != nil {
return "", err
}
o, err := c.get(u)
if err != nil {
return "", err
}
if h, ok := (o.Data).(string); ok {
return h, nil
}
return "", errors.New("invalid payload sent by the server")
return m.MD5, nil
}
func (c *Client) Metadata(gameID string) (repository.Metadata, error) {
@@ -139,6 +130,7 @@ func (c *Client) Metadata(gameID string) (repository.Metadata, error) {
Name: m["name"].(string),
Version: int(m["version"].(float64)),
Date: customtime.MustParse(time.RFC3339, m["date"].(string)),
MD5: m["md5"].(string),
}
return gm, nil
}
@@ -355,6 +347,7 @@ func (c *Client) All() ([]repository.Metadata, error) {
Name: v["name"].(string),
Version: int(v["version"].(float64)),
Date: customtime.MustParse(time.RFC3339, v["date"].(string)),
MD5: v["md5"].(string),
}
res = append(res, gm)
}

View File

@@ -9,6 +9,7 @@ import (
"log/slog"
"os"
"path/filepath"
"sync"
"time"
)
@@ -19,7 +20,7 @@ type (
Path string `json:"path"`
Version int `json:"version"`
Date time.Time `json:"date"`
MD5 string `json:"-"`
MD5 string `json:"md5,omitempty"`
}
Remote struct {
@@ -61,6 +62,7 @@ type (
EagerRepository struct {
Repository
mu sync.RWMutex
data map[string]Data
}
@@ -189,6 +191,7 @@ func (l *LazyRepository) WriteBlob(ID Identifier) (io.Writer, error) {
}
func (l *LazyRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
m.MD5 = ""
path := l.DataPath(id)
slog.Debug("writing metadata", "id", id, "metadata", m)
@@ -392,6 +395,9 @@ func NewEagerRepository(dataRootPath string) (*EagerRepository, error) {
}
func (r *EagerRepository) Preload() error {
r.mu.Lock()
defer r.mu.Unlock()
games, err := r.Repository.All()
if err != nil {
return fmt.Errorf("failed to load all data: %w", err)
@@ -435,6 +441,9 @@ func (r *EagerRepository) Preload() error {
}
func (r *EagerRepository) All() ([]string, error) {
r.mu.RLock()
defer r.mu.RUnlock()
var res []string
for _, g := range r.data {
res = append(res, g.Metadata.ID)
@@ -444,6 +453,9 @@ func (r *EagerRepository) All() ([]string, error) {
}
func (r *EagerRepository) AllHist(id GameIdentifier) ([]string, error) {
r.mu.RLock()
defer r.mu.RUnlock()
var res []string
if d, ok := r.data[id.gameID]; ok {
for _, b := range d.Backup {
@@ -454,12 +466,15 @@ func (r *EagerRepository) AllHist(id GameIdentifier) ([]string, error) {
}
func (r *EagerRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
r.mu.Lock()
defer r.mu.Unlock()
err := r.Repository.WriteMetadata(id, m)
if err != nil {
return err
}
// reload from disk because md5
// reload from disk because of md5
m, err = r.Repository.Metadata(id)
if err != nil {
return err
@@ -473,6 +488,9 @@ func (r *EagerRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
}
func (r *EagerRepository) Metadata(id GameIdentifier) (Metadata, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if d, ok := r.data[id.gameID]; ok {
return d.Metadata, nil
}
@@ -480,6 +498,9 @@ func (r *EagerRepository) Metadata(id GameIdentifier) (Metadata, error) {
}
func (r *EagerRepository) Backup(id BackupIdentifier) (Backup, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if d, ok := r.data[id.gameID]; ok {
if b, ok := d.Backup[id.backupID]; ok {
return b, nil
@@ -489,6 +510,9 @@ func (r *EagerRepository) Backup(id BackupIdentifier) (Backup, error) {
}
func (r *EagerRepository) SetRemote(id GameIdentifier, url string) error {
r.mu.Lock()
defer r.mu.Unlock()
err := r.Repository.SetRemote(id, url)
if err != nil {
return err
@@ -505,6 +529,9 @@ func (r *EagerRepository) SetRemote(id GameIdentifier, url string) error {
}
func (r *EagerRepository) Remove(id GameIdentifier) error {
r.mu.Lock()
defer r.mu.Unlock()
if err := r.Repository.Remove(id); err != nil {
return err
}