Documentation ¶
Overview ¶
Package helpers are general, shared functions.
Index ¶
- Constants
- Variables
- func Add1(a any) int64
- func ByteCount(b int64) string
- func ByteCountFloat(b int64) string
- func Capitalize(s string) string
- func CfUUID(cfid string) (string, error)
- func ChrLast(s string) string
- func CookieStore(envKey string) ([]byte, error)
- func Count(dir string) (int, error)
- func Day(i int) bool
- func DeObfuscate(s string) string
- func DeleteDupe(s ...string) []string
- func DeobfuscateID(id string) int
- func DeobfuscateURL(rawURL string) int
- func Determine(reader io.Reader) encoding.Encoding
- func DiskUsage(path string) (int64, error)
- func Duplicate(oldpath, newpath string) (int64, error)
- func DuplicateOW(oldpath, newpath string) (int64, error)
- func File(name string) bool
- func FileMatch(name1, name2 string) (bool, error)
- func Files(dir string) ([]string, error)
- func Finds(name string, names ...string) bool
- func FixSceneOrg(rawURL string) string
- func FmtSlice(s string) string
- func GetStat(url string) (int64, error)
- func Integrity(name string, fs embed.FS) (string, error)
- func IntegrityBytes(b []byte) string
- func IntegrityFile(name string) (string, error)
- func Latency() *time.Time
- func Lines(name string) (int, error)
- func LocalHostPing(uri string, proto string, port int) (int, int64, error)
- func LocalHosts() ([]string, error)
- func LocalIPs() ([]net.IP, error)
- func Logger(ctx context.Context) *zap.SugaredLogger
- func MaxLineLength(s string) int
- func MkContent(src string) (string, error)
- func Obfuscate(s string) string
- func ObfuscateID(key int64) string
- func Owner() ([]string, string, error)
- func PageCount(sum, limit int) uint
- func Ping(uri string) (int, int64, error)
- func Released(s string) (int16, int16, int16)
- func RenameCrossDevice(oldpath, newpath string) error
- func RenameFile(oldpath, newpath string) error
- func RenameFileOW(oldpath, newpath string) error
- func ReverseInt(i int) (int, error)
- func SearchTerm(input string) []string
- func ShortMonth(month int) string
- func Size(name string) int64
- func Slug(name string) string
- func SplitAsSpaces(s string) string
- func Stat(name string) bool
- func StrongIntegrity(name string) (string, error)
- func Sum386(f *os.File) (string, error)
- func TimeDistance(from, to time.Time, seconds bool) string
- func Titleize(s string) string
- func TmpDir() string
- func Touch(name string) error
- func TouchW(name string, data ...byte) (int, error)
- func TrimPunct(s string) string
- func TrimRoundBraket(s string) string
- func TruncFilename(w int, name string) string
- func UTF8(name string) (bool, error)
- func Year(i int) bool
- func Years(a, b int16) string
- type DownloadResponse
- type Extension
Constants ¶
const ( // Eraseline is an ANSI escape control to erase the active line of the terminal. Eraseline = "\x1b[2K" // Timeout is the HTTP client timeout. Timeout = 5 * time.Second // User-Agent to send with the HTTP request. UserAgent = "Defacto2 2024 app under construction (thanks!)" )
const ( // WriteWriteRead is the file mode for read and write access. // The file owner and group has read and write access, and others have read access. WriteWriteRead fs.FileMode = 0o664 DSStore = ".DS_Store" // DSStore is the macOS directory service store file. TempBase = "defacto2-server" // TempBase is the base subdirectory for temporary files. DirWriteReadRead = 0o755 // Directory permissions. )
const LoggerKey contextKey = "logger"
LoggerKey is the key used to store the logger in the context.
Variables ¶
var ( ErrDiffLength = errors.New("files are of different lengths") ErrDirPath = errors.New("directory path is a file") ErrExistPath = errors.New("path ready exists and will not overwrite") ErrFilePath = errors.New("file path is a directory") ErrKey = errors.New("could not generate a random session key") ErrOSFile = errors.New("os file is nil") ErrNoDir = errors.New("not a directory") ErrRead = errors.New("could not read files") )
Functions ¶
func Add1 ¶
Add1 returns the value of a + 1. The type of a must be an integer type or the result is 0.
func ByteCountFloat ¶
ByteCountFloat formats b as in a human-readable unit of measure. Units measured in gigabytes or larger are returned with 1 decimal place.
func Capitalize ¶
Capitalize returns a string with the first letter of the first word capitalized. If the first word is an acronym, it is capitalized as a word.
func CfUUID ¶
CfUUID formats a 35 character, Coldfusion Universally Unique Identifier to a standard, 36 character, Universally Unique Identifier.
func CookieStore ¶
CookieStore generates a key for use with the sessions cookie store middleware. envKey is the value of an imported environment session key. But if it is empty, a 32-bit randomized value is generated that changes on every restart.
The effect of using a randomized key will invalidate all existing sessions on every restart.
func DeObfuscate ¶
Deobfuscate the obfuscated string, or return the original string.
func DeleteDupe ¶
DeleteDupe removes duplicate strings from a slice. The returned slice is sorted and compacted.
func DeobfuscateID ¶
DeobfuscateID an obfuscated ID to return the primary key of the record. Returns a 0 if the id is not valid.
func DeobfuscateURL ¶
Deobfuscate an obfuscated record URL to return a record's primary key. A URL can point to a Defacto2 record download or detail page. Returns a 0 if the URL is not valid.
func Determine ¶
Determine returns the encoding of the plain text byte slice. If the byte slice contains Unicode multi-byte characters then nil is returned. Otherwise a charmap.ISO8859_1 or charmap.CodePage437 encoding is returned.
func Duplicate ¶
Duplicate is a workaround for renaming files across different devices. A cross device can also be a different file system such as a Docker volume. It returns the number of bytes written to the new file. The function returns an error if the newpath already exists.
func DuplicateOW ¶
DuplicateOW is a workaround for renaming files across different devices. A cross device can also be a different file system such as a Docker volume. It returns the number of bytes written to the new file. The function will truncate and overwrite the newpath if it already exists.
func FileMatch ¶
FileMatch returns true if the two named files are the same. It returns false if the files are of different lengths or if an error occurs while reading the files. The read buffer size is 4096 bytes.
func FixSceneOrg ¶
FixSceneOrg returns a working URL if the provided rawURL is a known, broken link to a scene.org file. Otherwise it returns the original URL.
func GetStat ¶
GetStat returns the content length of a remote URL. It returns an error if the URL is invalid or the request fails. The content length is -1 if it is unknown.
func Integrity ¶
Integrity returns the sha384 hash of the named embed file. This is intended to be used for Subresource Integrity (SRI) verification with integrity attributes in HTML script and link tags.
func IntegrityBytes ¶
IntegrityBytes returns the sha384 hash of the given byte slice.
func IntegrityFile ¶
IntegrityFile returns the sha384 hash of the named file. This can be used as a link cache buster.
func LocalHostPing ¶
LocalHostPing sends a HTTP GET request to the provided URI on the localhost and returns the status code and size of the response.
func LocalIPs ¶
LocalIPs returns a list of local IP addresses. credit: https://gosamples.dev/local-ip-address/
func Logger ¶
func Logger(ctx context.Context) *zap.SugaredLogger
Logger returns the logger from the context. If the logger is not found, it panics.
func MaxLineLength ¶
MaxLineLength counts the character length of the longest line in a string.
func MkContent ¶
MkContent returns the destination directory for the extracted archive content. The directory is created if it does not exist. The directory is named after the source file.
func Obfuscate ¶
Obfuscate a numeric string to insecurely hide database primary key values when passed along a URL. This function is a port of a CFWheels framework function programmed in ColdFusion (CFML). https://github.com/cfwheels/cfwheels/blob/cf8e6da4b9a216b642862e7205345dd5fca34b54/wheels/global/misc.cfm#L483
func ObfuscateID ¶
ObfuscateID the primary key of a record as a string that is used as a URL param or path.
func Owner ¶
Owner returns the running user and group of the web application. The function returns the group names and the username of the owner.
func PageCount ¶
PageCount returns the maximum pages possible for the sum of records with a record limit per-page.
func Ping ¶
Ping sends a HTTP GET request to the provided URI and returns the status code and size of the response.
func Released ¶
Released returns a string release date as year, month, day int16 values. The string is expected to be in the format "2024-07-15" or "2024-07" or "2024".
func RenameCrossDevice ¶
RenameCrossDevice is a workaround for renaming files across different devices. A cross device can also be a different file system such as a Docker volume.
func RenameFile ¶
RenameFile renames a file from oldpath to newpath. It returns an error if the oldpath does not exist or is a directory, newpath already exists, or the rename fails.
func RenameFileOW ¶
RenameFileOW renames a file from oldpath to newpath. It returns an error if the oldpath does not exist or is a directory or the rename fails.
func SearchTerm ¶
SearchTerm returns a list of search terms from the input string. The input string is split by commas.
func ShortMonth ¶
ShortMonth takes a month integer and abbreviates it to a three letter English month.
func SplitAsSpaces ¶
SplitAsSpaces splits a string at each capital letter.
func StrongIntegrity ¶
StrongIntegrity returns the SHA-386 checksum value of the named file.
func TimeDistance ¶
TimeDistance describes the difference between two time values. The seconds parameter determines if the string should include seconds.
func Titleize ¶
Titleize returns a string with the first letter each word capitalized. If a word is an acronym, it is capitalized as a word.
func TmpDir ¶
func TmpDir() string
TempDir returns the temporary directory for the server, which is a subdirectory of the system temp directory.
func Touch ¶
Touch creates a new, empty named file. If the file already exists, an error is returned.
func TouchW ¶
TouchW creates a new named file with the given data. If the file already exists, an error is returned.
func TrimRoundBraket ¶
TrimRoundBraket removes the tailing round brakets and any whitespace.
func TruncFilename ¶
TruncFilename reduces a filename to the length of w characters. The file extension is always preserved with the truncation.
func UTF8 ¶
UTF8 returns true if the named file is a valid UTF-8 encoded file. The function reads the first 512 bytes of the file to determine the encoding.
Types ¶
type DownloadResponse ¶
type DownloadResponse struct { ContentLength string // ContentLength is the size of the file in bytes. ContentType string // ContentType is the MIME type of the file. LastModified string // LastModified is the last modified date of the file. Path string // Path is the path to the downloaded file. }
DownloadResponse contains the details of a downloaded file.
func GetFile ¶
func GetFile(url string) (DownloadResponse, error)
GetFile downloads a file from a remote URL and saves it to the default temp directory. It returns the path to the downloaded file.