Skip to content

Commit 04f2a61

Browse files
committed
Add workaround for bad formatted inpx
1 parent 6b1323d commit 04f2a61

5 files changed

Lines changed: 77 additions & 4 deletions

File tree

internal/model/book.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ func (b *Book) PublishedAt() string {
9090
return b.PubDate.Format("2006-01-02")
9191
}
9292

93+
func (f *File) IsArchived() bool {
94+
return f.Folder == "" || strings.HasSuffix(f.Folder, ".zip")
95+
}
96+
97+
func (f *File) ArchivePath() string {
98+
if strings.HasSuffix(f.Folder, ".zip") {
99+
return f.Folder
100+
}
101+
102+
return fmt.Sprintf("%s.zip", f.Archive)
103+
}
104+
93105
func (a Author) String() string {
94106
var name string
95107
if a.FirstName == "" && a.MiddleName == "" {

internal/server/download.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (h *DownloadHandler) Download(w http.ResponseWriter, r *http.Request) {
5151
return
5252
}
5353

54-
if book.File.Folder == "" {
54+
if book.File.IsArchived() {
5555
data, err := h.getFileFromArchive(book)
5656
if err != nil {
5757
notFound(w, id)
@@ -117,7 +117,7 @@ func (h *DownloadHandler) DownloadConverted(w http.ResponseWriter, r *http.Reque
117117
}
118118

119119
var filename string
120-
if book.File.Folder == "" {
120+
if book.File.IsArchived() {
121121
data, err := h.getFileFromArchive(book)
122122
if err != nil {
123123
notFound(w, id)
@@ -189,7 +189,7 @@ func (h *DownloadHandler) getDirectFilePath(book *model.Book) (string, error) {
189189
}
190190

191191
func (h *DownloadHandler) getFileFromArchive(book *model.Book) ([]byte, error) {
192-
archivePath := filepath.Join(h.cfg.LibraryPath, book.File.Archive+".zip")
192+
archivePath := filepath.Join(h.cfg.LibraryPath, book.File.ArchivePath())
193193
zf, err := zip.OpenReader(archivePath)
194194
if err != nil {
195195
log.Printf("Can't open archive `%s` (id: %s) not found: %v", archivePath, book.LibId, err)

pkg/inpx/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func (p *Parser) getFileByName(name string) (io.ReadCloser, error) {
374374
// Read line from reader and returns it trimmed.
375375
func readCleanString(r *bufio.Reader) (string, error) {
376376
s, err := r.ReadString('\n')
377-
if err != nil {
377+
if err != nil && err != io.EOF {
378378
return "", err
379379
}
380380

pkg/inpx/parser_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,64 @@ func TestFlibustaRev20(t *testing.T) {
110110

111111
assert.Equal(53, bookCount)
112112
}
113+
114+
func TestFlibustaAllLocal20250202(t *testing.T) {
115+
assert := assert.New(t)
116+
collection, err := Open("testdata/flibusta_all_local-2025-02-02.inpx")
117+
if err != nil {
118+
t.Fatalf(`collection is not opened: %v`, err)
119+
}
120+
defer collection.Close()
121+
122+
assert.Equal("Flibusta Offline 2 February 2025", collection.Name)
123+
assert.Equal(65537, collection.Id)
124+
assert.Equal("Flibusta. A local collection. Total: 771171 books", collection.Comment)
125+
126+
assert.Equal("20250202", collection.Version)
127+
128+
bookCount := 0
129+
for book := range collection.Stream() {
130+
bookCount = bookCount + 1
131+
132+
if book.LibId == 754865 {
133+
assert.Equal("Книги фантастики українських видавництв за 1960-1991 рр.", book.Title)
134+
assert.Equal("", book.Series)
135+
assert.Equal(time.Date(2023, 11, 01, 0, 0, 0, 0, time.UTC), book.PublishedDate)
136+
assert.Equal([]string{"reference", "sf_etc"}, book.Genres)
137+
assert.Equal("754865", book.File.Name)
138+
assert.Equal("rtf", book.File.Ext)
139+
assert.Equal(1436043, book.File.Size)
140+
assert.Equal("f.usr-754754-759835.zip", book.File.Folder)
141+
assert.Equal("uk", book.Language)
142+
authors := []Author{
143+
{
144+
LastName: "Левченко",
145+
FirstName: "Александр",
146+
MiddleName: "Николаевич",
147+
},
148+
}
149+
assert.Equal(authors, book.Authors)
150+
}
151+
if book.LibId == 362143 {
152+
assert.Equal("Сан Феличе", book.Title)
153+
assert.Equal("", book.Series)
154+
assert.Equal(time.Date(2014, 05, 03, 0, 0, 0, 0, time.UTC), book.PublishedDate)
155+
assert.Equal([]string{"prose_history", "adventure", "sci_culture", "design"}, book.Genres)
156+
assert.Equal("362143", book.File.Name)
157+
assert.Equal("fb2", book.File.Ext)
158+
assert.Equal(2607193, book.File.Size)
159+
assert.Equal("f.fb2-361575-365133.zip", book.File.Folder)
160+
assert.Equal("bg", book.Language)
161+
authors := []Author{
162+
{
163+
LastName: "Дюма",
164+
FirstName: "Александр",
165+
MiddleName: "",
166+
},
167+
}
168+
assert.Equal(authors, book.Authors)
169+
}
170+
}
171+
172+
assert.Equal(3, bookCount)
173+
}
4.07 KB
Binary file not shown.

0 commit comments

Comments
 (0)