MCPcopy Create free account
hub / github.com/coder/wush / cpHandler

Function cpHandler

cmd/wush/serve.go:293–324  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

291}
292
293func cpHandler(w http.ResponseWriter, r *http.Request) {
294 if r.Method != "POST" {
295 w.WriteHeader(http.StatusOK)
296 w.Write([]byte("OK"))
297 return
298 }
299
300 fiName := strings.TrimPrefix(r.URL.Path, "/")
301 defer r.Body.Close()
302
303 fi, err := os.OpenFile(fiName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
304 if err != nil {
305 http.Error(w, err.Error(), http.StatusInternalServerError)
306 return
307 }
308
309 bar := progressbar.DefaultBytes(
310 r.ContentLength,
311 fmt.Sprintf("Downloading %q", fiName),
312 )
313 _, err = io.Copy(io.MultiWriter(fi, bar), r.Body)
314 if err != nil {
315 http.Error(w, err.Error(), http.StatusInternalServerError)
316 return
317 }
318 fi.Close()
319 bar.Close()
320
321 w.WriteHeader(http.StatusOK)
322 w.Write([]byte(fmt.Sprintf("File %q written", fiName)))
323 fmt.Printf("Received file %s from %s\n", fiName, r.RemoteAddr)
324}

Callers

nothing calls this directly

Calls 2

WriteMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected