A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1// SiYuan - Refactor your thinking
2// Copyright (c) 2020-present, b3log.org
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Affero General Public License for more details.
13//
14// You should have received a copy of the GNU Affero General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17package model
18
19import (
20 "fmt"
21 "io/fs"
22 "os"
23 "path"
24 "path/filepath"
25 "time"
26
27 "github.com/88250/gulu"
28 "github.com/emersion/go-webdav/caldav"
29 "github.com/emersion/go-webdav/carddav"
30 "github.com/siyuan-note/logging"
31 "github.com/siyuan-note/siyuan/kernel/util"
32)
33
34// PathJoinWithSlash joins the elements to a path with slash ('/') character
35func PathJoinWithSlash(elems ...string) string {
36 return filepath.ToSlash(filepath.Join(elems...))
37}
38
39// PathCleanWithSlash cleans the path
40func PathCleanWithSlash(p string) string {
41 return filepath.ToSlash(filepath.Clean(p))
42}
43
44// DavPath2DirectoryPath converts CalDAV/CardDAV path to absolute path of the file system
45func DavPath2DirectoryPath(davPath string) string {
46 return PathJoinWithSlash(util.DataDir, "storage", davPath)
47}
48
49func SaveMetaData[T []*caldav.Calendar | []*carddav.AddressBook](metaData T, metaDataFilePath string) error {
50 data, err := gulu.JSON.MarshalIndentJSON(metaData, "", " ")
51 if err != nil {
52 logging.LogErrorf("marshal address books meta data failed: %s", err)
53 return err
54 }
55
56 dirPath := path.Dir(metaDataFilePath)
57 if err := os.MkdirAll(dirPath, 0755); err != nil {
58 logging.LogErrorf("create directory [%s] failed: %s", dirPath, err)
59 return err
60 }
61
62 if err := os.WriteFile(metaDataFilePath, data, 0755); err != nil {
63 logging.LogErrorf("write file [%s] failed: %s", metaDataFilePath, err)
64 return err
65 }
66
67 return nil
68}
69
70// FileETag generates an ETag for a file
71func FileETag(fileInfo fs.FileInfo) string {
72 return fmt.Sprintf(
73 "%s-%x",
74 fileInfo.ModTime().Format(time.RFC3339),
75 fileInfo.Size(),
76 )
77}