From 9a77c0418199d9396015059b8f5f73d38ca5d8cc Mon Sep 17 00:00:00 2001 From: Quim Muntal Date: Tue, 26 Jan 2021 09:20:43 +0100 Subject: [PATCH] only add application/xml content type when needed --- package.go | 6 +----- testdata/component.3mf | Bin 1663 -> 1658 bytes writer.go | 8 ++++++-- writer_test.go | 3 --- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/package.go b/package.go index 6c693d4..0814436 100644 --- a/package.go +++ b/package.go @@ -46,11 +46,7 @@ func (p *pkg) add(part *Part) error { if err := part.validate(); err != nil { return err } - name := part.Name - if !strings.EqualFold(name, contentTypesName) { - name = NormalizePartName(part.Name) - } - name = strings.ToUpper(name) + name := strings.ToUpper(NormalizePartName(part.Name)) if p.partExists(name) { return newError(112, part.Name) } diff --git a/testdata/component.3mf b/testdata/component.3mf index 3acb78c1a0c27e050c73f004911de7161c48038c..45d8a648752f263e2cdca08000bc2e33be4b2962 100644 GIT binary patch delta 503 zcmey*^NWWkz?+#xgaHIj#ZBZ}3@sy{+c-YoK`4sPAIr;!eh_?%bEf8|9bOWb8e*@p9X-BfB+s%wx@~d?z}&qkZ$o|6JOQr(d7^ zx9*R*s&V-Yn;SFyQyW!P@#hu8U$Lqx|L95f6Jlh-gyF~&_k&U_C@ zE@Ls5OFMjh`R>ox7-JuL)oj(i%oyO!$Yjp|3P}zIxA@7wSrVjC0+ESt?~P)r^oGge zrOK=eIQJgiIN6x@YVV!9bLVdJUoW^R!Qh#npUcOvl{so3tgb6E6{T~W%zk9ltCjV6 zk$b>&-{Q4C4opkB!gx<+slR3BsVb{ikrRKHbML&Mo8~+7H(A#`Ux)XuU0C<*Z|5^- z{&(Dg$6u_uc!Xi=@qOQRTJZ(Ne~o)=li^(a=x#{NdZy<#2?Kz>P2$D6)I9 M5t|d+3>J`80L;0sU;qFB diff --git a/writer.go b/writer.go index 77dba07..d2e0877 100644 --- a/writer.go +++ b/writer.go @@ -41,7 +41,6 @@ func NewWriter(w io.Writer) *Writer { parts: make(map[string]struct{}, 0), contentTypes: contentTypes{ defaults: map[string]string{ - "xml": "application/xml", "rels": relationshipContentType, }, overrides: map[string]string{}, @@ -165,7 +164,12 @@ func (w *Writer) createCoreProperties() error { func (w *Writer) createContentTypes() error { // ISO/IEC 29500-2 M3.10 - cw, err := w.addToPackage(&Part{Name: contentTypesName, ContentType: "application/xml"}, CompressionNormal) + fh := &zip.FileHeader{ + Name: zipName(contentTypesName), + Modified: time.Now(), + } + w.setCompressor(fh, CompressionNormal) + cw, err := w.w.CreateHeader(fh) if err != nil { return err } diff --git a/writer_test.go b/writer_test.go index 2191cea..e89854d 100644 --- a/writer_test.go +++ b/writer_test.go @@ -27,8 +27,6 @@ func TestWriter_Close(t *testing.T) { p := newPackage() p.contentTypes.add("/a.xml", "a/b") p.contentTypes.add("/b.xml", "c/d") - pC := newPackage() - pC.parts["/[CONTENT_TYPES].XML"] = struct{}{} pCore := newPackage() pCore.parts["/PROPS/CORE.XML"] = struct{}{} pRel := newPackage() @@ -39,7 +37,6 @@ func TestWriter_Close(t *testing.T) { wantErr bool }{ {"base", NewWriter(&bytes.Buffer{}), false}, - {"invalidContentType", &Writer{p: pC, w: zip.NewWriter(&bytes.Buffer{})}, true}, {"withCt", &Writer{p: p, w: zip.NewWriter(&bytes.Buffer{})}, false}, {"invalidPartRel", &Writer{p: newPackage(), w: zip.NewWriter(&bytes.Buffer{}), last: &Part{Name: "/b.xml", Relationships: []*Relationship{{}}}}, true}, {"invalidOwnRel", &Writer{p: newPackage(), w: zip.NewWriter(&bytes.Buffer{}), Relationships: []*Relationship{{}}}, true},