From 9f6f41ac594cc196eb19dff80822bb3c30b32060 Mon Sep 17 00:00:00 2001 From: yimi Date: Tue, 10 Oct 2023 22:49:11 +0800 Subject: [PATCH] change d --- README.md | 1 + README_en.md | 2 +- control/control.go | 72 ++++++++++++++++------------------------------ utils/utils.go | 31 ++++---------------- 4 files changed, 31 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index ce90152..0110c75 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ tgState [English](https://github.com/csznet/tgState/blob/main/README_en.md) +【Notice】 1.2版本开始采用file_id形式留存外链,对以往版本外链不兼容,需要保留外链的谨慎更新 一款以Telegram作为储存的文件外链系统 diff --git a/README_en.md b/README_en.md index c5827be..3446175 100644 --- a/README_en.md +++ b/README_en.md @@ -33,7 +33,7 @@ Test image: Before deployment, you need to prepare a Telegram Bot (apply at @botfather). If you need to store files in a channel, you need to add the Bot to the channel as an administrator, make the channel public, and customize the channel link. -Vercel部署 +Vercel Deployment ==== [Click here to go to the Vercel configuration page](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fcsznet%2FtgState&env=token&env=channel&env=pass&env=mode&project-name=tgState&repository-name=tgState) diff --git a/control/control.go b/control/control.go index 3e15e59..1d02e13 100644 --- a/control/control.go +++ b/control/control.go @@ -3,6 +3,7 @@ package control import ( "encoding/json" "io" + "log" "net/http" "path/filepath" "strings" @@ -95,46 +96,24 @@ func errJsonMsg(msg string, w http.ResponseWriter) { } func D(w http.ResponseWriter, r *http.Request) { path := r.URL.Path - lastSlashIndex := strings.LastIndex(path, "/") - var fileName string - var fileExt string - tgType := "documents" - - if lastSlashIndex != -1 && lastSlashIndex < len(path)-1 { - contentAfterLastSlash := path[lastSlashIndex+1:] - fileName = contentAfterLastSlash - } else { + id := strings.TrimPrefix(path, "/d/") + if id == "" { // 设置响应的状态码为 404 w.WriteHeader(http.StatusNotFound) // 写入响应内容 w.Write([]byte("404 Not Found")) return } - lastDotIndex := strings.LastIndex(fileName, ".") - // 检查是否找到点 - if lastDotIndex != -1 { - // 从点的位置截取字符串的子串,即文件扩展名 - fileExt = fileName[lastDotIndex+1:] - } else { - http.Error(w, "Failed to show content", http.StatusInternalServerError) - return - } - rType := "application/" + fileExt - if isImageExtension(fileExt) { - rType = "image/" + fileExt - w.Header().Set("Content-Disposition", "inline") // 设置为 "inline" 以支持在线播放 - } else if isVideoExtension(fileExt) { - rType = "video/" + fileExt - w.Header().Set("Content-Disposition", "inline") // 设置为 "inline" 以支持在线播放 - tgType = "videos" - } + // 发起HTTP GET请求来获取Telegram图片 - resp, err := http.Get("https://api.telegram.org/file/bot" + conf.BotToken + "/" + tgType + "/file_" + fileName) + resp, err := http.Get(utils.GetDownloadUrl(id)) if err != nil { http.Error(w, "Failed to fetch content", http.StatusInternalServerError) return } defer resp.Body.Close() + rType := resp.Header.Get("Content-Type") + w.Header().Set("Content-Disposition", "inline") // 设置为 "inline" 以支持在线播放 // 检查Content-Type是否为图片类型 if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/octet-stream") { // 设置响应的状态码为 404 @@ -143,34 +122,31 @@ func D(w http.ResponseWriter, r *http.Request) { w.Write([]byte("404 Not Found")) return } + // 读取前512个字节以用于文件类型检测 + buffer := make([]byte, 512) + n, err := resp.Body.Read(buffer) + if err != nil { + log.Println("读取响应主体数据时发生错误:", err) + return + } + // 使用DetectContentType函数检测文件类型 + rType = http.DetectContentType(buffer) w.Header().Set("Content-Type", rType) - // 将图片内容写入响应正文 + // 写入前512个字节到响应w + _, err = w.Write(buffer[:n]) + if err != nil { + http.Error(w, "Failed to write content", http.StatusInternalServerError) + log.Println(http.StatusInternalServerError) + return + } _, err = io.Copy(w, resp.Body) if err != nil { http.Error(w, "Failed to show content", http.StatusInternalServerError) + log.Println(http.StatusInternalServerError) return } } -func isVideoExtension(extension string) bool { - supportedExtensions := []string{"mp4", "avi", "mov", "mkv", "flv"} - for _, ext := range supportedExtensions { - if extension == ext { - return true - } - } - return false -} -func isImageExtension(extension string) bool { - supportedExtensions := []string{"jpg", "jpeg", "png", "gif"} - for _, ext := range supportedExtensions { - if extension == ext { - return true - } - } - return false -} - const htmlHead string = `Telegram图床` // 首页 diff --git a/utils/utils.go b/utils/utils.go index 3444668..1b122ed 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,7 +3,6 @@ package utils import ( "encoding/json" "log" - "strings" "csz.net/tgstate/conf" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" @@ -16,26 +15,6 @@ func TgFileData(fileName string, fileData []byte) tgbotapi.FileBytes { } } -func SendImageToTelegram(inputFile tgbotapi.FileBytes) string { - botToken := conf.BotToken - bot, err := tgbotapi.NewBotAPI(botToken) - if err != nil { - log.Panic(err) - } - // 构建消息配置 - msg := tgbotapi.NewPhotoToChannel(conf.ChannelName, inputFile) - // msg.Caption = "Hello, Telegram!" - // 发送消息 - sentMessage, err := bot.Send(msg) - if err != nil { - log.Panic(err) - } - photos := sentMessage.Photo - res := getDownloadUrl(photos[len(photos)-1].FileID) - fileName := strings.TrimPrefix(res, "https://api.telegram.org/file/bot"+botToken+"/photos/file_") - return fileName -} - func UpDocument(fileData tgbotapi.FileBytes) string { bot, err := tgbotapi.NewBotAPI(conf.BotToken) if err != nil { @@ -60,16 +39,16 @@ func UpDocument(fileData tgbotapi.FileBytes) string { } var msg conf.Message err = json.Unmarshal([]byte(response.Result), &msg) - var fileName string + var resp string if msg.Document.FileID != "" { - fileName = strings.TrimPrefix(getDownloadUrl(msg.Document.FileID), "https://api.telegram.org/file/bot"+conf.BotToken+"/documents/file_") + resp = msg.Document.FileID } else { - fileName = strings.TrimPrefix(getDownloadUrl(msg.Video.FileID), "https://api.telegram.org/file/bot"+conf.BotToken+"/videos/file_") + resp = msg.Video.FileID } - return fileName + return resp } -func getDownloadUrl(fileID string) string { +func GetDownloadUrl(fileID string) string { bot, err := tgbotapi.NewBotAPI(conf.BotToken) if err != nil { log.Panic(err)