mirror of
https://github.com/fumiama/paper-manager.git
synced 2026-06-12 04:00:23 +08:00
fix: FileDB -> f
This commit is contained in:
@@ -117,7 +117,9 @@ func (f *FileDatabase) AddFile(lstid int, reg *Regex, istemp bool, progress func
|
|||||||
return nil, ErrInvalidRole
|
return nil, ErrInvalidRole
|
||||||
}
|
}
|
||||||
progress(1)
|
progress(1)
|
||||||
lst, err := sql.Find[List](&FileDB.db, FileTableList, "WHERE ID="+strconv.Itoa(lstid))
|
f.mu.RLock()
|
||||||
|
lst, err := sql.Find[List](&f.db, FileTableList, "WHERE ID="+strconv.Itoa(lstid))
|
||||||
|
f.mu.RUnlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -380,8 +382,8 @@ func (f *FileDatabase) AddFile(lstid int, reg *Regex, istemp bool, progress func
|
|||||||
}
|
}
|
||||||
var q Question
|
var q Question
|
||||||
dupmap := make(map[string]float64, 64)
|
dupmap := make(map[string]float64, 64)
|
||||||
FileDB.mu.RLock()
|
f.mu.RLock()
|
||||||
err = FileDB.db.FindFor(FileTableQuestion, &q, "", func() error {
|
err = f.db.FindFor(FileTableQuestion, &q, "", func() error {
|
||||||
r, err := q.GetDuplicateRate(que)
|
r, err := q.GetDuplicateRate(que)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Warnln("[global.AddFile] GetDuplicateRate err:", err)
|
logrus.Warnln("[global.AddFile] GetDuplicateRate err:", err)
|
||||||
@@ -395,7 +397,7 @@ func (f *FileDatabase) AddFile(lstid int, reg *Regex, istemp bool, progress func
|
|||||||
dupmap[hex.EncodeToString(buf[:])] = r
|
dupmap[hex.EncodeToString(buf[:])] = r
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
FileDB.mu.RUnlock()
|
f.mu.RUnlock()
|
||||||
if err == nil && len(dupmap) > 0 {
|
if err == nil && len(dupmap) > 0 {
|
||||||
que.Dup, _ = json.Marshal(dupmap)
|
que.Dup, _ = json.Marshal(dupmap)
|
||||||
}
|
}
|
||||||
@@ -407,20 +409,20 @@ func (f *FileDatabase) AddFile(lstid int, reg *Regex, istemp bool, progress func
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
m5 := md5.Sum(w.Bytes())
|
m5 := md5.Sum(w.Bytes())
|
||||||
quepath := questionpath + hex.EncodeToString(m5[:]) + ".docx"
|
quepath := questionpath + hex.EncodeToString(m5[:]) + ".docx"
|
||||||
f, err := os.Create(quepath)
|
qf, err := os.Create(quepath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_, _ = io.Copy(f, w)
|
_, _ = io.Copy(qf, w)
|
||||||
_ = f.Close()
|
_ = qf.Close()
|
||||||
}
|
}
|
||||||
que.Path = quepath
|
que.Path = quepath
|
||||||
if istemp {
|
if istemp {
|
||||||
FileDB.mu.Lock()
|
f.mu.Lock()
|
||||||
_ = FileDB.db.Insert(FileTableTempQuestion, que)
|
_ = f.db.Insert(FileTableTempQuestion, que)
|
||||||
FileDB.mu.Unlock()
|
f.mu.Unlock()
|
||||||
} else {
|
} else {
|
||||||
FileDB.mu.Lock()
|
f.mu.Lock()
|
||||||
for k, v := range dupmap {
|
for k, v := range dupmap {
|
||||||
err = FileDB.db.Find(FileTableQuestion, &q, "WHERE ID=0x"+k)
|
err = f.db.Find(FileTableQuestion, &q, "WHERE ID=0x"+k)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
thismap := make(map[string]float64, 64)
|
thismap := make(map[string]float64, 64)
|
||||||
err := json.Unmarshal(q.Dup, &thismap)
|
err := json.Unmarshal(q.Dup, &thismap)
|
||||||
@@ -428,13 +430,13 @@ func (f *FileDatabase) AddFile(lstid int, reg *Regex, istemp bool, progress func
|
|||||||
thismap[queidstr] = v
|
thismap[queidstr] = v
|
||||||
q.Dup, err = json.Marshal(thismap)
|
q.Dup, err = json.Marshal(thismap)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
_ = FileDB.db.Insert(FileTableQuestion, &q)
|
_ = f.db.Insert(FileTableQuestion, &q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ = FileDB.db.Insert(FileTableQuestion, que)
|
_ = f.db.Insert(FileTableQuestion, que)
|
||||||
FileDB.mu.Unlock()
|
f.mu.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r := 0.0
|
r := 0.0
|
||||||
@@ -473,16 +475,64 @@ func (f *FileDatabase) AddFile(lstid int, reg *Regex, istemp bool, progress func
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
progress(95)
|
progress(95)
|
||||||
FileDB.mu.Lock()
|
f.mu.Lock()
|
||||||
if istemp {
|
if istemp {
|
||||||
err = FileDB.db.Insert(FileTableTempFile, file)
|
err = f.db.Insert(FileTableTempFile, file)
|
||||||
lst.IsTemp = true
|
lst.IsTemp = true
|
||||||
} else {
|
} else {
|
||||||
err = FileDB.db.Insert(FileTableFile, file)
|
err = f.db.Insert(FileTableFile, file)
|
||||||
lst.IsTemp = false
|
lst.IsTemp = false
|
||||||
}
|
}
|
||||||
_ = FileDB.db.Insert(FileTableList, &lst)
|
_ = f.db.Insert(FileTableList, &lst)
|
||||||
FileDB.mu.Unlock()
|
f.mu.Unlock()
|
||||||
progress(100)
|
progress(100)
|
||||||
return file, err
|
return file, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DelFile by listid
|
||||||
|
func (f *FileDatabase) DelFile(fileid, uid int, istemp bool) error {
|
||||||
|
user, err := UserDB.GetUserByID(uid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !user.IsSuper() {
|
||||||
|
return ErrInvalidRole
|
||||||
|
}
|
||||||
|
var file File
|
||||||
|
f.mu.RLock()
|
||||||
|
if istemp {
|
||||||
|
file, err = sql.Find[File](&f.db, FileTableTempFile, "WHERE ID="+strconv.Itoa(fileid))
|
||||||
|
} else {
|
||||||
|
file, err = sql.Find[File](&f.db, FileTableFile, "WHERE ID="+strconv.Itoa(fileid))
|
||||||
|
}
|
||||||
|
f.mu.RUnlock()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f.mu.RLock()
|
||||||
|
lst, err := sql.Find[List](&f.db, FileTableList, "WHERE ID="+strconv.Itoa(file.ListID))
|
||||||
|
f.mu.RUnlock()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if lst.Path == "" || strings.Contains(lst.Path, "..") {
|
||||||
|
return os.ErrNotExist
|
||||||
|
}
|
||||||
|
ques := make([]QuestionJSON, 0, 64)
|
||||||
|
err = json.Unmarshal(file.Questions, &ques)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, q := range ques {
|
||||||
|
q.Delete(f, istemp)
|
||||||
|
}
|
||||||
|
if istemp {
|
||||||
|
err = f.db.Del(FileTableTempFile, "WHERE ID="+strconv.Itoa(fileid))
|
||||||
|
} else {
|
||||||
|
err = f.db.Del(FileTableFile, "WHERE ID="+strconv.Itoa(fileid))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
logrus.Warnln("[global.DelFile] err:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -45,9 +45,9 @@ func (f *FileDatabase) SaveFileToTemp(uploader int, file io.Reader, name string)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fpath := tmpdir + "/" + name
|
fpath := tmpdir + "/" + name
|
||||||
FileDB.mu.RLock()
|
f.mu.RLock()
|
||||||
lst, _ := sql.Find[List](&FileDB.db, FileTableList, "WHERE Path='"+fpath+"'")
|
lst, _ := sql.Find[List](&f.db, FileTableList, "WHERE Path='"+fpath+"'")
|
||||||
FileDB.mu.RUnlock()
|
f.mu.RUnlock()
|
||||||
lst.Uploader = uploader
|
lst.Uploader = uploader
|
||||||
lst.UpName = user.Name
|
lst.UpName = user.Name
|
||||||
lst.UpTime = time.Now().Unix()
|
lst.UpTime = time.Now().Unix()
|
||||||
@@ -65,9 +65,9 @@ func (f *FileDatabase) SaveFileToTemp(uploader int, file io.Reader, name string)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
lst.Size = sz
|
lst.Size = sz
|
||||||
FileDB.mu.Lock()
|
f.mu.Lock()
|
||||||
err = FileDB.db.Insert(FileTableList, &lst)
|
err = f.db.Insert(FileTableList, &lst)
|
||||||
FileDB.mu.Unlock()
|
f.mu.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -75,24 +75,24 @@ func (f *FileDatabase) SaveFileToTemp(uploader int, file io.Reader, name string)
|
|||||||
id = *lst.ID
|
id = *lst.ID
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
FileDB.mu.RLock()
|
f.mu.RLock()
|
||||||
err = FileDB.db.Find(FileTableList, &lst, "WHERE Path='"+fpath+"'")
|
err = f.db.Find(FileTableList, &lst, "WHERE Path='"+fpath+"'")
|
||||||
FileDB.mu.RUnlock()
|
f.mu.RUnlock()
|
||||||
id = *lst.ID
|
id = *lst.ID
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListUploadedFile will select all file that HasntAnalyzed && IsTemp or !HasntAnalyzed && !IsTemp
|
// ListUploadedFile will select all file that HasntAnalyzed && IsTemp or !HasntAnalyzed && !IsTemp
|
||||||
func (f *FileDatabase) ListUploadedFile() (lst []*List, err error) {
|
func (f *FileDatabase) ListUploadedFile() (lst []*List, err error) {
|
||||||
FileDB.mu.RLock()
|
f.mu.RLock()
|
||||||
lst, err = sql.FindAll[List](&FileDB.db, FileTableList, "WHERE (HasntAnalyzed AND IsTemp) OR (NOT HasntAnalyzed AND NOT IsTemp) ORDER BY UpTime DESC")
|
lst, err = sql.FindAll[List](&f.db, FileTableList, "WHERE (HasntAnalyzed AND IsTemp) OR (NOT HasntAnalyzed AND NOT IsTemp) ORDER BY UpTime DESC")
|
||||||
FileDB.mu.RUnlock()
|
f.mu.RUnlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileDatabase) GetFileInfo(id int) (lst List, err error) {
|
func (f *FileDatabase) GetFileInfo(id int) (lst List, err error) {
|
||||||
FileDB.mu.RLock()
|
f.mu.RLock()
|
||||||
lst, err = sql.Find[List](&FileDB.db, FileTableList, "WHERE ID="+strconv.Itoa(id))
|
lst, err = sql.Find[List](&f.db, FileTableList, "WHERE ID="+strconv.Itoa(id))
|
||||||
FileDB.mu.RUnlock()
|
f.mu.RUnlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/corona10/goimagehash"
|
"github.com/corona10/goimagehash"
|
||||||
"github.com/fumiama/paper-manager/backend/utils"
|
"github.com/fumiama/paper-manager/backend/utils"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// QuestionJSON is the struct representation of File.Questions
|
// QuestionJSON is the struct representation of File.Questions
|
||||||
@@ -17,6 +19,29 @@ type QuestionJSON struct {
|
|||||||
Sub []QuestionJSON `json:"sub,omitempty"`
|
Sub []QuestionJSON `json:"sub,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete me and all subs
|
||||||
|
func (q *QuestionJSON) Delete(f *FileDatabase, istemp bool) {
|
||||||
|
if b, err := hex.DecodeString(q.Name); err == nil {
|
||||||
|
err = f.DelQuestion(int64(binary.LittleEndian.Uint64(b)), istemp)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Warnln("[global.QuestionJSON] Delete", q.Name, "err:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, sq := range q.Sub {
|
||||||
|
sq.Delete(f, istemp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DelQuestion 删除问题, 其它问题的 dup 可能会残留有 id, 使用时需要排除
|
||||||
|
func (f *FileDatabase) DelQuestion(id int64, istemp bool) error {
|
||||||
|
f.mu.Lock()
|
||||||
|
defer f.mu.Unlock()
|
||||||
|
if istemp {
|
||||||
|
return f.db.Del(FileTableTempQuestion, "WHERE ID="+strconv.FormatInt(id, 10))
|
||||||
|
}
|
||||||
|
return f.db.Del(FileTableQuestion, "WHERE ID="+strconv.FormatInt(id, 10))
|
||||||
|
}
|
||||||
|
|
||||||
type Question struct {
|
type Question struct {
|
||||||
ID int64 // ID is the first 8 bytes of the Plain's md5
|
ID int64 // ID is the first 8 bytes of the Plain's md5
|
||||||
Path string // Path is the question's docx position
|
Path string // Path is the question's docx position
|
||||||
|
|||||||
Reference in New Issue
Block a user