mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-19 12:16:24 +08:00

提供一种类似大多数网盘的文件分享操作,这种分享方式可以通过强制 Web 代理隐藏文件源路径,可以设置分享码、最大访问数和过期时间,并且不需要启用 guest 用户。 在全局设置中可以调整: - 是否强制 Web 代理 - 是否允许预览 - 是否允许预览压缩文件 - 分享文件后,点击“复制链接”按钮复制的内容 前端部分:OpenListTeam/OpenList-Frontend#156 文档部分:OpenListTeam/OpenList-Docs#130 Close #183 Close #526 Close #860 Close #892 Close #1079 * feat(share): support more secure file sharing * feat(share): add archive preview * fix(share): fix some bugs * feat(openlist_share): add openlist share driver * fix(share): lack unwrap when get virtual path * fix: use unwrapPath instead of path for virtual file name comparison * fix(share): change request method of /api/share/list from GET to Any * fix(share): path traversal vulnerability in sharing path check * 修复分享alias驱动的文件 没开代理时无法获取URL * fix(sharing): update error message for sharing root link extraction --------- Co-authored-by: Suyunmeng <69945917+Suyunmeng@users.noreply.github.com> Co-authored-by: j2rong4cn <j2rong@qq.com>
63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package db
|
|
|
|
import (
|
|
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
|
"github.com/OpenListTeam/OpenList/v4/pkg/utils/random"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func GetSharingById(id string) (*model.SharingDB, error) {
|
|
s := model.SharingDB{ID: id}
|
|
if err := db.Where(s).First(&s).Error; err != nil {
|
|
return nil, errors.Wrapf(err, "failed get sharing")
|
|
}
|
|
return &s, nil
|
|
}
|
|
|
|
func GetSharings(pageIndex, pageSize int) (sharings []model.SharingDB, count int64, err error) {
|
|
sharingDB := db.Model(&model.SharingDB{})
|
|
if err := sharingDB.Count(&count).Error; err != nil {
|
|
return nil, 0, errors.Wrapf(err, "failed get sharings count")
|
|
}
|
|
if err := sharingDB.Order(columnName("id")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&sharings).Error; err != nil {
|
|
return nil, 0, errors.Wrapf(err, "failed get find sharings")
|
|
}
|
|
return sharings, count, nil
|
|
}
|
|
|
|
func GetSharingsByCreatorId(creator uint, pageIndex, pageSize int) (sharings []model.SharingDB, count int64, err error) {
|
|
sharingDB := db.Model(&model.SharingDB{})
|
|
cond := model.SharingDB{CreatorId: creator}
|
|
if err := sharingDB.Where(cond).Count(&count).Error; err != nil {
|
|
return nil, 0, errors.Wrapf(err, "failed get sharings count")
|
|
}
|
|
if err := sharingDB.Where(cond).Order(columnName("id")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&sharings).Error; err != nil {
|
|
return nil, 0, errors.Wrapf(err, "failed get find sharings")
|
|
}
|
|
return sharings, count, nil
|
|
}
|
|
|
|
func CreateSharing(s *model.SharingDB) (string, error) {
|
|
id := random.String(8)
|
|
for len(id) < 12 {
|
|
old := model.SharingDB{
|
|
ID: id,
|
|
}
|
|
if err := db.Where(old).First(&old).Error; err != nil {
|
|
s.ID = id
|
|
return id, errors.WithStack(db.Create(s).Error)
|
|
}
|
|
id += random.String(1)
|
|
}
|
|
return "", errors.New("failed find valid id")
|
|
}
|
|
|
|
func UpdateSharing(s *model.SharingDB) error {
|
|
return errors.WithStack(db.Save(s).Error)
|
|
}
|
|
|
|
func DeleteSharingById(id string) error {
|
|
s := model.SharingDB{ID: id}
|
|
return errors.WithStack(db.Where(s).Delete(&s).Error)
|
|
}
|