Skip to content

Commit

Permalink
feat: add new split routes (#31)
Browse files Browse the repository at this point in the history
* feat: add new split routes

* fix: linter issues fixes

---------

Co-authored-by: Alexander Pikeev <[email protected]>
  • Loading branch information
starwalkn and Alexander Pikeev authored Dec 23, 2024
1 parent 31e770c commit 5a9aa6b
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GOLANG_VERSION=1.23.2
GOTENBERG_VERSION=8.14.1
GOTENBERG_VERSION=edge
GOLANGCI_LINT_VERSION=1.61.0

REPO=starwalkn/gotenberg-go-client/v8
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

|Gotenberg version | Client version |
|:----------------:|:----------------------------------------------------------------------------------------------------------:|
|`8.x` **(actual)**| `8.7.4` **(actual)** <br/> |
|`8.x` **(actual)**| `8.8.0` **(actual)** <br/> |
|`7.x` | `<= 8.5.0` |
|`6.x` | <a href="https://github.com/thecodingmachine/gotenberg-go-client">thecodingmachine/gotenberg-go-client</a> |

Expand Down
2 changes: 1 addition & 1 deletion build/tests/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ARG GOTENBERG_VERSION

FROM golang:${GOLANG_VERSION:-1.23.2}-alpine AS golang

FROM gotenberg/gotenberg:${GOTENBERG_VERSION:-8.14.1}
FROM gotenberg/gotenberg:${GOTENBERG_VERSION:-8.15.0}

USER root

Expand Down
6 changes: 6 additions & 0 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ const (
fieldMergePdfA formField = "pdfa"
fieldMergePdfUA formField = "pdfua"
)

const (
fieldSplitMode = "splitMode"
fieldSplitSpan = "splitSpan"
fieldSplitUnify = "splitUnify"
)
4 changes: 4 additions & 0 deletions html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func TestHTMLScreenshot(t *testing.T) {
err = c.StoreScreenshot(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)

isValidJPEG, err := test.IsValidJPEG(dest)
require.NoError(t, err)
assert.True(t, isValidJPEG)
}

func TestHTMLPdfA(t *testing.T) {
Expand Down
26 changes: 7 additions & 19 deletions libreoffice_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gotenberg

import (
"archive/zip"
"context"
"fmt"
"net/http"
Expand Down Expand Up @@ -114,25 +113,10 @@ func TestLibreOfficeMultipleWithoutMerge(t *testing.T) {
require.NoError(t, err)
assert.FileExists(t, dest)

zipReader, err := zip.OpenReader(dest)
require.NoError(t, err)

expectedFiles := map[string]bool{
"document1.docx.pdf": false,
"document2.docx.pdf": false,
}

for _, file := range zipReader.File {
if _, ok := expectedFiles[file.Name]; ok {
expectedFiles[file.Name] = true
}
}

for fileName, found := range expectedFiles {
assert.True(t, found, "File %s not found in zip", fileName)
}
err = zipReader.Close()
count, isPDFs, err := test.IsPDFsInArchive(t, dest)
require.NoError(t, err)
assert.Equal(t, 2, count)
assert.True(t, isPDFs)
}

func TestLibreOfficeMultipleWithMerge(t *testing.T) {
Expand All @@ -156,6 +140,10 @@ func TestLibreOfficeMultipleWithMerge(t *testing.T) {
isPDF, err := test.IsPDF(dest)
require.NoError(t, err)
assert.True(t, isPDF)

count, err := test.GetPDFPageCount(dest)
require.NoError(t, err)
assert.Equal(t, 4, count)
}

func TestLibreOfficePdfA(t *testing.T) {
Expand Down
41 changes: 12 additions & 29 deletions markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,6 @@ func TestMarkdown(t *testing.T) {
})
require.NoError(t, err)

dirPath := t.TempDir()
dest := fmt.Sprintf("%s/foo.pdf", dirPath)
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)
}

func TestMarkdownComplete(t *testing.T) {
c, err := NewClient("http://localhost:3000", http.DefaultClient)
require.NoError(t, err)

index, err := document.FromPath("index.html", test.MarkdownTestFilePath(t, "index.html"))
require.NoError(t, err)
markdown1, err := document.FromPath("paragraph1.md", test.MarkdownTestFilePath(t, "paragraph1.md"))
require.NoError(t, err)
markdown2, err := document.FromPath("paragraph2.md", test.MarkdownTestFilePath(t, "paragraph2.md"))
require.NoError(t, err)
markdown3, err := document.FromPath("paragraph3.md", test.MarkdownTestFilePath(t, "paragraph3.md"))
require.NoError(t, err)
req := NewMarkdownRequest(index, markdown1, markdown2, markdown3)
req.Trace("testMarkdownComplete")
req.UseBasicAuth("foo", "bar")

err = req.ExtraHTTPHeaders(map[string]string{
"X-Header": "Value",
"X-Scoped-Header": `value;scope=https?:\\/\\/([a-zA-Z0-9-]+\\.)*domain\\.com\\/.*`,
})
require.NoError(t, err)

header, err := document.FromPath("header.html", test.MarkdownTestFilePath(t, "header.html"))
require.NoError(t, err)
req.Header(header)
Expand All @@ -87,6 +58,14 @@ func TestMarkdownComplete(t *testing.T) {
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)

isPDF, err := test.IsPDF(dest)
require.NoError(t, err)
assert.True(t, isPDF)

count, err := test.GetPDFPageCount(dest)
require.NoError(t, err)
assert.Equal(t, 2, count)
}

func TestMarkdownPageRanges(t *testing.T) {
Expand Down Expand Up @@ -146,4 +125,8 @@ func TestMarkdownScreenshot(t *testing.T) {
err = c.StoreScreenshot(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)

isValidJPEG, err := test.IsValidJPEG(dest)
require.NoError(t, err)
assert.True(t, isValidJPEG)
}
8 changes: 8 additions & 0 deletions pdfengines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ func TestMerge(t *testing.T) {
err = c.Store(context.Background(), req, dest)
require.NoError(t, err)
assert.FileExists(t, dest)

isPDF, err := test.IsPDF(dest)
require.NoError(t, err)
assert.True(t, isPDF)

count, err := test.GetPDFPageCount(dest)
require.NoError(t, err)
assert.Equal(t, 6, count)
}
42 changes: 42 additions & 0 deletions split_intervals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gotenberg

import (
"strconv"

"github.com/starwalkn/gotenberg-go-client/v8/document"
)

type SplitIntervalsRequest struct {
pdfs []document.Document

*baseRequest
}

func NewSplitIntervalsRequest(pdfs ...document.Document) *SplitIntervalsRequest {
br := newBaseRequest()
br.fields[fieldSplitMode] = "intervals"

return &SplitIntervalsRequest{
pdfs: pdfs,
baseRequest: br,
}
}

func (req *SplitIntervalsRequest) endpoint() string {
return "/forms/pdfengines/split"
}

func (req *SplitIntervalsRequest) formDocuments() map[string]document.Document {
files := make(map[string]document.Document)

for _, pdf := range req.pdfs {
files[pdf.Filename()] = pdf
}

return files
}

// SplitSpan sets the interval for split.
func (req *SplitIntervalsRequest) SplitSpan(span int) {
req.fields[fieldSplitSpan] = strconv.Itoa(span)
}
46 changes: 46 additions & 0 deletions split_pages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gotenberg

import (
"strconv"

"github.com/starwalkn/gotenberg-go-client/v8/document"
)

type SplitPagesRequest struct {
pdfs []document.Document

*baseRequest
}

func NewSplitPagesRequest(pdfs ...document.Document) *SplitPagesRequest {
br := newBaseRequest()
br.fields[fieldSplitMode] = "pages"

return &SplitPagesRequest{
pdfs: pdfs,
baseRequest: br,
}
}

func (req *SplitPagesRequest) endpoint() string {
return "/forms/pdfengines/split"
}

func (req *SplitPagesRequest) formDocuments() map[string]document.Document {
files := make(map[string]document.Document)

for _, pdf := range req.pdfs {
files[pdf.Filename()] = pdf
}

return files
}

// SplitSpan sets the interval for split.
func (req *SplitPagesRequest) SplitSpan(span string) {
req.fields[fieldSplitSpan] = span
}

func (req *SplitPagesRequest) SplitUnify(val bool) {
req.fields[fieldSplitUnify] = strconv.FormatBool(val)
}
Loading

0 comments on commit 5a9aa6b

Please sign in to comment.