38 lines
826 B
Go
38 lines
826 B
Go
package clipmon
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"path/filepath"
|
||
|
|
"regexp"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
const hourBucketLayout = "2006-01-02-15"
|
||
|
|
|
||
|
|
var logFilenamePattern = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}-\d{2}\.log$`)
|
||
|
|
|
||
|
|
func HourBucket(t time.Time) string {
|
||
|
|
return t.Local().Format(hourBucketLayout)
|
||
|
|
}
|
||
|
|
|
||
|
|
func LogFilename(bucket string) string {
|
||
|
|
return bucket + ".log"
|
||
|
|
}
|
||
|
|
|
||
|
|
func ValidLogFilename(name string) bool {
|
||
|
|
return logFilenamePattern.MatchString(filepath.Base(name))
|
||
|
|
}
|
||
|
|
|
||
|
|
func formatLine(event Event) string {
|
||
|
|
when := event.Time.UTC().Format("2006-01-02T15:04:05.000Z")
|
||
|
|
switch event.Kind {
|
||
|
|
case "image":
|
||
|
|
return fmt.Sprintf("%s [image] %s\n", when, event.ImagePath)
|
||
|
|
default:
|
||
|
|
text := strings.ReplaceAll(event.Text, "\r\n", "\n")
|
||
|
|
text = strings.ReplaceAll(text, "\r", "\n")
|
||
|
|
return fmt.Sprintf("%s [text]\n%s\n", when, text)
|
||
|
|
}
|
||
|
|
}
|