2 Commits

Author SHA1 Message Date
13adb26fba Merge pull request 'fix error while sync new save' (#3) from fix into main
Reviewed-on: #3
2025-08-08 21:35:46 +02:00
23ffa93615 fix error while sync new save 2025-08-08 21:30:58 +02:00
3 changed files with 21 additions and 34 deletions

12
.vscode/launch.json vendored
View File

@@ -4,9 +4,17 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "Launch Package", "name": "server",
"type": "go",
"request": "launch",
"mode": "auto",
"args": ["-document-root", "${workspaceFolder}/env"],
"console": "integratedTerminal",
"program": "${workspaceFolder}/cmd/server"
},
{
"name": "cli",
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "auto", "mode": "auto",

View File

@@ -3,12 +3,9 @@ package api
import ( import (
"cloudsave/cmd/server/data" "cloudsave/cmd/server/data"
"cloudsave/pkg/repository" "cloudsave/pkg/repository"
"crypto/md5"
"encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
@@ -343,41 +340,19 @@ func (s HTTPServer) histExists(w http.ResponseWriter, r *http.Request) {
func (s HTTPServer) hash(w http.ResponseWriter, r *http.Request) { func (s HTTPServer) hash(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id") id := chi.URLParam(r, "id")
path := filepath.Clean(filepath.Join(s.documentRoot, "data", id))
sdir, err := os.Stat(path) sum, err := data.Hash(id, s.documentRoot)
if err != nil { if err != nil {
notFound("id not found", w, r) if errors.Is(err, data.ErrNotExists) {
return notFound("id not found", w, r)
} return
}
if !sdir.IsDir() { fmt.Fprintln(os.Stderr, "error: an error occured while calculating the hash:", err)
notFound("id not found", w, r)
return
}
path = filepath.Join(path, "data.tar.gz")
f, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
notFound("id not found", w, r)
return
}
defer f.Close()
// Create MD5 hasher
hasher := md5.New()
// Copy file content into hasher
if _, err := io.Copy(hasher, f); err != nil {
fmt.Fprintln(os.Stderr, "error: an error occured while reading data:", err)
internalServerError(w, r) internalServerError(w, r)
return return
} }
// Get checksum result ok(sum, w, r)
sum := hasher.Sum(nil)
ok(hex.EncodeToString(sum), w, r)
} }
func (s HTTPServer) metadata(w http.ResponseWriter, r *http.Request) { func (s HTTPServer) metadata(w http.ResponseWriter, r *http.Request) {

View File

@@ -23,6 +23,7 @@ type (
var ( var (
ErrBackupNotExists error = errors.New("backup not found") ErrBackupNotExists error = errors.New("backup not found")
ErrNotExists error = errors.New("not found")
// singleton // singleton
hashCacheMu sync.RWMutex hashCacheMu sync.RWMutex
@@ -175,6 +176,9 @@ func Hash(gameID, documentRoot string) (string, error) {
sdir, err := os.Stat(path) sdir, err := os.Stat(path)
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", ErrNotExists
}
return "", err return "", err
} }