Use title as filename on gridfs
This commit is contained in:
parent
5d2f72c229
commit
fc50ba5fd6
2 changed files with 82 additions and 85 deletions
49
store.go
49
store.go
|
@ -10,55 +10,6 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func ParseFile(id bson.ObjectId) (string, error) {
|
||||
book := map[string]interface{}{}
|
||||
|
||||
e, err := OpenBook(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer e.Close()
|
||||
|
||||
for _, m := range e.MetadataFields() {
|
||||
data, err := e.Metadata(m)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
switch m {
|
||||
case "creator":
|
||||
book["author"] = parseAuthr(data)
|
||||
case "description":
|
||||
book[m] = parseDescription(data)
|
||||
case "subject":
|
||||
book[m] = parseSubject(data)
|
||||
case "date":
|
||||
book[m] = parseDate(data)
|
||||
case "language":
|
||||
book["lang"] = data
|
||||
case "title", "contributor", "publisher":
|
||||
book[m] = cleanStr(strings.Join(data, ", "))
|
||||
case "identifier":
|
||||
attr, _ := e.MetadataAttr(m)
|
||||
for i, d := range data {
|
||||
if attr[i]["scheme"] == "ISBN" {
|
||||
book["isbn"] = d
|
||||
}
|
||||
}
|
||||
default:
|
||||
book[m] = strings.Join(data, ", ")
|
||||
}
|
||||
}
|
||||
title, _ := book["title"].(string)
|
||||
book["file"] = id
|
||||
cover, coverSmall := GetCover(e, title)
|
||||
book["cover"] = cover
|
||||
book["coversmall"] = coverSmall
|
||||
book["keywords"] = keywords(book)
|
||||
|
||||
db.InsertBook(book)
|
||||
return title, nil
|
||||
}
|
||||
|
||||
func OpenBook(id bson.ObjectId) (*epubgo.Epub, error) {
|
||||
fs := db.GetFS(FS_BOOKS)
|
||||
f, err := fs.OpenId(id)
|
||||
|
|
118
upload.go
118
upload.go
|
@ -1,53 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"labix.org/v2/mgo/bson"
|
||||
"bytes"
|
||||
"git.gitorious.org/go-pkg/epubgo.git"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func storeFiles(r *http.Request) ([]bson.ObjectId, error) {
|
||||
r.ParseMultipartForm(20000000)
|
||||
filesForm := r.MultipartForm.File["epub"]
|
||||
ids := make([]bson.ObjectId, 0, len(filesForm))
|
||||
for _, f := range filesForm {
|
||||
log.Println("File uploaded:", f.Filename)
|
||||
file, err := f.Open()
|
||||
if err != nil {
|
||||
return ids, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
id, err := StoreNewFile(f.Filename, file)
|
||||
if err != nil {
|
||||
return ids, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
type uploadData struct {
|
||||
S Status
|
||||
}
|
||||
|
||||
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
sess := GetSession(r)
|
||||
ids, err := storeFiles(r)
|
||||
if err != nil {
|
||||
sess.Notify("Problem uploading!", "Some files were not stored. Try again or contact us if it keeps happening", "error")
|
||||
}
|
||||
|
||||
uploaded := ""
|
||||
for _, id := range ids {
|
||||
title, err := ParseFile(id)
|
||||
r.ParseMultipartForm(20000000)
|
||||
filesForm := r.MultipartForm.File["epub"]
|
||||
for _, f := range filesForm {
|
||||
log.Println("File uploaded:", f.Filename)
|
||||
file, err := f.Open()
|
||||
if err != nil {
|
||||
DeleteFile(id)
|
||||
sess.Notify("Problem uploading!", "The file is not a well formed epub: "+err.Error(), "error")
|
||||
} else {
|
||||
uploaded = uploaded + " '" + title + "'"
|
||||
sess.Notify("Problem uploading!", "The file '" + f.Filename + "' is not a well formed epub: "+err.Error(), "error")
|
||||
continue
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
epub, err := openMultipartEpub(file)
|
||||
if err != nil {
|
||||
sess.Notify("Problem uploading!", "The file '" + f.Filename + "' is not a well formed epub: "+err.Error(), "error")
|
||||
continue
|
||||
}
|
||||
defer epub.Close()
|
||||
|
||||
book := parseFile(epub)
|
||||
title, _ := book["title"].(string)
|
||||
id, err := StoreNewFile(title, file)
|
||||
if err != nil {
|
||||
log.Println("Error storing book (", title, "):", err)
|
||||
continue
|
||||
}
|
||||
|
||||
book["file"] = id
|
||||
db.InsertBook(book)
|
||||
uploaded += " '" + title + "'"
|
||||
}
|
||||
if uploaded != "" {
|
||||
sess.Notify("Upload successful!", "Added the books:"+uploaded+". Thank you for your contribution", "success")
|
||||
|
@ -59,3 +55,53 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||
data.S.Upload = true
|
||||
loadTemplate(w, "upload", data)
|
||||
}
|
||||
|
||||
type uploadData struct {
|
||||
S Status
|
||||
}
|
||||
|
||||
func openMultipartEpub(file multipart.File) (*epubgo.Epub, error) {
|
||||
buff, _ := ioutil.ReadAll(file)
|
||||
reader := bytes.NewReader(buff)
|
||||
return epubgo.Load(reader, int64(len(buff)))
|
||||
}
|
||||
|
||||
func parseFile(epub *epubgo.Epub) map[string]interface{} {
|
||||
book := map[string]interface{}{}
|
||||
for _, m := range epub.MetadataFields() {
|
||||
data, err := epub.Metadata(m)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
switch m {
|
||||
case "creator":
|
||||
book["author"] = parseAuthr(data)
|
||||
case "description":
|
||||
book[m] = parseDescription(data)
|
||||
case "subject":
|
||||
book[m] = parseSubject(data)
|
||||
case "date":
|
||||
book[m] = parseDate(data)
|
||||
case "language":
|
||||
book["lang"] = data
|
||||
case "title", "contributor", "publisher":
|
||||
book[m] = cleanStr(strings.Join(data, ", "))
|
||||
case "identifier":
|
||||
attr, _ := epub.MetadataAttr(m)
|
||||
for i, d := range data {
|
||||
if attr[i]["scheme"] == "ISBN" {
|
||||
book["isbn"] = d
|
||||
}
|
||||
}
|
||||
default:
|
||||
book[m] = strings.Join(data, ", ")
|
||||
}
|
||||
}
|
||||
title, _ := book["title"].(string)
|
||||
book["file"] = nil
|
||||
cover, coverSmall := GetCover(epub, title)
|
||||
book["cover"] = cover
|
||||
book["coversmall"] = coverSmall
|
||||
book["keywords"] = keywords(book)
|
||||
return book
|
||||
}
|
||||
|
|
Reference in a new issue