Add string utility functions for file handling

This commit is contained in:
Luis Eduardo Jeréz Girón
2024-07-19 23:13:55 -06:00
parent 45dd6a9a18
commit 3a40c818ef
4 changed files with 160 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
package strutil
import "strings"
// GetContentTypeFromFileName returns the content type
// of a file based on its file extension from the name.
//
// If the file extension is not recognized, it returns
// "application/octet-stream".
func GetContentTypeFromFileName(fileName string) string {
fileName = strings.ToLower(fileName)
if strings.HasSuffix(fileName, ".pdf") {
return "application/pdf"
}
if strings.HasSuffix(fileName, ".png") {
return "image/png"
}
if strings.HasSuffix(fileName, ".jpg") || strings.HasSuffix(fileName, ".jpeg") {
return "image/jpeg"
}
if strings.HasSuffix(fileName, ".gif") {
return "image/gif"
}
if strings.HasSuffix(fileName, ".bmp") {
return "image/bmp"
}
if strings.HasSuffix(fileName, ".json") {
return "application/json"
}
if strings.HasSuffix(fileName, ".csv") {
return "text/csv"
}
if strings.HasSuffix(fileName, ".xml") {
return "application/xml"
}
if strings.HasSuffix(fileName, ".txt") {
return "text/plain"
}
if strings.HasSuffix(fileName, ".html") {
return "text/html"
}
if strings.HasSuffix(fileName, ".zip") {
return "application/zip"
}
if strings.HasSuffix(fileName, ".sql") {
return "application/sql"
}
return "application/octet-stream"
}

View File

@@ -0,0 +1,38 @@
package strutil
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetContentTypeFromFileName(t *testing.T) {
tests := []struct {
fileName string
expected string
}{
{"documento.pdf", "application/pdf"},
{"imagen.png", "image/png"},
{"foto.jpg", "image/jpeg"},
{"foto.jpeg", "image/jpeg"},
{"animacion.gif", "image/gif"},
{"imagen.bmp", "image/bmp"},
{"datos.json", "application/json"},
{"tabla.csv", "text/csv"},
{"config.xml", "application/xml"},
{"nota.txt", "text/plain"},
{"pagina.html", "text/html"},
{"archivo.zip", "application/zip"},
{"archivo.sql", "application/sql"},
{"archivo.desconocido", "application/octet-stream"}, // unknown extension
{"MAYUSCULAS.JPG", "image/jpeg"}, // upper case
{"MezclaDeMayusculasYMinusculas.PnG", "image/png"}, // mixed case
}
for _, tt := range tests {
t.Run(tt.fileName, func(t *testing.T) {
result := GetContentTypeFromFileName(tt.fileName)
assert.Equal(t, tt.expected, result)
})
}
}

View File

@@ -0,0 +1,10 @@
package strutil
// RemoveLeadingSlash removes the leading slash from a string.
func RemoveLeadingSlash(str string) string {
if len(str) > 0 && str[0] == '/' {
return str[1:]
}
return str
}

View File

@@ -0,0 +1,50 @@
package strutil
import "testing"
func TestRemoveLeadingSlash(t *testing.T) {
tests := []struct {
name string
str string
want string
}{
{
name: "Remove leading slash",
str: "/test/",
want: "test/",
},
{
name: "Remove leading slash from empty string",
str: "",
want: "",
},
{
name: "Remove leading slash from string without leading slash",
str: "test/",
want: "test/",
},
{
name: "Remove leading slash from string with multiple leading slashes",
str: "//test/",
want: "/test/",
},
{
name: "With special characters",
str: "/test/!@#$%^&*()_+/",
want: "test/!@#$%^&*()_+/",
},
{
name: "With special characters (emojis)",
str: "/test/!@#$%^&*()_+😀/",
want: "test/!@#$%^&*()_+😀/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RemoveLeadingSlash(tt.str); got != tt.want {
t.Errorf("RemoveLeadingSlash() = %v, want %v", got, tt.want)
}
})
}
}