Script to add the size of the file to the book metadata

This commit is contained in:
Las Zenow 2013-09-19 00:49:48 +02:00
parent 43856dcd9b
commit f5ae093e02
6 changed files with 595 additions and 0 deletions

38
tools/addsize/addsize.go Normal file
View file

@ -0,0 +1,38 @@
package main
import (
"fmt"
"labix.org/v2/mgo/bson"
)
func main() {
db = initDB()
defer db.Close()
books, _, _ := db.GetBooks(bson.M{})
for _, book := range books {
size, err := getSize(book.File)
if err != nil {
fmt.Println(err)
continue
}
err = db.UpdateBook(bson.ObjectIdHex(book.Id), bson.M{"filesize": size})
if err != nil {
fmt.Println(err)
}
}
}
type file struct {
Length int
}
func getSize(id bson.ObjectId) (int, error) {
fs := db.GetFS(FS_BOOKS)
var f file
err := fs.Find(bson.M{"_id": id}).One(&f)
if err != nil {
return 0, err
}
return f.Length, nil
}