Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure bookmark files are correctly downloaded before deleting current ones #683

Merged
merged 37 commits into from Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7aa37d2
generate ebook get dstPath
Monirzadeh Jul 28, 2023
91937b3
Archive and ebook can recover if download faild
Monirzadeh Jul 28, 2023
c12e3f2
recover thumb if download faild
Monirzadeh Aug 5, 2023
379a3ba
thumb image create just if image processing is sucssesful
Monirzadeh Aug 5, 2023
42afd23
create epub in tmp if it sucssesful copy to destination
Monirzadeh Aug 5, 2023
624a746
archive file create in tmp if it successful move to destination
Monirzadeh Aug 5, 2023
2ec8e5e
move to destination as function
Monirzadeh Aug 5, 2023
44b9a13
update ebook download api and remove .epub from file name
Monirzadeh Aug 5, 2023
622ff9d
report faild item to user
Monirzadeh Aug 5, 2023
bf31c3b
not show dialog if error not happen
Monirzadeh Aug 5, 2023
658c80f
update thumbnail based on last status of bookmark fix #524
Monirzadeh Aug 5, 2023
87b6861
better warning massage
Monirzadeh Aug 6, 2023
c54da26
tmpFile without .epub
Monirzadeh Aug 6, 2023
0a752e1
MoveToDestination change to MoveFileToDestination
Monirzadeh Aug 6, 2023
89c04d6
return .epub
Monirzadeh Aug 6, 2023
2b489ea
log if downloadBookImage return error
Monirzadeh Aug 6, 2023
3588119
fix bug remove imgPath just if download last image be unsuccessful
Monirzadeh Aug 6, 2023
81703cb
update old unit test
Monirzadeh Aug 6, 2023
e56ef8f
add processing.go unit test
Monirzadeh Aug 6, 2023
947eefa
small massage for report failded item to the user
Monirzadeh Aug 6, 2023
757599f
add some more unit test and samplefile
Monirzadeh Aug 7, 2023
d33384a
use sample image in unit test
Monirzadeh Aug 7, 2023
d1a6412
use local sample file and unit test not need internet connection anymore
Monirzadeh Aug 7, 2023
a44caad
update error to user and log that too
Monirzadeh Aug 12, 2023
612c439
add more comment
Monirzadeh Aug 12, 2023
9a42103
update comment
Monirzadeh Aug 12, 2023
55bb071
change variable name parentDir to dstDir
Monirzadeh Aug 14, 2023
3da6206
more simpler error handling
Monirzadeh Aug 14, 2023
1630092
remove unneeded defer
Monirzadeh Aug 14, 2023
afba6ff
remvoe unneeded epubWriter.Close()
Monirzadeh Aug 14, 2023
17f984d
more readable unit test in processing
Monirzadeh Aug 14, 2023
bf95321
more readable unit test for ebooks
Monirzadeh Aug 14, 2023
7f26e3f
delete all defer os.RemoveAll from unit tests
Monirzadeh Aug 14, 2023
9341e4d
Merge branch 'master' into cache-recovery
Monirzadeh Aug 14, 2023
cdf6fab
Better comment
Monirzadeh Aug 20, 2023
587340b
Better Error output
Monirzadeh Aug 20, 2023
3003099
fix err.String() method
Monirzadeh Aug 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Monirzadeh marked this conversation as resolved.
Show resolved Hide resolved
// 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())
Monirzadeh marked this conversation as resolved.
Show resolved Hide resolved

// 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()
Monirzadeh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return book, errors.Wrap(err, "failed to close EPUB writer")
}
err = tmpFile.Close()
Monirzadeh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return book, errors.Wrap(err, "failed to close temporary EPUB file")
}
// open temporary file again
tmpFile, err = os.Open(tmpFile.Name())
fmartingr marked this conversation as resolved.
Show resolved Hide resolved
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