This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/admin.go

168 lines
3.9 KiB
Go
Raw Normal View History

2012-08-18 02:06:43 +02:00
package main
import (
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"net/http"
"os"
2012-08-20 14:25:18 +02:00
"os/exec"
"strconv"
2012-08-18 02:06:43 +02:00
)
2012-08-20 14:25:18 +02:00
func deleteHandler(coll *mgo.Collection, url string) func(http.ResponseWriter, *http.Request) {
2012-08-18 02:06:43 +02:00
return func(w http.ResponseWriter, r *http.Request) {
2012-08-19 02:29:34 +02:00
sess := GetSession(r)
if sess.User == "" {
2012-08-18 02:06:43 +02:00
http.NotFound(w, r)
return
}
2012-08-20 14:25:18 +02:00
// cutre hack: /delete/ and /delnew/ have the same lenght:
2012-08-19 15:58:37 +02:00
id := bson.ObjectIdHex(r.URL.Path[len("/delete/"):])
2012-08-21 11:25:02 +02:00
books, _, err := GetBook(coll, bson.M{"_id": id})
2012-08-19 15:58:37 +02:00
if err != nil {
2012-08-18 02:06:43 +02:00
http.NotFound(w, r)
return
}
2012-08-19 15:58:37 +02:00
book := books[0]
2012-08-22 10:42:38 +02:00
if book.Cover != "" {
os.RemoveAll(book.Cover[1:])
}
if book.CoverSmall != "" {
os.RemoveAll(book.CoverSmall[1:])
}
2012-08-18 02:06:43 +02:00
os.RemoveAll(book.Path)
coll.Remove(bson.M{"_id": id})
2012-08-19 02:29:34 +02:00
sess.Notify("Removed book!", "The book '"+book.Title+"' it's completly removed", "success")
sess.Save(w, r)
2012-08-20 14:25:18 +02:00
http.Redirect(w, r, url, 307)
2012-08-18 02:06:43 +02:00
}
}
2012-08-19 15:58:37 +02:00
func editHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
id := bson.ObjectIdHex(r.URL.Path[len("/edit/"):])
2012-08-21 11:25:02 +02:00
books, _, err := GetBook(coll, bson.M{"_id": id})
2012-08-19 15:58:37 +02:00
if err != nil {
http.NotFound(w, r)
return
}
var data bookData
data.Book = books[0]
data.S = GetStatus(w, r)
loadTemplate(w, "edit", data)
}
}
func cleanEmptyStr(s []string) []string {
var res []string
for _, v := range s {
if v != "" {
res = append(res, v)
}
}
return res
}
func saveHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(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
}
id := bson.ObjectIdHex(r.URL.Path[len("/save/"):])
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"])
err := coll.Update(bson.M{"_id": id}, bson.M{"$set": bson.M{"title": title,
"publisher": publisher,
"date": date,
"description": description,
"author": author,
"subject": subject,
"lang": lang}})
if err != nil {
http.NotFound(w, r)
return
}
sess.Notify("Book Modified!", "", "success")
sess.Save(w, r)
http.Redirect(w, r, "/book/"+title, 307)
}
}
2012-08-20 14:25:18 +02:00
type newData struct {
S Status
Found int
Books []Book
}
2012-08-19 15:58:37 +02:00
func newHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(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-08-21 11:25:02 +02:00
res, num, _ := GetBook(coll, bson.M{})
2012-08-20 14:25:18 +02:00
var data newData
data.S = GetStatus(w, r)
2012-08-21 11:25:02 +02:00
data.Found = num
2012-08-20 14:25:18 +02:00
data.Books = res
loadTemplate(w, "new", data)
}
}
func storeHandler(newColl, coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
id := bson.ObjectIdHex(r.URL.Path[len("/store/"):])
2012-08-21 11:25:02 +02:00
books, _, err := GetBook(newColl, bson.M{"_id": id})
2012-08-20 14:25:18 +02:00
if err != nil {
http.NotFound(w, r)
return
}
book := books[0]
2012-08-22 19:48:02 +02:00
path := BOOKS_PATH + book.Title[:1] + "/" + book.Title + ".epub"
2012-08-20 14:25:18 +02:00
_, err = os.Stat(path)
for i := 0; err == nil; i++ {
2012-08-22 19:48:02 +02:00
path := BOOKS_PATH + book.Title[:1] + "/" + book.Title + "_" + strconv.Itoa(i) + ".epub"
2012-08-20 14:25:18 +02:00
_, err = os.Stat(path)
}
2012-08-22 19:48:02 +02:00
os.Mkdir(BOOKS_PATH+book.Title[:1], os.ModePerm)
2012-08-20 14:25:18 +02:00
cmd := exec.Command("mv", book.Path, path)
cmd.Run()
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.Save(w, r)
http.Redirect(w, r, "/new/", 307)
2012-08-19 15:58:37 +02:00
}
}