MCPcopy
hub / github.com/EverythingSuckz/TG-FileStreamBot / getStreamRoute

Function getStreamRoute

internal/routes/stream.go:28–142  ·  view source on GitHub ↗
(ctx *gin.Context)

Source from the content-addressed store, hash-verified

26}
27
28func getStreamRoute(ctx *gin.Context) {
29 w := ctx.Writer
30 r := ctx.Request
31
32 messageIDParm := ctx.Param("messageID")
33 messageID, err := strconv.Atoi(messageIDParm)
34 if err != nil {
35 http.Error(w, err.Error(), http.StatusBadRequest)
36 return
37 }
38
39 authHash := ctx.Query("hash")
40 if authHash == "" {
41 http.Error(w, "missing hash param", http.StatusBadRequest)
42 return
43 }
44
45 worker := bot.GetNextWorker()
46
47 file, err := utils.TimeFuncWithResult(log, "FileFromMessage", func() (*types.File, error) {
48 return utils.FileFromMessage(ctx, worker.Client, messageID)
49 })
50 if err != nil {
51 http.Error(w, err.Error(), http.StatusBadRequest)
52 return
53 }
54
55 expectedHash := utils.PackFile(
56 file.FileName,
57 file.FileSize,
58 file.MimeType,
59 file.ID,
60 )
61 if !utils.CheckHash(authHash, expectedHash) {
62 http.Error(w, "invalid hash", http.StatusBadRequest)
63 return
64 }
65
66 // for photo messages
67 if file.FileSize == 0 {
68 res, err := worker.Client.API().UploadGetFile(ctx, &tg.UploadGetFileRequest{
69 Location: file.Location,
70 Offset: 0,
71 Limit: 1024 * 1024,
72 })
73 if err != nil {
74 http.Error(w, err.Error(), http.StatusInternalServerError)
75 return
76 }
77 result, ok := res.(*tg.UploadFile)
78 if !ok {
79 http.Error(w, "unexpected response", http.StatusInternalServerError)
80 return
81 }
82 fileBytes := result.GetBytes()
83 ctx.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", file.FileName))
84 if r.Method != "HEAD" {
85 ctx.Data(http.StatusOK, file.MimeType, fileBytes)

Callers

nothing calls this directly

Calls 9

CloseMethod · 0.95
GetNextWorkerFunction · 0.92
TimeFuncWithResultFunction · 0.92
FileFromMessageFunction · 0.92
PackFileFunction · 0.92
CheckHashFunction · 0.92
NewStreamPipeFunction · 0.92
IsClientDisconnectErrorFunction · 0.92
GetMethod · 0.80

Tested by

no test coverage detected