Files
openaccounting-server/core/util/util.go
Aaron Guise 8b6ba74ce9 feat: implement secure file upload system with JWT authentication
- Add JWT-based secure file access for local storage with 1-hour expiry
- Implement GORM repository methods for attachment CRUD operations
- Add secure file serving endpoint with token validation
- Update storage interface to support user context in URL generation
- Add comprehensive security features including path traversal protection
- Update documentation with security model and configuration examples
- Add utility functions for hex/byte conversion and UUID validation
- Configure secure file permissions (0600) for uploaded files

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-03 15:45:25 +12:00

78 lines
1.4 KiB
Go

package util
import (
"crypto/rand"
"encoding/hex"
"regexp"
"time"
)
func Round64(input float64) int64 {
if input < 0 {
return int64(input - 0.5)
}
return int64(input + 0.5)
}
func TimeToMs(date time.Time) int64 {
return date.UnixNano() / 1000000
}
func MsToTime(ms int64) time.Time {
return time.Unix(0, ms*1000000)
}
func NewGuid() (string, error) {
byteArray := make([]byte, 16)
_, err := rand.Read(byteArray)
if err != nil {
return "", err
}
return hex.EncodeToString(byteArray), nil
}
func NewInviteId() (string, error) {
byteArray := make([]byte, 4)
_, err := rand.Read(byteArray)
if err != nil {
return "", err
}
return hex.EncodeToString(byteArray), nil
}
func NewUUID() string {
guid, err := NewGuid()
if err != nil {
// Fallback to timestamp-based UUID if random generation fails
return hex.EncodeToString([]byte(time.Now().Format("20060102150405")))
}
return guid
}
func IsValidUUID(uuid string) bool {
// Check if the string is a valid 32-character hex string (16 bytes * 2 hex chars)
if len(uuid) != 32 {
return false
}
// Check if all characters are valid hex characters
matched, _ := regexp.MatchString("^[0-9a-f]{32}$", uuid)
return matched
}
// HexToBytes converts a hex string to bytes
func HexToBytes(hexString string) ([]byte, error) {
return hex.DecodeString(hexString)
}
// BytesToHex converts bytes to a hex string
func BytesToHex(bytes []byte) string {
return hex.EncodeToString(bytes)
}