2023-03-22 22:45:17 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
"octopus/internal/dal"
|
2023-03-23 15:44:57 +08:00
|
|
|
"octopus/internal/schema"
|
2023-03-22 22:45:17 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/xid"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const TableNameDocFolder = "doc_folders"
|
|
|
|
const TableNameDoc = "docs"
|
|
|
|
|
|
|
|
type DocFolder struct {
|
|
|
|
Base
|
|
|
|
Name string `gorm:"column:name;type:varchar;not null"` // 文件夹名称
|
|
|
|
IsDeletable bool `gorm:"column:is_deletable;type:boolean;not null;default:true"` // 是否允许被删除
|
|
|
|
IsEditable bool `gorm:"column:is_editable;type:boolean;not null;default:true"` // 是否允许被修改
|
|
|
|
Path string `gorm:"column:path;type:varchar;index:idx_path"` // 路径索引
|
|
|
|
CreatedBy string `gorm:"column:created_by;type:varchar;not null"` // 创建人
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*DocFolder) TableName() string {
|
|
|
|
return TableNameDocFolder
|
|
|
|
}
|
2023-03-23 15:44:57 +08:00
|
|
|
|
|
|
|
func (df *DocFolder) ToSchema() *schema.DocFolder {
|
|
|
|
return &schema.DocFolder{
|
|
|
|
ID: df.ID,
|
|
|
|
IsDeletable: df.IsDeletable,
|
|
|
|
IsEditable: df.IsEditable,
|
|
|
|
CreatedAt: df.CreatedAt,
|
|
|
|
UpdatedAt: df.UpdatedAt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DocFolder) BeforeCreate(*gorm.DB) error {
|
|
|
|
d.ID = xid.New().String()
|
2023-03-22 22:45:17 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Doc struct {
|
|
|
|
Base
|
2023-03-23 15:44:57 +08:00
|
|
|
Name string `gorm:"column:name;type:varchar;not null"` // 文件夹名称
|
|
|
|
IsDeletable bool `gorm:"column:is_deletable;type:boolean;not null;default:true"` // 是否允许被删除
|
|
|
|
IsEditable bool `gorm:"column:is_editable;type:boolean;not null;default:true"` // 是否允许编辑名称
|
|
|
|
FolderID string `gorm:"column:folder_id;type:varchar;index:idx_folder_id"` // 文件夹ID
|
|
|
|
ObjectName string `gorm:"column:object_name;type:varchar"` // 对象存储中对应的object_name
|
|
|
|
UploadedAt time.Time `gorm:"column:uploaded_at;type:datetime"` // 上传时间
|
|
|
|
CreatedBy string `gorm:"column:created_by;type:varchar;not null"` // 创建人
|
2023-03-22 22:45:17 +08:00
|
|
|
|
|
|
|
Folder *DocFolder `gorm:"foreignKey:FolderID;references:ID"`
|
|
|
|
}
|
|
|
|
|
2023-03-23 15:44:57 +08:00
|
|
|
func (d *Doc) ToSchema(ctx context.Context) *schema.Doc {
|
|
|
|
url, _ := d.PresignedURL(ctx)
|
|
|
|
|
|
|
|
return &schema.Doc{
|
|
|
|
ID: d.ID,
|
|
|
|
Folder: d.Folder.ToSchema(),
|
|
|
|
PresignedURL: url,
|
|
|
|
IsDeletable: d.IsDeletable,
|
|
|
|
IsEditable: d.IsEditable,
|
|
|
|
UploadedAt: d.UploadedAt,
|
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
|
UpdatedAt: d.UpdatedAt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Doc) PresignedURL(ctx context.Context) (*url.URL, error) {
|
|
|
|
return dal.GetStorage().PresignedGetObject(ctx, d.ObjectName, time.Hour)
|
2023-03-22 22:45:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Doc) TableName() string {
|
2023-03-23 17:43:27 +08:00
|
|
|
return TableNameDoc
|
2023-03-22 22:45:17 +08:00
|
|
|
}
|
2023-03-23 15:44:57 +08:00
|
|
|
func (d *Doc) BeforeCreate(*gorm.DB) error {
|
|
|
|
d.ID = xid.New().String()
|
2023-03-22 22:45:17 +08:00
|
|
|
return nil
|
|
|
|
}
|