Skip to content

Commit

Permalink
optimize upload data
Browse files Browse the repository at this point in the history
  • Loading branch information
csznet committed Oct 16, 2023
1 parent 059e3e6 commit a56e374
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tgState


旧版本:https://tgtu.ikun123.com/
搭建在Vercel,大文件可能会上传失败
搭建在Vercel,大文件可能会上传失败(资源限制)

测试图片:

Expand All @@ -37,6 +37,17 @@ tgState
部署前需要准备一个Telegram Bot(@botfather处申请)
如果是需要存储在频道,则需要将Bot拉入频道作为管理员,公开频道并自定义频道Link

后台管理
===

后台管理计划是全Telegram管理,Vercel目前不支持,目前实现的有:

获取FileID
---

对bot聊天中的文件引用并回复```get```可以获取FileID,搭建地址+/d/+FileID即可访问资源


Vercel部署
====

Expand Down
14 changes: 7 additions & 7 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ func UploadImageAPI(w http.ResponseWriter, r *http.Request) {
}

// 读取文件内容
fileBytes, err := io.ReadAll(file)
if err != nil {
errJsonMsg("Failed to read file", w)
// http.Error(w, "Failed to read file", http.StatusInternalServerError)
return
}
// fileBytes, err := io.ReadAll(file)
// if err != nil {
// errJsonMsg("Failed to read file", w)
// // http.Error(w, "Failed to read file", http.StatusInternalServerError)
// return
// }
res := conf.UploadResponse{
Code: 0,
Message: "error",
}
var img string
img = "/d/" + utils.UpDocument(utils.TgFileData(header.Filename, fileBytes))
img = "/d/" + utils.UpDocument(utils.TgFileData(header.Filename, file))
res = conf.UploadResponse{
Code: 1,
Message: img,
Expand Down
11 changes: 6 additions & 5 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package utils

import (
"encoding/json"
"io"
"log"

"csz.net/tgstate/conf"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func TgFileData(fileName string, fileData []byte) tgbotapi.FileBytes {
return tgbotapi.FileBytes{
Name: fileName,
Bytes: fileData,
func TgFileData(fileName string, fileData io.Reader) tgbotapi.FileReader {
return tgbotapi.FileReader{
Name: fileName,
Reader: fileData,
}
}

func UpDocument(fileData tgbotapi.FileBytes) string {
func UpDocument(fileData tgbotapi.FileReader) string {
bot, err := tgbotapi.NewBotAPI(conf.BotToken)
if err != nil {
log.Panic(err)
Expand Down

1 comment on commit a56e374

@vercel
Copy link

@vercel vercel bot commented on a56e374 Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.