mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Modify: change raw object ID to Hash ID in file service
This commit is contained in:
@@ -3,11 +3,9 @@ package controllers
|
||||
import (
|
||||
"context"
|
||||
ariaCall "github.com/HFO4/cloudreve/pkg/aria2"
|
||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||
"github.com/HFO4/cloudreve/service/aria2"
|
||||
"github.com/HFO4/cloudreve/service/explorer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AddAria2URL 添加离线下载URL
|
||||
@@ -38,15 +36,8 @@ func AddAria2Torrent(c *gin.Context) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service explorer.SingleFileService
|
||||
var service explorer.FileIDService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
// 验证必须是种子文件
|
||||
filePath := c.Param("path")
|
||||
if !strings.HasSuffix(filePath, ".torrent") {
|
||||
c.JSON(200, serializer.ParamErr("只能下载 .torrent 文件", nil))
|
||||
return
|
||||
}
|
||||
|
||||
// 获取种子内容的下载地址
|
||||
res := service.CreateDownloadSession(ctx, c)
|
||||
if res.Code != 0 {
|
||||
|
||||
@@ -38,7 +38,7 @@ func Archive(c *gin.Context) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service explorer.ItemService
|
||||
var service explorer.ItemIDService
|
||||
if err := c.ShouldBindJSON(&service); err == nil {
|
||||
res := service.Archive(ctx, c)
|
||||
c.JSON(200, res)
|
||||
@@ -86,7 +86,7 @@ func AnonymousGetContent(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// Source 获取文件的外链地址
|
||||
// GetSource 获取文件的外链地址
|
||||
func GetSource(c *gin.Context) {
|
||||
// 创建上下文
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
@@ -100,13 +100,13 @@ func GetSource(c *gin.Context) {
|
||||
defer fs.Recycle()
|
||||
|
||||
// 获取文件ID
|
||||
fileID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
c.JSON(200, serializer.ParamErr("无法解析文件ID", err))
|
||||
fileID, ok := c.Get("object_id")
|
||||
if !ok {
|
||||
c.JSON(200, serializer.ParamErr("文件不存在", err))
|
||||
return
|
||||
}
|
||||
|
||||
sourceURL, err := fs.GetSource(ctx, uint(fileID))
|
||||
sourceURL, err := fs.GetSource(ctx, fileID.(uint))
|
||||
if err != nil {
|
||||
c.JSON(200, serializer.Err(serializer.CodeNotSet, err.Error(), err))
|
||||
return
|
||||
@@ -135,14 +135,14 @@ func Thumb(c *gin.Context) {
|
||||
defer fs.Recycle()
|
||||
|
||||
// 获取文件ID
|
||||
fileID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
c.JSON(200, serializer.ParamErr("无法解析文件ID", err))
|
||||
fileID, ok := c.Get("object_id")
|
||||
if !ok {
|
||||
c.JSON(200, serializer.ParamErr("文件不存在", err))
|
||||
return
|
||||
}
|
||||
|
||||
// 获取缩略图
|
||||
resp, err := fs.GetThumb(ctx, uint(fileID))
|
||||
resp, err := fs.GetThumb(ctx, fileID.(uint))
|
||||
if err != nil {
|
||||
c.JSON(200, serializer.Err(serializer.CodeNotSet, "无法获取缩略图", err))
|
||||
return
|
||||
@@ -165,7 +165,7 @@ func Preview(c *gin.Context) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service explorer.SingleFileService
|
||||
var service explorer.FileIDService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.PreviewContent(ctx, c, false)
|
||||
// 是否需要重定向
|
||||
@@ -188,7 +188,7 @@ func PreviewText(c *gin.Context) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service explorer.SingleFileService
|
||||
var service explorer.FileIDService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.PreviewContent(ctx, c, true)
|
||||
// 是否有错误发生
|
||||
@@ -206,7 +206,7 @@ func GetDocPreview(c *gin.Context) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service explorer.SingleFileService
|
||||
var service explorer.FileIDService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.CreateDocPreviewSession(ctx, c)
|
||||
c.JSON(200, res)
|
||||
@@ -221,7 +221,7 @@ func CreateDownloadSession(c *gin.Context) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service explorer.SingleFileService
|
||||
var service explorer.FileIDService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.CreateDownloadSession(ctx, c)
|
||||
c.JSON(200, res)
|
||||
@@ -253,7 +253,7 @@ func PutContent(c *gin.Context) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service explorer.SingleFileService
|
||||
var service explorer.FileIDService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.PutContent(ctx, c)
|
||||
c.JSON(200, res)
|
||||
@@ -344,9 +344,9 @@ func GetUploadCredential(c *gin.Context) {
|
||||
|
||||
// SearchFile 搜索文件
|
||||
func SearchFile(c *gin.Context) {
|
||||
var service explorer.ItemDecompressService
|
||||
if err := c.ShouldBindJSON(&service); err == nil {
|
||||
res := service.CreateDecompressTask(c)
|
||||
var service explorer.ItemSearchService
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.Search(c)
|
||||
c.JSON(200, res)
|
||||
} else {
|
||||
c.JSON(200, ErrorResponse(err))
|
||||
|
||||
@@ -12,7 +12,7 @@ func Delete(c *gin.Context) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var service explorer.ItemService
|
||||
var service explorer.ItemIDService
|
||||
if err := c.ShouldBindJSON(&service); err == nil {
|
||||
res := service.Delete(ctx, c)
|
||||
c.JSON(200, res)
|
||||
|
||||
@@ -3,6 +3,7 @@ package routers
|
||||
import (
|
||||
"github.com/HFO4/cloudreve/middleware"
|
||||
"github.com/HFO4/cloudreve/pkg/conf"
|
||||
"github.com/HFO4/cloudreve/pkg/hashid"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/HFO4/cloudreve/routers/controllers"
|
||||
"github.com/gin-contrib/cors"
|
||||
@@ -245,22 +246,22 @@ func InitMasterRouter() *gin.Engine {
|
||||
}
|
||||
|
||||
// 文件
|
||||
file := auth.Group("file")
|
||||
file := auth.Group("file", middleware.HashID(hashid.FileID))
|
||||
{
|
||||
// 文件上传
|
||||
file.POST("upload", controllers.FileUploadStream)
|
||||
// 获取上传凭证
|
||||
file.GET("upload/credential", controllers.GetUploadCredential)
|
||||
// 更新文件
|
||||
file.PUT("update/*path", controllers.PutContent)
|
||||
file.PUT("update/:id", controllers.PutContent)
|
||||
// 创建文件下载会话
|
||||
file.PUT("download/*path", controllers.CreateDownloadSession)
|
||||
file.PUT("download/:id", controllers.CreateDownloadSession)
|
||||
// 预览文件
|
||||
file.GET("preview/*path", controllers.Preview)
|
||||
file.GET("preview/:id", controllers.Preview)
|
||||
// 获取文本文件内容
|
||||
file.GET("content/*path", controllers.PreviewText)
|
||||
file.GET("content/:id", controllers.PreviewText)
|
||||
// 取得Office文档预览地址
|
||||
file.GET("doc/*path", controllers.GetDocPreview)
|
||||
file.GET("doc/:id", controllers.GetDocPreview)
|
||||
// 获取缩略图
|
||||
file.GET("thumb/:id", controllers.Thumb)
|
||||
// 取得文件外链
|
||||
@@ -281,7 +282,7 @@ func InitMasterRouter() *gin.Engine {
|
||||
// 创建URL下载任务
|
||||
aria2.POST("url", controllers.AddAria2URL)
|
||||
// 创建种子下载任务
|
||||
aria2.POST("torrent/*path", controllers.AddAria2Torrent)
|
||||
aria2.POST("torrent/:id", middleware.HashID(hashid.FileID), controllers.AddAria2Torrent)
|
||||
// 重新选择要下载的文件
|
||||
aria2.PUT("select/:gid", controllers.SelectAria2File)
|
||||
// 取消下载任务
|
||||
|
||||
Reference in New Issue
Block a user