Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tiff and bmp #13

Merged
merged 6 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.3

require (
github.com/chai2010/webp v1.1.1
github.com/gabriel-vasile/mimetype v1.4.3
github.com/go-chi/chi/v5 v5.0.10
github.com/stretchr/testify v1.8.4
golang.org/x/image v0.14.0
Expand All @@ -12,5 +13,6 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/chai2010/webp v1.1.1 h1:jTRmEccAJ4MGrhFOrPMpNGIJ/eybIgwKpcACsrTEapk=
github.com/chai2010/webp v1.1.1/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -10,6 +12,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
15 changes: 7 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-chi/chi/v5/middleware"

"github.com/danvergara/morphos/pkg/files/images"
"github.com/gabriel-vasile/mimetype"
)

const (
Expand Down Expand Up @@ -91,8 +92,8 @@ func handleUploadFile(w http.ResponseWriter, r *http.Request) {

fileType := r.FormValue("input_format")

detectedFileType := http.DetectContentType(fileBytes)
convertedFile, err = images.ConverImage(detectedFileType, fileType, fileBytes)
detectedFileType := mimetype.Detect(fileBytes)
convertedFile, err = images.ConverImage(detectedFileType.String(), fileType, fileBytes)
if err != nil {
log.Printf("error ocurred while converting image %v", err)
renderError(w, "INVALID_FILE", http.StatusBadRequest)
Expand Down Expand Up @@ -151,17 +152,16 @@ func handleFileFormat(w http.ResponseWriter, r *http.Request) {
return
}

detectedFileType := http.DetectContentType(fileBytes)
detectedFileType := mimetype.Detect(fileBytes)

files := []string{
"templates/partials/form.tmpl",
}

tmpl, err := template.ParseFS(templatesHTML, files...)
formats := images.FileFormatsToConvert(detectedFileType)
formats := images.FileFormatsToConvert(detectedFileType.String())

err = tmpl.ExecuteTemplate(w, "format-elements", formats)
if err != nil {
if err = tmpl.ExecuteTemplate(w, "format-elements", formats); err != nil {
log.Printf("error occurred parsing template files: %v", err)
renderError(w, "FINTERNAL_ERROR", http.StatusInternalServerError)
return
Expand All @@ -182,8 +182,7 @@ func handleModal(w http.ResponseWriter, r *http.Request) {
return
}

err = tmpl.ExecuteTemplate(w, "content", ConvertedFile{Filename: filename})
if err != nil {
if err = tmpl.ExecuteTemplate(w, "content", ConvertedFile{Filename: filename}); err != nil {
log.Printf("error occurred executing template files: %v", err)
renderError(w, "INTERNAL_ERROR", http.StatusInternalServerError)
return
Expand Down
77 changes: 76 additions & 1 deletion pkg/files/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"

"github.com/chai2010/webp"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
webpx "golang.org/x/image/webp"
)

Expand All @@ -19,6 +21,8 @@ const (
JPG = "jpg"
GIF = "gif"
WEBP = "webp"
TIFF = "tiff"
BMP = "bmp"

imageMimeType = "image/"
)
Expand Down Expand Up @@ -62,6 +66,19 @@ func ConverImage(from, to string, imageBytes []byte) ([]byte, error) {
}
case WEBP:
img, err = webpx.Decode(bytes.NewReader(imageBytes))
if err != nil {
return nil, err
}
case TIFF:
img, err = tiff.Decode(bytes.NewReader(imageBytes))
if err != nil {
return nil, err
}
case BMP:
img, err = bmp.Decode(bytes.NewReader(imageBytes))
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("file format %s not supported", from)
}
Expand All @@ -87,6 +104,16 @@ func ConverImage(from, to string, imageBytes []byte) ([]byte, error) {
if err != nil {
return nil, err
}
case TIFF:
result, err = toTIFF(img)
if err != nil {
return nil, err
}
case BMP:
result, err = toBMP(img)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("file format to conver to %s not supported", to)
}
Expand Down Expand Up @@ -138,6 +165,27 @@ func toWEBP(img image.Image) ([]byte, error) {
return buf.Bytes(), nil
}

func toTIFF(img image.Image) ([]byte, error) {
buf := new(bytes.Buffer)

// encode the image as a TIFF file.
if err := tiff.Encode(buf, img, nil); err != nil {
return nil, err
}

return buf.Bytes(), nil
}

func toBMP(img image.Image) ([]byte, error) {
buf := new(bytes.Buffer)

if err := bmp.Encode(buf, img); err != nil {
return nil, err
}

return buf.Bytes(), nil
}

func FileFormatsToConvert(to string) map[string][]FileFormat {
formats := make(map[string][]FileFormat)

Expand All @@ -150,6 +198,8 @@ func FileFormatsToConvert(to string) map[string][]FileFormat {
{Name: PNG},
{Name: GIF},
{Name: WEBP},
{Name: TIFF},
{Name: BMP},
},
}
case PNG:
Expand All @@ -158,6 +208,8 @@ func FileFormatsToConvert(to string) map[string][]FileFormat {
{Name: JPG},
{Name: GIF},
{Name: WEBP},
{Name: TIFF},
{Name: BMP},
},
}
case GIF:
Expand All @@ -166,6 +218,8 @@ func FileFormatsToConvert(to string) map[string][]FileFormat {
{Name: JPG},
{Name: PNG},
{Name: WEBP},
{Name: TIFF},
{Name: BMP},
},
}
case WEBP:
Expand All @@ -174,10 +228,31 @@ func FileFormatsToConvert(to string) map[string][]FileFormat {
{Name: JPG},
{Name: PNG},
{Name: GIF},
{Name: TIFF},
{Name: BMP},
},
}
case TIFF:
formats = map[string][]FileFormat{
"Formats": {
{Name: JPG},
{Name: PNG},
{Name: GIF},
{Name: WEBP},
{Name: BMP},
},
}
case BMP:
formats = map[string][]FileFormat{
"Formats": {
{Name: JPG},
{Name: PNG},
{Name: GIF},
{Name: WEBP},
{Name: TIFF},
},
}
}

return formats
}

