2015-09-27 22:17:12 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2015-10-06 02:50:20 -04:00
|
|
|
"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()
|
|
|
|
}
|
|
|
|
|
2015-10-06 02:50:20 -04:00
|
|
|
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 {
|
2015-10-06 02:50:20 -04:00
|
|
|
return
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
|
|
|
|
2015-10-06 02:50:20 -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
|
|
|
}
|
2015-10-06 02:50:20 -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
|
|
|
|
}
|
|
|
|
}
|