Feat: get file content via anonymous url

pull/247/head
HFO4 5 years ago
parent ea7f034332
commit 297b507ca7

@ -7,18 +7,18 @@ MaxWidth = 400
MaxHeight = 300 MaxHeight = 300
FileSuffix = ._thumb FileSuffix = ._thumb
[Database] ; [Database]
Type = mysql ; Type = mysql
User = root ; User = root
Password = root ; Password = root
Host = 127.0.0.1:3306 ; Host = 127.0.0.1:3306
Name = v3 ; Name = v3
TablePrefix = v3_ ; TablePrefix = v3_
[Redis] ; [Redis]
Server = 127.0.0.1:6379 ; Server = 127.0.0.1:6379
Password = ; Password =
DB = 0 ; DB = 0
[Captcha] [Captcha]
Height = 60 Height = 60

@ -52,10 +52,17 @@ func (folder *Folder) GetChildFiles() ([]File, error) {
return files, result.Error return files, result.Error
} }
// GetFilesByIDs 根据文件ID批量获取文件 // GetFilesByIDs 根据文件ID批量获取文件,
// UID为0表示忽略用户只根据文件ID检索
// TODO 测试
func GetFilesByIDs(ids []uint, uid uint) ([]File, error) { func GetFilesByIDs(ids []uint, uid uint) ([]File, error) {
var files []File var files []File
result := DB.Where("id in (?) AND user_id = ?", ids, uid).Find(&files) var result *gorm.DB
if uid == 0 {
result = DB.Where("id in (?)", ids).Find(&files)
} else {
result = DB.Where("id in (?) AND user_id = ?", ids, uid).Find(&files)
}
return files, result.Error return files, result.Error
} }

@ -189,6 +189,18 @@ func addDefaultGroups() {
util.Log().Panic("无法创建初始注册会员用户组, %s", err) util.Log().Panic("无法创建初始注册会员用户组, %s", err)
} }
} }
err = nil
_, err = GetGroupByID(3)
// 未找到初始游客用户组时,则创建
if gorm.IsRecordNotFoundError(err) {
defaultAdminGroup := Group{
Name: "游客",
}
if err := DB.Create(&defaultAdminGroup).Error; err != nil {
util.Log().Panic("无法创建初始游客用户组, %s", err)
}
}
} }
func addDefaultUser() { func addDefaultUser() {

@ -82,21 +82,24 @@ func (fs *FileSystem) GetContent(ctx context.Context, path string) (io.ReadSeeke
} }
// 找到文件 // 找到文件
exist, file := fs.IsFileExist(path) if len(fs.FileTarget) == 0 {
if !exist { exist, file := fs.IsFileExist(path)
return nil, ErrObjectNotExist if !exist {
return nil, ErrObjectNotExist
}
fs.FileTarget = []model.File{*file}
ctx = context.WithValue(ctx, fsctx.FileModelCtx, file)
} }
fs.FileTarget = []model.File{*file}
ctx = context.WithValue(ctx, fsctx.FileModelCtx, file)
// 将当前存储策略重设为文件使用的 // 将当前存储策略重设为文件使用的
fs.Policy = file.GetPolicy() fs.Policy = fs.FileTarget[0].GetPolicy()
err = fs.dispatchHandler() err = fs.dispatchHandler()
if err != nil { if err != nil {
return nil, err return nil, err
} }
// 获取文件流 // 获取文件流
rs, err := fs.Handler.Get(ctx, file.SourceName) rs, err := fs.Handler.Get(ctx, fs.FileTarget[0].SourceName)
if err != nil { if err != nil {
return nil, ErrIO.WithError(err) return nil, ErrIO.WithError(err)
} }

@ -79,6 +79,22 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
return fs, err return fs, err
} }
// NewAnonymousFileSystem 初始化匿名文件系统
// TODO 测试
func NewAnonymousFileSystem() (*FileSystem, error) {
fs := &FileSystem{
User: &model.User{},
}
anonymousGroup, err := model.GetGroupByID(3)
if err != nil {
return nil, err
}
fs.User.Group = anonymousGroup
return fs, nil
}
// dispatchHandler 根据存储策略分配文件适配器 // dispatchHandler 根据存储策略分配文件适配器
func (fs *FileSystem) dispatchHandler() error { func (fs *FileSystem) dispatchHandler() error {
var policyType string var policyType string
@ -134,3 +150,13 @@ func (fs *FileSystem) SetTargetDir(dirs *[]model.Folder) {
} }
} }
// SetTargetFileByIDs 根据文件ID设置目标文件忽略用户ID
func (fs *FileSystem) SetTargetFileByIDs(ids []uint) error {
files, err := model.GetFilesByIDs(ids, 0)
if err != nil || len(files) == 0 {
return ErrFileExisted.WithError(err)
}
fs.SetTargetFile(&files)
return nil
}

@ -64,6 +64,8 @@ const (
CodePolicyNotAllowed = 50003 CodePolicyNotAllowed = 50003
// CodeIOFailed IO操作失败 // CodeIOFailed IO操作失败
CodeIOFailed = 50004 CodeIOFailed = 50004
// CodeGroupNotAllowed 当前用户组不允许
CodeGroupNotAllowed = 50005
//CodeParamErr 各种奇奇怪怪的参数错误 //CodeParamErr 各种奇奇怪怪的参数错误
CodeParamErr = 40001 CodeParamErr = 40001
// CodeNotSet 未定错误后续尝试从error中获取 // CodeNotSet 未定错误后续尝试从error中获取

@ -23,7 +23,7 @@ func AnonymousGetContent(c *gin.Context) {
defer cancel() defer cancel()
var service explorer.FileAnonymousGetService var service explorer.FileAnonymousGetService
if err := c.ShouldBind(&service); err == nil { if err := c.ShouldBindUri(&service); err == nil {
res := service.Download(ctx, c) res := service.Download(ctx, c)
if res.Code != 0 { if res.Code != 0 {
c.JSON(200, res) c.JSON(200, res)

@ -21,6 +21,25 @@ type FileAnonymousGetService struct {
// Download 签名的匿名文件下载 // Download 签名的匿名文件下载
func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Context) serializer.Response { func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Context) serializer.Response {
fs, err := filesystem.NewAnonymousFileSystem()
if err != nil {
return serializer.Err(serializer.CodeGroupNotAllowed, err.Error(), err)
}
// 查找文件
err = fs.SetTargetFileByIDs([]uint{service.ID})
if err != nil {
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
}
// 获取文件流
rs, err := fs.GetDownloadContent(ctx, "")
if err != nil {
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
}
// 发送文件
http.ServeContent(c.Writer, c.Request, service.Name, fs.FileTarget[0].UpdatedAt, rs)
return serializer.Response{ return serializer.Response{
Code: 0, Code: 0,

Loading…
Cancel
Save