Example #1
0
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)
}
Example #2
0
func storeImg(img io.Reader, id string, store *storage.Store) bool {
	/* open the files */
	fBig, err := store.Create(id, COVER_FILE)
	if err != nil {
		log.Error("Error creating cover ", id, ": ", err.Error())
		return false
	}
	defer fBig.Close()

	fSmall, err := store.Create(id, COVER_SMALL_FILE)
	if err != nil {
		log.Error("Error creating small cover ", id, ": ", err.Error())
		return false
	}
	defer fSmall.Close()

	/* resize img */
	var img2 bytes.Buffer
	img1 := io.TeeReader(img, &img2)
	jpgOptions := jpeg.Options{IMG_QUALITY}
	imgResized, err := resizeImg(img1, IMG_WIDTH_BIG)
	if err != nil {
		log.Error("Error resizing big image: ", err.Error())
		return false
	}
	err = jpeg.Encode(fBig, imgResized, &jpgOptions)
	if err != nil {
		log.Error("Error encoding big image: ", err.Error())
		return false
	}
	imgSmallResized, err := resizeImg(&img2, IMG_WIDTH_SMALL)
	if err != nil {
		log.Error("Error resizing small image: ", err.Error())
		return false
	}
	err = jpeg.Encode(fSmall, imgSmallResized, &jpgOptions)
	if err != nil {
		log.Error("Error encoding small image: ", err.Error())
		return false
	}
	return true
}