MCPcopy
hub / github.com/SurgeDM/Surge / startDownload

Method startDownload

internal/tui/process.go:81–220  ·  view source on GitHub ↗

startDownload initiates a new download

(url string, mirrors []string, headers map[string]string, path string, isDefaultPath bool, filename, id string)

Source from the content-addressed store, hash-verified

79
80// startDownload initiates a new download
81func (m RootModel) startDownload(url string, mirrors []string, headers map[string]string, path string, isDefaultPath bool, filename, id string) (RootModel, tea.Cmd) {
82 if m.Service == nil {
83 m.addLogEntry(LogStyleError.Render("\u2716 Service unavailable"))
84 return m, nil
85 }
86
87 // Enforce absolute path
88 path = utils.EnsureAbsPath(path)
89
90 candidateFilename := strings.TrimSpace(filename)
91 requestID := strings.TrimSpace(id)
92
93 resolvedPath := path
94 resolvedFilename := candidateFilename
95 optimisticFilename := candidateFilename
96 if p, f, err := processing.ResolveDestination(url, candidateFilename, path, isDefaultPath, m.Settings, nil, nil); err == nil {
97 resolvedPath = p
98 resolvedFilename = f
99 if candidateFilename != "" {
100 // Only mirror the resolved filename into the optimistic row when the
101 // user already chose it; probe-derived names can legitimately change.
102 optimisticFilename = f
103 }
104 } else {
105 utils.Debug("Optimistic destination resolve failed for %s: %v", url, err)
106 }
107
108 // Call Orchestrator Enqueue
109 req := &processing.DownloadRequest{
110 URL: url,
111 Filename: candidateFilename,
112 Path: path,
113 Mirrors: mirrors,
114 Headers: headers,
115 IsExplicitCategory: !isDefaultPath,
116 SkipApproval: true,
117 }
118
119 optimisticID := requestID
120 if optimisticID == "" {
121 optimisticID = fmt.Sprintf("pending-%d", time.Now().UnixNano())
122 }
123 displayName := optimisticFilename
124 if displayName == "" {
125 displayName = processing.InferFilenameFromURL(url)
126 }
127 if displayName == "" {
128 displayName = "Queued"
129 }
130
131 newDownload := NewDownloadModel(optimisticID, url, displayName, 0)
132 if resolvedFilename != "" {
133 newDownload.Destination = filepath.Join(resolvedPath, resolvedFilename)
134 } else {
135 newDownload.Destination = resolvedPath
136 }
137 m.downloads = append(m.downloads, newDownload)
138 m.SelectedDownloadID = optimisticID

Calls 15

addLogEntryMethod · 0.95
UpdateListItemsMethod · 0.95
removeDownloadByIDMethod · 0.95
FindDownloadByIDMethod · 0.95
EnsureAbsPathFunction · 0.92
ResolveDestinationFunction · 0.92
DebugFunction · 0.92
InferFilenameFromURLFunction · 0.92
NewDownloadModelFunction · 0.85
ErrorMethod · 0.80
EnqueueWithIDMethod · 0.80