Esempio n. 1
0
func (p *Engine) book(c *gin.Context) (*epub.Book, error) {
	var book Book
	if err := p.Db.Where("id = ?", c.Param("id")).First(&book).Error; err != nil {
		return nil, err
	}
	return epub.Open(fmt.Sprintf("%s/%s", booksROOT, book.Name))

}
Esempio n. 2
0
func (p *Engine) _scanBooks() error {
	const sep = ","
	const ext = ".epub"

	var books []Book
	err := filepath.Walk(booksROOT, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.Mode().IsRegular() && filepath.Ext(info.Name()) == ext {
			p.Logger.Infof("find book %s", path)
			bk, err := epub.Open(path)
			if err != nil {
				return err
			}
			defer bk.Close()

			var version []string
			for _, d := range bk.Opf.Metadata.Date {
				version = append(version, d.Data)
			}
			var creator []string
			for _, c := range bk.Opf.Metadata.Creator {
				creator = append(creator, c.Data)
			}

			book := Book{
				Name:      path[len(booksROOT)+1 : len(path)],
				Title:     strings.Join(bk.Opf.Metadata.Title, sep),
				Subject:   strings.Join(bk.Opf.Metadata.Subject, sep),
				Publisher: strings.Join(bk.Opf.Metadata.Publisher, sep),
				Creator:   strings.Join(creator, sep),
				Version:   strings.Join(version, sep),
			}

			books = append(books, book)
		}
		return nil
	})

	if err == nil {
		for _, b := range books {
			var bk Book
			if err = p.Db.Where("name = ?", b.Name).Find(&bk).Error; err == nil {
				err = p.Db.Model(&bk).Updates(b).Error
			} else {
				err = p.Db.Create(&b).Error
			}
			if err != nil {
				break
			}
		}

	}

	return err
}
Esempio n. 3
0
func validateEpubLink(path string) (bool, *list.List) {
	bk, err := epub.Open(path)
	if err != nil {
		return false, list.New()
	}
	defer bk.Close()
	manifest := bk.Opf.Manifest
	errors := list.New()
	var result bool = true
	for i := 0; i < len(manifest); i++ {
		_, e1 := bk.Open(manifest[i].Href)
		if e1 != nil {
			result = false
			errors.PushBack("\tmissing resource " + manifest[i].Href)
		}
	}
	ncx := bk.Ncx
	ps := ncx.Points
	files := bk.Files()

	for i := 0; i < len(ps); i++ {
		ps = append(ps, ps[i].Points...)
	}
	//fmt.Println(len(ps))
	for i := 0; i < len(ps); i++ {
		src := ps[i].Content.Src

		index := strings.Index(src, "#")
		if index < 0 {
			continue
		}
		anchor := Substr(ps[i].Content.Src, index+1, len(ps[i].Content.Src)+1)
		targetResource := Substr(ps[i].Content.Src, 0, index)
		chptExistd := false
		for j := 0; j < len(files); j++ {
			if strings.Index(files[j], targetResource) >= 0 {

				chptExistd = true
				r, e2 := bk.Open(targetResource)
				if e2 != nil {
					result = false
					errors.PushBack("\tcan not open resource: " + files[j] + "  err : " + e2.Error())
					continue
				}
				defer r.Close()
				resourceContent, e3 := ioutil.ReadAll(r)
				if e3 != nil {
					result = false
					errors.PushBack("\t " + e3.Error())
					continue
				}

				if strings.Index(string(resourceContent), anchor) >= 0 {
					continue
				} else {

					result = false
					errors.PushBack("\tcan not find link: " + src)
				}
				//fmt.Println(string(resourceContent))
			}
		}
		if !chptExistd {
			result = false
			//errors.PushBack("\tmissing html resource: " + targetResource)
		}
	}
	return result, errors
}