Cloudreve/routers/controllers/file.go

380 lines
9.2 KiB
Go
Raw Normal View History

2019-11-15 05:12:34 -05:00
package controllers
import (
2019-11-16 07:31:34 -05:00
"context"
"fmt"
2019-12-08 09:17:36 -05:00
"net/http"
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/cloudreve/Cloudreve/v3/service/explorer"
"github.com/gin-gonic/gin"
2019-11-15 05:12:34 -05:00
)
2019-12-13 02:10:44 -05:00
func DownloadArchive(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.DownloadService
2019-12-13 02:10:44 -05:00
if err := c.ShouldBindUri(&service); err == nil {
res := service.DownloadArchived(ctx, c)
2019-12-13 02:10:44 -05:00
if res.Code != 0 {
c.JSON(200, res)
}
} else {
c.JSON(200, ErrorResponse(err))
}
}
func Archive(c *gin.Context) {
2019-12-11 20:16:24 -05:00
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.ItemIDService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Archive(ctx, c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
2019-12-11 20:16:24 -05:00
}
}
2020-02-02 01:40:07 -05:00
// Compress 创建文件压缩任务
func Compress(c *gin.Context) {
var service explorer.ItemCompressService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.CreateCompressTask(c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
2020-02-03 00:23:33 -05:00
// Decompress 创建文件解压缩任务
func Decompress(c *gin.Context) {
var service explorer.ItemDecompressService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.CreateDecompressTask(c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
2019-12-10 04:10:34 -05:00
// AnonymousGetContent 匿名获取文件资源
func AnonymousGetContent(c *gin.Context) {
2019-12-10 07:17:21 -05:00
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.FileAnonymousGetService
if err := c.ShouldBindUri(&service); err == nil {
2019-12-10 07:17:21 -05:00
res := service.Download(ctx, c)
if res.Code != 0 {
c.JSON(200, res)
}
} else {
c.JSON(200, ErrorResponse(err))
}
}
// AnonymousPermLink 文件签名后的永久链接
func AnonymousPermLink(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.FileAnonymousGetService
if err := c.ShouldBindUri(&service); err == nil {
res := service.Source(ctx, c)
// 是否需要重定向
if res.Code == -302 {
c.Redirect(302, res.Data.(string))
return
}
// 是否有错误发生
if res.Code != 0 {
c.JSON(200, res)
}
} else {
c.JSON(200, ErrorResponse(err))
2019-12-10 07:17:21 -05:00
}
2019-12-10 04:10:34 -05:00
}
// GetSource 获取文件的外链地址
func GetSource(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err))
return
}
defer fs.Recycle()
// 获取文件ID
fileID, ok := c.Get("object_id")
if !ok {
c.JSON(200, serializer.ParamErr("文件不存在", err))
return
}
sourceURL, err := fs.GetSource(ctx, fileID.(uint))
if err != nil {
c.JSON(200, serializer.Err(serializer.CodeNotSet, err.Error(), err))
return
}
c.JSON(200, serializer.Response{
Code: 0,
Data: struct {
URL string `json:"url"`
}{URL: sourceURL},
})
}
2019-12-08 09:17:36 -05:00
// Thumb 获取文件缩略图
func Thumb(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err))
return
}
defer fs.Recycle()
2019-12-08 09:17:36 -05:00
// 获取文件ID
fileID, ok := c.Get("object_id")
if !ok {
c.JSON(200, serializer.ParamErr("文件不存在", err))
2019-12-08 09:17:36 -05:00
return
}
// 获取缩略图
resp, err := fs.GetThumb(ctx, fileID.(uint))
2019-12-08 09:17:36 -05:00
if err != nil {
c.JSON(200, serializer.Err(serializer.CodeNotSet, "无法获取缩略图", err))
return
}
if resp.Redirect {
c.Header("Cache-Control", fmt.Sprintf("max-age=%d", resp.MaxAge))
2019-12-08 09:17:36 -05:00
c.Redirect(http.StatusMovedPermanently, resp.URL)
return
}
2020-01-02 00:31:41 -05:00
defer resp.Content.Close()
http.ServeContent(c.Writer, c.Request, "thumb."+conf.ThumbConfig.EncodeMethod, fs.FileTarget[0].UpdatedAt, resp.Content)
2019-12-11 05:52:41 -05:00
2019-12-08 09:17:36 -05:00
}
// Preview 预览文件
func Preview(c *gin.Context) {
2019-12-14 01:28:01 -05:00
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.FileIDService
2019-12-14 01:28:01 -05:00
if err := c.ShouldBindUri(&service); err == nil {
res := service.PreviewContent(ctx, c, false)
// 是否需要重定向
if res.Code == -301 {
2019-12-14 01:28:01 -05:00
c.Redirect(301, res.Data.(string))
return
2019-12-14 01:28:01 -05:00
}
// 是否有错误发生
if res.Code != 0 {
c.JSON(200, res)
}
2019-12-14 01:28:01 -05:00
} else {
c.JSON(200, ErrorResponse(err))
}
}
// PreviewText 预览文本文件
func PreviewText(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.FileIDService
if err := c.ShouldBindUri(&service); err == nil {
res := service.PreviewContent(ctx, c, true)
// 是否有错误发生
if res.Code != 0 {
c.JSON(200, res)
}
} else {
c.JSON(200, ErrorResponse(err))
}
}
// GetDocPreview 获取DOC文件预览地址
func GetDocPreview(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.FileIDService
if err := c.ShouldBindUri(&service); err == nil {
res := service.CreateDocPreviewSession(ctx, c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
2019-12-14 01:28:01 -05:00
}
}
// CreateDownloadSession 创建文件下载会话
func CreateDownloadSession(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.FileIDService
if err := c.ShouldBindUri(&service); err == nil {
res := service.CreateDownloadSession(ctx, c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// Download 文件下载
func Download(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.DownloadService
2019-11-29 02:24:23 -05:00
if err := c.ShouldBindUri(&service); err == nil {
res := service.Download(ctx, c)
if res.Code != 0 {
c.JSON(200, res)
}
} else {
c.JSON(200, ErrorResponse(err))
}
}
2019-12-15 01:01:37 -05:00
// PutContent 更新文件内容
func PutContent(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.FileIDService
2019-12-15 01:01:37 -05:00
if err := c.ShouldBindUri(&service); err == nil {
res := service.PutContent(ctx, c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
2022-02-27 01:11:01 -05:00
// FileUpload 本地策略文件上传
func FileUpload(c *gin.Context) {
// 创建上下文
2019-11-17 06:12:10 -05:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2022-02-27 01:11:01 -05:00
var service explorer.UploadService
if err := c.ShouldBindUri(&service); err == nil {
res := service.Upload(ctx, c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
2022-02-27 01:11:01 -05:00
//fileData := fsctx.FileStream{
// MIMEType: c.Request.Header.Get("Content-Type"),
// File: c.Request.Body,
// Size: fileSize,
// Name: fileName,
// VirtualPath: filePath,
// Mode: fsctx.Create,
//}
//
//// 创建文件系统
//fs, err := filesystem.NewFileSystemFromContext(c)
//if err != nil {
// c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err))
// return
//}
//
//// 非可用策略时拒绝上传
//if !fs.Policy.IsTransitUpload(fileSize) {
// request.BlackHole(c.Request.Body)
// c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, "当前存储策略无法使用", nil))
// return
//}
//
//// 给文件系统分配钩子
//fs.Use("BeforeUpload", filesystem.HookValidateFile)
//fs.Use("BeforeUpload", filesystem.HookValidateCapacity)
//fs.Use("AfterUploadCanceled", filesystem.HookDeleteTempFile)
//fs.Use("AfterUploadCanceled", filesystem.HookGiveBackCapacity)
//fs.Use("AfterUpload", filesystem.GenericAfterUpload)
//fs.Use("AfterValidateFailed", filesystem.HookDeleteTempFile)
//fs.Use("AfterValidateFailed", filesystem.HookGiveBackCapacity)
//fs.Use("AfterUploadFailed", filesystem.HookGiveBackCapacity)
//
//// 执行上传
//ctx = context.WithValue(ctx, fsctx.ValidateCapacityOnceCtx, &sync.Once{})
//uploadCtx := context.WithValue(ctx, fsctx.GinCtx, c)
//err = fs.Upload(uploadCtx, &fileData)
//if err != nil {
// c.JSON(200, serializer.Err(serializer.CodeUploadFailed, err.Error(), err))
// return
//}
//
//c.JSON(200, serializer.Response{
// Code: 0,
//})
2019-11-17 06:12:10 -05:00
}
2019-12-28 02:50:56 -05:00
// GetUploadCredential 创建上传会话
2019-12-28 02:50:56 -05:00
func GetUploadCredential(c *gin.Context) {
// 创建上下文
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var service explorer.UploadSessionService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Create(ctx, c)
2019-12-28 02:50:56 -05:00
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}
// SearchFile 搜索文件
func SearchFile(c *gin.Context) {
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))
}
}
// CreateFile 创建空白文件
func CreateFile(c *gin.Context) {
var service explorer.SingleFileService
if err := c.ShouldBindJSON(&service); err == nil {
res := service.Create(c)
c.JSON(200, res)
} else {
c.JSON(200, ErrorResponse(err))
}
}