Add filesize to the books collection

This commit is contained in:
Las Zenow 2013-09-18 23:56:49 +02:00
parent 147ed57fc8
commit 9e4b92772c
3 changed files with 7 additions and 5 deletions

View file

@ -28,6 +28,7 @@ type Book struct {
Rights string Rights string
Meta string Meta string
File bson.ObjectId File bson.ObjectId
FileSize int
Cover bson.ObjectId Cover bson.ObjectId
CoverSmall bson.ObjectId CoverSmall bson.ObjectId
Active bool Active bool

View file

@ -24,17 +24,17 @@ func OpenBook(id bson.ObjectId) (*epubgo.Epub, error) {
return epubgo.Load(reader, int64(len(buff))) return epubgo.Load(reader, int64(len(buff)))
} }
func StoreNewFile(name string, file io.Reader) (bson.ObjectId, error) { func StoreNewFile(name string, file io.Reader) (bson.ObjectId, int64, error) {
fs := db.GetFS(FS_BOOKS) fs := db.GetFS(FS_BOOKS)
fw, err := fs.Create(name) fw, err := fs.Create(name)
if err != nil { if err != nil {
return "", err return "", 0, err
} }
defer fw.Close() defer fw.Close()
_, err = io.Copy(fw, file) size, err := io.Copy(fw, file)
id, _ := fw.Id().(bson.ObjectId) id, _ := fw.Id().(bson.ObjectId)
return id, err return id, size, err
} }
func DeleteFile(id bson.ObjectId) error { func DeleteFile(id bson.ObjectId) error {

View file

@ -41,13 +41,14 @@ func processFile(req uploadRequest) {
book := parseFile(epub) book := parseFile(epub)
title, _ := book["title"].(string) title, _ := book["title"].(string)
req.file.Seek(0, 0) req.file.Seek(0, 0)
id, err := StoreNewFile(title+".epub", req.file) id, size, err := StoreNewFile(title+".epub", req.file)
if err != nil { if err != nil {
log.Println("Error storing book (", title, "):", err) log.Println("Error storing book (", title, "):", err)
return return
} }
book["file"] = id book["file"] = id
book["filesize"] = size
err = db.InsertBook(book) err = db.InsertBook(book)
if err != nil { if err != nil {
log.Println("Error storing metadata (", title, "):", err) log.Println("Error storing metadata (", title, "):", err)