(ctx *gin.Context)
| 26 | } |
| 27 | |
| 28 | func 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) |
nothing calls this directly
no test coverage detected