mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-21 17:10:12 +01:00
:file: 移除文件锁 https://github.com/siyuan-note/siyuan/issues/6010
This commit is contained in:
parent
56129699b9
commit
69a9713776
38 changed files with 193 additions and 438 deletions
|
|
@ -24,7 +24,7 @@ import (
|
|||
|
||||
"github.com/88250/gulu"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/siyuan-note/siyuan/kernel/filesys"
|
||||
"github.com/siyuan-note/filelock"
|
||||
"github.com/siyuan-note/siyuan/kernel/model"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
|
@ -86,7 +86,7 @@ func setFileAnnotation(c *gin.Context) {
|
|||
ret.Msg = err.Error()
|
||||
return
|
||||
}
|
||||
if err := filesys.WriteFileSafer(writePath, []byte(data)); nil != err {
|
||||
if err := filelock.WriteFile(writePath, []byte(data)); nil != err {
|
||||
ret.Code = -1
|
||||
ret.Msg = err.Error()
|
||||
return
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ func checkBlockExist(c *gin.Context) {
|
|||
|
||||
id := arg["id"].(string)
|
||||
b, err := model.GetBlock(id)
|
||||
if errors.Is(err, filelock.ErrUnableLockFile) {
|
||||
if errors.Is(err, filelock.ErrUnableAccessFile) {
|
||||
ret.Code = 2
|
||||
ret.Data = id
|
||||
return
|
||||
|
|
@ -303,7 +303,7 @@ func getBlockInfo(c *gin.Context) {
|
|||
|
||||
id := arg["id"].(string)
|
||||
block, err := model.GetBlock(id)
|
||||
if errors.Is(err, filelock.ErrUnableLockFile) {
|
||||
if errors.Is(err, filelock.ErrUnableAccessFile) {
|
||||
ret.Code = 2
|
||||
ret.Data = id
|
||||
return
|
||||
|
|
@ -329,7 +329,7 @@ func getBlockInfo(c *gin.Context) {
|
|||
}
|
||||
|
||||
root, err := model.GetBlock(block.RootID)
|
||||
if errors.Is(err, filelock.ErrUnableLockFile) {
|
||||
if errors.Is(err, filelock.ErrUnableAccessFile) {
|
||||
ret.Code = 2
|
||||
ret.Data = id
|
||||
return
|
||||
|
|
|
|||
|
|
@ -29,8 +29,8 @@ import (
|
|||
"github.com/88250/gulu"
|
||||
"github.com/88250/lute/ast"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/siyuan-note/filelock"
|
||||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/siyuan/kernel/filesys"
|
||||
"github.com/siyuan-note/siyuan/kernel/model"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
|
@ -107,7 +107,7 @@ func extensionCopy(c *gin.Context) {
|
|||
}
|
||||
fName = fName + "-" + ast.NewNodeID() + ext
|
||||
writePath := filepath.Join(assets, fName)
|
||||
if err = filesys.WriteFileSafer(writePath, data); nil != err {
|
||||
if err = filelock.WriteFile(writePath, data); nil != err {
|
||||
ret.Code = -1
|
||||
ret.Msg = err.Error()
|
||||
break
|
||||
|
|
|
|||
|
|
@ -17,8 +17,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
|
|
@ -70,7 +69,7 @@ func copyFile(c *gin.Context) {
|
|||
}
|
||||
|
||||
dest := arg["dest"].(string)
|
||||
if err = gulu.File.CopyFile(src, dest); nil != err {
|
||||
if err = filelock.Copy(src, dest); nil != err {
|
||||
logging.LogErrorf("copy file [%s] to [%s] failed: %s", src, dest, err)
|
||||
ret.Code = -1
|
||||
ret.Msg = err.Error()
|
||||
|
|
@ -127,27 +126,40 @@ func putFile(c *gin.Context) {
|
|||
logging.LogErrorf("make a dir [%s] failed: %s", filePath, err)
|
||||
}
|
||||
} else {
|
||||
file, _ := c.FormFile("file")
|
||||
if nil == file {
|
||||
fileHeader, _ := c.FormFile("file")
|
||||
if nil == fileHeader {
|
||||
logging.LogErrorf("form file is nil [path=%s]", filePath)
|
||||
c.Status(400)
|
||||
return
|
||||
}
|
||||
|
||||
dir := filepath.Dir(filePath)
|
||||
if err = os.MkdirAll(dir, 0755); nil != err {
|
||||
logging.LogErrorf("put a file [%s] make dir [%s] failed: %s", filePath, dir, err)
|
||||
} else {
|
||||
if filelock.IsLocked(filePath) {
|
||||
msg := fmt.Sprintf("file [%s] is locked", filePath)
|
||||
logging.LogErrorf(msg)
|
||||
err = errors.New(msg)
|
||||
} else {
|
||||
err = writeFile(file, filePath)
|
||||
if nil != err {
|
||||
logging.LogErrorf("put a file [%s] failed: %s", filePath, err)
|
||||
}
|
||||
for {
|
||||
dir := filepath.Dir(filePath)
|
||||
if err = os.MkdirAll(dir, 0755); nil != err {
|
||||
logging.LogErrorf("put a file [%s] make dir [%s] failed: %s", filePath, dir, err)
|
||||
break
|
||||
}
|
||||
|
||||
var f multipart.File
|
||||
f, err = fileHeader.Open()
|
||||
if nil != err {
|
||||
logging.LogErrorf("open file failed: %s", err)
|
||||
break
|
||||
}
|
||||
|
||||
var data []byte
|
||||
data, err = io.ReadAll(f)
|
||||
if nil != err {
|
||||
logging.LogErrorf("read file failed: %s", err)
|
||||
break
|
||||
}
|
||||
|
||||
err = filelock.WriteFile(filePath, data)
|
||||
if nil != err {
|
||||
logging.LogErrorf("put a file [%s] failed: %s", filePath, err)
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if nil != err {
|
||||
|
|
@ -172,20 +184,6 @@ func putFile(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func writeFile(file *multipart.FileHeader, dst string) error {
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
err = gulu.File.WriteFileSaferByReader(dst, src, 0644)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func millisecond2Time(t int64) time.Time {
|
||||
sec := t / 1000
|
||||
msec := t % 1000
|
||||
|
|
|
|||
|
|
@ -488,10 +488,10 @@ func lockFile(c *gin.Context) {
|
|||
}
|
||||
|
||||
id := arg["id"].(string)
|
||||
locked, filePath := model.LockFileByBlockID(id)
|
||||
locked := model.TryAccessFileByBlockID(id)
|
||||
if !locked {
|
||||
ret.Code = -1
|
||||
ret.Msg = fmt.Sprintf(model.Conf.Language(75), filePath)
|
||||
ret.Msg = fmt.Sprintf(model.Conf.Language(75))
|
||||
ret.Data = map[string]interface{}{"closeTimeout": 5000}
|
||||
}
|
||||
}
|
||||
|
|
@ -635,7 +635,7 @@ func getDoc(c *gin.Context) {
|
|||
}
|
||||
|
||||
blockCount, childBlockCount, content, parentID, parent2ID, rootID, typ, eof, boxID, docPath, err := model.GetDoc(startID, endID, id, index, keyword, mode, size)
|
||||
if errors.Is(err, filelock.ErrUnableLockFile) {
|
||||
if errors.Is(err, filelock.ErrUnableAccessFile) {
|
||||
ret.Code = 2
|
||||
ret.Data = id
|
||||
return
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import (
|
|||
"github.com/88250/lute/parse"
|
||||
"github.com/88250/lute/render"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/siyuan-note/filelock"
|
||||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/siyuan/kernel/model"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
|
|
@ -111,7 +112,7 @@ func html2BlockDOM(c *gin.Context) {
|
|||
name = name[0 : len(name)-len(ext)]
|
||||
name = name + "-" + ast.NewNodeID() + ext
|
||||
targetPath := filepath.Join(util.DataDir, "assets", name)
|
||||
if err = gulu.File.CopyFile(localPath, targetPath); nil != err {
|
||||
if err = filelock.Copy(localPath, targetPath); nil != err {
|
||||
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", localPath, targetPath, err)
|
||||
return ast.WalkStop
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ func performTransactions(c *gin.Context) {
|
|||
err = model.PerformTransactions(&transactions)
|
||||
}
|
||||
|
||||
if errors.Is(err, filelock.ErrUnableLockFile) {
|
||||
if errors.Is(err, filelock.ErrUnableAccessFile) {
|
||||
ret.Code = 1
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue