mirror of
https://github.com/pocket-id/pocket-id.git
synced 2025-12-17 17:23:30 +03:00
fix: decouple images from app config service (#965)
Co-authored-by: Alessandro (Ale) Segala <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -24,6 +25,15 @@ func GetFileExtension(filename string) string {
|
||||
return filename
|
||||
}
|
||||
|
||||
// SplitFileName splits a full file name into name and extension.
|
||||
func SplitFileName(fullName string) (name, ext string) {
|
||||
dot := strings.LastIndex(fullName, ".")
|
||||
if dot == -1 || dot == 0 {
|
||||
return fullName, "" // no extension or hidden file like .gitignore
|
||||
}
|
||||
return fullName[:dot], fullName[dot+1:]
|
||||
}
|
||||
|
||||
func GetImageMimeType(ext string) string {
|
||||
switch ext {
|
||||
case "jpg", "jpeg":
|
||||
|
||||
@@ -2,8 +2,36 @@ package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSplitFileName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
fullName string
|
||||
wantName string
|
||||
wantExt string
|
||||
}{
|
||||
{"background.jpg", "background", "jpg"},
|
||||
{"archive.tar.gz", "archive.tar", "gz"},
|
||||
{".gitignore", ".gitignore", ""},
|
||||
{"noext", "noext", ""},
|
||||
{"a.b.c", "a.b", "c"},
|
||||
{".hidden.ext", ".hidden", "ext"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.fullName, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
name, ext := SplitFileName(tc.fullName)
|
||||
assert.Equal(t, tc.wantName, name)
|
||||
assert.Equal(t, tc.wantExt, ext)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFileExtension(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user