Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a14571c31 | |||
| 0f2c0e511f | |||
| 0d92b6b8a0 | |||
| 2ff191fecf | |||
| b2425d310b | |||
| 97cd8f065f | |||
| 573fba708e |
11
.vscode/launch.json
vendored
11
.vscode/launch.json
vendored
@@ -4,6 +4,15 @@
|
|||||||
// 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": "web",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"args": ["-config", "${workspaceFolder}/env/config.json"],
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"program": "${workspaceFolder}/cmd/web"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "server",
|
"name": "server",
|
||||||
"type": "go",
|
"type": "go",
|
||||||
@@ -18,7 +27,7 @@
|
|||||||
"type": "go",
|
"type": "go",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mode": "auto",
|
"mode": "auto",
|
||||||
"args": ["run"],
|
"args": ["sync"],
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"program": "${workspaceFolder}/cmd/cli"
|
"program": "${workspaceFolder}/cmd/cli"
|
||||||
}
|
}
|
||||||
|
|||||||
4
build.sh
4
build.sh
@@ -47,11 +47,11 @@ for platform in "${platforms[@]}"; do
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$MAKE_PACKAGE" == "true" ]; then
|
if [ "$MAKE_PACKAGE" == "true" ]; then
|
||||||
CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} go build -o build/cloudsave_server$EXT -a ./cmd/server
|
CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} GORISCV64=rva22u64 GOAMD64=v3 GOARM64=v8.2 go build -o build/cloudsave_server$EXT -a ./cmd/server
|
||||||
tar -czf build/server_${platform_split[0]}_${platform_split[1]}.tar.gz build/cloudsave_server$EXT
|
tar -czf build/server_${platform_split[0]}_${platform_split[1]}.tar.gz build/cloudsave_server$EXT
|
||||||
rm build/cloudsave_server$EXT
|
rm build/cloudsave_server$EXT
|
||||||
else
|
else
|
||||||
CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} go build -o build/cloudsave_server_${platform_split[0]}_${platform_split[1]}$EXT -a ./cmd/server
|
CGO_ENABLED=0 GOOS=${platform_split[0]} GOARCH=${platform_split[1]} GORISCV64=rva22u64 GOAMD64=v3 GOARM64=v8.2 go build -o build/cloudsave_server_${platform_split[0]}_${platform_split[1]}$EXT -a ./cmd/server
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func (p *AddCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
|
|||||||
return subcommands.ExitFailure
|
return subcommands.ExitFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.Service.Scan(gameID); err != nil {
|
if _, err := p.Service.Scan(gameID); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to scan:", err)
|
fmt.Fprintln(os.Stderr, "error: failed to scan:", err)
|
||||||
return subcommands.ExitFailure
|
return subcommands.ExitFailure
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,9 @@ func (p *ListCmd) local(includeBackup bool) error {
|
|||||||
for _, g := range games {
|
for _, g := range games {
|
||||||
fmt.Println("ID:", g.ID)
|
fmt.Println("ID:", g.ID)
|
||||||
fmt.Println("Name:", g.Name)
|
fmt.Println("Name:", g.Name)
|
||||||
fmt.Println("Last Version:", g.Date, "( Version Number", g.Version, ")")
|
fmt.Println("Last Version:", g.Date)
|
||||||
|
fmt.Println("Version:", g.Version)
|
||||||
|
fmt.Println("MD5:", g.MD5)
|
||||||
if includeBackup {
|
if includeBackup {
|
||||||
bk, err := p.Service.AllBackups(g.ID)
|
bk, err := p.Service.AllBackups(g.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -108,7 +110,9 @@ func (p *ListCmd) server(url, username, password string, includeBackup bool) err
|
|||||||
for _, g := range games {
|
for _, g := range games {
|
||||||
fmt.Println("ID:", g.ID)
|
fmt.Println("ID:", g.ID)
|
||||||
fmt.Println("Name:", g.Name)
|
fmt.Println("Name:", g.Name)
|
||||||
fmt.Println("Last Version:", g.Date, "( Version Number", g.Version, ")")
|
fmt.Println("Last Version:", g.Date)
|
||||||
|
fmt.Println("Version:", g.Version)
|
||||||
|
fmt.Println("MD5:", g.MD5)
|
||||||
if includeBackup {
|
if includeBackup {
|
||||||
bk, err := cli.ListArchives(g.ID)
|
bk, err := cli.ListArchives(g.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ func (p *RemoteCmd) print() error {
|
|||||||
for _, g := range games {
|
for _, g := range games {
|
||||||
r, err := remote.One(g.ID)
|
r, err := remote.One(g.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to load datastore: %w", err)
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
cli := client.New(r.URL, "", "")
|
cli := client.New(r.URL, "", "")
|
||||||
|
|||||||
@@ -37,15 +37,16 @@ func (p *RunCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, metadata := range datastore {
|
for _, metadata := range datastore {
|
||||||
if err := p.Service.MakeBackup(metadata.ID); err != nil {
|
changed, err := p.Service.Scan(metadata.ID)
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to make backup:", err)
|
if err != nil {
|
||||||
return subcommands.ExitFailure
|
fmt.Println("❌", metadata.Name, ":", err.Error())
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if err := p.Service.Scan(metadata.ID); err != nil {
|
if changed {
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to scan:", err)
|
fmt.Println("✅", metadata.Name, ": backed up")
|
||||||
return subcommands.ExitFailure
|
} else {
|
||||||
|
fmt.Println("🆗", metadata.Name, ": up to date")
|
||||||
}
|
}
|
||||||
fmt.Println("✅", metadata.Name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("done.")
|
fmt.Println("done.")
|
||||||
|
|||||||
@@ -66,7 +66,6 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
|||||||
pg.Finish()
|
pg.Finish()
|
||||||
pg.Clear()
|
pg.Clear()
|
||||||
pg.Close()
|
pg.Close()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pg.Describe(fmt.Sprintf("[%s] Checking status...", g.Name))
|
pg.Describe(fmt.Sprintf("[%s] Checking status...", g.Name))
|
||||||
@@ -88,19 +87,13 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
|||||||
destroyPg()
|
destroyPg()
|
||||||
slog.Warn("failed to push backup files", "err", err)
|
slog.Warn("failed to push backup files", "err", err)
|
||||||
}
|
}
|
||||||
|
destroyPg()
|
||||||
fmt.Println(g.Name + ": pushed")
|
fmt.Println(g.Name + ": pushed")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
pg.Describe(fmt.Sprintf("[%s] Fetching metadata...", g.Name))
|
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)
|
remoteMetadata, err := cli.Metadata(r.GameID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
destroyPg()
|
destroyPg()
|
||||||
@@ -118,7 +111,7 @@ func (p *SyncCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
|||||||
slog.Warn("failed to push backup files", "err", err)
|
slog.Warn("failed to push backup files", "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if g.MD5 == hremote {
|
if g.MD5 == remoteMetadata.MD5 {
|
||||||
destroyPg()
|
destroyPg()
|
||||||
if g.Version != remoteMetadata.Version {
|
if g.Version != remoteMetadata.Version {
|
||||||
slog.Debug("version is not the same, but the hash is equal. Updating local database")
|
slog.Debug("version is not the same, but the hash is equal. Updating local database")
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package api
|
|||||||
import (
|
import (
|
||||||
"cloudsave/pkg/data"
|
"cloudsave/pkg/data"
|
||||||
"cloudsave/pkg/repository"
|
"cloudsave/pkg/repository"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
@@ -36,7 +35,7 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
|
|||||||
}
|
}
|
||||||
router := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
router.NotFound(func(writer http.ResponseWriter, request *http.Request) {
|
router.NotFound(func(writer http.ResponseWriter, request *http.Request) {
|
||||||
notFound("This route does not exist", writer, request)
|
notFound("id not found", writer, request)
|
||||||
})
|
})
|
||||||
router.MethodNotAllowed(func(writer http.ResponseWriter, request *http.Request) {
|
router.MethodNotAllowed(func(writer http.ResponseWriter, request *http.Request) {
|
||||||
methodNotAllowed(writer, request)
|
methodNotAllowed(writer, request)
|
||||||
@@ -61,7 +60,6 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
|
|||||||
gamesRouter.Group(func(saveRouter chi.Router) {
|
gamesRouter.Group(func(saveRouter chi.Router) {
|
||||||
saveRouter.Post("/{id}/data", s.upload)
|
saveRouter.Post("/{id}/data", s.upload)
|
||||||
saveRouter.Get("/{id}/data", s.download)
|
saveRouter.Get("/{id}/data", s.download)
|
||||||
saveRouter.Get("/{id}/hash", s.hash)
|
|
||||||
saveRouter.Get("/{id}/metadata", s.metadata)
|
saveRouter.Get("/{id}/metadata", s.metadata)
|
||||||
|
|
||||||
saveRouter.Get("/{id}/hist", s.allHist)
|
saveRouter.Get("/{id}/hist", s.allHist)
|
||||||
@@ -81,43 +79,13 @@ func NewServer(documentRoot string, srv *data.Service, creds map[string]string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s HTTPServer) all(w http.ResponseWriter, r *http.Request) {
|
func (s HTTPServer) all(w http.ResponseWriter, r *http.Request) {
|
||||||
path := filepath.Join(s.documentRoot, "data")
|
datastore, err := s.Service.AllGames()
|
||||||
datastore := make([]repository.Metadata, 0)
|
|
||||||
|
|
||||||
if _, err := os.Stat(path); err != nil {
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
ok(datastore, w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintln(os.Stderr, "failed to open datastore (", s.documentRoot, "):", err)
|
|
||||||
internalServerError(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ds, err := os.ReadDir(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "failed to open datastore (", s.documentRoot, "):", err)
|
slog.Error(err.Error())
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range ds {
|
|
||||||
content, err := os.ReadFile(filepath.Join(path, d.Name(), "metadata.json"))
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("error: failed to load metadata.json", "err", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var m repository.Metadata
|
|
||||||
err = json.Unmarshal(content, &m)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "corrupted datastore: failed to parse %s/metadata.json: %s", d.Name(), err)
|
|
||||||
internalServerError(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
datastore = append(datastore, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
ok(datastore, w, r)
|
ok(datastore, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,32 +93,19 @@ func (s HTTPServer) download(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))
|
path := filepath.Clean(filepath.Join(s.documentRoot, "data", id))
|
||||||
|
|
||||||
sdir, err := os.Stat(path)
|
fi, err := os.Stat(filepath.Join(path, "data.tar.gz"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
notFound("id not found", w, r)
|
notFound("id not found", w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sdir.IsDir() {
|
f, err := s.Service.Repository().ReadBlob(repository.NewGameIdentifier(id))
|
||||||
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 {
|
if err != nil {
|
||||||
notFound("id not found", w, r)
|
slog.Error(err.Error())
|
||||||
return
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
// Get file info to set headers
|
|
||||||
fi, err := f.Stat()
|
|
||||||
if err != nil || fi.IsDir() {
|
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
// Set headers
|
// Set headers
|
||||||
w.Header().Set("Content-Disposition", "attachment; filename=\"data.tar.gz\"")
|
w.Header().Set("Content-Disposition", "attachment; filename=\"data.tar.gz\"")
|
||||||
@@ -209,6 +164,12 @@ func (s HTTPServer) upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := s.Service.ReloadCache(id); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "error: failed to reload data from the disk:", err)
|
||||||
|
internalServerError(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Respond success
|
// Respond success
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
}
|
}
|
||||||
@@ -260,7 +221,13 @@ func (s HTTPServer) histUpload(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
if err := s.Service.CopyBackup(gameID, uuid, file); err != nil {
|
if err := s.Service.CopyBackup(gameID, uuid, file); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "error: failed to write data to disk:", err)
|
fmt.Fprintln(os.Stderr, "error: failed to write data to the disk:", err)
|
||||||
|
internalServerError(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.Service.ReloadCache(gameID); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "error: failed to reload data from the disk:", err)
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -274,32 +241,19 @@ func (s HTTPServer) histDownload(w http.ResponseWriter, r *http.Request) {
|
|||||||
uuid := chi.URLParam(r, "uuid")
|
uuid := chi.URLParam(r, "uuid")
|
||||||
path := filepath.Clean(filepath.Join(s.documentRoot, "data", id, "hist", uuid))
|
path := filepath.Clean(filepath.Join(s.documentRoot, "data", id, "hist", uuid))
|
||||||
|
|
||||||
sdir, err := os.Stat(path)
|
fi, err := os.Stat(filepath.Join(path, "data.tar.gz"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
notFound("id not found", w, r)
|
notFound("id not found", w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sdir.IsDir() {
|
f, err := s.Service.Repository().ReadBlob(repository.NewBackupIdentifier(id, uuid))
|
||||||
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 {
|
if err != nil {
|
||||||
notFound("id not found", w, r)
|
slog.Error(err.Error())
|
||||||
return
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
// Get file info to set headers
|
|
||||||
fi, err := f.Stat()
|
|
||||||
if err != nil || fi.IsDir() {
|
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
// Set headers
|
// Set headers
|
||||||
w.Header().Set("Content-Disposition", "attachment; filename=\"data.tar.gz\"")
|
w.Header().Set("Content-Disposition", "attachment; filename=\"data.tar.gz\"")
|
||||||
@@ -329,56 +283,18 @@ func (s HTTPServer) histExists(w http.ResponseWriter, r *http.Request) {
|
|||||||
ok(finfo, w, r)
|
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) {
|
func (s HTTPServer) metadata(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))
|
metadata, err := s.Service.One(id)
|
||||||
|
|
||||||
sdir, err := os.Stat(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
notFound("id not found", w, r)
|
if errors.Is(err, repository.ErrNotFound) {
|
||||||
return
|
notFound("id not found", w, r)
|
||||||
}
|
return
|
||||||
|
}
|
||||||
if !sdir.IsDir() {
|
slog.Error(err.Error())
|
||||||
notFound("id not found", w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
path = filepath.Join(path, "metadata.json")
|
|
||||||
|
|
||||||
f, err := os.OpenFile(path, os.O_RDONLY, 0)
|
|
||||||
if err != nil {
|
|
||||||
notFound("id not found", w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
var metadata repository.Metadata
|
|
||||||
d := json.NewDecoder(f)
|
|
||||||
err = d.Decode(&metadata)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, "error: an error occured while reading data:", err)
|
|
||||||
internalServerError(w, r)
|
internalServerError(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ok(metadata, w, r)
|
ok(metadata, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ package api
|
|||||||
import (
|
import (
|
||||||
"cloudsave/pkg/remote/obj"
|
"cloudsave/pkg/remote/obj"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func internalServerError(w http.ResponseWriter, r *http.Request) {
|
func internalServerError(w http.ResponseWriter, r *http.Request) {
|
||||||
e := obj.HTTPError{
|
payload := obj.HTTPError{
|
||||||
HTTPCore: obj.HTTPCore{
|
HTTPCore: obj.HTTPCore{
|
||||||
Status: http.StatusInternalServerError,
|
Status: http.StatusInternalServerError,
|
||||||
Path: r.RequestURI,
|
Path: r.RequestURI,
|
||||||
@@ -19,20 +19,16 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
|
|||||||
Message: "The server encountered an unexpected condition that prevented it from fulfilling the request.",
|
Message: "The server encountered an unexpected condition that prevented it from fulfilling the request.",
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := json.Marshal(e)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
_, err = w.Write(payload)
|
e := json.NewEncoder(w)
|
||||||
if err != nil {
|
if err := e.Encode(payload); err != nil {
|
||||||
log.Println(err)
|
slog.Error(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func notFound(message string, w http.ResponseWriter, r *http.Request) {
|
func notFound(message string, w http.ResponseWriter, r *http.Request) {
|
||||||
e := obj.HTTPError{
|
payload := obj.HTTPError{
|
||||||
HTTPCore: obj.HTTPCore{
|
HTTPCore: obj.HTTPCore{
|
||||||
Status: http.StatusNotFound,
|
Status: http.StatusNotFound,
|
||||||
Path: r.RequestURI,
|
Path: r.RequestURI,
|
||||||
@@ -42,20 +38,16 @@ func notFound(message string, w http.ResponseWriter, r *http.Request) {
|
|||||||
Message: message,
|
Message: message,
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := json.Marshal(e)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
_, err = w.Write(payload)
|
e := json.NewEncoder(w)
|
||||||
if err != nil {
|
if err := e.Encode(payload); err != nil {
|
||||||
log.Println(err)
|
slog.Error(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
||||||
e := obj.HTTPError{
|
payload := obj.HTTPError{
|
||||||
HTTPCore: obj.HTTPCore{
|
HTTPCore: obj.HTTPCore{
|
||||||
Status: http.StatusMethodNotAllowed,
|
Status: http.StatusMethodNotAllowed,
|
||||||
Path: r.RequestURI,
|
Path: r.RequestURI,
|
||||||
@@ -65,20 +57,16 @@ func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
|||||||
Message: "The server knows the request method, but the target resource doesn't support this method",
|
Message: "The server knows the request method, but the target resource doesn't support this method",
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := json.Marshal(e)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
_, err = w.Write(payload)
|
e := json.NewEncoder(w)
|
||||||
if err != nil {
|
if err := e.Encode(payload); err != nil {
|
||||||
log.Println(err)
|
slog.Error(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func unauthorized(w http.ResponseWriter, r *http.Request) {
|
func unauthorized(w http.ResponseWriter, r *http.Request) {
|
||||||
e := obj.HTTPError{
|
payload := obj.HTTPError{
|
||||||
HTTPCore: obj.HTTPCore{
|
HTTPCore: obj.HTTPCore{
|
||||||
Status: http.StatusUnauthorized,
|
Status: http.StatusUnauthorized,
|
||||||
Path: r.RequestURI,
|
Path: r.RequestURI,
|
||||||
@@ -88,21 +76,17 @@ func unauthorized(w http.ResponseWriter, r *http.Request) {
|
|||||||
Message: "The request has not been completed because it lacks valid authentication credentials for the requested resource.",
|
Message: "The request has not been completed because it lacks valid authentication credentials for the requested resource.",
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := json.Marshal(e)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
w.Header().Add("WWW-Authenticate", "Custom realm=\"loginUserHandler via /api/login\"")
|
w.Header().Add("WWW-Authenticate", "Custom realm=\"loginUserHandler via /api/login\"")
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
_, err = w.Write(payload)
|
e := json.NewEncoder(w)
|
||||||
if err != nil {
|
if err := e.Encode(payload); err != nil {
|
||||||
log.Println(err)
|
slog.Error(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ok(o interface{}, w http.ResponseWriter, r *http.Request) {
|
func ok(o interface{}, w http.ResponseWriter, r *http.Request) {
|
||||||
e := obj.HTTPObject{
|
payload := obj.HTTPObject{
|
||||||
HTTPCore: obj.HTTPCore{
|
HTTPCore: obj.HTTPCore{
|
||||||
Status: http.StatusOK,
|
Status: http.StatusOK,
|
||||||
Path: r.RequestURI,
|
Path: r.RequestURI,
|
||||||
@@ -110,20 +94,15 @@ func ok(o interface{}, w http.ResponseWriter, r *http.Request) {
|
|||||||
},
|
},
|
||||||
Data: o,
|
Data: o,
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := json.Marshal(e)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
_, err = w.Write(payload)
|
e := json.NewEncoder(w)
|
||||||
if err != nil {
|
if err := e.Encode(payload); err != nil {
|
||||||
log.Println(err)
|
slog.Error(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func badRequest(message string, w http.ResponseWriter, r *http.Request) {
|
func badRequest(message string, w http.ResponseWriter, r *http.Request) {
|
||||||
e := obj.HTTPError{
|
payload := obj.HTTPError{
|
||||||
HTTPCore: obj.HTTPCore{
|
HTTPCore: obj.HTTPCore{
|
||||||
Status: http.StatusBadRequest,
|
Status: http.StatusBadRequest,
|
||||||
Path: r.RequestURI,
|
Path: r.RequestURI,
|
||||||
@@ -133,14 +112,10 @@ func badRequest(message string, w http.ResponseWriter, r *http.Request) {
|
|||||||
Message: message,
|
Message: message,
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := json.Marshal(e)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
_, err = w.Write(payload)
|
e := json.NewEncoder(w)
|
||||||
if err != nil {
|
if err := e.Encode(payload); err != nil {
|
||||||
log.Println(err)
|
slog.Error(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"cloudsave/pkg/repository"
|
"cloudsave/pkg/repository"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -18,18 +19,27 @@ func run() {
|
|||||||
|
|
||||||
var documentRoot string
|
var documentRoot string
|
||||||
var port int
|
var port int
|
||||||
var noCache bool
|
var noCache, verbose bool
|
||||||
flag.StringVar(&documentRoot, "document-root", defaultDocumentRoot, "Define the path to the document root")
|
flag.StringVar(&documentRoot, "document-root", defaultDocumentRoot, "Define the path to the document root")
|
||||||
flag.IntVar(&port, "port", 8080, "Define the port of the server")
|
flag.IntVar(&port, "port", 8080, "Define the port of the server")
|
||||||
flag.BoolVar(&noCache, "no-cache", false, "Disable the cache")
|
flag.BoolVar(&noCache, "no-cache", false, "Disable the cache")
|
||||||
|
flag.BoolVar(&verbose, "verbose", false, "Show more logs")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
if verbose {
|
||||||
|
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("loading .htpasswd")
|
||||||
h, err := htpasswd.Open(filepath.Join(documentRoot, ".htpasswd"))
|
h, err := htpasswd.Open(filepath.Join(documentRoot, ".htpasswd"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal("failed to load .htpasswd: "+err.Error(), 1)
|
fatal("failed to load .htpasswd: "+err.Error(), 1)
|
||||||
}
|
}
|
||||||
|
slog.Info("users loaded: " + strconv.Itoa(len(h.Content())) + " user(s) loaded")
|
||||||
|
|
||||||
var repo repository.Repository
|
var repo repository.Repository
|
||||||
if noCache {
|
if !noCache {
|
||||||
|
slog.Info("loading eager repository...")
|
||||||
r, err := repository.NewEagerRepository(filepath.Join(documentRoot, "data"))
|
r, err := repository.NewEagerRepository(filepath.Join(documentRoot, "data"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal("failed to load datastore: "+err.Error(), 1)
|
fatal("failed to load datastore: "+err.Error(), 1)
|
||||||
@@ -39,17 +49,19 @@ func run() {
|
|||||||
}
|
}
|
||||||
repo = r
|
repo = r
|
||||||
} else {
|
} else {
|
||||||
|
slog.Info("loading lazy repository...")
|
||||||
repo, err = repository.NewLazyRepository(filepath.Join(documentRoot, "data"))
|
repo, err = repository.NewLazyRepository(filepath.Join(documentRoot, "data"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal("failed to load datastore: "+err.Error(), 1)
|
fatal("failed to load datastore: "+err.Error(), 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Info("repository loaded")
|
||||||
s := data.NewService(repo)
|
s := data.NewService(repo)
|
||||||
|
|
||||||
server := api.NewServer(documentRoot, s, h.Content(), port)
|
server := api.NewServer(documentRoot, s, h.Content(), port)
|
||||||
|
|
||||||
fmt.Println("starting server at :" + strconv.Itoa(port))
|
fmt.Println("server started at :" + strconv.Itoa(port))
|
||||||
if err := server.Server.ListenAndServe(); err != nil {
|
if err := server.Server.ListenAndServe(); err != nil {
|
||||||
fatal("failed to start server: "+err.Error(), 1)
|
fatal("failed to start server: "+err.Error(), 1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,9 +162,8 @@ func (s *HTTPServer) detailled(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
var err1, err2, err3 error
|
var err1, err2 error
|
||||||
var save repository.Metadata
|
var save repository.Metadata
|
||||||
var h string
|
|
||||||
var ids []string
|
var ids []string
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
@@ -175,24 +174,18 @@ func (s *HTTPServer) detailled(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
h, err2 = cli.Hash(id)
|
ids, err2 = cli.ListArchives(id)
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
ids, err3 = cli.ListArchives(id)
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
if err1 != nil || err2 != nil || err3 != nil {
|
if err1 != nil || err2 != nil {
|
||||||
if errors.Is(err1, client.ErrUnauthorized) {
|
if errors.Is(err1, client.ErrUnauthorized) {
|
||||||
unauthorized("Unable to access resources", w, r)
|
unauthorized("Unable to access resources", w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
slog.Error("unable to connect to the remote", "err", err1)
|
slog.Error("failed to get metadata: unable to connect to the remote", "err", err1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,7 +198,7 @@ func (s *HTTPServer) detailled(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
b, err := cli.ArchiveInfo(id, i)
|
b, err := cli.ArchiveInfo(id, i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("unable to connect to the remote", "err", err)
|
slog.Error("failed to get backup: unable to connect to the remote", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
bm = append(bm, b)
|
bm = append(bm, b)
|
||||||
@@ -216,7 +209,6 @@ func (s *HTTPServer) detailled(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
payload := DetaillePayload{
|
payload := DetaillePayload{
|
||||||
Save: save,
|
Save: save,
|
||||||
Hash: h,
|
|
||||||
BackupMetadata: bm,
|
BackupMetadata: bm,
|
||||||
Version: constants.Version,
|
Version: constants.Version,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@
|
|||||||
<ul class="list-group list-group-flush">
|
<ul class="list-group list-group-flush">
|
||||||
<li class="list-group-item">UUID: {{.Save.ID}}</li>
|
<li class="list-group-item">UUID: {{.Save.ID}}</li>
|
||||||
<li class="list-group-item">Last Upload: {{.Save.Date}}</li>
|
<li class="list-group-item">Last Upload: {{.Save.Date}}</li>
|
||||||
<li class="list-group-item">Hash (MD5): {{.Hash}}</li>
|
<li class="list-group-item">Hash (MD5): {{.Save.MD5}}</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<hr />
|
<hr />
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
package constants
|
package constants
|
||||||
|
|
||||||
const Version = "0.0.4"
|
const Version = "0.0.4b"
|
||||||
|
|
||||||
const ApiVersion = 1
|
const ApiVersion = 1
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func (s *Service) Add(name, path, remote string) (string, error) {
|
|||||||
ID: gameID.Key(),
|
ID: gameID.Key(),
|
||||||
Name: name,
|
Name: name,
|
||||||
Path: path,
|
Path: path,
|
||||||
Version: 1,
|
Version: 0,
|
||||||
Date: time.Now(),
|
Date: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,47 +82,51 @@ func (s *Service) UpdateMetadata(gameID string, m repository.Metadata) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Scan(gameID string) error {
|
func (s *Service) Scan(gameID string) (bool, error) {
|
||||||
id := repository.NewGameIdentifier(gameID)
|
id := repository.NewGameIdentifier(gameID)
|
||||||
|
|
||||||
lastRun, err := s.repo.LastScan(id)
|
lastRun, err := s.repo.LastScan(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get last scan time: %w", err)
|
return false, fmt.Errorf("failed to get last scan time: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := s.repo.Metadata(id)
|
m, err := s.repo.Metadata(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get game metadata: %w", err)
|
return false, fmt.Errorf("failed to get game metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !IsDirectoryChanged(m.Path, lastRun) {
|
if !IsDirectoryChanged(m.Path, lastRun) {
|
||||||
return nil
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.MakeBackup(gameID); err != nil {
|
||||||
|
return false, fmt.Errorf("failed to make the backup: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := s.repo.WriteBlob(id)
|
f, err := s.repo.WriteBlob(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get datastore stream: %w", err)
|
return false, fmt.Errorf("failed to get datastore stream: %w", err)
|
||||||
}
|
}
|
||||||
if v, ok := f.(io.Closer); ok {
|
if v, ok := f.(io.Closer); ok {
|
||||||
defer v.Close()
|
defer v.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := archive.Tar(f, m.Path); err != nil {
|
if err := archive.Tar(f, m.Path); err != nil {
|
||||||
return fmt.Errorf("failed to make archive: %w", err)
|
return false, fmt.Errorf("failed to make archive: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.repo.ResetLastScan(id); err != nil {
|
if err := s.repo.ResetLastScan(id); err != nil {
|
||||||
return fmt.Errorf("failed to reset scan date: %w", err)
|
return false, fmt.Errorf("failed to reset scan date: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Date = time.Now()
|
m.Date = time.Now()
|
||||||
m.Version += 1
|
m.Version += 1
|
||||||
|
|
||||||
if err := s.repo.WriteMetadata(id, m); err != nil {
|
if err := s.repo.WriteMetadata(id, m); err != nil {
|
||||||
return fmt.Errorf("failed to update metadata: %w", err)
|
return false, fmt.Errorf("failed to update metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) MakeBackup(gameID string) error {
|
func (s *Service) MakeBackup(gameID string) error {
|
||||||
@@ -370,6 +374,17 @@ func (l Service) ApplyBackup(gameID, backupID string) error {
|
|||||||
return l.apply(filepath.Join(path, "data.tar.gz"), g.Path)
|
return l.apply(filepath.Join(path, "data.tar.gz"), g.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l Service) Repository() repository.Repository {
|
||||||
|
return l.repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l Service) ReloadCache(gameID string) error {
|
||||||
|
if er, ok := l.repo.(*repository.EagerRepository); ok {
|
||||||
|
return er.ReloadMetadata(repository.NewGameIdentifier(gameID))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (l Service) apply(src, dst string) error {
|
func (l Service) apply(src, dst string) error {
|
||||||
if err := os.RemoveAll(dst); err != nil {
|
if err := os.RemoveAll(dst); err != nil {
|
||||||
return fmt.Errorf("failed to remove old save: %w", err)
|
return fmt.Errorf("failed to remove old save: %w", err)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func New(baseURL, username, password string) *Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Exists(gameID string) (bool, error) {
|
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 {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@@ -104,22 +104,13 @@ func (c *Client) Version() (Information, error) {
|
|||||||
return Information{}, errors.New("invalid payload sent by the server")
|
return Information{}, errors.New("invalid payload sent by the server")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: use c.Metadata instead
|
||||||
func (c *Client) Hash(gameID string) (string, error) {
|
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
return m.MD5, nil
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Metadata(gameID string) (repository.Metadata, error) {
|
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),
|
Name: m["name"].(string),
|
||||||
Version: int(m["version"].(float64)),
|
Version: int(m["version"].(float64)),
|
||||||
Date: customtime.MustParse(time.RFC3339, m["date"].(string)),
|
Date: customtime.MustParse(time.RFC3339, m["date"].(string)),
|
||||||
|
MD5: m["md5"].(string),
|
||||||
}
|
}
|
||||||
return gm, nil
|
return gm, nil
|
||||||
}
|
}
|
||||||
@@ -175,6 +167,10 @@ func (c *Client) ListArchives(gameID string) ([]string, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if o.Data == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
if m, ok := (o.Data).([]any); ok {
|
if m, ok := (o.Data).([]any); ok {
|
||||||
var res []string
|
var res []string
|
||||||
for _, uuid := range m {
|
for _, uuid := range m {
|
||||||
@@ -346,6 +342,10 @@ func (c *Client) All() ([]repository.Metadata, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if o.Data == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
if games, ok := (o.Data).([]any); ok {
|
if games, ok := (o.Data).([]any); ok {
|
||||||
var res []repository.Metadata
|
var res []repository.Metadata
|
||||||
for _, g := range games {
|
for _, g := range games {
|
||||||
@@ -355,6 +355,7 @@ func (c *Client) All() ([]repository.Metadata, error) {
|
|||||||
Name: v["name"].(string),
|
Name: v["name"].(string),
|
||||||
Version: int(v["version"].(float64)),
|
Version: int(v["version"].(float64)),
|
||||||
Date: customtime.MustParse(time.RFC3339, v["date"].(string)),
|
Date: customtime.MustParse(time.RFC3339, v["date"].(string)),
|
||||||
|
MD5: v["md5"].(string),
|
||||||
}
|
}
|
||||||
res = append(res, gm)
|
res = append(res, gm)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,7 +20,7 @@ type (
|
|||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Version int `json:"version"`
|
Version int `json:"version"`
|
||||||
Date time.Time `json:"date"`
|
Date time.Time `json:"date"`
|
||||||
MD5 string `json:"-"`
|
MD5 string `json:"md5,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
Remote struct {
|
Remote struct {
|
||||||
@@ -60,6 +62,7 @@ type (
|
|||||||
EagerRepository struct {
|
EagerRepository struct {
|
||||||
Repository
|
Repository
|
||||||
|
|
||||||
|
mu sync.RWMutex
|
||||||
data map[string]Data
|
data map[string]Data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +77,7 @@ type (
|
|||||||
|
|
||||||
Metadata(gameID GameIdentifier) (Metadata, error)
|
Metadata(gameID GameIdentifier) (Metadata, error)
|
||||||
LastScan(gameID GameIdentifier) (time.Time, error)
|
LastScan(gameID GameIdentifier) (time.Time, error)
|
||||||
ReadBlob(gameID Identifier) (io.Reader, error)
|
ReadBlob(gameID Identifier) (io.ReadSeekCloser, error)
|
||||||
Backup(id BackupIdentifier) (Backup, error)
|
Backup(id BackupIdentifier) (Backup, error)
|
||||||
Remote(id GameIdentifier) (*Remote, error)
|
Remote(id GameIdentifier) (*Remote, error)
|
||||||
|
|
||||||
@@ -132,10 +135,16 @@ func NewLazyRepository(dataRootPath string) (*LazyRepository, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *LazyRepository) Mkdir(id Identifier) error {
|
func (l *LazyRepository) Mkdir(id Identifier) error {
|
||||||
return os.MkdirAll(l.DataPath(id), 0740)
|
path := l.DataPath(id)
|
||||||
|
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
||||||
|
slog.Debug("making directory", "path", path, "id", id, "perm", "0740")
|
||||||
|
return os.MkdirAll(path, 0740)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LazyRepository) All() ([]string, error) {
|
func (l *LazyRepository) All() ([]string, error) {
|
||||||
|
slog.Debug("loading all current data...")
|
||||||
dir, err := os.ReadDir(l.dataRoot)
|
dir, err := os.ReadDir(l.dataRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open directory: %w", err)
|
return nil, fmt.Errorf("failed to open directory: %w", err)
|
||||||
@@ -152,6 +161,7 @@ func (l *LazyRepository) All() ([]string, error) {
|
|||||||
func (l *LazyRepository) AllHist(id GameIdentifier) ([]string, error) {
|
func (l *LazyRepository) AllHist(id GameIdentifier) ([]string, error) {
|
||||||
path := l.DataPath(id)
|
path := l.DataPath(id)
|
||||||
|
|
||||||
|
slog.Debug("loading hist data...", "id", id)
|
||||||
dir, err := os.ReadDir(filepath.Join(path, "hist"))
|
dir, err := os.ReadDir(filepath.Join(path, "hist"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
@@ -171,6 +181,7 @@ func (l *LazyRepository) AllHist(id GameIdentifier) ([]string, error) {
|
|||||||
func (l *LazyRepository) WriteBlob(ID Identifier) (io.Writer, error) {
|
func (l *LazyRepository) WriteBlob(ID Identifier) (io.Writer, error) {
|
||||||
path := l.DataPath(ID)
|
path := l.DataPath(ID)
|
||||||
|
|
||||||
|
slog.Debug("loading write buffer...", "id", ID)
|
||||||
dst, err := os.OpenFile(filepath.Join(path, "data.tar.gz"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
|
dst, err := os.OpenFile(filepath.Join(path, "data.tar.gz"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open destination file: %w", err)
|
return nil, fmt.Errorf("failed to open destination file: %w", err)
|
||||||
@@ -180,8 +191,10 @@ func (l *LazyRepository) WriteBlob(ID Identifier) (io.Writer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *LazyRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
|
func (l *LazyRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
|
||||||
|
m.MD5 = ""
|
||||||
path := l.DataPath(id)
|
path := l.DataPath(id)
|
||||||
|
|
||||||
|
slog.Debug("writing metadata", "id", id, "metadata", m)
|
||||||
dst, err := os.OpenFile(filepath.Join(path, "metadata.json"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
|
dst, err := os.OpenFile(filepath.Join(path, "metadata.json"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open destination file: %w", err)
|
return fmt.Errorf("failed to open destination file: %w", err)
|
||||||
@@ -199,6 +212,7 @@ func (l *LazyRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
|
|||||||
func (l *LazyRepository) Metadata(id GameIdentifier) (Metadata, error) {
|
func (l *LazyRepository) Metadata(id GameIdentifier) (Metadata, error) {
|
||||||
path := l.DataPath(id)
|
path := l.DataPath(id)
|
||||||
|
|
||||||
|
slog.Debug("loading metadata", "id", id)
|
||||||
src, err := os.OpenFile(filepath.Join(path, "metadata.json"), os.O_RDONLY, 0)
|
src, err := os.OpenFile(filepath.Join(path, "metadata.json"), os.O_RDONLY, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
@@ -213,6 +227,14 @@ func (l *LazyRepository) Metadata(id GameIdentifier) (Metadata, error) {
|
|||||||
return Metadata{}, fmt.Errorf("corrupted datastore: failed to parse metadata: %w", err)
|
return Metadata{}, fmt.Errorf("corrupted datastore: failed to parse metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(filepath.Join(path, "data.tar.gz")); err != nil {
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
return Metadata{}, fmt.Errorf("failed to open archive: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("loading md5 hash", "id", id)
|
||||||
m.MD5, err = hash.FileMD5(filepath.Join(path, "data.tar.gz"))
|
m.MD5, err = hash.FileMD5(filepath.Join(path, "data.tar.gz"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Metadata{}, fmt.Errorf("failed to calculate md5: %w", err)
|
return Metadata{}, fmt.Errorf("failed to calculate md5: %w", err)
|
||||||
@@ -224,6 +246,7 @@ func (l *LazyRepository) Metadata(id GameIdentifier) (Metadata, error) {
|
|||||||
func (l *LazyRepository) Backup(id BackupIdentifier) (Backup, error) {
|
func (l *LazyRepository) Backup(id BackupIdentifier) (Backup, error) {
|
||||||
path := l.DataPath(id)
|
path := l.DataPath(id)
|
||||||
|
|
||||||
|
slog.Debug("loading hist metadata", "id", id)
|
||||||
fs, err := os.Stat(filepath.Join(path, "data.tar.gz"))
|
fs, err := os.Stat(filepath.Join(path, "data.tar.gz"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
@@ -232,6 +255,7 @@ func (l *LazyRepository) Backup(id BackupIdentifier) (Backup, error) {
|
|||||||
return Backup{}, fmt.Errorf("corrupted datastore: failed to open metadata: %w", err)
|
return Backup{}, fmt.Errorf("corrupted datastore: failed to open metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("loading md5 hash", "id", id)
|
||||||
h, err := hash.FileMD5(filepath.Join(path, "data.tar.gz"))
|
h, err := hash.FileMD5(filepath.Join(path, "data.tar.gz"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Backup{}, fmt.Errorf("corrupted datastore: failed to open metadata: %w", err)
|
return Backup{}, fmt.Errorf("corrupted datastore: failed to open metadata: %w", err)
|
||||||
@@ -267,6 +291,7 @@ func (l *LazyRepository) LastScan(id GameIdentifier) (time.Time, error) {
|
|||||||
func (l *LazyRepository) ResetLastScan(id GameIdentifier) error {
|
func (l *LazyRepository) ResetLastScan(id GameIdentifier) error {
|
||||||
path := l.DataPath(id)
|
path := l.DataPath(id)
|
||||||
|
|
||||||
|
slog.Debug("resetting last scan datetime for", "id", id)
|
||||||
f, err := os.OpenFile(filepath.Join(path, ".last_run"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
|
f, err := os.OpenFile(filepath.Join(path, ".last_run"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0740)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open file: %w", err)
|
return fmt.Errorf("failed to open file: %w", err)
|
||||||
@@ -282,9 +307,10 @@ func (l *LazyRepository) ResetLastScan(id GameIdentifier) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LazyRepository) ReadBlob(id Identifier) (io.Reader, error) {
|
func (l *LazyRepository) ReadBlob(id Identifier) (io.ReadSeekCloser, error) {
|
||||||
path := l.DataPath(id)
|
path := l.DataPath(id)
|
||||||
|
|
||||||
|
slog.Debug("loading read buffer...", "id", id)
|
||||||
dst, err := os.OpenFile(filepath.Join(path, "data.tar.gz"), os.O_RDONLY, 0)
|
dst, err := os.OpenFile(filepath.Join(path, "data.tar.gz"), os.O_RDONLY, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open blob: %w", err)
|
return nil, fmt.Errorf("failed to open blob: %w", err)
|
||||||
@@ -337,6 +363,7 @@ func (l *LazyRepository) Remote(id GameIdentifier) (*Remote, error) {
|
|||||||
func (l *LazyRepository) Remove(id GameIdentifier) error {
|
func (l *LazyRepository) Remove(id GameIdentifier) error {
|
||||||
path := l.DataPath(id)
|
path := l.DataPath(id)
|
||||||
|
|
||||||
|
slog.Debug("removing data", "id", id)
|
||||||
if err := os.RemoveAll(path); err != nil {
|
if err := os.RemoveAll(path); err != nil {
|
||||||
return fmt.Errorf("failed to remove game folder from the datastore: %w", err)
|
return fmt.Errorf("failed to remove game folder from the datastore: %w", err)
|
||||||
}
|
}
|
||||||
@@ -368,6 +395,9 @@ func NewEagerRepository(dataRootPath string) (*EagerRepository, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *EagerRepository) Preload() error {
|
func (r *EagerRepository) Preload() error {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
games, err := r.Repository.All()
|
games, err := r.Repository.All()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to load all data: %w", err)
|
return fmt.Errorf("failed to load all data: %w", err)
|
||||||
@@ -411,6 +441,9 @@ func (r *EagerRepository) Preload() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *EagerRepository) All() ([]string, error) {
|
func (r *EagerRepository) All() ([]string, error) {
|
||||||
|
r.mu.RLock()
|
||||||
|
defer r.mu.RUnlock()
|
||||||
|
|
||||||
var res []string
|
var res []string
|
||||||
for _, g := range r.data {
|
for _, g := range r.data {
|
||||||
res = append(res, g.Metadata.ID)
|
res = append(res, g.Metadata.ID)
|
||||||
@@ -420,6 +453,9 @@ func (r *EagerRepository) All() ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *EagerRepository) AllHist(id GameIdentifier) ([]string, error) {
|
func (r *EagerRepository) AllHist(id GameIdentifier) ([]string, error) {
|
||||||
|
r.mu.RLock()
|
||||||
|
defer r.mu.RUnlock()
|
||||||
|
|
||||||
var res []string
|
var res []string
|
||||||
if d, ok := r.data[id.gameID]; ok {
|
if d, ok := r.data[id.gameID]; ok {
|
||||||
for _, b := range d.Backup {
|
for _, b := range d.Backup {
|
||||||
@@ -430,6 +466,9 @@ func (r *EagerRepository) AllHist(id GameIdentifier) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *EagerRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
|
func (r *EagerRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
err := r.Repository.WriteMetadata(id, m)
|
err := r.Repository.WriteMetadata(id, m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -443,6 +482,9 @@ func (r *EagerRepository) WriteMetadata(id GameIdentifier, m Metadata) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *EagerRepository) Metadata(id GameIdentifier) (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 {
|
if d, ok := r.data[id.gameID]; ok {
|
||||||
return d.Metadata, nil
|
return d.Metadata, nil
|
||||||
}
|
}
|
||||||
@@ -450,6 +492,9 @@ func (r *EagerRepository) Metadata(id GameIdentifier) (Metadata, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *EagerRepository) Backup(id BackupIdentifier) (Backup, 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 d, ok := r.data[id.gameID]; ok {
|
||||||
if b, ok := d.Backup[id.backupID]; ok {
|
if b, ok := d.Backup[id.backupID]; ok {
|
||||||
return b, nil
|
return b, nil
|
||||||
@@ -459,6 +504,9 @@ func (r *EagerRepository) Backup(id BackupIdentifier) (Backup, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *EagerRepository) SetRemote(id GameIdentifier, url string) error {
|
func (r *EagerRepository) SetRemote(id GameIdentifier, url string) error {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
err := r.Repository.SetRemote(id, url)
|
err := r.Repository.SetRemote(id, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -475,6 +523,9 @@ func (r *EagerRepository) SetRemote(id GameIdentifier, url string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *EagerRepository) Remove(id GameIdentifier) error {
|
func (r *EagerRepository) Remove(id GameIdentifier) error {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
if err := r.Repository.Remove(id); err != nil {
|
if err := r.Repository.Remove(id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -482,3 +533,39 @@ func (r *EagerRepository) Remove(id GameIdentifier) error {
|
|||||||
delete(r.data, id.gameID)
|
delete(r.data, id.gameID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *EagerRepository) ReloadMetadata(id GameIdentifier) error {
|
||||||
|
backup, err := r.Repository.AllHist(id)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] failed to load hist data: %w", id, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
remote, err := r.Repository.Remote(id)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] failed to load remote metadata: %w", id, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
m, err := r.Repository.Metadata(id)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] failed to load metadata: %w", id, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backups := make(map[string]Backup)
|
||||||
|
for _, b := range backup {
|
||||||
|
info, err := r.Repository.Backup(NewBackupIdentifier(id.gameID, b))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] failed to get backup information: %w", id, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
backups[b] = info
|
||||||
|
}
|
||||||
|
|
||||||
|
r.data[id.gameID] = Data{
|
||||||
|
Metadata: m,
|
||||||
|
Remote: remote,
|
||||||
|
DataPath: r.DataPath(id),
|
||||||
|
Backup: backups,
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user