hibiscus/files.go

92 lines
2.7 KiB
Go
Raw Normal View History

2024-03-15 18:34:24 +03:00
package main
import (
2024-03-30 14:33:39 +03:00
"bytes"
"errors"
2024-03-20 16:18:23 +03:00
"log/slog"
2024-03-15 18:34:24 +03:00
"os"
2024-03-18 20:04:14 +03:00
"path"
2024-03-15 18:34:24 +03:00
"path/filepath"
"strings"
2024-03-18 15:50:20 +03:00
"time"
2024-03-15 18:34:24 +03:00
)
// DataFile modifies file path to ensure it's a .txt inside the data folder.
2024-05-09 23:44:37 +03:00
func DataFile(filename string) string {
return "data/" + path.Clean(filename) + ".txt"
}
// ReadFile returns contents of a file.
func ReadFile(filename string) ([]byte, error) {
2024-03-18 20:04:14 +03:00
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
return nil, err
2024-03-15 18:34:24 +03:00
}
2024-03-18 20:04:14 +03:00
fileContents, err := os.ReadFile(filename)
2024-03-15 18:34:24 +03:00
if err != nil {
slog.Error("error reading file", "error", err, "file", filename)
return nil, err
2024-03-15 18:34:24 +03:00
}
return fileContents, nil
2024-03-15 18:34:24 +03:00
}
// SaveFile Writes contents to a file.
func SaveFile(filename string, contents []byte) error {
contents = bytes.TrimSpace(contents)
if len(contents) == 0 { // Delete empty files
err := os.Remove(filename)
slog.Error("error deleting empty file", "error", err, "file", filename)
return err
}
2024-05-04 22:42:47 +03:00
err := os.MkdirAll(path.Dir(filename), 0755) // Create dir in case it doesn't exist yet to avoid errors
if err != nil {
slog.Error("error creating directory", "error", err, "file", filename)
return err
}
2024-03-30 14:33:39 +03:00
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
2024-03-15 18:34:24 +03:00
if err != nil {
slog.Error("error opening/creating file", "error", err, "file", filename)
return err
2024-03-15 18:34:24 +03:00
}
if _, err := f.Write(contents); err != nil {
slog.Error("error writing to file", "error", err, "file", filename)
return err
2024-03-15 18:34:24 +03:00
}
return nil
2024-03-15 18:34:24 +03:00
}
// ListFiles returns slice of filenames in a directory without extensions or path.
2024-05-09 23:44:37 +03:00
// NOTE: What if I ever want to list non-text files or those outside data directory?
func ListFiles(directory string) ([]string, error) {
2024-05-09 23:44:37 +03:00
filenames, err := filepath.Glob("data/" + path.Clean(directory) + "/*.txt")
2024-03-15 18:34:24 +03:00
if err != nil {
return nil, err
2024-03-15 18:34:24 +03:00
}
for i, file := range filenames {
file, _ := strings.CutSuffix(filepath.Base(file), filepath.Ext(file))
filenames[i] = file
}
return filenames, nil
2024-03-15 18:34:24 +03:00
}
2024-03-18 15:50:20 +03:00
// GraceActive returns whether the grace period (Cfg.GraceTime) is active. Grace period has minute precision
2024-05-05 13:06:20 +03:00
func GraceActive() bool {
2024-05-06 14:53:18 +03:00
t := time.Now().In(Cfg.Timezone)
active := (60*t.Hour() + t.Minute()) < int(Cfg.GraceTime.Minutes())
if active {
slog.Debug("grace period active",
"time", 60*t.Hour()+t.Minute(),
"grace", Cfg.GraceTime.Minutes())
}
return active
2024-05-05 13:06:20 +03:00
}
// TodayDate returns today's formatted date. It accounts for Config.GraceTime.
2024-05-05 13:06:20 +03:00
func TodayDate() string {
dateFormatted := time.Now().In(Cfg.Timezone).Format(time.DateOnly)
if GraceActive() {
dateFormatted = time.Now().In(Cfg.Timezone).AddDate(0, 0, -1).Format(time.DateOnly)
}
2024-05-09 23:44:37 +03:00
slog.Debug("today", "time", time.Now().In(Cfg.Timezone).Format(time.DateTime))
2024-05-05 13:06:20 +03:00
return dateFormatted
}