Skip to content

Commit

Permalink
fix cbz and epub
Browse files Browse the repository at this point in the history
  • Loading branch information
tigeryu8900 committed Dec 2, 2024
1 parent 09757f4 commit cf9e531
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 68 deletions.
8 changes: 5 additions & 3 deletions models/chapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ func (c *Chapter) Manga() *Manga {

for _, rel := range c.Relationships {
if rel.Type == "manga" {
m, err := CastRelationship[Manga](&rel)
c.manga = &Manga{}
a, err := CastRelationship[MangaAttributes](&rel)
if err != nil {
slog.Error("error casting to manga", "error", err)
return nil
}
m.RelData()
c.manga.ID = uuid.MustParse(rel.ID)
c.manga.Attributes = a
c.manga.RelData()

c.manga = &m
return c.manga
}
}
Expand Down
9 changes: 7 additions & 2 deletions shared/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestOptions() {
Host: defaultBind,
Language: "en",
DataSaver: true,
DevApi: true,
DevApi: false,
ExpVars: true,
LogLevel: slog.LevelDebug,
}
Expand Down Expand Up @@ -135,7 +135,12 @@ func loadDotEnv() {
continue
}

os.Setenv(strings.TrimSpace(key), strings.TrimSpace(val))
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
err := os.Setenv(key, val)
if err != nil {
slog.Warn("setting environment variable failed", key, val)
}
}
}

Expand Down
110 changes: 69 additions & 41 deletions shared/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ func QueryAPI[T any](
if err == nil {
slog.InfoContext(ctx, "loading cache of API", "url", queryUrl.String())
} else {
for i := 0; i < 3 && err != nil; i++ {
var req *http.Request
var res *http.Response

for i := 0; i < 3; i++ {
if res != nil {
_ = res.Body.Close()
}

err = QueryAPILimiter.Wait(ctx)
if err != nil {
continue
Expand All @@ -126,33 +133,32 @@ func QueryAPI[T any](
continue
}
}
req, err := makeRequest(ctx, &queryUrl)

req, err = makeRequest(ctx, &queryUrl)
if err != nil {
return out, err
continue
}

req.Header.Set("Accept", "application/json")

res, err := http.DefaultClient.Do(req)
res, err = http.DefaultClient.Do(req)
if err != nil {
return out, err
continue
}
defer res.Body.Close()

slog.InfoContext(ctx, "querying API", "url", queryUrl.String())

req, err2 := makeRequest(ctx, &queryUrl)
if err2 != nil {
err2 = err
req, err = makeRequest(ctx, &queryUrl)
if err != nil {
continue
}
req.Header.Set("Accept", "application/json")

res, err3 := http.DefaultClient.Do(req)
if err3 != nil {
err3 = err
res, err = http.DefaultClient.Do(req)
if err != nil {
res = nil
continue
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
err = fmt.Errorf("upstream error: %s", res.Status)
Expand All @@ -166,9 +172,15 @@ func QueryAPI[T any](
}

err = cache.Set(queryUrl.String(), entry)
if err == nil {
break
if err != nil {
continue
}

break
}

if res != nil {
_ = res.Body.Close()
}
}

Expand All @@ -193,56 +205,72 @@ func QueryImage(
return nil
}

for i := 0; i < 3 && err != nil; i++ {
err = QueryImageLimiter.Wait(ctx)
if err != nil {
continue
}
if limiter != nil {
err = limiter.Wait(ctx)
slog.InfoContext(ctx, "querying image", "url", imgUrl.String())

entry, err := cache.Get(imgUrl.String())

if err == nil {
slog.InfoContext(ctx, "loading cache of image", "url", imgUrl.String())
} else {
var req *http.Request
var res *http.Response

for i := 0; i < 3; i++ {
if res != nil {
_ = res.Body.Close()
}

err = QueryImageLimiter.Wait(ctx)
if err != nil {
continue
}
}
slog.InfoContext(ctx, "querying image", "url", imgUrl.String())

entry, err := cache.Get(imgUrl.String())
if err == nil {
slog.InfoContext(ctx, "loading cache of image", "url", imgUrl.String())
_, err = w.Write(entry)
} else {
req, err := makeRequest(ctx, imgUrl)
if limiter != nil {
err = limiter.Wait(ctx)
if err != nil {
continue
}
}

req, err = makeRequest(ctx, imgUrl)
if err != nil {
return err
continue
}

req.Header.Add("Accept", mime.TypeByExtension(".png"))
req.Header.Add("Accept", mime.TypeByExtension(".jpg"))

res, err := http.DefaultClient.Do(req)
res, err = http.DefaultClient.Do(req)
if err != nil {
return err
res = nil
continue
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return fmt.Errorf("upstream error: %s", res.Status)
}

err = cache.Set(imgUrl.String(), entry)
entry, err = io.ReadAll(res.Body)
if err != nil {
return err
continue
}

_, err = w.Write(entry)
if err == nil {
break
err = cache.Set(imgUrl.String(), entry)
if err != nil {
_ = res.Body.Close()
continue
}

slog.DebugContext(ctx, "finished image download", "url", imgUrl)
break
}

slog.DebugContext(ctx, "finished image download", "url", imgUrl)
if res != nil {
_ = res.Body.Close()
}
}

_, err = w.Write(entry)

return err
}

Expand Down
4 changes: 2 additions & 2 deletions tmpl/formats/cbz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ChapterID = "9a612118-1441-431a-979d-85958fb20cf2"

// We use size to check validity because we can't use hashes due to ModTime
// and it is extremely unlikely that an invalid zip would have exactly the right size
const ChapterSize = 1_173_541
const CBZChapterSize = 1_173_566

func Test_WriteCBZ(t *testing.T) {
shared.TestOptions()
Expand All @@ -30,5 +30,5 @@ func Test_WriteCBZ(t *testing.T) {

err = WriteCBZ(ctx, &c, &buf)
shared.AssertEq(t, err, nil)
shared.AssertEq(t, buf.Len(), ChapterSize)
shared.AssertEq(t, buf.Len(), CBZChapterSize)
}
33 changes: 13 additions & 20 deletions tmpl/formats/epub_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
package formats

import (
"bytes"
"context"
"github.com/rushsteve1/mangadex-opds/shared"
"os"
"testing"

"github.com/google/uuid"
"github.com/rushsteve1/mangadex-opds/models"
)

func Test_WriteEpub(t *testing.T) {
ctx := context.Background()
c := models.Chapter{ID: uuid.MustParse(ChapterID)}
const EPUBChapterSize = 1_188_189

file, err := os.CreateTemp("", "*.epub")
if err != nil {
t.Fatal(err)
}
defer file.Close()
func Test_WriteEpub(t *testing.T) {
shared.TestOptions()

t.Log("temp file created at: " + file.Name())
ctx := context.Background()

err = WriteEpub(ctx, &c, file)
if err != nil {
t.Fatal(err)
}
c, err := models.FetchChapter(ctx, uuid.MustParse(ChapterID), nil)
shared.AssertEq(t, err, nil)

stats, err := file.Stat()
if err != nil {
t.Fatal(err)
}
buf := bytes.Buffer{}

if stats.Size() == 0 {
t.Fatal("size zero")
}
err = os.Chdir("../../")
err = WriteEpub(ctx, &c, &buf)
shared.AssertEq(t, err, nil)
shared.AssertEq(t, buf.Len(), EPUBChapterSize)
}

0 comments on commit cf9e531

Please sign in to comment.