add local file sync db

This commit is contained in:
xiaoyaofenfen 2022-05-16 14:22:56 +08:00
parent 24765ba2af
commit 7f4675331f
3 changed files with 248 additions and 2 deletions

View File

@ -60,6 +60,40 @@ type (
// LocalFileItem 本地文件信息
LocalFileItem struct {
// FileName 文件名
FileName string `json:"fileName"`
// FileSize 文件大小
FileSize int64 `json:"fileSize"`
// 文件类别 folder / file
FileType string `json:"fileType"`
// 创建时间
CreatedAt string `json:"createdAt"`
// 最后修改时间
UpdatedAt string `json:"updatedAt"`
// 后缀名例如dmg
FileExtension string `json:"fileExtension"`
// 内容Hash值只有文件才会有
Sha1Hash string `json:"sha1Hash"`
// FilePath 文件的完整路径
Path string `json:"path"`
}
LocalFileList []*LocalFileItem
LocalSyncDb interface {
// Open 打开并准备数据库
Open() (bool, error)
// Add 存储一个数据项
Add(item *LocalFileItem) (bool, error)
// Get 获取一个数据项
Get(filePath string) (*LocalFileItem, error)
// GetFileList 获取文件夹下的所有的文件列表
GetFileList(filePath string) (LocalFileList, error)
// Delete 删除一个数据项,如果是文件夹,则会删除文件夹下面所有的文件列表
Delete(filePath string) (bool, error)
// Update 更新一个数据项数据
Update(item *LocalFileItem) (bool, error)
// Close 关闭数据库
Close() (bool, error)
}
)
@ -102,3 +136,19 @@ func (item *PanFileItem) IsFolder() bool {
func NewPanSyncDb(dbFilePath string) PanSyncDb {
return interface{}(newPanSyncDbBolt(dbFilePath)).(PanSyncDb)
}
func (item *LocalFileItem) FormatFileName() string {
return item.FileName
}
func (item *LocalFileItem) FormatFilePath() string {
return FormatFilePath(item.Path)
}
func (item *LocalFileItem) IsFolder() bool {
return item.FileType == "folder"
}
func NewLocalSyncDb(dbFilePath string) LocalSyncDb {
return interface{}(newLocalSyncDbBolt(dbFilePath)).(LocalSyncDb)
}

View File

@ -3,7 +3,6 @@ package syncdrive
import (
"encoding/json"
"fmt"
"github.com/tickstep/bolt"
"sync"
)
@ -19,7 +18,7 @@ type (
// LocalSyncDbBolt 存储本地文件信息的数据库
LocalSyncDbBolt struct {
Path string
db *bolt.DB
db *BoltDb
locker *sync.Mutex
}
)
@ -87,6 +86,9 @@ func (p *PanSyncDbBolt) GetFileList(filePath string) (PanFileList, error) {
dataList, err := p.db.GetFileList(filePath)
if err == nil && len(dataList) > 0 {
for _, data := range dataList {
if data == "" {
continue
}
item := &PanFileItem{}
if err := json.Unmarshal([]byte(data), item); err != nil {
return nil, err
@ -131,3 +133,110 @@ func (p *PanSyncDbBolt) Close() (bool, error) {
defer p.locker.Unlock()
return p.db.Close()
}
func newLocalSyncDbBolt(dbFilePath string) *LocalSyncDbBolt {
return &LocalSyncDbBolt{
Path: dbFilePath,
locker: &sync.Mutex{},
}
}
func (p *LocalSyncDbBolt) Open() (bool, error) {
p.db = NewBoltDb(p.Path)
return p.db.Open()
}
// Add 增加一个数据项
func (p *LocalSyncDbBolt) Add(item *LocalFileItem) (bool, error) {
if item == nil {
return false, fmt.Errorf("item is nil")
}
p.locker.Lock()
defer p.locker.Unlock()
data, err := json.Marshal(item)
if err != nil {
return false, err
}
return p.db.Add(item.Path, item.IsFolder(), string(data))
}
// Get 获取一个数据项,数据项不存在返回错误
func (p *LocalSyncDbBolt) Get(filePath string) (*LocalFileItem, error) {
filePath = FormatFilePath(filePath)
if filePath == "" {
return nil, fmt.Errorf("item is nil")
}
p.locker.Lock()
defer p.locker.Unlock()
data, err := p.db.Get(filePath)
if err == nil && data != "" {
item := &LocalFileItem{}
if err := json.Unmarshal([]byte(data), item); err != nil {
return nil, err
}
return item, nil
}
return nil, err
}
func (p *LocalSyncDbBolt) GetFileList(filePath string) (LocalFileList, error) {
filePath = FormatFilePath(filePath)
if filePath == "" {
return nil, fmt.Errorf("item is nil")
}
p.locker.Lock()
defer p.locker.Unlock()
LocalFileList := LocalFileList{}
dataList, err := p.db.GetFileList(filePath)
if err == nil && len(dataList) > 0 {
for _, data := range dataList {
if data == "" {
continue
}
item := &LocalFileItem{}
if err := json.Unmarshal([]byte(data), item); err != nil {
return nil, err
}
LocalFileList = append(LocalFileList, item)
}
return LocalFileList, nil
}
return nil, err
}
// Delete 删除一个数据项
func (p *LocalSyncDbBolt) Delete(filePath string) (bool, error) {
filePath = FormatFilePath(filePath)
if filePath == "" {
return false, fmt.Errorf("item is nil")
}
p.locker.Lock()
defer p.locker.Unlock()
return p.db.Delete(filePath)
}
// Update 更新数据项,数据项不存在返回错误
func (p *LocalSyncDbBolt) Update(item *LocalFileItem) (bool, error) {
if item == nil {
return false, fmt.Errorf("item is nil")
}
p.locker.Lock()
defer p.locker.Unlock()
data, err := json.Marshal(item)
if err != nil {
return false, err
}
return p.db.Update(item.Path, item.IsFolder(), string(data))
}
// Close 关闭数据库
func (p *LocalSyncDbBolt) Close() (bool, error) {
p.locker.Lock()
defer p.locker.Unlock()
return p.db.Close()
}

View File

@ -5,6 +5,11 @@ import (
"github.com/tickstep/aliyunpan-api/aliyunpan"
"github.com/tickstep/aliyunpan-api/aliyunpan/apierror"
"github.com/tickstep/library-go/logger"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
"time"
)
@ -60,3 +65,85 @@ func TestGetFileList(t *testing.T) {
fmt.Println(b.GetFileList("/Parallels Desktop/v17"))
}
func WalkAllFile(dirPath string, walkFn filepath.WalkFunc) error {
dirPath = strings.ReplaceAll(dirPath, "\\", "/")
info, err := os.Lstat(dirPath)
if err != nil {
err = walkFn(dirPath, nil, err)
} else {
err = walkAllFile(dirPath, info, walkFn)
}
return err
}
func walkAllFile(dirPath string, info os.FileInfo, walkFn filepath.WalkFunc) error {
if !info.IsDir() {
return walkFn(dirPath, info, nil)
}
files, err := ioutil.ReadDir(dirPath)
if err != nil {
return walkFn(dirPath, nil, err)
}
for _, fi := range files {
subFilePath := dirPath + "/" + fi.Name()
err = walkFn(subFilePath, fi, err)
if err != nil {
return err
}
if fi.IsDir() {
err = walkAllFile(subFilePath, fi, walkFn)
if err != nil {
return err
}
}
}
return nil
}
func TestLocalSyncDb(t *testing.T) {
b := NewLocalSyncDb("D:\\smb\\feny\\goprojects\\dev\\local.db")
b.Open()
defer b.Close()
var walkFunc filepath.WalkFunc
walkFunc = func(file string, fi os.FileInfo, err error) error {
fmt.Println(file)
fileType := "file"
if fi.IsDir() {
fileType = "folder"
}
item := &LocalFileItem{
FileName: path.Base(file),
FileSize: fi.Size(),
FileType: fileType,
CreatedAt: fi.ModTime().Format("2006-01-02 15:04:05"),
UpdatedAt: fi.ModTime().Format("2006-01-02 15:04:05"),
FileExtension: "",
Sha1Hash: "",
Path: file,
}
if _, e := b.Add(item); e != nil {
fmt.Println(e)
}
return nil
}
WalkAllFile("D:\\smb\\feny\\goprojects\\dl\\a761171495", walkFunc)
}
func TestLocalGet(t *testing.T) {
b := NewLocalSyncDb("D:\\smb\\feny\\goprojects\\dev\\local.db")
b.Open()
defer b.Close()
fmt.Println(b.Get("D:\\smb\\feny\\goprojects\\dl\\a761171495\\1.jpg"))
}
func TestLocalGetFileList(t *testing.T) {
b := NewLocalSyncDb("D:\\smb\\feny\\goprojects\\dev\\local.db")
b.Open()
defer b.Close()
fmt.Println(b.GetFileList("D:/smb/feny/goprojects/dl/a761171495"))
}