fix(files): add location to file info

This commit is contained in:
hexxa 2022-09-07 20:19:46 +08:00 committed by Hexxa
parent b226bc6f9f
commit df5c820c68
8 changed files with 127 additions and 63 deletions

View file

@ -128,19 +128,24 @@ func (st *SQLiteStore) addFileInfo(ctx context.Context, userId uint64, itemPath
return err
}
location, err := getLocation(itemPath)
if err != nil {
return err
}
dirPath, itemName := path.Split(itemPath)
_, err = st.db.ExecContext(
ctx,
`insert into t_file_info
(path, user, parent, name, is_dir, size, share_id, info) values (?, ?, ?, ?, ?, ?, ?, ?)`,
itemPath,
userId,
dirPath,
itemName,
info.IsDir,
info.Size,
info.ShareID,
infoStr,
`insert into t_file_info (
path, user, location, parent, name,
is_dir, size, share_id, info
)
values (
?, ?, ?, ?, ?,
?, ?, ?, ?
)`,
itemPath, userId, location, dirPath, itemName,
info.IsDir, info.Size, info.ShareID, infoStr,
)
return err
}
@ -248,7 +253,7 @@ func (st *SQLiteStore) DelFileInfo(ctx context.Context, userID uint64, itemPath
return err
}
func (st *SQLiteStore) MoveFileInfos(ctx context.Context, userId uint64, oldPath, newPath string, isDir bool) error {
func (st *SQLiteStore) MoveFileInfo(ctx context.Context, userId uint64, oldPath, newPath string, isDir bool) error {
st.Lock()
defer st.Unlock()
@ -268,3 +273,12 @@ func (st *SQLiteStore) MoveFileInfos(ctx context.Context, userId uint64, oldPath
}
return st.addFileInfo(ctx, userId, newPath, info)
}
func getLocation(itemPath string) (string, error) {
// location is taken from item path
itemPathParts := strings.Split(itemPath, "/")
if len(itemPathParts) == 0 {
return "", fmt.Errorf("invalid item path '%s'", itemPath)
}
return itemPathParts[0], nil
}

View file

@ -29,11 +29,10 @@ 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, error) {
func (st *SQLiteStore) IsSharing(ctx context.Context, dirPath string) (bool, error) {
st.RLock()
defer st.RUnlock()
// TODO: userId is not used, becauser it is searcher's userId
var shareId string
err := st.db.QueryRowContext(
ctx,
@ -88,6 +87,11 @@ func (st *SQLiteStore) AddSharing(ctx context.Context, userId uint64, dirPath st
return err
}
location, err := getLocation(dirPath)
if err != nil {
return err
}
_, err = st.getFileInfo(ctx, dirPath)
if err != nil && !errors.Is(err, db.ErrFileInfoNotFound) {
return err
@ -104,9 +108,16 @@ func (st *SQLiteStore) AddSharing(ctx context.Context, userId uint64, dirPath st
_, err = st.db.ExecContext(
ctx,
`insert into t_file_info
(path, user, parent, name, is_dir, size, share_id, info) values (?, ?, ?, ?, ?, ?, ?, ?)`,
dirPath, userId, parentPath, name, true, 0, shareID, infoStr,
`insert into t_file_info (
path, user, location, parent, name,
is_dir, size, share_id, info
)
values (
?, ?, ?, ?, ?,
?, ?, ?, ?
)`,
dirPath, userId, location, parentPath, name,
true, 0, shareID, infoStr,
)
return err
}
@ -135,7 +146,7 @@ func (st *SQLiteStore) DelSharing(ctx context.Context, userId uint64, dirPath st
return err
}
func (st *SQLiteStore) ListUserSharings(ctx context.Context, userId uint64) (map[string]string, error) {
func (st *SQLiteStore) ListSharingsByLocation(ctx context.Context, location string) (map[string]string, error) {
st.RLock()
defer st.RUnlock()
@ -143,8 +154,8 @@ func (st *SQLiteStore) ListUserSharings(ctx context.Context, userId uint64) (map
ctx,
`select path, share_id
from t_file_info
where user=? and share_id <> ''`,
userId,
where location=? and share_id<>''`,
location,
)
if err != nil {
return nil, err

View file

@ -11,8 +11,12 @@ import (
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
(real_path, tmp_path, user, size, uploaded) values (?, ?, ?, ?, ?)`,
`insert into t_file_uploading (
real_path, tmp_path, user, size, uploaded
)
values (
?, ?, ?, ?, ?
)`,
filePath, tmpPath, userId, fileSize, 0,
)
return err

View file

@ -129,6 +129,7 @@ func (st *SQLiteStore) InitFileTables(ctx context.Context) error {
`create table if not exists t_file_info (
path varchar not null,
user bigint not null,
location varchar not null,
parent varchar not null,
name varchar not null,
is_dir boolean not null,