Files
zineshop/utils/utils.go
kalipso 7025f526c1
All checks were successful
Go / build (push) Successful in 12m14s
add invoice view
2025-07-01 15:30:06 +02:00

67 lines
1.1 KiB
Go

package utils
import (
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/dslipak/pdf"
"io"
)
func GenerateSessionId(length int) string {
bytes := make([]byte, length) // 16 bytes = 128 bits
_, err := rand.Read(bytes)
if err != nil {
panic("failed to generate session ID")
}
return hex.EncodeToString(bytes)
}
func GenerateToken() string {
return GenerateSessionId(16)
}
const match = "/Page\x00"
// Pages reads the given io.ByteReader until EOF is reached, returning the
// number of pages encountered.
func Pages(reader io.ByteReader) (pages int) {
i := 0
for {
b, err := reader.ReadByte()
if err != nil {
fmt.Println(err)
return
}
check:
switch match[i] {
case 0:
if !(b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z') {
pages++
}
i = 0
goto check
case b:
i++
default:
i = 0
}
}
}
// PagesAtPath opens a PDF file at the given file path, returning the number
// of pages found.
func CountPagesAtPath(path string) (pages int) {
r, err := pdf.Open(path)
if err != nil {
fmt.Println("LOL")
fmt.Println(err)
return 0
}
pages = r.NumPage()
return
}