| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package main
- import (
- "time"
- "fmt"
- "math"
- "os"
- "io"
- "bytes"
- "compress/gzip"
- "archive/tar"
- )
- type Item struct {
- Name string
- Size int64
- URL string
- Date time.Time
- Valid bool
- }
- func (i Item) FDate() string {
- if i.Date.IsZero() {
- return "-"
- }
- return i.Date.Format(time.RFC822)
- }
- func (i Item) FSize() string {
- if i.Size == 0 {
- return "-"
- }
- if i.Size < 1024 {
- return fmt.Sprintf("%d B", i.Size)
- }
- exp := int(math.Log(float64(i.Size)) / math.Log(1024))
- pre := string("KMGTPE"[(exp-1)])
- fsize := float64(i.Size) / math.Pow(1024, float64(exp))
- return fmt.Sprintf("%.1f %siB", fsize, pre)
- }
- type Page struct {
- Title string
- Items []Item
- Error int
- }
- type Config struct {
- Directories map[string]Directory `json:"data"`
- Address string `json:"address"`
- }
- type Directory struct {
- Path string `json:"path"`
- Extension string `json:"ext"`
- }
- func compress(file *os.File, buf *bytes.Buffer) error {
- gw := gzip.NewWriter(buf)
- defer gw.Close()
- tw := tar.NewWriter(gw)
- defer tw.Close()
- if stat, err := file.Stat(); err == nil {
- header := new(tar.Header)
- header.Name = file.Name()
- header.Size = stat.Size()
- header.Mode = int64(stat.Mode())
- header.ModTime = stat.ModTime()
- if err := tw.WriteHeader(header); err != nil {
- return err
- }
- if _, err := io.Copy(tw, file); err != nil {
- return err
- }
- }
- return nil
- }
|