all repos — flounder @ 0c5bc7386da9f63a7a10c1c6036b081e06c3c765

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	fmt.Println(len(fileBytes))
36	if len(fileBytes) > c.MaxFileSize {
37		return fmt.Errorf("File too large. File was %d bytes, Max file size is %d", len(fileBytes), c.MaxFileSize)
38	}
39	return nil
40}