Expand Down
83 changes: 77 additions & 6 deletions pkg/files/images/images_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package images_test

import (
"net/http"
"os"
"testing"

"github.com/danvergara/morphos/pkg/files/images"
"github.com/gabriel-vasile/mimetype"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -67,21 +67,54 @@ func TestConvertImage(t *testing.T) {
mimetype: "image/webp",
},
},
{
name: "webp to tiff",
input: input{
filename: "testdata/gopher.webp",
mimetype: "image/webp",
targetFormat: "tiff",
},
expected: expected{
mimetype: "image/tiff",
},
},
{
name: "bmp to png",
input: input{
filename: "testdata/sunset.bmp",
mimetype: "image/bmp",
targetFormat: "png",
},
expected: expected{
mimetype: "image/png",
},
},
{
name: "jpg to bmp",
input: input{
filename: "testdata/Golang_Gopher.jpg",
mimetype: "image/jpeg",
targetFormat: "bmp",
},
expected: expected{
mimetype: "image/bmp",
},
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
inputImg, err := os.ReadFile(tc.input.filename)
require.NoError(t, err)

detectedFileType := http.DetectContentType(inputImg)
require.Equal(t, tc.input.mimetype, detectedFileType)
detectedFileType := mimetype.Detect(inputImg)
require.Equal(t, tc.input.mimetype, detectedFileType.String())

convertedImg, err := images.ConverImage(detectedFileType, tc.input.targetFormat, inputImg)
convertedImg, err := images.ConverImage(detectedFileType.String(), tc.input.targetFormat, inputImg)
require.NoError(t, err)

detectedFileType = http.DetectContentType(convertedImg)
require.Equal(t, tc.expected.mimetype, detectedFileType)
detectedFileType = mimetype.Detect(convertedImg)
require.Equal(t, tc.expected.mimetype, detectedFileType.String())
})
}
}
Expand Down Expand Up @@ -109,6 +142,8 @@ func TestFileFormatsToConvert(t *testing.T) {
{Name: images.PNG},
{Name: images.GIF},
{Name: images.WEBP},
{Name: images.TIFF},
{Name: images.BMP},
},
},
},
Expand All @@ -122,6 +157,8 @@ func TestFileFormatsToConvert(t *testing.T) {
{Name: images.JPG},
{Name: images.GIF},
{Name: images.WEBP},
{Name: images.TIFF},
{Name: images.BMP},
},
},
},
Expand All @@ -135,6 +172,8 @@ func TestFileFormatsToConvert(t *testing.T) {
{Name: images.JPG},
{Name: images.PNG},
{Name: images.WEBP},
{Name: images.TIFF},
{Name: images.BMP},
},
},
},
Expand All @@ -148,6 +187,38 @@ func TestFileFormatsToConvert(t *testing.T) {
{Name: images.JPG},
{Name: images.PNG},
{Name: images.GIF},
{Name: images.TIFF},
{Name: images.BMP},
},
},
},
{
name: "TIFF",
input: input{
format: images.TIFF,
},
expected: expected{
targetFormats: []images.FileFormat{
{Name: images.JPG},
{Name: images.PNG},
{Name: images.GIF},
{Name: images.WEBP},
{Name: images.BMP},
},
},
},
{
name: "BMP",
input: input{
format: images.BMP,
},
expected: expected{
targetFormats: []images.FileFormat{
{Name: images.JPG},
{Name: images.PNG},
{Name: images.GIF},
{Name: images.WEBP},
{Name: images.TIFF},
},
},
},
Expand Down
Binary file added pkg/files/images/testdata/sunset.bmp
Binary file not shown.