2023-06-24 20:39:55 +08:00
|
|
|
// SiYuan - Refactor your thinking
|
2022-05-26 15:18:53 +08:00
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-29 21:52:01 +08:00
|
|
|
"github.com/siyuan-note/filelock"
|
2022-07-17 12:22:32 +08:00
|
|
|
"github.com/siyuan-note/logging"
|
2022-05-26 15:18:53 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/sql"
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
)
|
|
|
|
|
2025-07-24 11:19:25 +08:00
|
|
|
func InsertLocalAssets(id string, assetAbsPaths []string, isUpload bool) (succMap map[string]interface{}, err error) {
|
2022-05-26 15:18:53 +08:00
|
|
|
succMap = map[string]interface{}{}
|
|
|
|
|
|
|
|
bt := treenode.GetBlockTree(id)
|
|
|
|
if nil == bt {
|
|
|
|
err = errors.New(Conf.Language(71))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
docDirLocalPath := filepath.Join(util.DataDir, bt.BoxID, path.Dir(bt.Path))
|
2022-09-27 14:45:24 +08:00
|
|
|
assetsDirPath := getAssetsDir(filepath.Join(util.DataDir, bt.BoxID), docDirLocalPath)
|
|
|
|
if !gulu.File.IsExist(assetsDirPath) {
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = os.MkdirAll(assetsDirPath, 0755); err != nil {
|
2022-09-26 20:20:50 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-24 11:19:25 +08:00
|
|
|
for _, assetAbsPath := range assetAbsPaths {
|
|
|
|
baseName := filepath.Base(assetAbsPath)
|
2023-09-30 20:19:26 +08:00
|
|
|
fName := baseName
|
2022-05-26 15:18:53 +08:00
|
|
|
fName = util.FilterUploadFileName(fName)
|
|
|
|
ext := filepath.Ext(fName)
|
|
|
|
fName = strings.TrimSuffix(fName, ext)
|
|
|
|
ext = strings.ToLower(ext)
|
|
|
|
fName += ext
|
2025-07-24 11:19:25 +08:00
|
|
|
if gulu.File.IsDir(assetAbsPath) || !isUpload {
|
|
|
|
if !strings.HasPrefix(assetAbsPath, "\\\\") {
|
|
|
|
assetAbsPath = "file://" + assetAbsPath
|
2022-09-27 15:31:48 +08:00
|
|
|
}
|
2025-07-24 11:19:25 +08:00
|
|
|
succMap[baseName] = assetAbsPath
|
2022-05-26 15:18:53 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2025-07-24 11:19:25 +08:00
|
|
|
if util.IsSubPath(assetsDirPath, assetAbsPath) {
|
|
|
|
// 已经位于 assets 目录下的资源文件不处理
|
|
|
|
// Dragging a file from the assets folder into the editor causes the kernel to exit https://github.com/siyuan-note/siyuan/issues/15355
|
|
|
|
succMap[baseName] = "assets/" + fName
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, statErr := os.Stat(assetAbsPath)
|
2022-05-27 12:56:23 +08:00
|
|
|
if nil != statErr {
|
|
|
|
err = statErr
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
2025-07-24 11:19:25 +08:00
|
|
|
f, openErr := os.Open(assetAbsPath)
|
2022-05-27 12:56:23 +08:00
|
|
|
if nil != openErr {
|
|
|
|
err = openErr
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hash, hashErr := util.GetEtagByHandle(f, fi.Size())
|
|
|
|
if nil != hashErr {
|
|
|
|
f.Close()
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if existAsset := sql.QueryAssetByHash(hash); nil != existAsset {
|
|
|
|
// 已经存在同样数据的资源文件的话不重复保存
|
|
|
|
succMap[baseName] = existAsset.Path
|
|
|
|
} else {
|
2024-12-04 22:37:22 +08:00
|
|
|
fName = util.AssetName(fName)
|
2022-09-27 14:45:24 +08:00
|
|
|
writePath := filepath.Join(assetsDirPath, fName)
|
2024-09-04 04:40:50 +03:00
|
|
|
if _, err = f.Seek(0, io.SeekStart); err != nil {
|
2022-05-27 12:56:23 +08:00
|
|
|
f.Close()
|
|
|
|
return
|
|
|
|
}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = filelock.WriteFileByReader(writePath, f); err != nil {
|
2022-05-27 12:56:23 +08:00
|
|
|
f.Close()
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
2022-05-27 12:56:23 +08:00
|
|
|
f.Close()
|
2024-08-30 09:43:16 +08:00
|
|
|
succMap[baseName] = "assets/" + fName
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-14 21:50:46 +08:00
|
|
|
IncSync()
|
2022-05-26 15:18:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func Upload(c *gin.Context) {
|
|
|
|
ret := gulu.Ret.NewResult()
|
|
|
|
defer c.JSON(200, ret)
|
|
|
|
|
|
|
|
form, err := c.MultipartForm()
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-07-17 12:22:32 +08:00
|
|
|
logging.LogErrorf("insert asset failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assetsDirPath := filepath.Join(util.DataDir, "assets")
|
|
|
|
if nil != form.Value["id"] {
|
|
|
|
id := form.Value["id"][0]
|
|
|
|
bt := treenode.GetBlockTree(id)
|
|
|
|
if nil == bt {
|
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = Conf.Language(71)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
docDirLocalPath := filepath.Join(util.DataDir, bt.BoxID, path.Dir(bt.Path))
|
|
|
|
assetsDirPath = getAssetsDir(filepath.Join(util.DataDir, bt.BoxID), docDirLocalPath)
|
|
|
|
}
|
2023-02-23 10:20:21 +08:00
|
|
|
|
|
|
|
relAssetsDirPath := "assets"
|
2022-05-26 15:18:53 +08:00
|
|
|
if nil != form.Value["assetsDirPath"] {
|
2023-02-23 10:20:21 +08:00
|
|
|
relAssetsDirPath = form.Value["assetsDirPath"][0]
|
|
|
|
assetsDirPath = filepath.Join(util.DataDir, relAssetsDirPath)
|
2024-12-11 17:15:54 +08:00
|
|
|
if !util.IsAbsPathInWorkspace(assetsDirPath) {
|
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = "Path [" + assetsDirPath + "] is not in workspace"
|
|
|
|
return
|
|
|
|
}
|
2022-09-26 20:20:50 +08:00
|
|
|
}
|
|
|
|
if !gulu.File.IsExist(assetsDirPath) {
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = os.MkdirAll(assetsDirPath, 0755); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var errFiles []string
|
|
|
|
succMap := map[string]interface{}{}
|
|
|
|
files := form.File["file[]"]
|
2024-03-20 23:18:32 +08:00
|
|
|
skipIfDuplicated := false // 默认不跳过重复文件,但是有的场景需要跳过,比如上传 PDF 标注图片 https://github.com/siyuan-note/siyuan/issues/10666
|
|
|
|
if nil != form.Value["skipIfDuplicated"] {
|
|
|
|
skipIfDuplicated = "true" == form.Value["skipIfDuplicated"][0]
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
for _, file := range files {
|
2023-09-30 20:18:27 +08:00
|
|
|
baseName := file.Filename
|
|
|
|
|
2023-11-04 09:54:45 +08:00
|
|
|
needUnzip2Dir := false
|
|
|
|
if gulu.OS.IsDarwin() {
|
|
|
|
if strings.HasSuffix(baseName, ".rtfd.zip") {
|
|
|
|
needUnzip2Dir = true
|
|
|
|
}
|
|
|
|
}
|
2023-11-04 08:58:35 +08:00
|
|
|
|
2023-09-30 20:18:27 +08:00
|
|
|
fName := baseName
|
2022-05-26 15:18:53 +08:00
|
|
|
fName = util.FilterUploadFileName(fName)
|
|
|
|
ext := filepath.Ext(fName)
|
|
|
|
fName = strings.TrimSuffix(fName, ext)
|
|
|
|
ext = strings.ToLower(ext)
|
|
|
|
fName += ext
|
2022-05-27 12:56:23 +08:00
|
|
|
f, openErr := file.Open()
|
|
|
|
if nil != openErr {
|
2022-05-26 15:18:53 +08:00
|
|
|
errFiles = append(errFiles, fName)
|
2022-05-27 12:56:23 +08:00
|
|
|
ret.Msg = openErr.Error()
|
2022-05-26 15:18:53 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:56:23 +08:00
|
|
|
hash, hashErr := util.GetEtagByHandle(f, file.Size)
|
|
|
|
if nil != hashErr {
|
2022-05-26 15:18:53 +08:00
|
|
|
errFiles = append(errFiles, fName)
|
|
|
|
ret.Msg = err.Error()
|
2022-05-27 12:56:23 +08:00
|
|
|
f.Close()
|
2022-05-26 15:18:53 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if existAsset := sql.QueryAssetByHash(hash); nil != existAsset {
|
|
|
|
// 已经存在同样数据的资源文件的话不重复保存
|
|
|
|
succMap[baseName] = existAsset.Path
|
|
|
|
} else {
|
2024-03-20 23:18:32 +08:00
|
|
|
if skipIfDuplicated {
|
2024-03-20 23:21:26 +08:00
|
|
|
// https://github.com/siyuan-note/siyuan/issues/10666
|
2024-03-20 23:18:32 +08:00
|
|
|
matches, globErr := filepath.Glob(assetsDirPath + string(os.PathSeparator) + strings.TrimSuffix(fName, ext) + "*")
|
|
|
|
if nil != globErr {
|
|
|
|
logging.LogErrorf("glob failed: %s", globErr)
|
|
|
|
} else {
|
|
|
|
if 0 < len(matches) {
|
|
|
|
fName = filepath.Base(matches[0])
|
|
|
|
succMap[baseName] = strings.TrimPrefix(path.Join(relAssetsDirPath, fName), "/")
|
|
|
|
f.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 11:29:05 +08:00
|
|
|
fName = util.AssetName(fName)
|
2022-05-26 15:18:53 +08:00
|
|
|
writePath := filepath.Join(assetsDirPath, fName)
|
2023-11-04 09:54:45 +08:00
|
|
|
tmpDir := filepath.Join(util.TempDir, "convert", "zip", gulu.Rand.String(7))
|
|
|
|
if needUnzip2Dir {
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = os.MkdirAll(tmpDir, 0755); err != nil {
|
2023-11-04 09:54:45 +08:00
|
|
|
errFiles = append(errFiles, fName)
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
f.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
writePath = filepath.Join(tmpDir, fName)
|
|
|
|
}
|
|
|
|
|
2024-09-04 04:40:50 +03:00
|
|
|
if _, err = f.Seek(0, io.SeekStart); err != nil {
|
2023-11-04 09:54:45 +08:00
|
|
|
logging.LogErrorf("seek failed: %s", err)
|
2022-05-27 12:56:23 +08:00
|
|
|
errFiles = append(errFiles, fName)
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
f.Close()
|
|
|
|
break
|
|
|
|
}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = filelock.WriteFileByReader(writePath, f); err != nil {
|
2023-11-04 09:54:45 +08:00
|
|
|
logging.LogErrorf("write file failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
errFiles = append(errFiles, fName)
|
|
|
|
ret.Msg = err.Error()
|
2022-05-27 12:56:23 +08:00
|
|
|
f.Close()
|
2022-05-26 15:18:53 +08:00
|
|
|
break
|
|
|
|
}
|
2022-05-27 12:56:23 +08:00
|
|
|
f.Close()
|
2023-11-04 09:54:45 +08:00
|
|
|
|
|
|
|
if needUnzip2Dir {
|
|
|
|
baseName = strings.TrimSuffix(file.Filename, ".rtfd.zip") + ".rtfd"
|
|
|
|
fName = baseName
|
|
|
|
fName = util.FilterUploadFileName(fName)
|
|
|
|
ext = filepath.Ext(fName)
|
|
|
|
fName = strings.TrimSuffix(fName, ext)
|
|
|
|
ext = strings.ToLower(ext)
|
|
|
|
fName += ext
|
|
|
|
fName = util.AssetName(fName)
|
|
|
|
tmpDir2 := filepath.Join(util.TempDir, "convert", "zip", gulu.Rand.String(7))
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.Zip.Unzip(writePath, tmpDir2); err != nil {
|
2023-11-04 09:54:45 +08:00
|
|
|
errFiles = append(errFiles, fName)
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, readErr := os.ReadDir(tmpDir2)
|
|
|
|
if nil != readErr {
|
|
|
|
logging.LogErrorf("read dir [%s] failed: %s", tmpDir2, readErr)
|
|
|
|
errFiles = append(errFiles, fName)
|
|
|
|
ret.Msg = readErr.Error()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if 1 > len(entries) {
|
|
|
|
logging.LogErrorf("read dir [%s] failed: no entry", tmpDir2)
|
|
|
|
errFiles = append(errFiles, fName)
|
|
|
|
ret.Msg = "no entry"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
dirName := entries[0].Name()
|
|
|
|
srcDir := filepath.Join(tmpDir2, dirName)
|
|
|
|
entries, readErr = os.ReadDir(srcDir)
|
|
|
|
if nil != readErr {
|
|
|
|
logging.LogErrorf("read dir [%s] failed: %s", filepath.Join(tmpDir2, entries[0].Name()), readErr)
|
|
|
|
errFiles = append(errFiles, fName)
|
|
|
|
ret.Msg = readErr.Error()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
destDir := filepath.Join(assetsDirPath, fName)
|
|
|
|
for _, entry := range entries {
|
|
|
|
from := filepath.Join(srcDir, entry.Name())
|
|
|
|
to := filepath.Join(destDir, entry.Name())
|
|
|
|
if copyErr := gulu.File.Copy(from, to); nil != copyErr {
|
|
|
|
logging.LogErrorf("copy [%s] to [%s] failed: %s", from, to, copyErr)
|
|
|
|
errFiles = append(errFiles, fName)
|
|
|
|
ret.Msg = copyErr.Error()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
os.RemoveAll(tmpDir2)
|
|
|
|
}
|
|
|
|
|
2024-08-30 09:43:16 +08:00
|
|
|
succMap[baseName] = strings.TrimPrefix(path.Join(relAssetsDirPath, fName), "/")
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Data = map[string]interface{}{
|
|
|
|
"errFiles": errFiles,
|
|
|
|
"succMap": succMap,
|
|
|
|
}
|
|
|
|
|
2022-07-14 21:50:46 +08:00
|
|
|
IncSync()
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAssetsDir(boxLocalPath, docDirLocalPath string) (assets string) {
|
|
|
|
assets = filepath.Join(docDirLocalPath, "assets")
|
2023-11-06 22:13:04 +08:00
|
|
|
if !filelock.IsExist(assets) {
|
2022-05-26 15:18:53 +08:00
|
|
|
assets = filepath.Join(boxLocalPath, "assets")
|
2023-11-06 22:13:04 +08:00
|
|
|
if !filelock.IsExist(assets) {
|
2022-05-26 15:18:53 +08:00
|
|
|
assets = filepath.Join(util.DataDir, "assets")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|