Feat: get file content via anonymous url
This commit is contained in:
parent
ea7f034332
commit
297b507ca7
8 changed files with 90 additions and 21 deletions
|
@ -7,18 +7,18 @@ MaxWidth = 400
|
|||
MaxHeight = 300
|
||||
FileSuffix = ._thumb
|
||||
|
||||
[Database]
|
||||
Type = mysql
|
||||
User = root
|
||||
Password = root
|
||||
Host = 127.0.0.1:3306
|
||||
Name = v3
|
||||
TablePrefix = v3_
|
||||
; [Database]
|
||||
; Type = mysql
|
||||
; User = root
|
||||
; Password = root
|
||||
; Host = 127.0.0.1:3306
|
||||
; Name = v3
|
||||
; TablePrefix = v3_
|
||||
|
||||
[Redis]
|
||||
Server = 127.0.0.1:6379
|
||||
Password =
|
||||
DB = 0
|
||||
; [Redis]
|
||||
; Server = 127.0.0.1:6379
|
||||
; Password =
|
||||
; DB = 0
|
||||
|
||||
[Captcha]
|
||||
Height = 60
|
||||
|
|
|
@ -52,10 +52,17 @@ func (folder *Folder) GetChildFiles() ([]File, error) {
|
|||
return files, result.Error
|
||||
}
|
||||
|
||||
// GetFilesByIDs 根据文件ID批量获取文件
|
||||
// GetFilesByIDs 根据文件ID批量获取文件,
|
||||
// UID为0表示忽略用户,只根据文件ID检索
|
||||
// TODO 测试
|
||||
func GetFilesByIDs(ids []uint, uid uint) ([]File, error) {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -189,6 +189,18 @@ func addDefaultGroups() {
|
|||
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() {
|
||||
|
|
|
@ -82,21 +82,24 @@ func (fs *FileSystem) GetContent(ctx context.Context, path string) (io.ReadSeeke
|
|||
}
|
||||
|
||||
// 找到文件
|
||||
exist, file := fs.IsFileExist(path)
|
||||
if !exist {
|
||||
return nil, ErrObjectNotExist
|
||||
if len(fs.FileTarget) == 0 {
|
||||
exist, file := fs.IsFileExist(path)
|
||||
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()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取文件流
|
||||
rs, err := fs.Handler.Get(ctx, file.SourceName)
|
||||
rs, err := fs.Handler.Get(ctx, fs.FileTarget[0].SourceName)
|
||||
if err != nil {
|
||||
return nil, ErrIO.WithError(err)
|
||||
}
|
||||
|
|
|
@ -79,6 +79,22 @@ func NewFileSystem(user *model.User) (*FileSystem, error) {
|
|||
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 根据存储策略分配文件适配器
|
||||
func (fs *FileSystem) dispatchHandler() error {
|
||||
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
|
||||
// CodeIOFailed IO操作失败
|
||||
CodeIOFailed = 50004
|
||||
// CodeGroupNotAllowed 当前用户组不允许
|
||||
CodeGroupNotAllowed = 50005
|
||||
//CodeParamErr 各种奇奇怪怪的参数错误
|
||||
CodeParamErr = 40001
|
||||
// CodeNotSet 未定错误,后续尝试从error中获取
|
||||
|
|
|
@ -23,7 +23,7 @@ func AnonymousGetContent(c *gin.Context) {
|
|||
defer cancel()
|
||||
|
||||
var service explorer.FileAnonymousGetService
|
||||
if err := c.ShouldBind(&service); err == nil {
|
||||
if err := c.ShouldBindUri(&service); err == nil {
|
||||
res := service.Download(ctx, c)
|
||||
if res.Code != 0 {
|
||||
c.JSON(200, res)
|
||||
|
|
|
@ -21,6 +21,25 @@ type FileAnonymousGetService struct {
|
|||
|
||||
// Download 签名的匿名文件下载
|
||||
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{
|
||||
Code: 0,
|
||||
|
|
Loading…
Add table
Reference in a new issue