mirror of
https://github.com/skidoodle/pastebin
synced 2026-04-28 03:07:40 +02:00
48 lines
916 B
Go
48 lines
916 B
Go
package handler
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"math/big"
|
|
"time"
|
|
)
|
|
|
|
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
func generateId() (string, error) {
|
|
result := make([]byte, 10)
|
|
max := big.NewInt(int64(len(charset)))
|
|
for i := 0; i < 10; i++ {
|
|
n, err := rand.Int(rand.Reader, max)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
result[i] = charset[n.Int64()]
|
|
}
|
|
return string(result), nil
|
|
}
|
|
|
|
func TimeAgo(t time.Time) string {
|
|
ago := time.Since(t)
|
|
seconds := int(ago.Seconds())
|
|
|
|
switch {
|
|
case seconds < 60:
|
|
return fmt.Sprintf("%ds ago", seconds)
|
|
case seconds < 3600:
|
|
return fmt.Sprintf("%dm ago", seconds/60)
|
|
case seconds < 86400:
|
|
return fmt.Sprintf("%dh ago", seconds/3600)
|
|
default:
|
|
return fmt.Sprintf("%dd ago", seconds/86400)
|
|
}
|
|
}
|
|
|
|
func hash(content string) string {
|
|
h := sha256.New()
|
|
h.Write([]byte(content))
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|