From 4f0390815cb0f8e71b0a178243de94d1194387cf Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Fri, 8 Mar 2024 23:46:49 +0800 Subject: [PATCH] feat alpha: support sqlite engine --- addition/article/utils.go | 2 +- app/src/conf/env.ts | 2 +- connection/database.go | 4 ++-- utils/compress.go | 4 ++-- utils/fs.go | 15 ++++++++++----- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/addition/article/utils.go b/addition/article/utils.go index e2a1f50..8dad4d8 100644 --- a/addition/article/utils.go +++ b/addition/article/utils.go @@ -31,7 +31,7 @@ func GenerateDocxFile(target, title, content string) error { func CreateArticleFile(hash, title, content string) string { target := fmt.Sprintf("addition/article/data/%s/%s.docx", hash, title) - utils.CreateFolderOnFile(target) + utils.FileDirSafe(target) if err := GenerateDocxFile(target, title, content); err != nil { globals.Debug(fmt.Sprintf("[article] error during generate article %s: %s", title, err.Error())) } diff --git a/app/src/conf/env.ts b/app/src/conf/env.ts index c287a30..98fa1b6 100644 --- a/app/src/conf/env.ts +++ b/app/src/conf/env.ts @@ -116,7 +116,7 @@ export function setAnnouncement(announcement: string): void { */ if (!announcement || announcement.trim() === "") return; - const firstReceived = getMemory("announcement") !== announcement; + const firstReceived = getMemory("announcement").trim() !== announcement.trim(); setMemory("announcement", announcement); announcementEvent.emit({ diff --git a/connection/database.go b/connection/database.go index 4ae7c1f..7809008 100644 --- a/connection/database.go +++ b/connection/database.go @@ -23,8 +23,8 @@ func InitMySQLSafe() *sql.DB { func getConn() *sql.DB { if viper.GetString("mysql.host") == "" { globals.SqliteEngine = true - globals.Warn("[connection] mysql host is not set, using sqlite (chatnio.db)") - db, err := sql.Open("sqlite3", "chatnio.db") + globals.Warn("[connection] mysql host is not set, using sqlite (~/db/chatnio.db)") + db, err := sql.Open("sqlite3", utils.FileSafe("./db/chatnio.db")) if err != nil { panic(err) } diff --git a/utils/compress.go b/utils/compress.go index 478fb99..63b4fc5 100644 --- a/utils/compress.go +++ b/utils/compress.go @@ -57,7 +57,7 @@ func handlePath(path string) string { } func CreateZipObject(output string, files []string, replacer string) error { - CreateFolderOnFile(output) + FileDirSafe(output) file, err := os.Create(output) if err != nil { return err @@ -107,7 +107,7 @@ func addFileToZip(zipWriter *zip.Writer, path string, replacer string) error { } func CreateGzipObject(output string, files []string, replacer string) error { - CreateFolderOnFile(output) + FileDirSafe(output) tarFile, err := os.Create(output) if err != nil { return err diff --git a/utils/fs.go b/utils/fs.go index 18a01e6..7cdf8fc 100644 --- a/utils/fs.go +++ b/utils/fs.go @@ -23,22 +23,27 @@ func Exists(path string) bool { return err != nil && os.IsExist(err) } -func CreateFolderNotExists(path string) string { +func DirSafe(path string) string { CreateFolder(path) return path } -func CreateFolderOnFile(file string) string { +func FileDirSafe(file string) string { if strings.LastIndex(file, "/") == -1 { return file } - return CreateFolderNotExists(file[:strings.LastIndex(file, "/")]) + return DirSafe(file[:strings.LastIndex(file, "/")]) +} + +func FileSafe(file string) string { + FileDirSafe(file) + return file } func WriteFile(path string, data string, folderSafe bool) error { if folderSafe { - CreateFolderOnFile(path) + FileDirSafe(path) } file, err := os.Create(path) @@ -152,7 +157,7 @@ func CopyFile(src string, dst string) error { } }(in) - CreateFolderOnFile(dst) + FileDirSafe(dst) out, err := os.Create(dst) if err != nil { return err