From 7a3f47bb9766cc1e8a8f5f6f56e8ab67de229b7c Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Sat, 25 Aug 2012 16:22:56 +0200 Subject: [PATCH] Store books preserving the _id --- admin.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/admin.go b/admin.go index feda8cc..b73538e 100644 --- a/admin.go +++ b/admin.go @@ -141,27 +141,29 @@ func storeHandler(newColl, coll *mgo.Collection) func(http.ResponseWriter, *http } 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 { http.NotFound(w, r) 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) 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) } - os.Mkdir(BOOKS_PATH+book.Title[:1], os.ModePerm) - cmd := exec.Command("mv", book.Path, path) + oldPath, _ := book["path"].(string) + os.Mkdir(BOOKS_PATH+title[:1], os.ModePerm) + cmd := exec.Command("mv", oldPath, path) cmd.Run() - book.Path = path + book["path"] = path coll.Insert(book) 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) http.Redirect(w, r, "/new/", 307) }