Skip to content

Commit

Permalink
Fix #815
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Mar 2, 2024
1 parent 88f1b3d commit 8735421
Show file tree
Hide file tree
Showing 35 changed files with 117 additions and 364 deletions.
15 changes: 7 additions & 8 deletions pkg/api/annotation.go
Expand Up @@ -19,7 +19,6 @@ package api
import (
"io"
"os"
"time"

"github.com/pdfcpu/pdfcpu/pkg/log"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
Expand All @@ -38,7 +37,7 @@ func Annotations(rs io.ReadSeeker, selectedPages []string, conf *model.Configura
}
conf.Cmd = model.LISTANNOTATIONS

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -66,7 +65,7 @@ func AddAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages []string, ann m
}
conf.Cmd = model.ADDANNOTATIONS

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -110,7 +109,7 @@ func AddAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages []string, a
}
conf.Cmd = model.ADDANNOTATIONS

ctx, _, _, err := readAndValidate(rws, conf, time.Now())
ctx, err := ReadAndValidate(rws, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -212,7 +211,7 @@ func AddAnnotationsMap(rs io.ReadSeeker, w io.Writer, m map[int][]model.Annotati
}
conf.Cmd = model.ADDANNOTATIONS

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -251,7 +250,7 @@ func AddAnnotationsMapAsIncrement(rws io.ReadWriteSeeker, m map[int][]model.Anno
}
conf.Cmd = model.ADDANNOTATIONS

ctx, _, _, err := readAndValidate(rws, conf, time.Now())
ctx, err := ReadAndValidate(rws, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -350,7 +349,7 @@ func RemoveAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages, idsAndTypes
}
conf.Cmd = model.REMOVEANNOTATIONS

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -395,7 +394,7 @@ func RemoveAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages, idsAndT
}
conf.Cmd = model.REMOVEANNOTATIONS

ctx, _, _, err := readAndValidate(rws, conf, time.Now())
ctx, err := ReadAndValidate(rws, conf)
if err != nil {
return err
}
Expand Down
31 changes: 11 additions & 20 deletions pkg/api/api.go
Expand Up @@ -38,7 +38,6 @@ import (
"io"
"os"
"sync"
"time"

"github.com/pdfcpu/pdfcpu/pkg/log"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
Expand Down Expand Up @@ -144,43 +143,35 @@ func WriteContextFile(ctx *model.Context, outFile string) error {
return WriteContext(ctx, f)
}

func readAndValidate(rs io.ReadSeeker, conf *model.Configuration, from1 time.Time) (ctx *model.Context, dur1, dur2 float64, err error) {
// ReadAndValidate returns a model.Context of rs ready for processing.
func ReadAndValidate(rs io.ReadSeeker, conf *model.Configuration) (ctx *model.Context, err error) {
if ctx, err = ReadContext(rs, conf); err != nil {
return nil, 0, 0, err
return nil, err
}

dur1 = time.Since(from1).Seconds()

from2 := time.Now()

if ctx.Version() == model.V20 {
logDisclaimerPDF20()
}

if err = validate.XRefTable(ctx.XRefTable); err != nil {
return nil, 0, 0, err
return nil, err
}

dur2 = time.Since(from2).Seconds()

return ctx, dur1, dur2, nil
return ctx, nil
}

// ReadValidateAndOptimize returns the model.Context of rs ready for processing.
func ReadValidateAndOptimize(rs io.ReadSeeker, conf *model.Configuration, from1 time.Time) (ctx *model.Context, dur1, dur2, dur3 float64, err error) {
ctx, dur1, dur2, err = readAndValidate(rs, conf, from1)
// ReadValidateAndOptimize returns an optimized model.Context of rs ready for processing.
func ReadValidateAndOptimize(rs io.ReadSeeker, conf *model.Configuration) (ctx *model.Context, err error) {
ctx, err = ReadAndValidate(rs, conf)
if err != nil {
return nil, 0, 0, 0, err
return nil, err
}

from3 := time.Now()
if err = OptimizeContext(ctx); err != nil {
return nil, 0, 0, 0, err
return nil, err
}

dur3 = time.Since(from3).Seconds()

return ctx, dur1, dur2, dur3, nil
return ctx, nil
}

func logOperationStats(ctx *model.Context, op string, durRead, durVal, durOpt, durWrite, durTotal float64) {
Expand Down
28 changes: 4 additions & 24 deletions pkg/api/attach.go
Expand Up @@ -21,7 +21,6 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/pdfcpu/pdfcpu/pkg/log"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
Expand All @@ -39,8 +38,7 @@ func Attachments(rs io.ReadSeeker, conf *model.Configuration) ([]model.Attachmen
}
conf.Cmd = model.LISTATTACHMENTS

fromStart := time.Now()
ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, fromStart)
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return nil, err
}
Expand All @@ -64,13 +62,11 @@ func AddAttachments(rs io.ReadSeeker, w io.Writer, files []string, coll bool, co
}
conf.Cmd = model.ADDATTACHMENTS

fromStart := time.Now()
ctx, durRead, durVal, durOpt, err := ReadValidateAndOptimize(rs, conf, fromStart)
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}

from := time.Now()
var ok bool

for _, fn := range files {
Expand Down Expand Up @@ -111,17 +107,10 @@ func AddAttachments(rs io.ReadSeeker, w io.Writer, files []string, coll bool, co
return errors.New("pdfcpu: AddAttachments: No attachment added")
}

durAdd := time.Since(from).Seconds()
fromWrite := time.Now()

if err = WriteContext(ctx, w); err != nil {
return err
}

durWrite := durAdd + time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logOperationStats(ctx, "add attachment, write", durRead, durVal, durOpt, durWrite, durTotal)

return nil
}

Expand Down Expand Up @@ -178,14 +167,11 @@ func RemoveAttachments(rs io.ReadSeeker, w io.Writer, files []string, conf *mode
}
conf.Cmd = model.ADDATTACHMENTS

fromStart := time.Now()
ctx, durRead, durVal, durOpt, err := ReadValidateAndOptimize(rs, conf, fromStart)
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}

