Store books preserving the _id

This commit is contained in:
Las Zenow 2012-08-25 16:22:56 +02:00
parent ea906a7681
commit 7a3f47bb97

View file

@ -141,27 +141,29 @@ func storeHandler(newColl, coll *mgo.Collection) func(http.ResponseWriter, *http
} }
id := bson.ObjectIdHex(r.URL.Path[len("/store/"):]) id := bson.ObjectIdHex(r.URL.Path[len("/store/"):])
books, _, err := GetBook(newColl, bson.M{"_id": id}) var book bson.M
err := newColl.Find(bson.M{"_id": id}).One(&book)
if err != nil { if err != nil {
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
book := books[0]
path := BOOKS_PATH + book.Title[:1] + "/" + book.Title + ".epub" title, _ := book["title"].(string)
path := BOOKS_PATH + title[:1] + "/" + title + ".epub"
_, err = os.Stat(path) _, err = os.Stat(path)
for i := 0; err == nil; i++ { for i := 0; err == nil; i++ {
path := BOOKS_PATH + book.Title[:1] + "/" + book.Title + "_" + strconv.Itoa(i) + ".epub" path := BOOKS_PATH + title[:1] + "/" + title + "_" + strconv.Itoa(i) + ".epub"
_, err = os.Stat(path) _, err = os.Stat(path)
} }
os.Mkdir(BOOKS_PATH+book.Title[:1], os.ModePerm) oldPath, _ := book["path"].(string)
cmd := exec.Command("mv", book.Path, path) os.Mkdir(BOOKS_PATH+title[:1], os.ModePerm)
cmd := exec.Command("mv", oldPath, path)
cmd.Run() cmd.Run()
book.Path = path book["path"] = path
coll.Insert(book) coll.Insert(book)
newColl.Remove(bson.M{"_id": id}) newColl.Remove(bson.M{"_id": id})
sess.Notify("Store book!", "The book '"+book.Title+"' it's stored for public download", "success") sess.Notify("Store book!", "The book '"+title+"' it's stored for public download", "success")
sess.Save(w, r) sess.Save(w, r)
http.Redirect(w, r, "/new/", 307) http.Redirect(w, r, "/new/", 307)
} }