mirror of
https://github.com/ctwj/urldb.git
synced 2025-11-25 11:29:37 +08:00
update: 优化获取链接的方式
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
pan "github.com/ctwj/urldb/common"
|
||||
commonutils "github.com/ctwj/urldb/common/utils"
|
||||
"github.com/ctwj/urldb/db/converter"
|
||||
"github.com/ctwj/urldb/db/dto"
|
||||
"github.com/ctwj/urldb/db/entity"
|
||||
@@ -16,7 +19,7 @@ import (
|
||||
func GetResources(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
|
||||
|
||||
utils.Info("资源列表请求 - page: %d, pageSize: %d", page, pageSize)
|
||||
|
||||
params := map[string]interface{}{
|
||||
@@ -292,14 +295,14 @@ func IncrementResourceViewCount(c *gin.Context) {
|
||||
ErrorResponse(c, "无效的资源ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 增加资源访问量
|
||||
err = repoManager.ResourceRepository.IncrementViewCount(uint(id))
|
||||
if err != nil {
|
||||
ErrorResponse(c, "增加浏览次数失败", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 记录访问记录
|
||||
ipAddress := c.ClientIP()
|
||||
userAgent := c.GetHeader("User-Agent")
|
||||
@@ -308,7 +311,7 @@ func IncrementResourceViewCount(c *gin.Context) {
|
||||
// 记录访问失败不影响主要功能,只记录日志
|
||||
utils.Error("记录资源访问失败: %v", err)
|
||||
}
|
||||
|
||||
|
||||
SuccessResponse(c, gin.H{"message": "浏览次数+1"})
|
||||
}
|
||||
|
||||
@@ -329,3 +332,305 @@ func BatchDeleteResources(c *gin.Context) {
|
||||
}
|
||||
SuccessResponse(c, gin.H{"deleted": count, "message": "批量删除成功"})
|
||||
}
|
||||
|
||||
// GetResourceLink 获取资源链接(智能转存)
|
||||
func GetResourceLink(c *gin.Context) {
|
||||
// 获取资源ID
|
||||
resourceIDStr := c.Param("id")
|
||||
resourceID, err := strconv.ParseUint(resourceIDStr, 10, 32)
|
||||
if err != nil {
|
||||
ErrorResponse(c, "无效的资源ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
utils.Info("获取资源链接请求 - resourceID: %d", resourceID)
|
||||
|
||||
// 查询资源信息
|
||||
resource, err := repoManager.ResourceRepository.FindByID(uint(resourceID))
|
||||
if err != nil {
|
||||
utils.Error("查询资源失败: %v", err)
|
||||
ErrorResponse(c, "资源不存在", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// 查询平台信息
|
||||
var panInfo entity.Pan
|
||||
if resource.PanID != nil {
|
||||
panPtr, err := repoManager.PanRepository.FindByID(*resource.PanID)
|
||||
if err != nil {
|
||||
utils.Error("查询平台信息失败: %v", err)
|
||||
} else if panPtr != nil {
|
||||
panInfo = *panPtr
|
||||
}
|
||||
}
|
||||
|
||||
utils.Info("资源信息 - 平台: %s, 原始链接: %s, 转存链接: %s", panInfo.Name, resource.URL, resource.SaveURL)
|
||||
|
||||
// 统计访问次数
|
||||
err = repoManager.ResourceRepository.IncrementViewCount(uint(resourceID))
|
||||
if err != nil {
|
||||
utils.Error("增加资源访问量失败: %v", err)
|
||||
}
|
||||
|
||||
// 记录访问记录
|
||||
ipAddress := c.ClientIP()
|
||||
userAgent := c.GetHeader("User-Agent")
|
||||
err = repoManager.ResourceViewRepository.RecordView(uint(resourceID), ipAddress, userAgent)
|
||||
if err != nil {
|
||||
utils.Error("记录资源访问失败: %v", err)
|
||||
}
|
||||
|
||||
// 如果不是夸克网盘,直接返回原链接
|
||||
if panInfo.Name != "quark" {
|
||||
utils.Info("非夸克资源,直接返回原链接")
|
||||
SuccessResponse(c, gin.H{
|
||||
"url": resource.URL,
|
||||
"type": "original",
|
||||
"platform": panInfo.Remark,
|
||||
"resource_id": resource.ID,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 夸克资源处理逻辑
|
||||
utils.Info("夸克资源处理开始")
|
||||
|
||||
// 如果已存在转存链接,直接返回
|
||||
if resource.SaveURL != "" {
|
||||
utils.Info("已存在转存链接,直接返回: %s", resource.SaveURL)
|
||||
SuccessResponse(c, gin.H{
|
||||
"url": resource.SaveURL,
|
||||
"type": "transferred",
|
||||
"platform": panInfo.Remark,
|
||||
"resource_id": resource.ID,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否开启自动转存
|
||||
autoTransferEnabled, err := repoManager.SystemConfigRepository.GetConfigBool(entity.ConfigKeyAutoTransferEnabled)
|
||||
if err != nil {
|
||||
utils.Error("获取自动转存配置失败: %v", err)
|
||||
// 配置获取失败,返回原链接
|
||||
SuccessResponse(c, gin.H{
|
||||
"url": resource.URL,
|
||||
"type": "original",
|
||||
"platform": panInfo.Remark,
|
||||
"resource_id": resource.ID,
|
||||
"message": "",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if !autoTransferEnabled {
|
||||
utils.Info("自动转存功能未开启,返回原链接")
|
||||
SuccessResponse(c, gin.H{
|
||||
"url": resource.URL,
|
||||
"type": "original",
|
||||
"platform": panInfo.Remark,
|
||||
"resource_id": resource.ID,
|
||||
"message": "",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 执行自动转存
|
||||
utils.Info("开始执行自动转存")
|
||||
transferResult := performAutoTransfer(resource)
|
||||
|
||||
if transferResult.Success {
|
||||
utils.Info("自动转存成功,返回转存链接: %s", transferResult.SaveURL)
|
||||
SuccessResponse(c, gin.H{
|
||||
"url": transferResult.SaveURL,
|
||||
"type": "transferred",
|
||||
"platform": panInfo.Remark,
|
||||
"resource_id": resource.ID,
|
||||
"message": "资源易和谐,请及时用手机夸克扫码转存",
|
||||
})
|
||||
} else {
|
||||
utils.Error("自动转存失败: %s", transferResult.ErrorMsg)
|
||||
SuccessResponse(c, gin.H{
|
||||
"url": resource.URL,
|
||||
"type": "original",
|
||||
"platform": panInfo.Remark,
|
||||
"resource_id": resource.ID,
|
||||
"message": "",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TransferResult 转存结果
|
||||
type TransferResult struct {
|
||||
Success bool `json:"success"`
|
||||
SaveURL string `json:"save_url"`
|
||||
ErrorMsg string `json:"error_msg"`
|
||||
}
|
||||
|
||||
// performAutoTransfer 执行自动转存
|
||||
func performAutoTransfer(resource *entity.Resource) TransferResult {
|
||||
utils.Info("开始执行资源转存 - ID: %d, URL: %s", resource.ID, resource.URL)
|
||||
|
||||
// 获取夸克平台ID
|
||||
quarkPanID, err := getQuarkPanID()
|
||||
if err != nil {
|
||||
utils.Error("获取夸克平台ID失败: %v", err)
|
||||
return TransferResult{
|
||||
Success: false,
|
||||
ErrorMsg: fmt.Sprintf("获取夸克平台ID失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取可用的夸克账号
|
||||
accounts, err := repoManager.CksRepository.FindAll()
|
||||
if err != nil {
|
||||
utils.Error("获取网盘账号失败: %v", err)
|
||||
return TransferResult{
|
||||
Success: false,
|
||||
ErrorMsg: fmt.Sprintf("获取网盘账号失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取最小存储空间配置
|
||||
autoTransferMinSpace, err := repoManager.SystemConfigRepository.GetConfigInt(entity.ConfigKeyAutoTransferMinSpace)
|
||||
if err != nil {
|
||||
utils.Error("获取最小存储空间配置失败: %v", err)
|
||||
autoTransferMinSpace = 5 // 默认5GB
|
||||
}
|
||||
|
||||
// 过滤:只保留已激活、夸克平台、剩余空间足够的账号
|
||||
minSpaceBytes := int64(autoTransferMinSpace) * 1024 * 1024 * 1024
|
||||
var validAccounts []entity.Cks
|
||||
for _, acc := range accounts {
|
||||
if acc.IsValid && acc.PanID == quarkPanID && acc.LeftSpace >= minSpaceBytes {
|
||||
validAccounts = append(validAccounts, acc)
|
||||
}
|
||||
}
|
||||
|
||||
if len(validAccounts) == 0 {
|
||||
utils.Info("没有可用的夸克网盘账号")
|
||||
return TransferResult{
|
||||
Success: false,
|
||||
ErrorMsg: "没有可用的夸克网盘账号",
|
||||
}
|
||||
}
|
||||
|
||||
utils.Info("找到 %d 个可用夸克网盘账号,开始转存处理...", len(validAccounts))
|
||||
|
||||
// 使用第一个可用账号进行转存
|
||||
account := validAccounts[0]
|
||||
|
||||
// 创建网盘服务工厂
|
||||
factory := pan.NewPanFactory()
|
||||
|
||||
// 执行转存
|
||||
result := transferSingleResource(resource, account, factory)
|
||||
|
||||
if result.Success {
|
||||
// 更新资源的转存信息
|
||||
resource.SaveURL = result.SaveURL
|
||||
resource.ErrorMsg = ""
|
||||
if err := repoManager.ResourceRepository.Update(resource); err != nil {
|
||||
utils.Error("更新资源转存信息失败: %v", err)
|
||||
}
|
||||
} else {
|
||||
// 更新错误信息
|
||||
resource.ErrorMsg = result.ErrorMsg
|
||||
if err := repoManager.ResourceRepository.Update(resource); err != nil {
|
||||
utils.Error("更新资源错误信息失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// transferSingleResource 转存单个资源
|
||||
func transferSingleResource(resource *entity.Resource, account entity.Cks, factory *pan.PanFactory) TransferResult {
|
||||
utils.Info("开始转存资源 - 资源ID: %d, 账号: %s", resource.ID, account.Username)
|
||||
|
||||
service, err := factory.CreatePanService(resource.URL, &pan.PanConfig{
|
||||
URL: resource.URL,
|
||||
ExpiredType: 0,
|
||||
IsType: 0,
|
||||
Cookie: account.Ck,
|
||||
})
|
||||
if err != nil {
|
||||
utils.Error("创建网盘服务失败: %v", err)
|
||||
return TransferResult{
|
||||
Success: false,
|
||||
ErrorMsg: fmt.Sprintf("创建网盘服务失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
// 提取分享ID
|
||||
shareID, _ := commonutils.ExtractShareIdString(resource.URL)
|
||||
if shareID == "" {
|
||||
return TransferResult{
|
||||
Success: false,
|
||||
ErrorMsg: "无效的分享链接",
|
||||
}
|
||||
}
|
||||
|
||||
// 执行转存
|
||||
transferResult, err := service.Transfer(shareID)
|
||||
if err != nil {
|
||||
utils.Error("转存失败: %v", err)
|
||||
return TransferResult{
|
||||
Success: false,
|
||||
ErrorMsg: fmt.Sprintf("转存失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
if transferResult == nil || !transferResult.Success {
|
||||
errMsg := "转存失败"
|
||||
if transferResult != nil && transferResult.Message != "" {
|
||||
errMsg = transferResult.Message
|
||||
}
|
||||
utils.Error("转存失败: %s", errMsg)
|
||||
return TransferResult{
|
||||
Success: false,
|
||||
ErrorMsg: errMsg,
|
||||
}
|
||||
}
|
||||
|
||||
// 提取转存链接
|
||||
var saveURL string
|
||||
if data, ok := transferResult.Data.(map[string]interface{}); ok {
|
||||
if v, ok := data["shareUrl"]; ok {
|
||||
saveURL, _ = v.(string)
|
||||
}
|
||||
}
|
||||
if saveURL == "" {
|
||||
saveURL = transferResult.ShareURL
|
||||
}
|
||||
|
||||
if saveURL == "" {
|
||||
return TransferResult{
|
||||
Success: false,
|
||||
ErrorMsg: "转存成功但未获取到分享链接",
|
||||
}
|
||||
}
|
||||
|
||||
utils.Info("转存成功 - 资源ID: %d, 转存链接: %s", resource.ID, saveURL)
|
||||
|
||||
return TransferResult{
|
||||
Success: true,
|
||||
SaveURL: saveURL,
|
||||
}
|
||||
}
|
||||
|
||||
// getQuarkPanID 获取夸克网盘ID
|
||||
func getQuarkPanID() (uint, error) {
|
||||
// 通过FindAll方法查找所有平台,然后过滤出quark平台
|
||||
pans, err := repoManager.PanRepository.FindAll()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("查询平台信息失败: %v", err)
|
||||
}
|
||||
|
||||
for _, p := range pans {
|
||||
if p.Name == "quark" {
|
||||
return p.ID, nil
|
||||
}
|
||||
}
|
||||
|
||||
return 0, fmt.Errorf("未找到quark平台")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user