|
- package pubshards
-
- import (
- "fmt"
-
- stgtypes "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/types"
- jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
- "gorm.io/gorm"
- "gorm.io/gorm/clause"
- )
-
- type LoadedStore struct {
- ShardStore stgtypes.ShardStore
- Config jcstypes.PubShards
- PasswordHash []byte
- ClientFileHashDB *gorm.DB
- }
-
- func (s *LoadedStore) StoreShard(userID jcstypes.UserID, path jcstypes.JPath, hash jcstypes.FileHash, size int64) (stgtypes.FileInfo, error) {
- info, err := s.ShardStore.Store(path, hash, size)
- if err != nil {
- return stgtypes.FileInfo{}, err
- }
-
- err = s.ClientFileHashDB.Clauses(clause.Insert{Modifier: "or ignore"}).Create(&FileEntry{
- UserID: userID,
- Path: info.Path,
- Hash: hash,
- Size: size,
- }).Error
- if err != nil {
- return stgtypes.FileInfo{}, err
- }
-
- return info, nil
- }
-
- func (s *LoadedStore) InfoShard(hash jcstypes.FileHash) (stgtypes.FileInfo, error) {
- return s.ShardStore.Info(hash)
- }
-
- func (s *LoadedStore) ListUserAll(userID jcstypes.UserID) ([]stgtypes.FileInfo, error) {
- var files []FileEntry
- err := s.ClientFileHashDB.Table("Files").Where("UserID = ?", userID).Find(&files).Error
- if err != nil {
- return nil, err
- }
-
- infos := make([]stgtypes.FileInfo, len(files))
- for i, file := range files {
- infos[i] = stgtypes.FileInfo{
- Path: file.Path,
- Size: file.Size,
- Hash: file.Hash,
- }
- }
-
- return infos, nil
- }
-
- func (s *LoadedStore) GC(userID jcstypes.UserID, fileHashes []jcstypes.FileHash) error {
- return s.ClientFileHashDB.Transaction(func(tx *gorm.DB) error {
- if err := tx.Delete(&FileEntry{}, "UserID = ?", userID).Error; err != nil {
- return fmt.Errorf("delete all hashes: %w", err)
- }
-
- hashes := make([]FileEntry, len(fileHashes))
- for i, hash := range fileHashes {
- hashes[i] = FileEntry{
- UserID: userID,
- Hash: hash,
- }
- }
-
- return tx.Clauses(clause.Insert{Modifier: "or ignore"}).Create(&hashes).Error
- })
- }
-
- func (s *LoadedStore) GetUserStats(userID jcstypes.UserID) stgtypes.Stats {
- // TODO 实现
- return stgtypes.Stats{}
- }
-
- func (s *LoadedStore) GetAllHashes() ([]jcstypes.FileHash, error) {
- var hashes []jcstypes.FileHash
- return hashes, s.ClientFileHashDB.Distinct("FileHash").Find(&hashes).Error
- }
-
- type FileEntry struct {
- UserID jcstypes.UserID `gorm:"column:UserID; type:bigint; primaryKey; not null" json:"userID"`
- Hash jcstypes.FileHash `gorm:"column:Hash; type:char(68); primaryKey; not null" json:"hash"`
- Path jcstypes.JPath `gorm:"column:Path; type:varchar(1024); not null; serializer:string" json:"path"`
- Size int64 `gorm:"column:Size; type:bigint; not null" json:"size"`
- }
-
- func (t FileEntry) TableName() string {
- return "Files"
- }
|