2019-11-15 18:12:34 +08:00
|
|
|
package controllers
|
|
|
|
|
2019-12-08 22:17:36 +08:00
|
|
|
import "C"
|
2019-11-15 18:12:34 +08:00
|
|
|
import (
|
2019-11-16 20:31:34 +08:00
|
|
|
"context"
|
2019-11-16 16:11:37 +08:00
|
|
|
"github.com/HFO4/cloudreve/models"
|
2019-11-17 19:12:10 +08:00
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem"
|
2020-01-18 14:08:43 +08:00
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/driver/local"
|
2019-12-10 17:10:34 +08:00
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
2019-11-16 16:11:37 +08:00
|
|
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
2019-11-19 16:42:36 +08:00
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
2019-11-26 19:51:54 +08:00
|
|
|
"github.com/HFO4/cloudreve/service/explorer"
|
2019-11-15 18:12:34 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-12-08 22:17:36 +08:00
|
|
|
"net/http"
|
2019-11-25 21:12:28 +08:00
|
|
|
"net/url"
|
2019-11-17 19:12:10 +08:00
|
|
|
"strconv"
|
2019-11-15 18:12:34 +08:00
|
|
|
)
|
|
|
|
|
2019-12-13 15:10:44 +08:00
|
|
|
func DownloadArchive(c *gin.Context) {
|
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2019-12-13 20:46:11 +08:00
|
|
|
var service explorer.DownloadService
|
2019-12-13 15:10:44 +08:00
|
|
|
if err := c.ShouldBindUri(&service); err == nil {
|
2019-12-13 20:46:11 +08:00
|
|
|
res := service.DownloadArchived(ctx, c)
|
2019-12-13 15:10:44 +08:00
|
|
|
if res.Code != 0 {
|
|
|
|
c.JSON(200, res)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Archive(c *gin.Context) {
|
2019-12-12 09:16:24 +08:00
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2019-12-13 11:41:22 +08:00
|
|
|
var service explorer.ItemService
|
|
|
|
if err := c.ShouldBindJSON(&service); err == nil {
|
|
|
|
res := service.Archive(ctx, c)
|
|
|
|
c.JSON(200, res)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
2019-12-12 09:16:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 17:10:34 +08:00
|
|
|
// AnonymousGetContent 匿名获取文件资源
|
|
|
|
func AnonymousGetContent(c *gin.Context) {
|
2019-12-10 20:17:21 +08:00
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var service explorer.FileAnonymousGetService
|
2019-12-10 20:42:40 +08:00
|
|
|
if err := c.ShouldBindUri(&service); err == nil {
|
2019-12-10 20:17:21 +08:00
|
|
|
res := service.Download(ctx, c)
|
|
|
|
if res.Code != 0 {
|
|
|
|
c.JSON(200, res)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
2019-12-10 17:10:34 +08:00
|
|
|
}
|
|
|
|
|
2019-12-10 11:55:28 +08: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
|
|
|
|
}
|
2019-12-29 14:31:03 +08:00
|
|
|
defer fs.Recycle()
|
2019-12-10 11:55:28 +08:00
|
|
|
|
|
|
|
// 获取文件ID
|
|
|
|
fileID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(200, serializer.ParamErr("无法解析文件ID", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceURL, err := fs.GetSource(ctx, uint(fileID))
|
|
|
|
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 22:17:36 +08: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
|
|
|
|
}
|
2019-12-29 14:31:03 +08:00
|
|
|
defer fs.Recycle()
|
2019-12-08 22:17:36 +08:00
|
|
|
|
|
|
|
// 获取文件ID
|
|
|
|
fileID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(200, serializer.ParamErr("无法解析文件ID", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取缩略图
|
|
|
|
resp, err := fs.GetThumb(ctx, uint(fileID))
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(200, serializer.Err(serializer.CodeNotSet, "无法获取缩略图", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Redirect {
|
|
|
|
c.Redirect(http.StatusMovedPermanently, resp.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-02 13:31:41 +08:00
|
|
|
defer resp.Content.Close()
|
|
|
|
http.ServeContent(c.Writer, c.Request, "thumb.png", fs.FileTarget[0].UpdatedAt, resp.Content)
|
2019-12-11 18:52:41 +08:00
|
|
|
|
2019-12-08 22:17:36 +08:00
|
|
|
}
|
|
|
|
|
2019-12-14 15:06:30 +08:00
|
|
|
// Preview 预览文件
|
|
|
|
func Preview(c *gin.Context) {
|
2019-12-14 14:28:01 +08:00
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2019-12-15 14:01:37 +08:00
|
|
|
var service explorer.SingleFileService
|
2019-12-14 14:28:01 +08:00
|
|
|
if err := c.ShouldBindUri(&service); err == nil {
|
2020-01-04 15:49:08 +08:00
|
|
|
res := service.PreviewContent(ctx, c, false)
|
2019-12-14 15:06:30 +08:00
|
|
|
// 是否需要重定向
|
|
|
|
if res.Code == -301 {
|
2019-12-14 14:28:01 +08:00
|
|
|
c.Redirect(301, res.Data.(string))
|
2019-12-31 13:09:26 +08:00
|
|
|
return
|
2019-12-14 14:28:01 +08:00
|
|
|
}
|
2019-12-31 13:09:26 +08:00
|
|
|
// 是否有错误发生
|
2019-12-14 15:06:30 +08:00
|
|
|
if res.Code != 0 {
|
|
|
|
c.JSON(200, res)
|
|
|
|
}
|
2019-12-14 14:28:01 +08:00
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
2019-12-16 13:24:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 15:49:08 +08:00
|
|
|
// PreviewText 预览文本文件
|
|
|
|
func PreviewText(c *gin.Context) {
|
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var service explorer.SingleFileService
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 13:24:09 +08:00
|
|
|
// GetDocPreview 获取DOC文件预览地址
|
|
|
|
func GetDocPreview(c *gin.Context) {
|
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var service explorer.SingleFileService
|
|
|
|
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 14:28:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 20:46:11 +08:00
|
|
|
// CreateDownloadSession 创建文件下载会话
|
|
|
|
func CreateDownloadSession(c *gin.Context) {
|
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2019-12-15 14:01:37 +08:00
|
|
|
var service explorer.SingleFileService
|
2019-12-13 20:46:11 +08:00
|
|
|
if err := c.ShouldBindUri(&service); err == nil {
|
|
|
|
res := service.CreateDownloadSession(ctx, c)
|
|
|
|
c.JSON(200, res)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 15:06:30 +08:00
|
|
|
// Download 文件下载
|
2019-11-26 19:51:54 +08:00
|
|
|
func Download(c *gin.Context) {
|
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2019-12-13 20:46:11 +08:00
|
|
|
var service explorer.DownloadService
|
2019-11-29 15:24:23 +08:00
|
|
|
if err := c.ShouldBindUri(&service); err == nil {
|
2019-11-26 19:51:54 +08:00
|
|
|
res := service.Download(ctx, c)
|
2019-11-27 12:49:31 +08:00
|
|
|
if res.Code != 0 {
|
|
|
|
c.JSON(200, res)
|
|
|
|
}
|
2019-11-26 19:51:54 +08:00
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 14:01:37 +08:00
|
|
|
// PutContent 更新文件内容
|
|
|
|
func PutContent(c *gin.Context) {
|
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var service explorer.SingleFileService
|
|
|
|
if err := c.ShouldBindUri(&service); err == nil {
|
|
|
|
res := service.PutContent(ctx, c)
|
|
|
|
c.JSON(200, res)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 13:26:32 +08:00
|
|
|
// FileUploadStream 本地策略流式上传
|
2019-11-17 19:12:10 +08:00
|
|
|
func FileUploadStream(c *gin.Context) {
|
2019-11-18 13:26:32 +08:00
|
|
|
// 创建上下文
|
2019-11-17 19:12:10 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2019-11-18 13:26:32 +08:00
|
|
|
// 非本地策略时拒绝上传
|
|
|
|
if user, ok := c.Get("user"); ok && user.(*model.User).Policy.Type != "local" {
|
2019-11-30 15:09:56 +08:00
|
|
|
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, "当前存储策略无法使用", nil))
|
2019-11-18 13:26:32 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 取得文件大小
|
2019-11-17 19:12:10 +08:00
|
|
|
fileSize, err := strconv.ParseUint(c.Request.Header.Get("Content-Length"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-25 21:12:28 +08:00
|
|
|
// 解码文件名和路径
|
|
|
|
fileName, err := url.QueryUnescape(c.Request.Header.Get("X-FileName"))
|
|
|
|
filePath, err := url.QueryUnescape(c.Request.Header.Get("X-Path"))
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-17 19:12:10 +08:00
|
|
|
fileData := local.FileStream{
|
2019-11-19 16:42:36 +08:00
|
|
|
MIMEType: c.Request.Header.Get("Content-Type"),
|
|
|
|
File: c.Request.Body,
|
|
|
|
Size: fileSize,
|
2019-11-25 21:12:28 +08:00
|
|
|
Name: fileName,
|
|
|
|
VirtualPath: util.DotPathToStandardPath(filePath),
|
2019-11-17 19:12:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建文件系统
|
2019-11-24 13:06:15 +08:00
|
|
|
fs, err := filesystem.NewFileSystemFromContext(c)
|
2019-11-17 19:12:10 +08:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(200, serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 给文件系统分配钩子
|
2019-11-26 11:42:26 +08:00
|
|
|
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)
|
2019-11-17 19:12:10 +08:00
|
|
|
|
|
|
|
// 执行上传
|
2019-12-10 17:10:34 +08:00
|
|
|
uploadCtx := context.WithValue(ctx, fsctx.GinCtx, c)
|
2019-11-18 13:26:32 +08:00
|
|
|
err = fs.Upload(uploadCtx, fileData)
|
2019-11-17 19:12:10 +08:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(200, serializer.Err(serializer.CodeUploadFailed, err.Error(), err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, serializer.Response{
|
|
|
|
Code: 0,
|
|
|
|
})
|
|
|
|
}
|
2019-12-28 15:50:56 +08:00
|
|
|
|
|
|
|
// GetUploadCredential 获取上传凭证
|
|
|
|
func GetUploadCredential(c *gin.Context) {
|
|
|
|
// 创建上下文
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var service explorer.UploadCredentialService
|
|
|
|
if err := c.ShouldBindQuery(&service); err == nil {
|
|
|
|
res := service.Get(ctx, c)
|
|
|
|
c.JSON(200, res)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, ErrorResponse(err))
|
|
|
|
}
|
|
|
|
}
|