From 1d7b969b9123e4cbcabac17057d60d92f490def0 Mon Sep 17 00:00:00 2001 From: Gunnsteinn Hall Date: Sun, 4 Aug 2019 09:28:42 +0000 Subject: [PATCH 1/3] Simplify license loading and support environment variables --- common/license/util.go | 29 ++++++++++++++++++++++++++++- extractor/utils.go | 13 ++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/common/license/util.go b/common/license/util.go index 0755b2c1..5b0c6a05 100644 --- a/common/license/util.go +++ b/common/license/util.go @@ -8,10 +8,14 @@ package license import ( "fmt" + "io/ioutil" + "os" "strings" + + "github.com/unidoc/unipdf/v3/common" ) -// Defaults to the open source license. +// Defaults to unlicensed. var licenseKey = MakeUnlicensedKey() // SetLicenseKey sets and validates the license key. @@ -35,6 +39,29 @@ func SetLicenseKey(content string, customerName string) error { return nil } +const licensePathEnvironmentVar = `UNIPDF_LICENSE_PATH` +const licenseCustomerNameEnvironmentVar = `UNIPDF_CUSTOMER_NAME` + +func init() { + lpath := os.Getenv(licensePathEnvironmentVar) + custName := os.Getenv(licenseCustomerNameEnvironmentVar) + + if len(lpath) == 0 && len(custName) == 0 { + return + } + + data, err := ioutil.ReadFile(lpath) + if err != nil { + common.Log.Debug("Unable to read license file: %v", err) + return + } + err = SetLicenseKey(string(data), custName) + if err != nil { + common.Log.Debug("Unable to load license: %v", err) + return + } +} + func GetLicenseKey() *LicenseKey { if licenseKey == nil { return nil diff --git a/extractor/utils.go b/extractor/utils.go index bc5343d7..d4b906c1 100644 --- a/extractor/utils.go +++ b/extractor/utils.go @@ -54,7 +54,7 @@ func maxFloat(a, b float64) float64 { return b } -func procBuf(buf *bytes.Buffer) { +func procBuf(pt *PageText) { if isTesting { return } @@ -66,12 +66,23 @@ func procBuf(buf *bytes.Buffer) { fmt.Printf("Unlicensed copy of unidoc\n") fmt.Printf("To get rid of the watermark and keep entire text - Please get a license on https://unidoc.io\n") + var buf bytes.Buffer + buf.WriteString(pt.viewText) + s := "- [Unlicensed UniDoc - Get a license on https://unidoc.io]" if buf.Len() > 100 { s = "... [Truncated - Unlicensed UniDoc - Get a license on https://unidoc.io]" buf.Truncate(buf.Len() - 100) } buf.WriteString(s) + pt.viewText = buf.String() + + if len(pt.marks) > 200 { + pt.marks = pt.marks[:200] + } + if len(pt.viewMarks) > 200 { + pt.viewMarks = pt.viewMarks[:200] + } } // truncate returns the first `n` characters in string `s`. From 21141a9d3ed5aab60a285baf05d0807957b194c6 Mon Sep 17 00:00:00 2001 From: Gunnsteinn Hall Date: Sun, 4 Aug 2019 09:29:21 +0000 Subject: [PATCH 2/3] Add Append to TextMarkArray Useful when processing and grouping text marks. --- extractor/text.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/extractor/text.go b/extractor/text.go index 75bee8d7..7d66a636 100644 --- a/extractor/text.go +++ b/extractor/text.go @@ -47,6 +47,8 @@ func (e *Extractor) ExtractPageText() (*PageText, int, int, error) { return nil, numChars, numMisses, err } pt.computeViews() + procBuf(pt) + return pt, numChars, numMisses, err } @@ -937,6 +939,11 @@ type TextMarkArray struct { marks []TextMark } +// Append appends `mark` to the mark array. +func (ma *TextMarkArray) Append(mark TextMark) { + ma.marks = append(ma.marks, mark) +} + // String returns a string describing `ma`. func (ma TextMarkArray) String() string { n := len(ma.marks) From 9a4127349445d5109196d86ae593e6ad78e09e2b Mon Sep 17 00:00:00 2001 From: Gunnsteinn Hall Date: Sun, 4 Aug 2019 10:21:14 +0000 Subject: [PATCH 3/3] Fix - Both path and customer name must be set --- common/license/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/license/util.go b/common/license/util.go index 5b0c6a05..a46e0e79 100644 --- a/common/license/util.go +++ b/common/license/util.go @@ -46,7 +46,7 @@ func init() { lpath := os.Getenv(licensePathEnvironmentVar) custName := os.Getenv(licenseCustomerNameEnvironmentVar) - if len(lpath) == 0 && len(custName) == 0 { + if len(lpath) == 0 || len(custName) == 0 { return }