forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontenttypes.go
84 lines (72 loc) · 2.76 KB
/
contenttypes.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
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting [email protected].
package common
import (
"log"
"strings"
"baliance.com/gooxml/schema/schemas.openxmlformats.org/package/2006/content_types"
)
// ContentTypes is the top level "[Content_Types].xml" in a zip package.
type ContentTypes struct {
x *content_types.Types
}
// NewContentTypes returns a wrapper around a newly constructed content-types.
func NewContentTypes() ContentTypes {
ct := ContentTypes{x: content_types.NewTypes()}
// add content type defaults
ct.AddDefault("xml", "application/xml")
ct.AddDefault("rels", "application/vnd.openxmlformats-package.relationships+xml")
ct.AddDefault("png", "image/png")
ct.AddDefault("jpeg", "image/jpeg")
ct.AddDefault("jpg", "image/jpg")
ct.AddDefault("wmf", "image/x-wmf")
ct.AddOverride("/docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml")
ct.AddOverride("/docProps/app.xml", "application/vnd.openxmlformats-officedocument.extended-properties+xml")
return ct
}
// X returns the inner raw content types.
func (c ContentTypes) X() *content_types.Types {
return c.x
}
// AddDefault registers a default content type for a given file extension.
func (c ContentTypes) AddDefault(fileExtension string, contentType string) {
def := content_types.NewDefault()
def.ExtensionAttr = fileExtension
def.ContentTypeAttr = contentType
c.x.Default = append(c.x.Default, def)
}
// AddOverride adds an override content type for a given path name.
func (c ContentTypes) AddOverride(path, contentType string) {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if strings.HasPrefix(contentType, "http") {
log.Printf("content type '%s' is incorrect, must not start with http", contentType)
}
or := content_types.NewOverride()
or.PartNameAttr = path
or.ContentTypeAttr = contentType
c.x.Override = append(c.x.Override, or)
}
// EnsureOverride ensures that an override for the given path exists, adding it if necessary
func (c ContentTypes) EnsureOverride(path, contentType string) {
for _, ovr := range c.x.Override {
// found one, so just ensure the content type matches and bail
if ovr.PartNameAttr == path {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if strings.HasPrefix(contentType, "http") {
log.Printf("content type '%s' is incorrect, must not start with http", contentType)
}
ovr.ContentTypeAttr = contentType
return
}
}
// Didn't find a matching override for the target path, so add one
c.AddOverride(path, contentType)
}