Add hashes for each book
Check if the uploaded book is already in the library
This commit is contained in:
parent
154867c50c
commit
a1ee320eba
4 changed files with 41 additions and 5 deletions
|
@ -1,13 +1,14 @@
|
|||
package trantor
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
log "github.com/cihub/seelog"
|
||||
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
|
||||
"github.com/meskio/epubgo"
|
||||
"gitlab.com/trantor/trantor/lib/database"
|
||||
|
@ -41,6 +42,19 @@ func uploadWorker(database database.DB, store storage.Store) {
|
|||
func (req uploadRequest) processFile(db database.DB, store storage.Store) {
|
||||
defer req.file.Close()
|
||||
|
||||
h := sha256.New()
|
||||
if _, err := io.Copy(h, req.file); err != nil {
|
||||
log.Warn("Can't read uploaded file ", req.header.Filename, ": ", err)
|
||||
db.UpdateSubmission(req.id, "There was a problem with the uploaded book, try again later.", nil)
|
||||
return
|
||||
}
|
||||
fileHash := h.Sum(nil)
|
||||
if db.ExistsBookHash(fileHash) {
|
||||
log.Info("Uploaded file ", req.header.Filename, " already in the library.")
|
||||
db.UpdateSubmission(req.id, "The uploaded book is already in the library (it could be in the moderation queue)", nil)
|
||||
return
|
||||
}
|
||||
|
||||
epub, err := epubgo.Load(req.file, req.header.Size)
|
||||
if err != nil {
|
||||
log.Warn("Not valid epub uploaded file ", req.header.Filename, ": ", err)
|
||||
|
@ -61,6 +75,7 @@ func (req uploadRequest) processFile(db database.DB, store storage.Store) {
|
|||
}
|
||||
|
||||
book.FileSize = int(size)
|
||||
book.FileHash = fileHash
|
||||
book.Cover = GetCover(epub, book.ID, store)
|
||||
|
||||
err = db.AddBook(book)
|
||||
|
|
Reference in a new issue