all repos — flounder @ 8834a9bcd3e4c3fc0511cda6c34defd6126405a9

A small site builder for the Gemini protocol

utils.go (view raw)

 1package main
 2
 3import (
 4	"fmt"
 5	"path"
 6	"strings"
 7	"time"
 8)
 9
10func timeago(t *time.Time) string {
11	d := time.Since(*t)
12	if d.Seconds() < 60 {
13		return fmt.Sprintf("%d seconds ago", int(d.Seconds()))
14	} else if d.Minutes() < 60 {
15		return fmt.Sprintf("%d minutes ago", int(d.Minutes()))
16	} else if d.Hours() < 24 {
17		return fmt.Sprintf("%d hours ago", int(d.Hours()))
18	} else {
19		return fmt.Sprintf("%d days ago", int(d.Hours())/24)
20	}
21}
22
23/// Perform some checks to make sure the file is OK
24func checkIfValidFile(filename string, fileBytes []byte) error {
25	ext := strings.ToLower(path.Ext(filename))
26	found := false
27	for _, mimetype := range c.OkExtensions {
28		if ext == mimetype {
29			found = true
30		}
31	}
32	if !found {
33		return fmt.Errorf("Invalid file extension: %s", ext)
34	}
35	if len(fileBytes) > c.MaxFileSize {
36		return fmt.Errorf("File too large. File was %s bytes, Max file size is %s", len(fileBytes), c.MaxFileSize)
37	}
38	return nil
39}