|
| 1 | +package download |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// ScriptEvent represents the type of script event |
| 13 | +type ScriptEvent string |
| 14 | + |
| 15 | +const ( |
| 16 | + ScriptEventDownloadDone ScriptEvent = "DOWNLOAD_DONE" |
| 17 | + ScriptEventDownloadError ScriptEvent = "DOWNLOAD_ERROR" |
| 18 | +) |
| 19 | + |
| 20 | +// ScriptData is the internal data structure for passing script information |
| 21 | +type ScriptData struct { |
| 22 | + Event ScriptEvent |
| 23 | + Time int64 // Unix timestamp in milliseconds |
| 24 | + Payload *ScriptPayload |
| 25 | +} |
| 26 | + |
| 27 | +// ScriptPayload contains the task data |
| 28 | +type ScriptPayload struct { |
| 29 | + Task *Task |
| 30 | +} |
| 31 | + |
| 32 | +// getScriptPaths extracts script paths from config |
| 33 | +func (d *Downloader) getScriptPaths() []string { |
| 34 | + cfg := d.cfg.DownloaderStoreConfig |
| 35 | + if cfg == nil { |
| 36 | + return nil |
| 37 | + } |
| 38 | + |
| 39 | + // Check new script config |
| 40 | + if cfg.Script != nil && cfg.Script.Enable && len(cfg.Script.Paths) > 0 { |
| 41 | + paths := make([]string, 0, len(cfg.Script.Paths)) |
| 42 | + for _, path := range cfg.Script.Paths { |
| 43 | + if path != "" { |
| 44 | + paths = append(paths, path) |
| 45 | + } |
| 46 | + } |
| 47 | + if len(paths) > 0 { |
| 48 | + return paths |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// executeScriptAtPath executes a single script with the given data |
| 56 | +// Returns any error that occurred during execution |
| 57 | +func (d *Downloader) executeScriptAtPath(scriptPath string, data *ScriptData) error { |
| 58 | + if scriptPath == "" { |
| 59 | + return fmt.Errorf("script path is empty") |
| 60 | + } |
| 61 | + |
| 62 | + // Check if script file exists |
| 63 | + if _, err := os.Stat(scriptPath); os.IsNotExist(err) { |
| 64 | + return fmt.Errorf("script file does not exist: %s", scriptPath) |
| 65 | + } |
| 66 | + |
| 67 | + // Determine the script interpreter based on file extension |
| 68 | + var cmd *exec.Cmd |
| 69 | + ext := filepath.Ext(scriptPath) |
| 70 | + |
| 71 | + switch ext { |
| 72 | + case ".sh", ".bash": |
| 73 | + cmd = exec.Command("bash", scriptPath) |
| 74 | + case ".py": |
| 75 | + cmd = exec.Command("python3", scriptPath) |
| 76 | + case ".js": |
| 77 | + cmd = exec.Command("node", scriptPath) |
| 78 | + case ".bat", ".cmd": |
| 79 | + // Windows batch files |
| 80 | + if runtime.GOOS == "windows" { |
| 81 | + cmd = exec.Command("cmd", "/c", scriptPath) |
| 82 | + } else { |
| 83 | + // Batch files are Windows-specific |
| 84 | + return fmt.Errorf("batch files (.bat/.cmd) are only supported on Windows") |
| 85 | + } |
| 86 | + case ".ps1": |
| 87 | + // PowerShell scripts |
| 88 | + if runtime.GOOS == "windows" { |
| 89 | + cmd = exec.Command("powershell", "-ExecutionPolicy", "Bypass", "-File", scriptPath) |
| 90 | + } else { |
| 91 | + // Try pwsh (PowerShell Core) on non-Windows systems |
| 92 | + cmd = exec.Command("pwsh", "-File", scriptPath) |
| 93 | + } |
| 94 | + case "": |
| 95 | + // No extension, try to execute directly (assumes shebang or executable) |
| 96 | + cmd = exec.Command(scriptPath) |
| 97 | + default: |
| 98 | + // Unknown extension, try to execute directly |
| 99 | + cmd = exec.Command(scriptPath) |
| 100 | + } |
| 101 | + |
| 102 | + // Set environment variables with task information |
| 103 | + cmd.Env = append(os.Environ(), |
| 104 | + fmt.Sprintf("GOPEED_EVENT=%s", data.Event), |
| 105 | + fmt.Sprintf("GOPEED_TASK_ID=%s", data.Payload.Task.ID), |
| 106 | + fmt.Sprintf("GOPEED_TASK_NAME=%s", data.Payload.Task.Name()), |
| 107 | + fmt.Sprintf("GOPEED_TASK_STATUS=%s", data.Payload.Task.Status), |
| 108 | + ) |
| 109 | + |
| 110 | + // Add task path using the same logic as task deletion |
| 111 | + if data.Payload.Task.Meta != nil && data.Payload.Task.Meta.Res != nil { |
| 112 | + var taskPath string |
| 113 | + if data.Payload.Task.Meta.Res.Name != "" { |
| 114 | + // Multi-file task (folder) |
| 115 | + taskPath = data.Payload.Task.Meta.FolderPath() |
| 116 | + } else { |
| 117 | + // Single file task |
| 118 | + taskPath = data.Payload.Task.Meta.SingleFilepath() |
| 119 | + } |
| 120 | + cmd.Env = append(cmd.Env, |
| 121 | + fmt.Sprintf("GOPEED_TASK_PATH=%s", taskPath), |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + // Start and wait for the command to complete (no timeout) |
| 126 | + return cmd.Run() |
| 127 | +} |
| 128 | + |
| 129 | +// triggerScripts executes all configured scripts |
| 130 | +func (d *Downloader) triggerScripts(event ScriptEvent, task *Task, err error) { |
| 131 | + paths := d.getScriptPaths() |
| 132 | + if len(paths) == 0 { |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + data := &ScriptData{ |
| 137 | + Event: event, |
| 138 | + Time: time.Now().UnixMilli(), |
| 139 | + Payload: &ScriptPayload{ |
| 140 | + Task: task.clone(), |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + go d.executeScripts(paths, data) |
| 145 | +} |
| 146 | + |
| 147 | +func (d *Downloader) executeScripts(paths []string, data *ScriptData) { |
| 148 | + for _, path := range paths { |
| 149 | + if path == "" { |
| 150 | + continue |
| 151 | + } |
| 152 | + go func(scriptPath string) { |
| 153 | + err := d.executeScriptAtPath(scriptPath, data) |
| 154 | + if err != nil { |
| 155 | + d.Logger.Warn().Err(err).Str("path", scriptPath).Msg("script: failed to execute") |
| 156 | + return |
| 157 | + } |
| 158 | + d.Logger.Debug().Str("path", scriptPath).Msg("script: executed successfully") |
| 159 | + }(path) |
| 160 | + } |
| 161 | +} |
0 commit comments