2012-08-18 02:06:43 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"labix.org/v2/mgo/bson"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2012-08-20 14:25:18 +02:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
2012-09-02 23:07:21 +02:00
|
|
|
"strings"
|
2012-08-18 02:06:43 +02:00
|
|
|
)
|
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
func deleteHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sess := GetSession(r)
|
|
|
|
if sess.User == "" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2012-08-18 02:06:43 +02:00
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
// cutre hack: /delete/ and /delnew/ have the same lenght:
|
|
|
|
id := bson.ObjectIdHex(r.URL.Path[len("/delete/"):])
|
|
|
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
book := books[0]
|
|
|
|
if book.Cover != "" {
|
|
|
|
os.RemoveAll(book.Cover[1:])
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
2012-09-12 00:19:19 +02:00
|
|
|
if book.CoverSmall != "" {
|
|
|
|
os.RemoveAll(book.CoverSmall[1:])
|
|
|
|
}
|
|
|
|
os.RemoveAll(book.Path)
|
|
|
|
db.RemoveBook(id)
|
|
|
|
sess.Notify("Removed book!", "The book '"+book.Title+"' it's completly removed", "success")
|
|
|
|
sess.Save(w, r)
|
2012-09-12 21:47:43 +02:00
|
|
|
if book.Active {
|
|
|
|
http.Redirect(w, r, "/", 307)
|
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, "/new/", 307)
|
|
|
|
}
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
2012-08-19 15:58:37 +02:00
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
func editHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sess := GetSession(r)
|
|
|
|
if sess.User == "" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
2012-09-12 00:19:19 +02:00
|
|
|
id := bson.ObjectIdHex(r.URL.Path[len("/edit/"):])
|
|
|
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var data bookData
|
|
|
|
data.Book = books[0]
|
|
|
|
data.S = GetStatus(w, r)
|
|
|
|
loadTemplate(w, "edit", data)
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanEmptyStr(s []string) []string {
|
|
|
|
var res []string
|
|
|
|
for _, v := range s {
|
|
|
|
if v != "" {
|
|
|
|
res = append(res, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
func saveHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sess := GetSession(r)
|
|
|
|
if sess.User == "" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2012-08-19 15:58:37 +02:00
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
idStr := r.URL.Path[len("/save/"):]
|
|
|
|
id := bson.ObjectIdHex(idStr)
|
|
|
|
title := r.FormValue("title")
|
|
|
|
publisher := r.FormValue("publisher")
|
|
|
|
date := r.FormValue("date")
|
|
|
|
description := r.FormValue("description")
|
|
|
|
author := cleanEmptyStr(r.Form["author"])
|
|
|
|
subject := cleanEmptyStr(r.Form["subject"])
|
|
|
|
lang := cleanEmptyStr(r.Form["lang"])
|
|
|
|
book := map[string]interface{}{"title": title,
|
|
|
|
"publisher": publisher,
|
|
|
|
"date": date,
|
|
|
|
"description": description,
|
|
|
|
"author": author,
|
|
|
|
"subject": subject,
|
|
|
|
"lang": lang}
|
|
|
|
book["keywords"] = keywords(book)
|
|
|
|
err := db.UpdateBook(id, book)
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
2012-09-12 00:19:19 +02:00
|
|
|
|
|
|
|
sess.Notify("Book Modified!", "", "success")
|
|
|
|
sess.Save(w, r)
|
2012-09-12 21:47:43 +02:00
|
|
|
if db.BookActive(id) {
|
|
|
|
http.Redirect(w, r, "/book/"+idStr, 307)
|
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, "/new/", 307)
|
|
|
|
}
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
|
|
|
|
2012-09-09 22:42:03 +02:00
|
|
|
type newBook struct {
|
|
|
|
TitleFound int
|
|
|
|
AuthorFound int
|
|
|
|
B Book
|
|
|
|
}
|
2012-08-20 14:25:18 +02:00
|
|
|
type newData struct {
|
|
|
|
S Status
|
|
|
|
Found int
|
2012-09-09 22:42:03 +02:00
|
|
|
Books []newBook
|
2012-08-20 14:25:18 +02:00
|
|
|
}
|
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
func newHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sess := GetSession(r)
|
|
|
|
if sess.User == "" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2012-08-25 16:41:54 +02:00
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
if len(r.URL.Path) > len("/new/") {
|
|
|
|
http.ServeFile(w, r, r.URL.Path[1:])
|
|
|
|
return
|
|
|
|
}
|
2012-08-20 14:25:18 +02:00
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
res, num, _ := db.GetNewBooks()
|
|
|
|
var data newData
|
|
|
|
data.S = GetStatus(w, r)
|
|
|
|
data.Found = num
|
|
|
|
data.Books = make([]newBook, num)
|
|
|
|
for i, b := range res {
|
|
|
|
data.Books[i].B = b
|
|
|
|
_, data.Books[i].TitleFound, _ = db.GetBooks(buildQuery("title:" + b.Title), 1)
|
|
|
|
_, data.Books[i].AuthorFound, _ = db.GetBooks(buildQuery("author:" + strings.Join(b.Author, " author:")), 1)
|
2012-08-20 14:25:18 +02:00
|
|
|
}
|
2012-09-12 00:19:19 +02:00
|
|
|
loadTemplate(w, "new", data)
|
2012-08-20 14:25:18 +02:00
|
|
|
}
|
|
|
|
|
2012-09-02 23:07:21 +02:00
|
|
|
func ValidFileName(path string, title string, extension string) string {
|
|
|
|
title = strings.Replace(title, "/", "_", -1)
|
2012-09-09 03:18:47 +02:00
|
|
|
title = strings.Replace(title, "?", "_", -1)
|
2012-09-02 23:07:21 +02:00
|
|
|
file := path + "/" + title + extension
|
|
|
|
_, err := os.Stat(file)
|
|
|
|
for i := 0; err == nil; i++ {
|
2012-09-09 03:16:43 +02:00
|
|
|
file = path + "/" + title + "_" + strconv.Itoa(i) + extension
|
2012-09-02 23:07:21 +02:00
|
|
|
_, err = os.Stat(file)
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
func storeHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sess := GetSession(r)
|
|
|
|
if sess.User == "" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2012-08-20 14:25:18 +02:00
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
id := bson.ObjectIdHex(r.URL.Path[len("/store/"):])
|
|
|
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
2012-09-12 00:19:19 +02:00
|
|
|
book := books[0]
|
|
|
|
|
|
|
|
title := book.Title
|
|
|
|
path := ValidFileName(BOOKS_PATH + title[:1], title, ".epub")
|
|
|
|
|
|
|
|
oldPath := book.Path
|
|
|
|
os.Mkdir(BOOKS_PATH+title[:1], os.ModePerm)
|
|
|
|
cmd := exec.Command("mv", oldPath, path)
|
|
|
|
cmd.Run()
|
|
|
|
db.UpdateBook(id, bson.M{"active": true, "path": path})
|
|
|
|
sess.Notify("Store book!", "The book '"+title+"' it's stored for public download", "success")
|
|
|
|
sess.Save(w, r)
|
|
|
|
http.Redirect(w, r, "/new/", 307)
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|