浏览代码

Remove StemExt

Viktor Sokolov 1 月之前
父节点
当前提交
1713082991
共有 2 个文件被更改,包括 0 次插入215 次删除
  1. 0 89
      stemext/stem_ext.go
  2. 0 126
      stemext/stem_ext_test.go

+ 0 - 89
stemext/stem_ext.go

@@ -1,89 +0,0 @@
-// stemext package provides methods which help to generate correct
-// content-disposition header.
-package contentdisposition
-
-import (
-	"mime"
-	"net/url"
-	"path/filepath"
-)
-
-const (
-	// fallbackStem is used when the stem cannot be determined from the URL.
-	fallbackStem = "image"
-)
-
-// StemExt helps to detect correct stem and ext for content-disposition header.
-type StemExt struct {
-	stem string
-	ext  string
-}
-
-// FromURL creates a new StemExt instance from the provided URL.
-// Returns a value type to avoid heap allocation.
-func FromURL(url *url.URL) StemExt {
-	_, filename := filepath.Split(url.Path)
-	ext := filepath.Ext(filename)
-
-	// Avoid strings.TrimSuffix allocation by using slice operation
-	var stem string
-	if ext != "" {
-		stem = filename[:len(filename)-len(ext)]
-	} else {
-		stem = filename
-	}
-
-	return StemExt{
-		stem: stem,
-		ext:  ext,
-	}
-}
-
-// SetExtFromContentTypeIfEmpty sets the ext field based on the provided content type.
-// Uses pointer receiver for zero-copy method chaining.
-func (cd *StemExt) SetExtFromContentTypeIfEmpty(contentType string) *StemExt {
-	if len(contentType) == 0 || len(cd.ext) > 0 {
-		return cd
-	}
-
-	if exts, err := mime.ExtensionsByType(contentType); err == nil && len(exts) != 0 {
-		cd.ext = exts[0]
-	}
-
-	return cd
-}
-
-// OverrideExt sets the ext field if the provided ext is not empty.
-// Uses pointer receiver for zero-copy method chaining.
-func (cd *StemExt) OverrideExt(ext string) *StemExt {
-	if len(ext) > 0 {
-		cd.ext = ext
-	}
-
-	return cd
-}
-
-// OverrideStem sets the stem field if the provided stem is not empty.
-// Uses pointer receiver for zero-copy method chaining.
-func (cd *StemExt) OverrideStem(stem string) *StemExt {
-	if len(stem) > 0 {
-		cd.stem = stem
-	}
-
-	return cd
-}
-
-// StemExtWithFallback returns stem and ext, but if stem is empty, it uses a fallback value.
-func (cd StemExt) StemExtWithFallback() (string, string) {
-	stem := cd.stem
-	if len(stem) == 0 {
-		stem = fallbackStem
-	}
-
-	return stem, cd.ext
-}
-
-// StemExt returns the tuple of stem and ext.
-func (cd StemExt) StemExt() (string, string) {
-	return cd.stem, cd.ext
-}

+ 0 - 126
stemext/stem_ext_test.go

@@ -1,126 +0,0 @@
-package contentdisposition
-
-import (
-	"net/url"
-	"testing"
-
-	"github.com/stretchr/testify/require"
-)
-
-func TestStemExt(t *testing.T) {
-	// Test cases for stem and ext detection
-	tests := []struct {
-		name string
-		url  string
-		stem string
-		ext  string
-		fn   func(StemExt) (string, string)
-	}{
-		{
-			name: "BasicURL",
-			url:  "http://example.com/test.jpg",
-			stem: "test",
-			ext:  ".jpg",
-			fn: func(se StemExt) (string, string) {
-				return se.StemExt()
-			},
-		},
-		{
-			name: "EmptyFilename",
-			url:  "http://example.com/path/to/",
-			stem: "",
-			ext:  "",
-			fn: func(se StemExt) (string, string) {
-				return se.StemExt()
-			},
-		},
-		{
-			name: "EmptyFilenameWithContentType",
-			url:  "http://example.com/path/to/",
-			stem: "",
-			ext:  ".png",
-			fn: func(se StemExt) (string, string) {
-				return (&se).SetExtFromContentTypeIfEmpty("image/png").StemExt()
-			},
-		},
-		{
-			name: "EmptyFilenameWithContentTypeAndOverride",
-			url:  "http://example.com/path/to/",
-			stem: "example",
-			ext:  ".png",
-			fn: func(se StemExt) (string, string) {
-				return (&se).OverrideStem("example").SetExtFromContentTypeIfEmpty("image/png").StemExt()
-			},
-		},
-		{
-			name: "EmptyFilenameWithOverride",
-			url:  "http://example.com/path/to/",
-			stem: "example",
-			ext:  ".jpg",
-			fn: func(se StemExt) (string, string) {
-				return (&se).OverrideStem("example").OverrideExt(".jpg").StemExt()
-			},
-		},
-		{
-			name: "PresentFilenameWithOverride",
-			url:  "http://example.com/path/to/face",
-			stem: "face",
-			ext:  ".jpg",
-			fn: func(se StemExt) (string, string) {
-				return (&se).OverrideExt(".jpg").StemExt()
-			},
-		},
-		{
-			name: "PresentFilenameWithOverride",
-			url:  "http://example.com/path/to/123",
-			stem: "face",
-			ext:  ".jpg",
-			fn: func(se StemExt) (string, string) {
-				return (&se).OverrideStem("face").OverrideExt(".jpg").StemExt()
-			},
-		},
-		{
-			name: "EmptyFilenameWithFallback",
-			url:  "http://example.com/path/to/",
-			stem: "image",
-			ext:  ".png",
-			fn: func(se StemExt) (string, string) {
-				return (&se).SetExtFromContentTypeIfEmpty("image/png").StemExtWithFallback()
-			},
-		},
-	}
-
-	for _, tc := range tests {
-		t.Run(tc.name, func(t *testing.T) {
-			u, err := url.Parse(tc.url)
-			require.NoError(t, err)
-
-			se := FromURL(u)
-			stem, ext := tc.fn(se)
-
-			require.Equal(t, tc.stem, stem)
-			require.Equal(t, tc.ext, ext)
-		})
-	}
-}
-
-func BenchmarkFromURL(b *testing.B) {
-	u, _ := url.Parse("http://example.com/path/to/test.jpg")
-
-	b.ResetTimer()
-	for i := 0; i < b.N; i++ {
-		se := FromURL(u)
-		_, _ = se.StemExt()
-	}
-}
-
-func BenchmarkMethodChaining(b *testing.B) {
-	u, _ := url.Parse("http://example.com/path/to/")
-
-	b.ResetTimer()
-	for i := 0; i < b.N; i++ {
-		se := FromURL(u)
-		(&se).SetExtFromContentTypeIfEmpty("image/png").OverrideStem("example")
-		_, _ = se.StemExtWithFallback()
-	}
-}