from := time.Now()

var ok bool
if ok, err = ctx.RemoveAttachments(files); err != nil {
return err
Expand All @@ -194,16 +180,10 @@ func RemoveAttachments(rs io.ReadSeeker, w io.Writer, files []string, conf *mode
return errors.New("pdfcpu: RemoveAttachments: No attachment removed")
}

durRemove := time.Since(from).Seconds()
fromWrite := time.Now()
if err = WriteContext(ctx, w); err != nil {
return err
}

durWrite := durRemove + time.Since(fromWrite).Seconds()
durTotal := time.Since(fromStart).Seconds()
logOperationStats(ctx, "remove att, write", durRead, durVal, durOpt, durWrite, durTotal)

return nil
}

Expand Down Expand Up @@ -256,7 +236,7 @@ func ExtractAttachmentsRaw(rs io.ReadSeeker, outDir string, fileNames []string,
}
conf.Cmd = model.EXTRACTATTACHMENTS

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadAndValidate(rs, conf)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/booklet.go
Expand Up @@ -19,7 +19,6 @@ package api
import (
"io"
"os"
"time"

"github.com/pdfcpu/pdfcpu/pkg/log"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
Expand Down Expand Up @@ -84,7 +83,7 @@ func Booklet(rs io.ReadSeeker, w io.Writer, imgFiles, selectedPages []string, nu

} else {

if ctx, _, _, err = readAndValidate(rs, conf, time.Now()); err != nil {
if ctx, err = ReadAndValidate(rs, conf); err != nil {
return err
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/api/bookmark.go
Expand Up @@ -19,7 +19,6 @@ package api
import (
"io"
"os"
"time"

"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
Expand All @@ -44,7 +43,7 @@ func Bookmarks(rs io.ReadSeeker, conf *model.Configuration) ([]pdfcpu.Bookmark,
}
conf.Cmd = model.LISTBOOKMARKS

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return nil, err
}
Expand All @@ -66,7 +65,7 @@ func ExportBookmarksJSON(rs io.ReadSeeker, w io.Writer, source string, conf *mod
}
conf.Cmd = model.EXPORTBOOKMARKS

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -134,7 +133,7 @@ func ImportBookmarks(rs io.ReadSeeker, rd io.Reader, w io.Writer, replace bool,
}
conf.Cmd = model.IMPORTBOOKMARKS

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -213,7 +212,7 @@ func AddBookmarks(rs io.ReadSeeker, w io.Writer, bms []pdfcpu.Bookmark, replace
return errors.New("pdfcpu: AddBookmarks: missing bms")
}

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -280,7 +279,7 @@ func RemoveBookmarks(rs io.ReadSeeker, w io.Writer, conf *model.Configuration) e
}
conf.Cmd = model.REMOVEBOOKMARKS

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/api/box.go
Expand Up @@ -19,7 +19,6 @@ package api
import (
"io"
"os"
"time"

"github.com/pdfcpu/pdfcpu/pkg/log"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
Expand Down Expand Up @@ -53,7 +52,7 @@ func Boxes(rs io.ReadSeeker, selectedPages []string, conf *model.Configuration)
}
conf.Cmd = model.LISTBOXES

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -81,7 +80,7 @@ func AddBoxes(rs io.ReadSeeker, w io.Writer, selectedPages []string, pb *model.P
}
conf.Cmd = model.ADDBOXES

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -162,7 +161,7 @@ func RemoveBoxes(rs io.ReadSeeker, w io.Writer, selectedPages []string, pb *mode
}
conf.Cmd = model.REMOVEBOXES

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down Expand Up @@ -244,7 +243,7 @@ func Crop(rs io.ReadSeeker, w io.Writer, selectedPages []string, b *model.Box, c
}
conf.Cmd = model.CROP

ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/api/collect.go
Expand Up @@ -19,7 +19,6 @@ package api
import (
"io"
"os"
"time"

"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
Expand All @@ -37,8 +36,7 @@ func Collect(rs io.ReadSeeker, w io.Writer, selectedPages []string, conf *model.
}
conf.Cmd = model.COLLECT

fromStart := time.Now()
ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, fromStart)
ctx, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/create.go
Expand Up @@ -19,7 +19,6 @@ package api
import (
"io"
"os"
"time"

"github.com/pdfcpu/pdfcpu/pkg/log"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
Expand Down Expand Up @@ -59,7 +58,7 @@ func Create(rs io.ReadSeeker, rd io.Reader, w io.Writer, conf *model.Configurati
)

if rs != nil {
ctx, _, _, _, err = ReadValidateAndOptimize(rs, conf, time.Now())
ctx, err = ReadValidateAndOptimize(rs, conf)
} else {
ctx, err = pdfcpu.CreateContextWithXRefTable(conf, types.PaperSize["A4"])
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/cut.go
Expand Up @@ -23,7 +23,6 @@ import (
"path/filepath"
"sort"
"strings"
"time"

"github.com/pdfcpu/pdfcpu/pkg/log"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
Expand All @@ -33,7 +32,7 @@ import (
)

func prepareForCut(rs io.ReadSeeker, selectedPages []string, conf *model.Configuration) (*model.Context, types.IntSet, error) {
ctxSrc, _, _, _, err := ReadValidateAndOptimize(rs, conf, time.Now())
ctxSrc, err := ReadValidateAndOptimize(rs, conf)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 8735421

Please sign in to comment.