Skip to content

Commit

Permalink
fix: Ensure bookmark files are correctly downloaded before deleting c…
Browse files Browse the repository at this point in the history
…urrent ones (#683)

* generate ebook get dstPath

* Archive and ebook can recover if download faild

* recover thumb if download faild

* thumb image create just if image processing is sucssesful

* create epub in tmp if it sucssesful copy to destination

* archive file create in tmp if it successful move to destination

* move to destination as function

* update ebook download api and remove .epub from file name

* report faild item to user

* not show dialog if error not happen

* update thumbnail based on last status of bookmark fix #524

* better warning massage

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>

* tmpFile without .epub

* MoveToDestination change to MoveFileToDestination

* return .epub

* log if downloadBookImage return error

* fix bug remove imgPath just if download last image be unsuccessful

* update old unit test

* add processing.go unit test

* small massage for report failded item to the user

* add some more unit test and samplefile

* use sample image in unit test

* use local sample file and unit test not need internet connection anymore

* update error to user and log that too

* add more comment

* update comment

* change variable name parentDir to dstDir

* more simpler error handling

* remove unneeded defer

* remvoe unneeded epubWriter.Close()

* more readable unit test in processing

* more readable unit test for ebooks

* delete all defer os.RemoveAll from unit tests

* Better comment

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>

* Better Error output

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>

* fix err.String() method

---------

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>
  • Loading branch information
Monirzadeh and fmartingr committed Aug 20, 2023
1 parent 8b015a3 commit f4817cb
Show file tree
Hide file tree
Showing 11 changed files with 682 additions and 267 deletions.
55 changes: 34 additions & 21 deletions internal/core/ebook.go
Expand Up @@ -16,7 +16,10 @@ import (
"github.com/pkg/errors"
)

func GenerateEbook(req ProcessRequest) (book model.Bookmark, err error) {
// GenerateEbook receives a `ProcessRequest` and generates an ebook file in the destination path specified.
// The destination path `dstPath` should include file name with ".epub" extension
// The bookmark model will be used to update the UI based on whether this function is successful or not.
func GenerateEbook(req ProcessRequest, dstPath string) (book model.Bookmark, err error) {
// variable for store generated html code
var html string

Expand All @@ -27,6 +30,7 @@ func GenerateEbook(req ProcessRequest) (book model.Bookmark, err error) {
return book, errors.New("bookmark ID is not valid")
}

// get current state of bookmark
// cheak archive and thumb
strID := strconv.Itoa(book.ID)

Expand All @@ -40,35 +44,23 @@ func GenerateEbook(req ProcessRequest) (book model.Bookmark, err error) {
if _, err := os.Stat(archivePath); err == nil {
book.HasArchive = true
}
ebookfile := fp.Join(req.DataDir, "ebook", fmt.Sprintf("%d.epub", book.ID))
// if epub exist finish prosess else continue
if _, err := os.Stat(ebookfile); err == nil {
book.HasEbook = true
return book, nil
}

// this function create ebook from reader mode of bookmark so
// we can't create ebook from PDF so we return error here if bookmark is a pdf
contentType := req.ContentType
if strings.Contains(contentType, "application/pdf") {
return book, errors.New("can't create ebook for pdf")
}

ebookDir := fp.Join(req.DataDir, "ebook")
// check if directory not exsist create that
if _, err := os.Stat(ebookDir); os.IsNotExist(err) {
err := os.MkdirAll(ebookDir, model.DataDirPerm)
if err != nil {
return book, errors.Wrap(err, "can't create ebook directory")
}
}
// create epub file
epubFile, err := os.Create(ebookfile)
// create temporary epub file
tmpFile, err := os.CreateTemp("", "ebook")
if err != nil {
return book, errors.Wrap(err, "can't create ebook")
return book, errors.Wrap(err, "can't create temporary EPUB file")
}
defer epubFile.Close()
defer os.Remove(tmpFile.Name())

// Create zip archive
epubWriter := zip.NewWriter(epubFile)
defer epubWriter.Close()
epubWriter := zip.NewWriter(tmpFile)

// Create the mimetype file
mimetypeWriter, err := epubWriter.Create("mimetype")
Expand Down Expand Up @@ -223,6 +215,27 @@ img {
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
err = MoveFileToDestination(dstPath, tmpFile)
if err != nil {
return book, errors.Wrap(err, "failed move ebook to destination")
}

book.HasEbook = true
return book, nil
}
Expand Down

0 comments on commit f4817cb

Please sign in to comment.