package task_pack_release import ( "fmt" "os" "os/exec" "path/filepath" "regexp" "strings" ) var rarPath = `C:\Program Files\WinRAR\WinRAR.exe` func compress(srcPath string, dstPath string, releaseName string, archivePwd string, archiveSize string, nfoFilePath string) error { // Check if the content is a directory or a single file fi, err := os.Stat(srcPath) if err != nil { return fmt.Errorf("failed to stat content path: %w", err) } // Collect the files to be archived var files []string if fi.IsDir() { dirEntries, err := os.ReadDir(srcPath) if err != nil { return fmt.Errorf("failed to read directory: %w", err) } for _, entry := range dirEntries { if entry.IsDir() { continue } filePath := filepath.Join(srcPath, entry.Name()) if filepath.Ext(filePath) != ".nfo" { files = append(files, filePath) } } } else { files = append(files, srcPath) } os.MkdirAll(dstPath, os.ModePerm) // Process the files for _, entry := range files { fileName := filepath.Base(entry) fixedFileName := getFixedName(fileName) fixedFilePath := filepath.Join(dstPath, fixedFileName) if err := copyFile(entry, fixedFilePath); err != nil { return err } archiveName := buildArchiveName(fileName, releaseName) archivePath := filepath.Join(dstPath, archiveName) args := []string{ "a", "-ma4", "-m0", "-ep", "-v" + archiveSize, "-rr3p", "-hp" + archivePwd, archivePath, fixedFilePath, } if nfoFilePath != "" { args = append(args, nfoFilePath) } cmd := exec.Command(rarPath, args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr fmt.Printf("Creating archive: %s\n", archiveName) err = cmd.Run() if err != nil { return fmt.Errorf("winrar failed: %w", err) } os.Remove(fixedFilePath) } return nil } func getFixedName(name string) string { replacer := strings.NewReplacer( "ä", "ae", "ö", "oe", "ü", "ue", //" ", ".", ) return replacer.Replace(name) } func buildArchiveName(fileName string, releaseName string) string { re := regexp.MustCompile(`(?i)^(.*?)[\-\.\s]*(?:s?(\d+))?([ex])(\d+)([a-f]?)[\-\.\s]*(.*)$`) m := re.FindStringSubmatch(fileName) if len(m) == 0 { return fmt.Sprintf("%s.rar", releaseName) } season := m[2] epd := m[3] ep := m[4] suff := m[5] if season != "" { return fmt.Sprintf("%s.S%s%s%s.rar", releaseName, season, epd, ep) } return fmt.Sprintf("%s.%s%s%s.rar", releaseName, epd, ep, suff) }