forked from jason/cpolis
Change filepaths to use filepath.Join() where possible
This commit is contained in:
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@ -334,9 +335,9 @@ func (db *DB) DeleteArticle(id int64) error {
|
||||
}
|
||||
|
||||
func WriteArticleToFile(c *Config, articleUUID uuid.UUID, content []byte) error {
|
||||
articleAbsName := fmt.Sprint(c.ArticleDir, "/", articleUUID, ".md")
|
||||
articlePath := filepath.Join(c.ArticleDir, fmt.Sprint(articleUUID, ".md"))
|
||||
|
||||
if err := os.WriteFile(articleAbsName, content, 0644); err != nil {
|
||||
if err := os.WriteFile(articlePath, content, 0644); err != nil {
|
||||
return fmt.Errorf("error writing article %v to file: %v", articleUUID, err)
|
||||
}
|
||||
|
||||
|
@ -58,12 +58,9 @@ func newConfig() *Config {
|
||||
}
|
||||
|
||||
func mkDir(path string, perm fs.FileMode) (string, error) {
|
||||
var err error
|
||||
name := filepath.Base(path)
|
||||
|
||||
stringSlice := strings.Split(path, "/")
|
||||
name := stringSlice[len(stringSlice)-1]
|
||||
|
||||
path, err = filepath.Abs(path)
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error finding absolute path for %v directory: %v", name, err)
|
||||
}
|
||||
@ -82,20 +79,20 @@ func mkFile(path string, filePerm, dirPerm fs.FileMode) (string, error) {
|
||||
return "", fmt.Errorf("error finding absolute path for %v: %v", path, err)
|
||||
}
|
||||
|
||||
stringSlice := strings.Split(path, "/")
|
||||
_, err = os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
dir := strings.Join(stringSlice[:len(stringSlice)-1], "/")
|
||||
dir := filepath.Dir(path)
|
||||
if err = os.MkdirAll(dir, dirPerm); err != nil {
|
||||
return "", fmt.Errorf("error creating %v: %v", dir, err)
|
||||
}
|
||||
|
||||
fileName := stringSlice[len(stringSlice)-1]
|
||||
fileName := filepath.Base(path)
|
||||
file, err := os.Create(filepath.Join(dir, fileName))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error creating %v: %v", fileName, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if err = file.Chmod(filePerm); err != nil {
|
||||
return "", fmt.Errorf("error setting permissions for %v: %v", fileName, err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user