@@ -2,6 +2,7 @@ package parser
2
2
3
3
import (
4
4
"errors"
5
+ "path"
5
6
"path/filepath"
6
7
"strconv"
7
8
"strings"
@@ -34,16 +35,19 @@ func EpubParser(filePath string) (models.Publication, error) {
34
35
fileExt := filepath .Ext (filePath )
35
36
if fileExt == "" {
36
37
book , err = epub .OpenDir (filePath )
38
+ if err != nil {
39
+ return models.Publication {}, errors .New ("can't open or parse epub file with err : " + err .Error ())
40
+ }
37
41
publication .Internal = append (publication .Internal , models.Internal {Name : "type" , Value : "epub_dir" })
38
42
publication .Internal = append (publication .Internal , models.Internal {Name : "basepath" , Value : filePath })
39
43
} else {
40
44
book , err = epub .Open (filePath )
45
+ if err != nil {
46
+ return models.Publication {}, errors .New ("can't open or parse epub file with err : " + err .Error ())
47
+ }
41
48
publication .Internal = append (publication .Internal , models.Internal {Name : "type" , Value : "epub" })
42
49
publication .Internal = append (publication .Internal , models.Internal {Name : "epub" , Value : book .ZipReader ()})
43
50
}
44
- if err != nil {
45
- return models.Publication {}, errors .New ("can't open or parse epub file with err : " + err .Error ())
46
- }
47
51
48
52
if book .Container .Rootfile .Version != "" {
49
53
epubVersion = book .Container .Rootfile .Version
@@ -106,6 +110,8 @@ func EpubParser(filePath string) (models.Publication, error) {
106
110
}
107
111
108
112
fillCalibreSerieInfo (& publication , book )
113
+ fillEncryptionInfo (& publication , book )
114
+
109
115
return publication , nil
110
116
}
111
117
@@ -558,3 +564,33 @@ func fillCalibreSerieInfo(publication *models.Publication, book *epub.Book) {
558
564
}
559
565
560
566
}
567
+
568
+ func fillEncryptionInfo (publication * models.Publication , book * epub.Book ) {
569
+
570
+ for _ , encInfo := range book .Encryption .EncryptedData {
571
+ resURI := encInfo .CipherData .CipherReference .URI
572
+ for i , l := range publication .Resources {
573
+ if resURI == FilePath (* publication , l .Href ) {
574
+ publication .Resources [i ].CryptAlgorithm = encInfo .EncryptionMethod .Algorithm
575
+ }
576
+ }
577
+ for i , l := range publication .Spine {
578
+ if resURI == FilePath (* publication , l .Href ) {
579
+ publication .Spine [i ].CryptAlgorithm = encInfo .EncryptionMethod .Algorithm
580
+ }
581
+ }
582
+ }
583
+ }
584
+
585
+ // FilePath return the complete path for the ressource
586
+ func FilePath (publication models.Publication , publicationResource string ) string {
587
+ var rootFile string
588
+
589
+ for _ , data := range publication .Internal {
590
+ if data .Name == "rootfile" {
591
+ rootFile = data .Value .(string )
592
+ }
593
+ }
594
+
595
+ return path .Join (path .Dir (rootFile ), publicationResource )
596
+ }
0 commit comments