test(server): fix bugs and tests

This commit is contained in:
hexxa 2022-09-03 23:32:32 +08:00 committed by Hexxa
parent 4265ab593e
commit ce77eb7534
13 changed files with 266 additions and 288 deletions

View file

@ -23,7 +23,7 @@ var (
maxHashingTime = 10
)
func (st *SQLiteStore) getFileInfo(ctx context.Context, userId uint64, itemPath string) (*db.FileInfo, error) {
func (st *SQLiteStore) getFileInfo(ctx context.Context, itemPath string) (*db.FileInfo, error) {
var infoStr string
fInfo := &db.FileInfo{}
var isDir bool
@ -33,10 +33,8 @@ func (st *SQLiteStore) getFileInfo(ctx context.Context, userId uint64, itemPath
ctx,
`select is_dir, size, share_id, info
from t_file_info
where path=? and user=?
`,
where path=?`,
itemPath,
userId,
).Scan(
&isDir,
&size,
@ -61,11 +59,11 @@ func (st *SQLiteStore) getFileInfo(ctx context.Context, userId uint64, itemPath
return fInfo, nil
}
func (st *SQLiteStore) GetFileInfo(ctx context.Context, userId uint64, itemPath string) (*db.FileInfo, error) {
func (st *SQLiteStore) GetFileInfo(ctx context.Context, itemPath string) (*db.FileInfo, error) {
st.RLock()
defer st.RUnlock()
return st.getFileInfo(ctx, userId, itemPath)
return st.getFileInfo(ctx, itemPath)
}
func (st *SQLiteStore) ListFileInfos(ctx context.Context, itemPaths []string) (map[string]*db.FileInfo, error) {
@ -160,31 +158,22 @@ func (st *SQLiteStore) AddFileInfo(ctx context.Context, userId uint64, itemPath
return st.setUsed(ctx, userId, true, info.Size)
}
func (st *SQLiteStore) delFileInfo(ctx context.Context, userId uint64, itemPath string) error {
func (st *SQLiteStore) delFileInfo(ctx context.Context, itemPath string) error {
_, err := st.db.ExecContext(
ctx,
`delete from t_file_info
where path=? and user=?
where path=?
`,
itemPath,
userId,
)
return err
}
// func (st *SQLiteStore) DelFileInfo(ctx context.Context, itemPath string) error {
// st.Lock()
// defer st.Unlock()
// return st.delFileInfo(ctx, itemPath)
// }
// sharings
func (st *SQLiteStore) SetSha1(ctx context.Context, userId uint64, itemPath, sign string) error {
func (st *SQLiteStore) SetSha1(ctx context.Context, itemPath, sign string) error {
st.Lock()
defer st.Unlock()
info, err := st.getFileInfo(ctx, userId, itemPath)
info, err := st.getFileInfo(ctx, itemPath)
if err != nil {
return err
}
@ -199,10 +188,9 @@ func (st *SQLiteStore) SetSha1(ctx context.Context, userId uint64, itemPath, sig
ctx,
`update t_file_info
set info=?
where path=? and user=?`,
where path=?`,
infoStr,
itemPath,
userId,
)
return err
}
@ -264,11 +252,17 @@ func (st *SQLiteStore) MoveFileInfos(ctx context.Context, userId uint64, oldPath
st.Lock()
defer st.Unlock()
info, err := st.getFileInfo(ctx, userId, oldPath)
info, err := st.getFileInfo(ctx, oldPath)
if err != nil {
if errors.Is(err, db.ErrFileInfoNotFound) {
// info for file does not exist so no need to move it
// e.g. folder info is not created before
// TODO: but sometimes it could be a bug
return nil
}
return err
}
err = st.delFileInfo(ctx, userId, oldPath)
err = st.delFileInfo(ctx, oldPath)
if err != nil {
return err
}

View file

@ -29,16 +29,29 @@ func (st *SQLiteStore) generateShareID(payload string) (string, error) {
return fmt.Sprintf("%x", h.Sum(nil))[:7], nil
}
func (st *SQLiteStore) IsSharing(ctx context.Context, userId uint64, dirPath string) bool {
func (st *SQLiteStore) IsSharing(ctx context.Context, userId uint64, dirPath string) (bool, error) {
st.RLock()
defer st.RUnlock()
// TODO: differentiate error and not exist
info, err := st.getFileInfo(ctx, userId, dirPath)
// TODO: userId is not used, becauser it is searcher's userId
var shareId string
err := st.db.QueryRowContext(
ctx,
`select share_id
from t_file_info
where path=?`,
dirPath,
).Scan(
&shareId,
)
if err != nil {
return false
if errors.Is(err, sql.ErrNoRows) {
return false, db.ErrFileInfoNotFound
}
return false, err
}
return info.ShareID != ""
return shareId != "", nil
}
func (st *SQLiteStore) GetSharingDir(ctx context.Context, hashID string) (string, error) {
@ -75,7 +88,7 @@ func (st *SQLiteStore) AddSharing(ctx context.Context, userId uint64, dirPath st
return err
}
_, err = st.getFileInfo(ctx, userId, dirPath)
_, err = st.getFileInfo(ctx, dirPath)
if err != nil && !errors.Is(err, db.ErrFileInfoNotFound) {
return err
}
@ -102,8 +115,8 @@ func (st *SQLiteStore) AddSharing(ctx context.Context, userId uint64, dirPath st
ctx,
`update t_file_info
set share_id=?
where path=? and user=?`,
shareID, dirPath, userId,
where path=?`,
shareID, dirPath,
)
return err
}
@ -116,9 +129,8 @@ func (st *SQLiteStore) DelSharing(ctx context.Context, userId uint64, dirPath st
ctx,
`update t_file_info
set share_id=''
where path=? and user=?`,
where path=?`,
dirPath,
userId,
)
return err
}

View file

@ -8,7 +8,7 @@ import (
"github.com/ihexxa/quickshare/src/db"
)
func (st *SQLiteStore) addUploadInfoOnly(ctx context.Context, userId uint64, filePath, tmpPath string, fileSize int64) error {
func (st *SQLiteStore) addUploadInfoOnly(ctx context.Context, userId uint64, tmpPath, filePath string, fileSize int64) error {
_, err := st.db.ExecContext(
ctx,
`insert into t_file_uploading
@ -42,7 +42,7 @@ func (st *SQLiteStore) AddUploadInfos(ctx context.Context, userId uint64, tmpPat
return err
}
return st.addUploadInfoOnly(ctx, userId, filePath, tmpPath, info.Size)
return st.addUploadInfoOnly(ctx, userId, tmpPath, filePath, info.Size)
}
func (st *SQLiteStore) DelUploadingInfos(ctx context.Context, userId uint64, realPath string) error {
@ -82,22 +82,22 @@ func (st *SQLiteStore) delUploadInfoOnly(ctx context.Context, userId uint64, fil
return err
}
// func (st *SQLiteStore) MoveUploadingInfos(ctx context.Context, userId uint64, uploadPath, itemPath string) error {
// st.Lock()
// defer st.Unlock()
func (st *SQLiteStore) MoveUploadingInfos(ctx context.Context, userId uint64, uploadPath, itemPath string) error {
st.Lock()
defer st.Unlock()
// _, size, _, err := st.getUploadInfo(ctx, userId, itemPath)
// if err != nil {
// return err
// }
// err = st.delUploadInfoOnly(ctx, userId, itemPath)
// if err != nil {
// return err
// }
// return st.addFileInfo(ctx, userId, itemPath, &db.FileInfo{
// Size: size,
// })
// }
_, size, _, err := st.getUploadInfo(ctx, userId, itemPath)
if err != nil {
return err
}
err = st.delUploadInfoOnly(ctx, userId, itemPath)
if err != nil {
return err
}
return st.addFileInfo(ctx, userId, itemPath, &db.FileInfo{
Size: size,
})
}
func (st *SQLiteStore) SetUploadInfo(ctx context.Context, userId uint64, filePath string, newUploaded int64) error {
st.Lock()