Add filesize to the books collection
This commit is contained in:
parent
147ed57fc8
commit
9e4b92772c
3 changed files with 7 additions and 5 deletions
|
@ -28,6 +28,7 @@ type Book struct {
|
|||
Rights string
|
||||
Meta string
|
||||
File bson.ObjectId
|
||||
FileSize int
|
||||
Cover bson.ObjectId
|
||||
CoverSmall bson.ObjectId
|
||||
Active bool
|
||||
|
|
8
store.go
8
store.go
|
@ -24,17 +24,17 @@ func OpenBook(id bson.ObjectId) (*epubgo.Epub, error) {
|
|||
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)
|
||||
fw, err := fs.Create(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", 0, err
|
||||
}
|
||||
defer fw.Close()
|
||||
|
||||
_, err = io.Copy(fw, file)
|
||||
size, err := io.Copy(fw, file)
|
||||
id, _ := fw.Id().(bson.ObjectId)
|
||||
return id, err
|
||||
return id, size, err
|
||||
}
|
||||
|
||||
func DeleteFile(id bson.ObjectId) error {
|
||||
|
|
|
@ -41,13 +41,14 @@ func processFile(req uploadRequest) {
|
|||
book := parseFile(epub)
|
||||
title, _ := book["title"].(string)
|
||||
req.file.Seek(0, 0)
|
||||
id, err := StoreNewFile(title+".epub", req.file)
|
||||
id, size, err := StoreNewFile(title+".epub", req.file)
|
||||
if err != nil {
|
||||
log.Println("Error storing book (", title, "):", err)
|
||||
return
|
||||
}
|
||||
|
||||
book["file"] = id
|
||||
book["filesize"] = size
|
||||
err = db.InsertBook(book)
|
||||
if err != nil {
|
||||
log.Println("Error storing metadata (", title, "):", err)
|
||||
|
|
Reference in a new issue