Feat experimental WebAuth API

This commit is contained in:
HFO4
2019-12-08 22:17:36 +08:00
parent 0932a10fed
commit f35c585edf
14 changed files with 269 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ import (
"errors"
"github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
"github.com/HFO4/cloudreve/pkg/filesystem/response"
"github.com/gin-gonic/gin"
"io"
)
@@ -27,6 +28,8 @@ type Handler interface {
Delete(ctx context.Context, files []string) ([]string, error)
// 获取文件
Get(ctx context.Context, path string) (io.ReadSeeker, error)
// 获取缩略图
Thumb(ctx context.Context, path string) (*response.ContentResponse, error)
}
// FileSystem 管理文件的文件系统

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/response"
"github.com/HFO4/cloudreve/pkg/thumb"
"github.com/HFO4/cloudreve/pkg/util"
)
@@ -16,6 +17,24 @@ import (
// HandledExtension 可以生成缩略图的文件扩展名
var HandledExtension = []string{"jpg", "jpeg", "png", "gif"}
// GetThumb 获取文件的缩略图
func (fs *FileSystem) GetThumb(ctx context.Context, id uint) (*response.ContentResponse, error) {
// 根据 ID 查找文件
file, err := model.GetFilesByIDs([]uint{id}, fs.User.ID)
if err != nil || len(file) == 0 || file[0].PicInfo == "" {
return &response.ContentResponse{
Redirect: false,
}, ErrObjectNotExist
}
fs.FileTarget = []model.File{file[0]}
res, err := fs.Handler.Thumb(ctx, file[0].SourceName)
// TODO 出错时重新生成缩略图
return res, err
}
// GenerateThumbnail 尝试为本地策略文件生成缩略图并获取图像原始大小
func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
// 判断是否可以生成缩略图
@@ -61,6 +80,7 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
}
// GenerateThumbnailSize 获取要生成的缩略图的尺寸
// TODO 从配置文件读取
func (fs *FileSystem) GenerateThumbnailSize(w, h int) (uint, uint) {
return 230, 200
return 400, 300
}

View File

@@ -2,6 +2,7 @@ package local
import (
"context"
"github.com/HFO4/cloudreve/pkg/filesystem/response"
"github.com/HFO4/cloudreve/pkg/util"
"io"
"os"
@@ -82,3 +83,16 @@ func (handler Handler) Delete(ctx context.Context, files []string) ([]string, er
return deleteFailed, retErr
}
// Thumb 获取文件缩略图
func (handler Handler) Thumb(ctx context.Context, path string) (*response.ContentResponse, error) {
file, err := handler.Get(ctx, path+"._thumb")
if err != nil {
return nil, err
}
return &response.ContentResponse{
Redirect: false,
Content: file,
}, nil
}

View File

@@ -0,0 +1,12 @@
package response
import "io"
// ContentResponse 获取文件内容类方法的通用返回值。
// 有些上传策略需要重定向,
// 有些直接写文件数据到浏览器
type ContentResponse struct {
Redirect bool
Content io.ReadSeeker
URL string
}

View File

@@ -5,6 +5,7 @@ import (
"errors"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/local"
"github.com/HFO4/cloudreve/pkg/filesystem/response"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
@@ -34,6 +35,11 @@ func (m FileHeaderMock) Delete(ctx context.Context, files []string) ([]string, e
return args.Get(0).([]string), args.Error(1)
}
func (m FileHeaderMock) Thumb(ctx context.Context, files string) (*response.ContentResponse, error) {
args := m.Called(ctx, files)
return args.Get(0).(*response.ContentResponse), args.Error(1)
}
func TestFileSystem_Upload(t *testing.T) {
asserts := assert.New(t)