Store or remove all the books at once
This commit is contained in:
parent
de89cbc931
commit
f3882881ce
2 changed files with 70 additions and 36 deletions
92
admin.go
92
admin.go
|
@ -16,28 +16,41 @@ func deleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// cutre hack: /delete/ and /delnew/ have the same lenght:
|
var titles []string
|
||||||
id := bson.ObjectIdHex(r.URL.Path[len("/delete/"):])
|
var isNew bool
|
||||||
books, _, err := db.GetBooks(bson.M{"_id": id})
|
ids:= strings.Split(r.URL.Path[len("/delete/"):], "/")
|
||||||
if err != nil {
|
for _, idStr := range ids {
|
||||||
http.NotFound(w, r)
|
if idStr == "" {
|
||||||
return
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
id := bson.ObjectIdHex(idStr)
|
||||||
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
||||||
|
if err != nil {
|
||||||
|
sess.Notify("Book not found!", "The book with id '"+idStr+"' is not there", "error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
book := books[0]
|
||||||
|
if book.Cover != "" {
|
||||||
|
os.RemoveAll(book.Cover[1:])
|
||||||
|
}
|
||||||
|
if book.CoverSmall != "" {
|
||||||
|
os.RemoveAll(book.CoverSmall[1:])
|
||||||
|
}
|
||||||
|
os.RemoveAll(book.Path)
|
||||||
|
db.RemoveBook(id)
|
||||||
|
|
||||||
|
if ! book.Active {
|
||||||
|
isNew = true
|
||||||
|
}
|
||||||
|
titles = append(titles, book.Title)
|
||||||
}
|
}
|
||||||
book := books[0]
|
sess.Notify("Removed books!", "The books "+ strings.Join(titles, ", ") +" are completly removed", "success")
|
||||||
if book.Cover != "" {
|
|
||||||
os.RemoveAll(book.Cover[1:])
|
|
||||||
}
|
|
||||||
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)
|
sess.Save(w, r)
|
||||||
if book.Active {
|
if isNew {
|
||||||
http.Redirect(w, r, "/", 307)
|
|
||||||
} else {
|
|
||||||
http.Redirect(w, r, "/new/", 307)
|
http.Redirect(w, r, "/new/", 307)
|
||||||
|
} else {
|
||||||
|
http.Redirect(w, r, "/", 307)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,23 +181,32 @@ func storeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id := bson.ObjectIdHex(r.URL.Path[len("/store/"):])
|
var titles []string
|
||||||
books, _, err := db.GetBooks(bson.M{"_id": id})
|
ids := strings.Split(r.URL.Path[len("/store/"):], "/")
|
||||||
if err != nil {
|
for _, idStr := range ids {
|
||||||
http.NotFound(w, r)
|
if idStr == "" {
|
||||||
return
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
id := bson.ObjectIdHex(idStr)
|
||||||
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
||||||
|
if err != nil {
|
||||||
|
sess.Notify("Book not found!", "The book with id '"+idStr+"' is not there", "error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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})
|
||||||
|
titles = append(titles, book.Title)
|
||||||
}
|
}
|
||||||
book := books[0]
|
sess.Notify("Store books!", "The books '"+ strings.Join(titles, ", ") +"' are stored for public download", "success")
|
||||||
|
|
||||||
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)
|
sess.Save(w, r)
|
||||||
http.Redirect(w, r, "/new/", 307)
|
http.Redirect(w, r, "/new/", 307)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
{{template "header.html" .S}}
|
{{template "header.html" .S}}
|
||||||
|
|
||||||
<p class="centered">Found {{.Found}} books.</p>
|
{{if .Books}}
|
||||||
|
<div class="centered btn-group">
|
||||||
|
<a href="/store/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-success"><i class="icon-ok"></i> Save All</a>
|
||||||
|
<a href="/delete/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-danger"><i class="icon-remove"></i> Delete All</a>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
|
<p class="centered">Found {{.Found}} books.<br /></p>
|
||||||
|
|
||||||
{{range .Books}}
|
{{range .Books}}
|
||||||
{{$titleFound := .TitleFound}}
|
{{$titleFound := .TitleFound}}
|
||||||
|
@ -35,5 +41,11 @@
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
{{if .Books}}
|
||||||
|
<div class="centered btn-group">
|
||||||
|
<a href="/store/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-success"><i class="icon-ok"></i> Save All</a>
|
||||||
|
<a href="/delete/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-danger"><i class="icon-remove"></i> Delete All</a>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{template "footer.html"}}
|
{{template "footer.html"}}
|
||||||
|
|
Reference in a new issue