linx-server/meta.go

85 lines
1.6 KiB
Go
Raw Normal View History

2015-09-27 22:17:12 -04:00
package main
import (
"bufio"
"errors"
"fmt"
"os"
"path"
"strconv"
"time"
2015-09-27 22:17:12 -04:00
)
// Write metadata from Upload struct to file
func metadataWrite(filename string, upload *Upload) error {
// Write metadata, overwriting if necessary
file, err := os.Create(path.Join(Config.metaDir, upload.Filename))
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
fmt.Fprintln(w, upload.Expiry)
fmt.Fprintln(w, upload.DeleteKey)
return w.Flush()
}
// Return list of strings from a filename's metadata source
func metadataRead(filename string) ([]string, error) {
2015-09-28 23:46:43 -04:00
file, err := os.Open(path.Join(Config.metaDir, filename))
2015-09-27 22:17:12 -04:00
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func metadataGetExpiry(filename string) (expiry time.Time, err error) {
2015-09-27 22:17:12 -04:00
metadata, err := metadataRead(filename)
// XXX in this case it's up to the caller to determine proper behavior
// for a nonexistant metadata file or broken file
if err != nil {
return
2015-09-27 22:17:12 -04:00
}
if len(metadata) < 1 {
err = errors.New("ERR: Metadata file does not contain expiry")
return
2015-09-27 22:17:12 -04:00
}
expirySecs, err := strconv.ParseInt(metadata[0], 10, 64)
expiry = time.Unix(expirySecs, 0)
return
2015-09-27 22:17:12 -04:00
}
func metadataGetDeleteKey(filename string) (string, error) {
metadata, err := metadataRead(filename)
if len(metadata) < 2 {
err := errors.New("ERR: Metadata file does not contain deletion key")
return "", err
}
if err != nil {
return "", err
} else {
return metadata[1], err
}
}