contented/Metadata.go

46 lines
806 B
Go

package contented
import (
"encoding/json"
"os"
"time"
"github.com/boltdb/bolt"
)
type Metadata struct {
FileSize int64
UploadTime time.Time
UploadIP string
Filename string
MimeType string
}
func (this *Server) Metadata(fileID string) (*Metadata, error) {
var m Metadata
err := this.db.View(func(tx *bolt.Tx) error {
content := tx.Bucket(this.metadataBucket).Get([]byte(fileID))
if len(content) == 0 {
return os.ErrNotExist
}
return json.Unmarshal(content, &m)
})
if err != nil {
return nil, err
}
return &m, nil
}
func (this *Server) SetMetadata(fileID string, m Metadata) error {
jb, err := json.Marshal(m)
if err != nil {
return err
}
return this.db.Update(func(tx *bolt.Tx) error {
return tx.Bucket(this.metadataBucket).Put([]byte(fileID), jb)
})
}