Skip to content

Commit

Permalink
change d
Browse files Browse the repository at this point in the history
  • Loading branch information
csznet committed Oct 10, 2023
1 parent 2ae3318 commit 9f6f41a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 75 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ tgState

[English](https://github.com/csznet/tgState/blob/main/README_en.md)

【Notice】 1.2版本开始采用file_id形式留存外链,对以往版本外链不兼容,需要保留外链的谨慎更新

一款以Telegram作为储存的文件外链系统

Expand Down
2 changes: 1 addition & 1 deletion README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
72 changes: 24 additions & 48 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package control
import (
"encoding/json"
"io"
"log"
"net/http"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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
Expand All @@ -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 = `<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Telegram图床</title><meta name="keywords" content="telegram图床,tg图床,免费图床,永久图床,图片外链,免费图片外链,纸飞机图床,电报图床"><meta name="description" content="telegram图床,tg图床,免费图床,永久图床,图片外链,免费图片外链,纸飞机图床,电报图床"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><style>#uploadButton,#uploadFileLabel{display:block;max-width:200px;margin:0 auto;margin-bottom:10px}body{font-family:Arial,sans-serif;text-align:center}h1{color:#333}.custom-file-input{display:none}.custom-file-label{background-color:#007bff;color:#fff;padding:10px 20px;cursor:pointer}.custom-file-label:hover{background-color:#0056b3}#uploadButton{background-color:#007bff;color:#fff;padding:10px 20px;border:none;cursor:pointer}#uploadButton[disabled]{background-color:#ccc;cursor:not-allowed}#uploadButton:hover{background-color:#0056b3}#response{margin-top:20px;padding:10px}.response-item{margin-bottom:10px;padding:10px;border-radius:5px}.response-success{background-color:#d4edda;border-color:#c3e6cb;color:#155724}.response-error{background-color:#f8d7da;border-color:#f5c6cb;color:#721c24}#loading{display:none}.copy-code{margin:5px}.copy-links{margin-top:5px}#uploadButton[disabled]:hover{background-color:#ccc;cursor:not-allowed}.password{margin:0;padding:0;display:flex;justify-content:center;align-items:center;height:100vh;background-color:#f2f2f2}.form-container{text-align:center;background-color:#fff;padding:20px;border-radius:10px;box-shadow:0 0 10px rgba(0,0,0,.2)}.form-input{width:300px;padding:10px;margin:10px;border:1px solid #ccc;border-radius:5px;font-size:16px}.form-button{padding:10px 20px;background-color:#007bff;color:#fff;border:none;border-radius:5px;font-size:18px;cursor:pointer}@media (max-width:465px){.form-container{padding:0;border-radius:0}.form-input{margin-top:30px}}</style><script src="https://code.jquery.com/jquery-3.6.0.min.js"></script></head></html>`

// 首页
Expand Down
31 changes: 5 additions & 26 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 9f6f41a

Please sign in to comment.