-
Notifications
You must be signed in to change notification settings - Fork 6
/
filenames.go
197 lines (176 loc) · 5.34 KB
/
filenames.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
190
191
192
193
194
195
196
197
// 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 gooxml
import (
"fmt"
"strings"
"github.com/qifengzhang007/gooxml/algo"
)
// Common filenames used in zip packages.
const (
ContentTypesFilename = "[Content_Types].xml"
BaseRelsFilename = "_rels/.rels"
)
// DocType represents one of the three document types supported (docx/xlsx/pptx)
type DocType byte
// Document Type constants
const (
Unknown DocType = iota
DocTypeSpreadsheet
DocTypeDocument
DocTypePresentation
)
// RelativeFilename returns a filename relative to the source file referenced
// from a relationships file. Index is used in some cases for files which there
// may be more than one of (e.g. worksheets/drawings/charts)
func RelativeFilename(dt DocType, relToTyp, typ string, index int) string {
orig := AbsoluteFilename(dt, typ, index)
if relToTyp == "" {
return orig
}
relTo := AbsoluteFilename(dt, relToTyp, index)
relToSp := strings.Split(relTo, "/")
origSp := strings.Split(orig, "/")
// determine how many segments match
matching := 0
for i := 0; i < len(relToSp); i++ {
if relToSp[i] == origSp[i] {
matching++
}
if i+1 == len(origSp) {
break
}
}
relToSp = relToSp[matching:]
origSp = origSp[matching:]
nm := len(relToSp) - 1
if nm > 0 {
return algo.RepeatString("../", nm) + strings.Join(origSp, "/")
}
return strings.Join(origSp, "/")
}
// AbsoluteFilename returns the full path to a file from the root of the zip
// container. Index is used in some cases for files which there may be more than
// one of (e.g. worksheets/drawings/charts)
func AbsoluteFilename(dt DocType, typ string, index int) string {
switch typ {
case CorePropertiesType:
return "docProps/core.xml"
case ExtendedPropertiesType, ExtendedPropertiesTypeOclc:
return "docProps/app.xml"
case CustomPropertiesType:
return "docProps/custom.xml"
case ThumbnailType:
return "docProps/thumbnail.jpeg"
case OfficeDocumentType, OfficeDocumentTypeOclc:
switch dt {
case DocTypeSpreadsheet:
return "xl/workbook.xml"
case DocTypeDocument:
return "word/document.xml"
case DocTypePresentation:
return "ppt/presentation.xml"
default:
Log("unsupported type %s pair and %v", typ, dt)
}
case ThemeType, ThemeTypeOclc, ThemeContentType:
switch dt {
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/theme/theme%d.xml", index)
case DocTypeDocument:
return fmt.Sprintf("word/theme/theme%d.xml", index)
case DocTypePresentation:
return fmt.Sprintf("ppt/theme/theme%d.xml", index)
default:
Log("unsupported type %s pair and %v", typ, dt)
}
case StylesType, StylesTypeOclc:
switch dt {
case DocTypeSpreadsheet:
return "xl/styles.xml"
case DocTypeDocument:
return "word/styles.xml"
case DocTypePresentation:
return "ppt/styles.xml"
default:
Log("unsupported type %s pair and %v", typ, dt)
}
case ChartType, ChartContentType:
switch dt {
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/charts/chart%d.xml", index)
default:
Log("unsupported type %s pair and %v", typ, dt)
}
case TableType, TableContentType:
return fmt.Sprintf("xl/tables/table%d.xml", index)
case DrawingType, DrawingContentType:
switch dt {
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/drawings/drawing%d.xml", index)
default:
Log("unsupported type %s pair and %v", typ, dt)
}
case CommentsType, CommentsContentType:
switch dt {
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/comments%d.xml", index)
default:
Log("unsupported type %s pair and %v", typ, dt)
}
case VMLDrawingType, VMLDrawingContentType:
switch dt {
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/drawings/vmlDrawing%d.vml", index)
default:
Log("unsupported type %s pair and %v", typ, dt)
}
case ImageType:
switch dt {
case DocTypeDocument:
return fmt.Sprintf("word/media/image%d.png", index)
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/media/image%d.png", index)
case DocTypePresentation:
return fmt.Sprintf("ppt/media/image%d.png", index)
default:
Log("unsupported type %s pair and %v", typ, dt)
}
// SML
case WorksheetType, WorksheetContentType:
return fmt.Sprintf("xl/worksheets/sheet%d.xml", index)
case SharedStingsType, SharedStringsContentType:
return "xl/sharedStrings.xml"
// WML
case FontTableType, FontTableTypeOclc:
return "word/fontTable.xml"
case EndNotesType, EndNotesTypeOclc:
return "word/endnotes.xml"
case FootNotesType, FootNotesTypeOclc:
return "word/footnotes.xml"
case NumberingType:
return "word/numbering.xml"
case WebSettingsType, WebSettingsTypeOclc:
return "word/webSettings.xml"
case SettingsType, SettingsTypeOclc:
return "word/settings.xml"
case HeaderType:
return fmt.Sprintf("word/header%d.xml", index)
case FooterType:
return fmt.Sprintf("word/footer%d.xml", index)
// PML
case SlideType:
return fmt.Sprintf("ppt/slides/slide%d.xml", index)
case SlideLayoutType:
return fmt.Sprintf("ppt/slideLayouts/slideLayout%d.xml", index)
case SlideMasterType:
return fmt.Sprintf("ppt/slideMasters/slideMaster%d.xml", index)
default:
Log("unsupported type %s", typ)
}
return ""
}