func processFile(req uploadRequest, db *database.DB, store *storage.Store) { defer req.file.Close() epub, err := openMultipartEpub(req.file) if err != nil { log.Warn("Not valid epub uploaded file ", req.filename, ": ", err) return } defer epub.Close() book, id := parseFile(epub, store) req.file.Seek(0, 0) size, err := store.Store(id, req.file, EPUB_FILE) if err != nil { log.Error("Error storing book (", id, "): ", err) return } book["filesize"] = size err = db.AddBook(book) if err != nil { log.Error("Error storing metadata (", id, "): ", err) return } log.Info("File uploaded: ", req.filename) }
func getNews(num int, days int, db *database.DB) []newsEntry { dbnews, _ := db.GetNews(num, days) news := make([]newsEntry, len(dbnews)) for i, n := range dbnews { news[i].Text = n.Text news[i].Date = n.Date.Format("Jan 2, 2006") } return news }
func GetSession(r *http.Request, db *database.DB) (s *Session) { s = new(Session) var err error s.S, err = sesStore.Get(r, "session") if err == nil && !s.S.IsNew { s.User, _ = s.S.Values["user"].(string) s.Role = db.User(s.User).Role() } if s.S.IsNew { s.S.Values["id"] = hex.EncodeToString(securecookie.GenerateRandomKey(16)) } return }
func getVisits(funcLabel func(time.Time) string, db *database.DB, visitType database.VisitType) []visitData { var visits []visitData visit, err := db.GetVisits(visitType) if err != nil { log.Warn("GetVisits error (", visitType, "): ", err) } for _, v := range visit { var elem visitData elem.Label = funcLabel(v.Date.UTC()) elem.Count = v.Count visits = append(visits, elem) } return visits }
func uploadWorker(database *database.DB, store *storage.Store) { db := database.Copy() defer db.Close() for req := range uploadChannel { processFile(req, db, store) } }