MCPcopy
hub / github.com/wavetermdev/waveterm / handleStreamFileFromReader

Function handleStreamFileFromReader

pkg/web/web.go:247–325  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request, path string, no404 bool)

Source from the content-addressed store, hash-verified

245}
246
247func handleStreamFileFromReader(w http.ResponseWriter, r *http.Request, path string, no404 bool) error {
248 startTime := time.Now()
249 rangeHeader := r.Header.Get("Range")
250 log.Printf("stream-file path=%q range=%q\n", path, rangeHeader)
251
252 writerRouteId, err := wshfs.GetConnectionRouteId(r.Context(), path)
253 if err != nil {
254 return err
255 }
256
257 byteRange := ""
258 if rangeHeader != "" {
259 stripped := strings.TrimPrefix(rangeHeader, "bytes=")
260 br, parseErr := fileutil.ParseByteRange(stripped)
261 if parseErr != nil || br.All {
262 http.Error(w, "invalid range", http.StatusRequestedRangeNotSatisfiable)
263 return nil
264 }
265 byteRange = stripped
266 }
267
268 bareRpc := wshclient.GetBareRpcClient()
269 readerRouteId := wshclient.GetBareRpcClientRouteId()
270 reader, streamMeta := bareRpc.StreamBroker.CreateStreamReader(readerRouteId, writerRouteId, 256*1024)
271 defer reader.Close()
272 go func() {
273 <-r.Context().Done()
274 reader.Close()
275 }()
276
277 data := wshrpc.CommandFileStreamData{
278 Info: &wshrpc.FileInfo{Path: path},
279 ByteRange: byteRange,
280 StreamMeta: *streamMeta,
281 }
282 fileInfo, err := wshfs.FileStream(r.Context(), data)
283 if err != nil {
284 if no404 {
285 serveTransparentGIF(w)
286 return nil
287 }
288 return err
289 }
290 if fileInfo.NotFound {
291 if no404 {
292 serveTransparentGIF(w)
293 return nil
294 }
295 http.Error(w, fmt.Sprintf("file not found: %q", path), http.StatusNotFound)
296 return nil
297 }
298 if fileInfo.IsDir {
299 http.Error(w, fmt.Sprintf("cannot stream directory: %q", path), http.StatusBadRequest)
300 return nil
301 }
302 log.Printf("stream-file headers-ready path=%q time-to-headers=%v\n", path, time.Since(startTime))
303 w.Header().Set(ContentTypeHeaderKey, fileInfo.MimeType)
304 w.Header().Set("Accept-Ranges", "bytes")

Callers 1

handleStreamFileFunction · 0.85

Calls 15

GetConnectionRouteIdFunction · 0.92
ParseByteRangeFunction · 0.92
GetBareRpcClientFunction · 0.92
GetBareRpcClientRouteIdFunction · 0.92
FileStreamFunction · 0.92
serveTransparentGIFFunction · 0.85
CreateStreamReaderMethod · 0.80
SetWriteDeadlineMethod · 0.80
CopyMethod · 0.80
ErrMethod · 0.80
CloseMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected