Browse Source
Merge pull request #6 from matthazinski/metadata
Merge pull request #6 from matthazinski/metadata
Add preliminary metadata supportpull/12/head
Andrei Marcu
9 years ago
6 changed files with 220 additions and 12 deletions
-
1.gitignore
-
44expiry.go
-
12fileserve.go
-
89meta.go
-
30server.go
-
56upload.go
@ -0,0 +1,44 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
// Get what the unix timestamp will be in "seconds".
|
||||
|
func getFutureTimestamp(seconds int32) (ts int32) { |
||||
|
now := int32(time.Now().Unix()) |
||||
|
|
||||
|
if seconds == 0 { |
||||
|
ts = 0 |
||||
|
} else { |
||||
|
ts = now + seconds |
||||
|
} |
||||
|
|
||||
|
return |
||||
|
} |
||||
|
|
||||
|
// Determine if a file with expiry set to "ts" has expired yet
|
||||
|
func isTsExpired(ts int32) (expired bool) { |
||||
|
now := int32(time.Now().Unix()) |
||||
|
|
||||
|
if ts == 0 { |
||||
|
expired = false |
||||
|
} else if now > ts { |
||||
|
expired = true |
||||
|
} else { |
||||
|
expired = false |
||||
|
} |
||||
|
|
||||
|
return |
||||
|
} |
||||
|
|
||||
|
// Determine if the given filename is expired
|
||||
|
func isFileExpired(filename string) (bool, error) { |
||||
|
exp, err := metadataGetExpiry(filename) |
||||
|
|
||||
|
if err != nil { |
||||
|
return true, err |
||||
|
} else { |
||||
|
return isTsExpired(exp), err |
||||
|
} |
||||
|
} |
@ -0,0 +1,89 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"bufio" |
||||
|
"errors" |
||||
|
"fmt" |
||||
|
"os" |
||||
|
"path" |
||||
|
"strconv" |
||||
|
) |
||||
|
|
||||
|
// 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) |
||||
|
fmt.Fprintln(w, upload.DebugInfo) |
||||
|
|
||||
|
return w.Flush() |
||||
|
} |
||||
|
|
||||
|
// Return list of strings from a filename's metadata source
|
||||
|
func metadataRead(filename string) ([]string, error) { |
||||
|
file, err := os.Create(path.Join(Config.metaDir, filename)) |
||||
|
|
||||
|
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) (int32, error) { |
||||
|
metadata, err := metadataRead(filename) |
||||
|
|
||||
|
if len(metadata) < 1 { |
||||
|
err := errors.New("ERR: Metadata file does not contain expiry") |
||||
|
return 0, err |
||||
|
} |
||||
|
|
||||
|
// 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 0, err |
||||
|
} |
||||
|
|
||||
|
var expiry int64 |
||||
|
expiry, err = strconv.ParseInt(metadata[0], 10, 32) |
||||
|
|
||||
|
if err != nil { |
||||
|
return 0, err |
||||
|
} else { |
||||
|
return int32(expiry), err |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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 |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue