forked from pdfcpu/pdfcpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaData.go
189 lines (156 loc) · 6.26 KB
/
metaData.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Copyright 2023 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package validate
import (
"encoding/xml"
"fmt"
"strings"
"time"
"github.com/meandrewdev/pdfcpu/pkg/filter"
"github.com/meandrewdev/pdfcpu/pkg/pdfcpu/model"
"github.com/meandrewdev/pdfcpu/pkg/pdfcpu/types"
)
const userDateFormatNoTimeZone = "2006-01-02T15:04:05Z"
const userDateFormatNegTimeZone = "2006-01-02T15:04:05-07:00"
const userDateFormatPosTimeZone = "2006-01-02T15:04:05+07:00"
type userDate time.Time
func (ud *userDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
dateString := ""
err := d.DecodeElement(&dateString, &start)
if err != nil {
return err
}
dat, err := time.Parse(userDateFormatNoTimeZone, dateString)
if err == nil {
*ud = userDate(dat)
return nil
}
dat, err = time.Parse(userDateFormatPosTimeZone, dateString)
if err == nil {
*ud = userDate(dat)
return nil
}
dat, err = time.Parse(userDateFormatNegTimeZone, dateString)
if err == nil {
*ud = userDate(dat)
return nil
}
return err
}
type Alt struct {
//XMLName xml.Name `xml:"http://www.w3.org/1999/02/22-rdf-syntax-ns# Alt"`
Entries []string `xml:"http://www.w3.org/1999/02/22-rdf-syntax-ns# li"`
}
type Seq struct {
//XMLName xml.Name `xml:"http://www.w3.org/1999/02/22-rdf-syntax-ns# Seq"`
Entries []string `xml:"http://www.w3.org/1999/02/22-rdf-syntax-ns# li"`
}
type Title struct {
//XMLName xml.Name `xml:"http://purl.org/dc/elements/1.1/ title"`
Alt Alt `xml:"http://www.w3.org/1999/02/22-rdf-syntax-ns# Alt"`
}
type Desc struct {
//XMLName xml.Name `xml:"http://purl.org/dc/elements/1.1/ description"`
Alt Alt `xml:"http://www.w3.org/1999/02/22-rdf-syntax-ns# Alt"`
}
type Creator struct {
//XMLName xml.Name `xml:"http://purl.org/dc/elements/1.1/ creator"`
Seq Seq `xml:"http://www.w3.org/1999/02/22-rdf-syntax-ns# Seq"`
}
type Description struct {
//XMLName xml.Name `xml:"http://www.w3.org/1999/02/22-rdf-syntax-ns# Description"`
Title Title `xml:"http://purl.org/dc/elements/1.1/ title"`
Author Creator `xml:"http://purl.org/dc/elements/1.1/ creator"`
Subject Desc `xml:"http://purl.org/dc/elements/1.1/ description"`
Creator string `xml:"http://ns.adobe.com/xap/1.0/ CreatorTool"`
CreationDate userDate `xml:"http://ns.adobe.com/xap/1.0/ CreateDate"`
ModDate userDate `xml:"http://ns.adobe.com/xap/1.0/ ModifyDate"`
Producer string `xml:"http://ns.adobe.com/pdf/1.3/ Producer"`
Trapped bool `xml:"http://ns.adobe.com/pdf/1.3/ Trapped"`
Keywords string `xml:"http://ns.adobe.com/pdf/1.3/ Keywords"`
}
type RDF struct {
XMLName xml.Name `xml:"http://www.w3.org/1999/02/22-rdf-syntax-ns# RDF"`
Description Description
}
type XMPMeta struct {
XMLName xml.Name `xml:"adobe:ns:meta/ xmpmeta"`
RDF RDF
}
func validateMetadataStream(xRefTable *model.XRefTable, d types.Dict, required bool, sinceVersion model.Version) (*types.StreamDict, error) {
if xRefTable.ValidationMode == model.ValidationRelaxed {
sinceVersion = model.V13
}
sd, err := validateStreamDictEntry(xRefTable, d, "dict", "Metadata", required, sinceVersion, nil)
if err != nil || sd == nil {
return nil, err
}
dictName := "metaDataDict"
if _, err = validateNameEntry(xRefTable, sd.Dict, dictName, "Type", OPTIONAL, sinceVersion, func(s string) bool { return s == "Metadata" }); err != nil {
return nil, err
}
if _, err = validateNameEntry(xRefTable, sd.Dict, dictName, "Subtype", OPTIONAL, sinceVersion, func(s string) bool { return s == "XML" }); err != nil {
return nil, err
}
return sd, nil
}
func validateMetadata(xRefTable *model.XRefTable, d types.Dict, required bool, sinceVersion model.Version) error {
// => 14.3 Metadata
// In general, any PDF stream or dictionary may have metadata attached to it
// as long as the stream or dictionary represents an actual information resource,
// as opposed to serving as an implementation artifact.
// Some PDF constructs are considered implementational, and hence may not have associated metadata.
_, err := validateMetadataStream(xRefTable, d, required, sinceVersion)
return err
}
func validateRootMetadata(xRefTable *model.XRefTable, rootDict types.Dict, required bool, sinceVersion model.Version) error {
sd, err := validateMetadataStream(xRefTable, rootDict, required, sinceVersion)
if err != nil || sd == nil {
return err
}
if xRefTable.Version() < model.V20 {
return nil
}
// Decode streamDict for supported filters only.
if err := sd.Decode(); err == filter.ErrUnsupportedFilter {
return nil
}
if err != nil {
return err
}
x := XMPMeta{}
if err = xml.Unmarshal(sd.Content, &x); err != nil {
fmt.Printf("error: %v", err)
return err
}
// fmt.Printf(" Title: %v\n", x.RDF.Description.Title.Alt.Entries)
// fmt.Printf(" Author: %v\n", x.RDF.Description.Author.Seq.Entries)
// fmt.Printf(" Subject: %v\n", x.RDF.Description.Subject.Alt.Entries)
// fmt.Printf(" Creator: %s\n", x.RDF.Description.Creator)
// fmt.Printf("CreationDate: %v\n", time.Time(x.RDF.Description.CreationDate).Format(time.RFC3339Nano))
// fmt.Printf(" ModDate: %v\n", time.Time(x.RDF.Description.ModDate).Format(time.RFC3339Nano))
// fmt.Printf(" Producer: %s\n", x.RDF.Description.Producer)
// fmt.Printf(" Trapped: %t\n", x.RDF.Description.Trapped)
// fmt.Printf(" Keywords: %s\n", x.RDF.Description.Keywords)
d := x.RDF.Description
xRefTable.Title = strings.Join(d.Title.Alt.Entries, ", ")
xRefTable.Author = strings.Join(d.Author.Seq.Entries, ", ")
xRefTable.Subject = strings.Join(d.Subject.Alt.Entries, ", ")
xRefTable.Creator = d.Creator
xRefTable.CreationDate = time.Time(d.CreationDate).Format(time.RFC3339Nano)
xRefTable.ModDate = time.Time(d.ModDate).Format(time.RFC3339Nano)
xRefTable.Producer = d.Producer
//xRefTable.Trapped = d.Trapped
xRefTable.Keywords = d.Keywords
return nil
}