mirror of
https://github.com/fumiama/paper-manager.git
synced 2026-06-17 16:20:25 +08:00
implement delfile
This commit is contained in:
@@ -490,7 +490,7 @@ func (f *FileDatabase) AddFile(lstid int, reg *Regex, istemp bool, progress func
|
||||
}
|
||||
|
||||
// DelFile by listid
|
||||
func (f *FileDatabase) DelFile(fileid, uid int, istemp bool) error {
|
||||
func (f *FileDatabase) DelFile(lstid, uid int, istemp bool) error {
|
||||
user, err := UserDB.GetUserByID(uid)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -498,19 +498,14 @@ func (f *FileDatabase) DelFile(fileid, uid int, istemp bool) error {
|
||||
if !user.IsSuper() {
|
||||
return ErrInvalidRole
|
||||
}
|
||||
var file File
|
||||
f.mu.RLock()
|
||||
ftable := ""
|
||||
if istemp {
|
||||
file, err = sql.Find[File](&f.db, FileTableTempFile, "WHERE ID="+strconv.Itoa(fileid))
|
||||
ftable = FileTableTempFile
|
||||
} else {
|
||||
file, err = sql.Find[File](&f.db, FileTableFile, "WHERE ID="+strconv.Itoa(fileid))
|
||||
}
|
||||
f.mu.RUnlock()
|
||||
if err != nil {
|
||||
return err
|
||||
ftable = FileTableFile
|
||||
}
|
||||
f.mu.RLock()
|
||||
lst, err := sql.Find[List](&f.db, FileTableList, "WHERE ID="+strconv.Itoa(file.ListID))
|
||||
lst, err := sql.Find[List](&f.db, FileTableList, "WHERE ID="+strconv.Itoa(lstid))
|
||||
f.mu.RUnlock()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -518,21 +513,33 @@ func (f *FileDatabase) DelFile(fileid, uid int, istemp bool) error {
|
||||
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
|
||||
i := strings.LastIndex(lst.Path, "/")
|
||||
if i <= 0 {
|
||||
return os.ErrNotExist
|
||||
}
|
||||
for _, q := range ques {
|
||||
q.Delete(f, istemp)
|
||||
parentfolder := lst.Path[:i]
|
||||
if utils.IsNotExist(parentfolder) {
|
||||
return os.ErrNotExist
|
||||
}
|
||||
if istemp {
|
||||
err = f.db.Del(FileTableTempFile, "WHERE ID="+strconv.Itoa(fileid))
|
||||
} else {
|
||||
err = f.db.Del(FileTableFile, "WHERE ID="+strconv.Itoa(fileid))
|
||||
if !lst.HasntAnalyzed {
|
||||
f.mu.RLock()
|
||||
file, err := sql.Find[File](&f.db, ftable, "WHERE ListID="+strconv.Itoa(lstid))
|
||||
f.mu.RUnlock()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = f.db.Del(ftable, "WHERE ListID="+strconv.Itoa(lstid))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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 err != nil {
|
||||
logrus.Warnln("[global.DelFile] err:", err)
|
||||
}
|
||||
|
||||
return os.RemoveAll(parentfolder)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
sql "github.com/FloatTech/sqlite"
|
||||
"github.com/corona10/goimagehash"
|
||||
base14 "github.com/fumiama/go-base16384"
|
||||
"github.com/fumiama/paper-manager/backend/utils"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -19,7 +21,7 @@ type QuestionJSON struct {
|
||||
Sub []QuestionJSON `json:"sub,omitempty"`
|
||||
}
|
||||
|
||||
// Delete me and all subs
|
||||
// Delete me and all subs, ignore errors
|
||||
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)
|
||||
@@ -32,14 +34,49 @@ func (q *QuestionJSON) Delete(f *FileDatabase, istemp bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// DelQuestion 删除问题, 其它问题的 dup 可能会残留有 id, 使用时需要排除
|
||||
// DelQuestion 删除问题, 与其它问题的 dup
|
||||
func (f *FileDatabase) DelQuestion(id int64, istemp bool) error {
|
||||
qtable := ""
|
||||
if istemp {
|
||||
qtable = FileTableTempQuestion
|
||||
} else {
|
||||
qtable = FileTableQuestion
|
||||
}
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if istemp {
|
||||
return f.db.Del(FileTableTempQuestion, "WHERE ID="+strconv.FormatInt(id, 10))
|
||||
q, err := sql.Find[Question](&f.db, qtable, "WHERE ID="+strconv.FormatInt(id, 10))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return f.db.Del(FileTableQuestion, "WHERE ID="+strconv.FormatInt(id, 10))
|
||||
if len(q.Dup) > 2 {
|
||||
dupmap := make(map[string]float64, 64)
|
||||
err = json.Unmarshal(q.Dup, &dupmap)
|
||||
if err == nil {
|
||||
var buf [8]byte
|
||||
for k := range dupmap {
|
||||
_, err = hex.Decode(buf[:], base14.StringToBytes(k))
|
||||
if err == nil {
|
||||
qid := int64(binary.LittleEndian.Uint64(buf[:]))
|
||||
qq, err := sql.Find[Question](&f.db, qtable, "WHERE ID="+strconv.FormatInt(qid, 10))
|
||||
if err == nil && len(qq.Dup) > 2 {
|
||||
dupmap2 := make(map[string]float64, 64)
|
||||
err = json.Unmarshal(qq.Dup, &dupmap2)
|
||||
if err == nil {
|
||||
delete(dupmap2, k)
|
||||
qq.Dup, err = json.Marshal(dupmap2)
|
||||
if err == nil {
|
||||
err = f.db.Insert(qtable, &qq)
|
||||
if err != nil {
|
||||
logrus.Warnln("[global.DelQuestion] insert modified dup to id", k, "table", qtable, "err:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return f.db.Del(qtable, "WHERE ID="+strconv.FormatInt(id, 10))
|
||||
}
|
||||
|
||||
type Question struct {
|
||||
|
||||
@@ -23,6 +23,7 @@ var analyzeper = ttl.NewCache[int, uint](time.Hour)
|
||||
|
||||
var (
|
||||
errNoAnalyzePermission = errors.New("no analyze permission")
|
||||
errNoDeletePermission = errors.New("no delete permission")
|
||||
)
|
||||
|
||||
type filelist struct {
|
||||
@@ -200,6 +201,34 @@ func init() {
|
||||
writeresult(w, codeSuccess, &message{C: 0, M: "分析完成"}, messageOk, typeSuccess)
|
||||
}
|
||||
}}
|
||||
apimap["/api/delFile"] = &apihandler{"GET", func(w http.ResponseWriter, r *http.Request) {
|
||||
token := r.Header.Get("Authorization")
|
||||
user := usertokens.Get(token)
|
||||
if user == nil {
|
||||
writeresult(w, codeError, nil, errInvalidToken.Error(), typeError)
|
||||
return
|
||||
}
|
||||
if !user.IsSuper() {
|
||||
writeresult(w, codeError, nil, errNoDeletePermission.Error(), typeError)
|
||||
return
|
||||
}
|
||||
idstr := r.URL.Query().Get("id")
|
||||
if idstr == "" {
|
||||
writeresult(w, codeError, nil, "empty id", typeError)
|
||||
return
|
||||
}
|
||||
id, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
writeresult(w, codeError, nil, err.Error(), typeError)
|
||||
return
|
||||
}
|
||||
err = global.FileDB.DelFile(id, *user.ID, false)
|
||||
if err != nil {
|
||||
writeresult(w, codeError, nil, err.Error(), typeError)
|
||||
return
|
||||
}
|
||||
writeresult(w, codeSuccess, "删除成功", messageOk, typeSuccess)
|
||||
}}
|
||||
}
|
||||
|
||||
// PaperHandler serves protected contents in global.FileFolder
|
||||
|
||||
Reference in New Issue
Block a user