From 35ce8c4707c1b708d68b6dc9dae68dfc0f79c04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lie=20DELHAIE?= Date: Thu, 15 May 2025 01:16:37 +0200 Subject: [PATCH] compile plus --- cmd/cli/commands/sync/sync.go | 30 ++++++++++++++++++--- cmd/server/api/api.go | 51 +++++++++++++++++++++++++++++++---- pkg/game/game.go | 7 +++++ 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/cmd/cli/commands/sync/sync.go b/cmd/cli/commands/sync/sync.go index 8dd477f..dfaad50 100644 --- a/cmd/cli/commands/sync/sync.go +++ b/cmd/cli/commands/sync/sync.go @@ -3,8 +3,10 @@ package sync import ( "cloudsave/pkg/remote" "context" + "crypto/md5" "flag" "fmt" + "io" "os" "github.com/google/subcommands" @@ -33,9 +35,31 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) return subcommands.ExitFailure } - /*for _, remote := range remotes { - - }*/ + for _, remote := range remotes { + + } return subcommands.ExitSuccess } + +func hash(path string) string { + 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) + return + } + + // Get checksum result + sum := hasher.Sum(nil) +} diff --git a/cmd/server/api/api.go b/cmd/server/api/api.go index 2cd015f..e0cc0ee 100644 --- a/cmd/server/api/api.go +++ b/cmd/server/api/api.go @@ -2,6 +2,7 @@ package api import ( "cloudsave/pkg/game" + "crypto/md5" "encoding/json" "fmt" "io" @@ -48,13 +49,14 @@ func NewServer(documentRoot string, creds map[string]string, port int) *HTTPServ // Secured routes r.Group(func(secureRouter chi.Router) { // Save files routes - secureRouter.Route("/games", func(gameRouter chi.Router) { + secureRouter.Route("/games", func(gamesRouter chi.Router) { // List all available saves - gameRouter.Get("/", s.all) + gamesRouter.Get("/", s.all) // Data routes - gameRouter.Group(func(uploadRouter chi.Router) { - uploadRouter.Post("/{id}/data", s.upload) - uploadRouter.Get("/{id}/data", s.download) + gamesRouter.Group(func(saveRouter chi.Router) { + saveRouter.Post("/{id}/data", s.upload) + saveRouter.Get("/{id}/data", s.download) + saveRouter.Get("/{id}/hash", s.hash) }) }) }) @@ -179,3 +181,42 @@ func (s HTTPServer) upload(w http.ResponseWriter, r *http.Request) { // Respond success w.WriteHeader(http.StatusCreated) } + +func (s HTTPServer) hash(w http.ResponseWriter, r *http.Request) { + id := chi.URLParam(r, "id") + path := filepath.Clean(filepath.Join(s.documentRoot, "data", id)) + + sdir, err := os.Stat(path) + if err != nil { + notFound("id not found", w, r) + return + } + + if !sdir.IsDir() { + 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) + return + } + + // Get checksum result + sum := hasher.Sum(nil) + ok(sum, w, r) +} diff --git a/pkg/game/game.go b/pkg/game/game.go index 919d837..528fd09 100644 --- a/pkg/game/game.go +++ b/pkg/game/game.go @@ -97,3 +97,10 @@ func Remove(gameID string) error { } return nil } + +func Hash(gameID string) (string, error) { + content, err := os.ReadFile(filepath.Join(datastorepath, d.Name(), "data.tar.gz")) + if err != nil { + + } +}