From 27960741a35f796f8f1945dae00bafdb8c6ec6c5 Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Sun, 25 Jan 2026 22:30:01 +0100 Subject: [PATCH] more fixes --- domain/repository.file.go | 73 +++++++++++++++++++----- domain/service.software_updater.go | 6 +- domain/service.software_updater_tic80.go | 58 +++++++++++-------- http/controller.play.go | 2 +- http/controller.software_updater.go | 8 ++- 5 files changed, 102 insertions(+), 45 deletions(-) diff --git a/domain/repository.file.go b/domain/repository.file.go index fbe7847..27ad767 100644 --- a/domain/repository.file.go +++ b/domain/repository.file.go @@ -15,11 +15,14 @@ type FileRepositoryInterface interface { CreateDir(dirPath string) error DeleteFile(fileName, basePath string) error MoveFile(srcFileName, destPath, basePath string) error - UnzipHTMLContent(zipFileName, baseName, basePath string) error + UnzipHTMLContent(zipFilePath, softwareName, version, basePath string) error GetSoftwareDir(softwareName, basePath string) string + GetSoftwareVersionDir(softwareName, version, basePath string) string + GetFileInSoftwareVersionDir(softwareName, version, fileName, basePath string) string + GetHTMLContentDir(softwareName, version, basePath string) string GetCartridgePath(softwareName, version, basePath string) string GetSourcePath(softwareName, version, basePath string) string - ReadMetaFromFile(fileName, basePath string) (map[string]string, error) + ReadMetaFromFile(filePath string, basePath string) (map[string]string, error) } type FileRepository struct{} @@ -29,7 +32,15 @@ func NewFileRepository() *FileRepository { } func (r *FileRepository) FileExists(fileName, basePath string) bool { - filePath := filepath.Join(basePath, fileName) + var filePath string + if filepath.IsAbs(fileName) { + filePath = fileName + } else if basePath != "" { + filePath = filepath.Join(basePath, fileName) + } else { + filePath = fileName // Assume it's relative to current working dir or absolute + } + _, err := os.Stat(filePath) return err == nil } @@ -39,18 +50,31 @@ func (r *FileRepository) CreateDir(dirPath string) error { } func (r *FileRepository) DeleteFile(fileName, basePath string) error { - filePath := filepath.Join(basePath, fileName) + var filePath string + if filepath.IsAbs(fileName) { + filePath = fileName + } else if basePath != "" { + filePath = filepath.Join(basePath, fileName) + } else { + filePath = fileName + } return os.Remove(filePath) } func (r *FileRepository) MoveFile(srcFileName, destPath, basePath string) error { - srcPath := filepath.Join(basePath, srcFileName) + var srcPath string + if filepath.IsAbs(srcFileName) { + srcPath = srcFileName + } else if basePath != "" { + srcPath = filepath.Join(basePath, srcFileName) + } else { + srcPath = srcFileName + } return os.Rename(srcPath, destPath) } -func (r *FileRepository) UnzipHTMLContent(zipFileName, baseName, basePath string) error { - zipFilePath := filepath.Join(basePath, zipFileName) - destDir := filepath.Join(basePath, "html", baseName) +func (r *FileRepository) UnzipHTMLContent(zipFilePath, softwareName, version, basePath string) error { + destDir := r.GetHTMLContentDir(softwareName, version, basePath) fmt.Printf("FileRepository: Unzipping %s to %s\n", zipFilePath, destDir) @@ -83,6 +107,7 @@ func (r *FileRepository) UnzipHTMLContent(zipFileName, baseName, basePath string outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { + outFile.Close() return err } @@ -108,19 +133,37 @@ func (r *FileRepository) GetSoftwareDir(softwareName, basePath string) string { return filepath.Join(basePath, softwareName) } +func (r *FileRepository) GetSoftwareVersionDir(softwareName, version, basePath string) string { + return filepath.Join(r.GetSoftwareDir(softwareName, basePath), version) +} + +func (r *FileRepository) GetFileInSoftwareVersionDir(softwareName, version, fileName, basePath string) string { + return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), fileName) +} + +func (r *FileRepository) GetHTMLContentDir(softwareName, version, basePath string) string { + return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), "html") +} + func (r *FileRepository) GetCartridgePath(softwareName, version, basePath string) string { - softwareDir := r.GetSoftwareDir(softwareName, basePath) - return filepath.Join(softwareDir, fmt.Sprintf("%s-%s.tic", softwareName, version)) + return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), fmt.Sprintf("%s.tic", softwareName)) } func (r *FileRepository) GetSourcePath(softwareName, version, basePath string) string { - softwareDir := r.GetSoftwareDir(softwareName, basePath) - return filepath.Join(softwareDir, fmt.Sprintf("%s-%s.lua", softwareName, version)) + return filepath.Join(r.GetSoftwareVersionDir(softwareName, version, basePath), fmt.Sprintf("%s.lua", softwareName)) } -func (r *FileRepository) ReadMetaFromFile(fileName, basePath string) (map[string]string, error) { - filePath := filepath.Join(basePath, fileName) - file, err := os.Open(filePath) +func (r *FileRepository) ReadMetaFromFile(filePath string, basePath string) (map[string]string, error) { + var fullPath string + if filepath.IsAbs(filePath) { + fullPath = filePath + } else if basePath != "" { + fullPath = filepath.Join(basePath, filePath) + } else { + fullPath = filePath + } + + file, err := os.Open(fullPath) if err != nil { return nil, err } diff --git a/domain/service.software_updater.go b/domain/service.software_updater.go index a34e36b..ce3cd77 100644 --- a/domain/service.software_updater.go +++ b/domain/service.software_updater.go @@ -5,7 +5,7 @@ import ( ) type SoftwareUpdaterServiceInterface interface { - Update(platform, name string) error + Update(platform, name, version string) error } type SoftwareUpdaterService struct { @@ -16,9 +16,9 @@ func NewSoftwareUpdaterService(tic80Updater SoftwareUpdaterTIC80ServiceInterface return &SoftwareUpdaterService{tic80Updater: tic80Updater} } -func (s *SoftwareUpdaterService) Update(platform, name string) error { +func (s *SoftwareUpdaterService) Update(platform, name, version string) error { if platform == "tic80" { - return s.tic80Updater.Update(name) + return s.tic80Updater.Update(name, version) } return fmt.Errorf("unsupported platform: %s", platform) } diff --git a/domain/service.software_updater_tic80.go b/domain/service.software_updater_tic80.go index 737e0be..0c1156b 100644 --- a/domain/service.software_updater_tic80.go +++ b/domain/service.software_updater_tic80.go @@ -6,7 +6,7 @@ import ( ) type SoftwareUpdaterTIC80ServiceInterface interface { - Update(name string) error + Update(name, version string) error } type SoftwareUpdaterTIC80Service struct { @@ -27,15 +27,15 @@ func NewSoftwareUpdaterTIC80Service( } } -func (s *SoftwareUpdaterTIC80Service) Update(name string) error { - fmt.Printf("TIC80 Updater: Starting update for name: %s\n", name) +func (s *SoftwareUpdaterTIC80Service) Update(name, version string) error { + fmt.Printf("TIC80 Updater: Starting update for name: %s, version: %s\n", name, version) contentsPath, _ := os.LookupEnv("GAMES_DIR") - if err := s.handleHTMLContent(name, contentsPath); err == nil { + if err := s.handleHTMLContent(name, version, contentsPath); err == nil { fmt.Printf("TIC80 Updater: Successfully processed HTML content: %s\n", name) } - if err := s.handleLuaCartridge(name, contentsPath); err != nil { + if err := s.handleLuaCartridge(name, version, contentsPath); err != nil { return err } @@ -43,36 +43,44 @@ func (s *SoftwareUpdaterTIC80Service) Update(name string) error { return nil } -func (s *SoftwareUpdaterTIC80Service) handleHTMLContent(name, contentsPath string) error { +func (s *SoftwareUpdaterTIC80Service) handleHTMLContent(name, version, contentsPath string) error { zipFileName := fmt.Sprintf("%s.html.zip", name) - baseName := name + // The zip file is now in the versioned folder + zipFilePathInVersionDir := s.fileRepository.GetFileInSoftwareVersionDir(name, version, zipFileName, contentsPath) - if err := s.fileRepository.UnzipHTMLContent(zipFileName, baseName, contentsPath); err != nil { + if err := s.fileRepository.UnzipHTMLContent(zipFilePathInVersionDir, name, version, contentsPath); err != nil { return err } - return s.fileRepository.DeleteFile(zipFileName, contentsPath) + return s.fileRepository.DeleteFile(zipFilePathInVersionDir, contentsPath) } -func (s *SoftwareUpdaterTIC80Service) handleLuaCartridge(name, contentsPath string) error { - luaFileName := fmt.Sprintf("%s.lua", name) - cartridgeFileName := fmt.Sprintf("%s.tic", name) +func (s *SoftwareUpdaterTIC80Service) handleLuaCartridge(name, version, contentsPath string) error { + // The lua and tic files are now in the versioned folder. + luaFileName := s.fileRepository.GetFileInSoftwareVersionDir(name, version, fmt.Sprintf("%s.lua", name), contentsPath) + cartridgeFileName := s.fileRepository.GetFileInSoftwareVersionDir(name, version, fmt.Sprintf("%s.tic", name), contentsPath) - if !s.fileRepository.FileExists(luaFileName, contentsPath) { + + if !s.fileRepository.FileExists(luaFileName, "") { // basePath is already included in luaFileName return fmt.Errorf("no recognizable content file found for '%s' in '%s'", name, contentsPath) } - if !s.fileRepository.FileExists(cartridgeFileName, contentsPath) { + if !s.fileRepository.FileExists(cartridgeFileName, "") { // basePath is already included in cartridgeFileName return fmt.Errorf("missing cartridge file '%s' for '%s'", cartridgeFileName, luaFileName) } - software, version, err := s.parseMeta(luaFileName, name, contentsPath) + software, parsedVersion, err := s.parseMeta(luaFileName, name, contentsPath) if err != nil { return err } + // Use the version from the webhook for consistency + if version != parsedVersion { + fmt.Printf("TIC80 Updater: Warning - parsed version '%s' from Lua file differs from provided version '%s'\n", parsedVersion, version) + } + if version == "" { - return fmt.Errorf("missing version info in '%s'", luaFileName) + return fmt.Errorf("missing version info (webhook or parsed) for '%s'", luaFileName) } if err := s.softwareRepository.UpdateOrCreate(&software); err != nil { @@ -94,24 +102,24 @@ func (s *SoftwareUpdaterTIC80Service) handleLuaCartridge(name, contentsPath stri return s.releaseRepository.Create(release) } -func (s *SoftwareUpdaterTIC80Service) moveCartridgeFiles(name string, software Software, version, luaFileName, cartridgeFileName, contentsPath string) error { - softwareDir := s.fileRepository.GetSoftwareDir(software.Name, contentsPath) +func (s *SoftwareUpdaterTIC80Service) moveCartridgeFiles(name string, software Software, version, luaSrcPath, cartridgeSrcPath, contentsPath string) error { + softwareVersionDir := s.fileRepository.GetSoftwareVersionDir(software.Name, version, contentsPath) - fmt.Printf("TIC80 Updater: Creating software directory: %s\n", softwareDir) - if err := s.fileRepository.CreateDir(softwareDir); err != nil { + fmt.Printf("TIC80 Updater: Creating software version directory: %s\n", softwareVersionDir) + if err := s.fileRepository.CreateDir(softwareVersionDir); err != nil { return err } newCartridgePath := s.fileRepository.GetCartridgePath(software.Name, version, contentsPath) newSourcePath := s.fileRepository.GetSourcePath(software.Name, version, contentsPath) - fmt.Printf("TIC80 Updater: Moving cartridge from %s to %s\n", cartridgeFileName, newCartridgePath) - if err := s.fileRepository.MoveFile(cartridgeFileName, newCartridgePath, contentsPath); err != nil { + fmt.Printf("TIC80 Updater: Moving cartridge from %s to %s\n", cartridgeSrcPath, newCartridgePath) + if err := s.fileRepository.MoveFile(cartridgeSrcPath, newCartridgePath, ""); err != nil { // srcPath includes basePath return err } - fmt.Printf("TIC80 Updater: Moving Lua source from %s to %s\n", luaFileName, newSourcePath) - if err := s.fileRepository.MoveFile(luaFileName, newSourcePath, contentsPath); err != nil { + fmt.Printf("TIC80 Updater: Moving Lua source from %s to %s\n", luaSrcPath, newSourcePath) + if err := s.fileRepository.MoveFile(luaSrcPath, newSourcePath, ""); err != nil { // srcPath includes basePath return err } @@ -122,7 +130,7 @@ func (s *SoftwareUpdaterTIC80Service) parseMeta(luaFileName, name, contentsPath var software Software var version string - metaData, err := s.fileRepository.ReadMetaFromFile(luaFileName, contentsPath) + metaData, err := s.fileRepository.ReadMetaFromFile(luaFileName, "") // luaFileName already contains basePath if err != nil { return software, "", err } diff --git a/http/controller.play.go b/http/controller.play.go index eb220bc..539dfd8 100644 --- a/http/controller.play.go +++ b/http/controller.play.go @@ -157,7 +157,7 @@ func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) { return } - htmlBaseDir := filepath.Join(contentsPath, name, fmt.Sprintf("%s-%s-html", name, version)) + htmlBaseDir := filepath.Join(contentsPath, name, version, "html") if _, err := os.Stat(htmlBaseDir); os.IsNotExist(err) { http.Error(w, fmt.Sprintf("Content for '%s' not found.", name), http.StatusNotFound) diff --git a/http/controller.software_updater.go b/http/controller.software_updater.go index fc525ab..e8a016a 100644 --- a/http/controller.software_updater.go +++ b/http/controller.software_updater.go @@ -24,8 +24,14 @@ func (c *SoftwareUpdaterController) update(w http.ResponseWriter, r *http.Reques platform := r.URL.Query().Get("platform") name := r.URL.Query().Get("name") + version := r.URL.Query().Get("version") - if err := c.service.Update(platform, name); err != nil { + if version == "" { + http.Error(w, "Version not provided", http.StatusBadRequest) + return + } + + if err := c.service.Update(platform, name, version); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }