`+book.HTML+lastline, book.Title, "", "")
if err != nil {
- return book, errors.Wrap(err, "can't write into style.css file")
+ return book, errors.Wrap(err, "can't add ebook Section")
}
- // Create the toc.ncx file
- tocNcxWriter, err := epubWriter.Create("OEBPS/toc.ncx")
+ ebook.EmbedImages()
+ err = ebook.Write(tmpFile.Name())
if err != nil {
- return book, errors.Wrap(err, "can't create toc.ncx")
+ return book, errors.Wrap(err, "can't create ebook file")
}
- _, err = tocNcxWriter.Write([]byte(`
-
-
-
-
-
-
-
-
-
- ` + book.Title + `
-
-
-
-
- ` + book.Title + `
-
-
-
-
-`))
- if err != nil {
- return book, errors.Wrap(err, "can't write into toc.ncx file")
- }
-
- // get list of images tag in html
- imageList, _ := GetImages(book.HTML)
- imgRegex := regexp.MustCompile(``)
-
- // Create a set to store unique image URLs
- imageSet := make(map[string]bool)
-
- // Download image in html file and generate new html
- html = book.HTML
- for _, match := range imgRegex.FindAllStringSubmatch(book.HTML, -1) {
- imageURL := match[1]
- if _, ok := imageList[imageURL]; ok && !imageSet[imageURL] {
- // Add the image URL to the set
- imageSet[imageURL] = true
-
- // Download the image
- resp, err := http.Get(imageURL)
- if err != nil {
- log.Fatal(err)
- }
- defer resp.Body.Close()
- // Get the image data
- imageData, err := io.ReadAll(resp.Body)
- if err != nil {
- return book, errors.Wrap(err, "can't get image from the internet")
- }
-
- fileName := fp.Base(imageURL)
- filePath := "images/" + fileName
- imageWriter, err := epubWriter.Create(filePath)
- if err != nil {
- log.Fatal(err)
- }
-
- // Write the image to the file
- _, err = imageWriter.Write(imageData)
- if err != nil {
- return book, errors.Wrap(err, "can't create image file")
- }
- // Replace the image tag with the new downloaded image
- html = strings.ReplaceAll(html, match[0], fmt.Sprintf(``, filePath))
- }
- }
- // Create the content.html file
- contentHtmlWriter, err := epubWriter.Create("OEBPS/content.html")
- if err != nil {
- return book, errors.Wrap(err, "can't create content.xml")
- }
- _, err = contentHtmlWriter.Write([]byte("\n\n\n\t" + book.Title + "\n\t\n\n\n\t
" + book.Title + "
" + "\n\n" + html + "\n" + "\n"))
- if err != nil {
- return book, errors.Wrap(err, "can't write into content.html")
- }
- // close epub and tmpFile
- err = epubWriter.Close()
- if err != nil {
- return book, errors.Wrap(err, "failed to close EPUB writer")
- }
- err = tmpFile.Close()
- if err != nil {
- return book, errors.Wrap(err, "failed to close temporary EPUB file")
- }
- // open temporary file again
- tmpFile, err = os.Open(tmpFile.Name())
- if err != nil {
- return book, errors.Wrap(err, "can't open temporary EPUB file")
- }
defer tmpFile.Close()
- // if everitings go well we start move ebook to dstPath
+
+ // If everything go well we move ebook to dstPath
err = MoveFileToDestination(dstPath, tmpFile)
if err != nil {
return book, errors.Wrap(err, "failed move ebook to destination")
@@ -239,29 +85,3 @@ img {
book.HasEbook = true
return book, nil
}
-
-// function get html and return list of image url inside html file
-func GetImages(html string) (map[string]string, error) {
- // Regular expression to match image tags and their URLs
- imageTagRegex := regexp.MustCompile(``)
-
- // Find all matches in the HTML string
- imageTagMatches := imageTagRegex.FindAllStringSubmatch(html, -1)
- // Create a dictionary to store the image URLs
- images := make(map[string]string)
-
- // Check if there are any matches
- if len(imageTagMatches) == 0 {
- return nil, nil
- }
-
- // Loop through all the matches and add them to the dictionary
- for _, match := range imageTagMatches {
- imageURL := match[1]
- if !strings.HasPrefix(imageURL, "data:image/") {
- images[imageURL] = match[0]
- }
- }
-
- return images, nil
-}
diff --git a/internal/core/ebook_test.go b/internal/core/ebook_test.go
index 25b0eca0d..da7072021 100644
--- a/internal/core/ebook_test.go
+++ b/internal/core/ebook_test.go
@@ -171,74 +171,3 @@ func TestGenerateEbook(t *testing.T) {
})
})
}
-
-// Add more unit tests for other scenarios that missing specialy
-// can't create ebook directory and can't write situatuin
-// writing inside zip file
-// html variable that not export and image download loop
-
-func TestGetImages(t *testing.T) {
- // Test case 1: HTML with no image tags
- html1 := `
Hello, World!
`
- expected1 := make(map[string]string)
- result1, err1 := core.GetImages(html1)
- if err1 != nil {
- t.Errorf("Unexpected error: %v", err1)
- }
- if len(result1) != len(expected1) {
- t.Errorf("Expected %d images, but got %d", len(expected1), len(result1))
- }
-
- // Test case 2: HTML with one image tag
- html2 := ``
- expected2 := map[string]string{"image1.jpg": ""}
- result2, err2 := core.GetImages(html2)
- if err2 != nil {
- t.Errorf("Unexpected error: %v", err2)
- }
- if len(result2) != len(expected2) {
- t.Errorf("Expected %d images, but got %d", len(expected2), len(result2))
- }
- for key, value := range expected2 {
- if result2[key] != value {
- t.Errorf("Expected image URL %s with tag %s, but got %s", key, value, result2[key])
- }
- }
-
- // Test case 3: HTML with multiple image tags
- html3 := ``
- expected3 := map[string]string{
- "image1.jpg": "",
- "image2.jpg": "",
- }
- result3, err3 := core.GetImages(html3)
- if err3 != nil {
- t.Errorf("Unexpected error: %v", err3)
- }
- if len(result3) != len(expected3) {
- t.Errorf("Expected %d images, but got %d", len(expected3), len(result3))
- }
- for key, value := range expected3 {
- if result3[key] != value {
- t.Errorf("Expected image URL %s with tag %s, but got %s", key, value, result3[key])
- }
- }
- // Test case 4: HTML with multiple image tags with duplicayr
- html4 := ``
- expected4 := map[string]string{
- "image1.jpg": "",
- "image2.jpg": "",
- }
- result4, err4 := core.GetImages(html4)
- if err4 != nil {
- t.Errorf("Unexpected error: %v", err4)
- }
- if len(result4) != len(expected4) {
- t.Errorf("Expected %d images, but got %d", len(expected4), len(result4))
- }
- for key, value := range expected4 {
- if result4[key] != value {
- t.Errorf("Expected image URL %s with tag %s, but got %s", key, value, result4[key])
- }
- }
-}