From e1859088055cbd1e792ee4cc5cb96b227b7e198d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Omar=20Vergara=20P=C3=A9rez?= Date: Wed, 3 Jan 2024 14:07:00 -0600 Subject: [PATCH] refactor(images): remove avif --- Dockerfile | 72 +++++----------------------- README.md | 2 - go.mod | 2 - go.sum | 4 -- pkg/files/image_factory.go | 2 - pkg/files/images/avif.go | 84 --------------------------------- pkg/files/images/bmp.go | 1 - pkg/files/images/gif.go | 1 - pkg/files/images/images.go | 17 ------- pkg/files/images/images_test.go | 31 ------------ pkg/files/images/jpeg.go | 1 - pkg/files/images/png.go | 1 - pkg/files/images/tiff.go | 1 - pkg/files/images/webp.go | 1 - 14 files changed, 11 insertions(+), 209 deletions(-) delete mode 100644 pkg/files/images/avif.go diff --git a/Dockerfile b/Dockerfile index 97e2265..acd7148 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,75 +1,25 @@ -FROM ubuntu:22.04 AS build +# Build the application from source +FROM golang:1.21 AS builder -ARG GO_VERSION TARGETOS TARGETARCH +ARG TARGETOS +ARG TARGETARCH -ENV GO_VERSION=${GO_VERSION} -ENV GOPATH /go -ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH - -RUN apt-get update && \ - apt-get install -y software-properties-common && \ - add-apt-repository ppa:strukturag/libde265 && \ - add-apt-repository ppa:strukturag/libheif && \ - apt-get install -y --no-install-recommends cmake \ - wget \ - git \ - gcc \ - make \ - pkg-config \ - x265 \ - libx265-dev \ - libde265-dev \ - libjpeg-dev \ - libtool \ - zlib1g-dev \ - libaom-dev \ - libheif1 \ - libheif-dev && \ - apt-get autoremove -y && \ - apt-get purge -y --auto-remove && \ - rm -rf /var/lib/apt/lists/* - -RUN wget --no-check-certificate -P /tmp "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz" && \ - tar -C /usr/local -xzf "/tmp/go${GO_VERSION}.linux-amd64.tar.gz" && \ - rm "/tmp/go${GO_VERSION}.linux-amd64.tar.gz" - -RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH" - -WORKDIR /build +WORKDIR /app COPY go.* ./ RUN go mod download COPY . . -RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags='-s -w' -trimpath -o /app/morphos . - -FROM ubuntu:22.04 +RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o morphos . -WORKDIR /app +# Deploy the application binary into a lean image +FROM debian:bookworm-slim AS release -COPY --from=build /app/morphos . +WORKDIR / -RUN apt-get update && \ - apt-get install -y software-properties-common && \ - add-apt-repository ppa:strukturag/libde265 && \ - add-apt-repository ppa:strukturag/libheif && \ - apt-get install -y --no-install-recommends cmake \ - make \ - pkg-config \ - x265 \ - libx265-dev \ - libde265-dev \ - libjpeg-dev \ - libtool \ - zlib1g-dev \ - libaom-dev \ - libheif1 \ - libheif-dev && \ - apt-get autoremove -y && \ - apt-get purge -y --auto-remove && \ - rm -rf /var/lib/apt/lists/* +COPY --from=builder /app/morphos /bin/morphos EXPOSE 8080 -ENTRYPOINT ["/app/morphos"] +ENTRYPOINT ["/bin/morphos"] diff --git a/README.md b/README.md index a35230d..d219068 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,6 @@ The project is written in Go 1.21. 1. You can run the project on bare metal (this uses air for live-reloading): ``` -$ brew install cmake make pkg-config x265 libde265 libjpeg libtool aom -$ brew install libheif $ make run ``` diff --git a/go.mod b/go.mod index 918a2f6..ed3f6dc 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,10 @@ module github.com/danvergara/morphos go 1.21.3 require ( - github.com/Kagami/go-avif v0.1.0 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 - github.com/strukturag/libheif v1.17.6 golang.org/x/image v0.14.0 golang.org/x/text v0.14.0 ) diff --git a/go.sum b/go.sum index 1ffa75b..494c789 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/Kagami/go-avif v0.1.0 h1:8GHAGLxCdFfhpd4Zg8j1EqO7rtcQNenxIDerC/uu68w= -github.com/Kagami/go-avif v0.1.0/go.mod h1:OPmPqzNdQq3+sXm0HqaUJQ9W/4k+Elbc3RSfJUemDKA= 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= @@ -12,8 +10,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/strukturag/libheif v1.17.6 h1:UFz4FI7kKLINWyL7bcNEBu4gZxK7rHRkwq49IOzHyvE= -github.com/strukturag/libheif v1.17.6/go.mod h1:E/PNRlmVtrtj9j2AvBZlrO4dsBDu6KfwDZn7X1Ce8Ks= 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= diff --git a/pkg/files/image_factory.go b/pkg/files/image_factory.go index 4c00e0a..6681f4c 100644 --- a/pkg/files/image_factory.go +++ b/pkg/files/image_factory.go @@ -26,8 +26,6 @@ func (i *ImageFactory) NewFile(f string) (File, error) { return images.NewTiff(), nil case images.BMP: return images.NewBmp(), nil - case images.AVIF: - return images.NewAvif(), nil default: return nil, fmt.Errorf("type file %s not recognized", f) } diff --git a/pkg/files/images/avif.go b/pkg/files/images/avif.go deleted file mode 100644 index bdfbddb..0000000 --- a/pkg/files/images/avif.go +++ /dev/null @@ -1,84 +0,0 @@ -package images - -import ( - "bytes" - "fmt" - "image" - "slices" - "strings" - - _ "github.com/strukturag/libheif/go/heif" -) - -// Avif struct implements the File and Image interface from the files pkg. -type Avif struct { - compatibleFormats map[string][]string -} - -// NewAvif returns a pointer to a Avif instance. -// The Avif object is set with a map with list of supported file formats. -func NewAvif() *Avif { - a := Avif{ - compatibleFormats: map[string][]string{ - "Image": { - JPG, - JPEG, - PNG, - GIF, - WEBP, - TIFF, - BMP, - }, - }, - } - - return &a -} - -// SupportedFormats returns a map with a slice of supported files. -// Every key of the map represents the kind of a file. -func (a *Avif) SupportedFormats() map[string][]string { - return a.compatibleFormats -} - -// ConvertTo method converts a given file to a target format. -// This method returns a file in form of a slice of bytes. -func (a *Avif) ConvertTo(fileType, subType string, fileBytes []byte) ([]byte, error) { - var result []byte - - compatibleFormats, ok := a.SupportedFormats()[fileType] - if !ok { - return nil, fmt.Errorf("ConvertTo: file type not supported: %s", fileType) - } - - if !slices.Contains(compatibleFormats, subType) { - return nil, fmt.Errorf("ConvertTo: file sub-type not supported: %s", subType) - } - - switch strings.ToLower(fileType) { - case imageType: - img, _, err := image.Decode(bytes.NewReader(fileBytes)) - if err != nil { - return nil, fmt.Errorf( - "ConvertTo: error at decoding avif image: %w", - err, - ) - } - - result, err = convertToImage(subType, img) - if err != nil { - return nil, fmt.Errorf( - "ConvertTo: error at converting image to another format: %w", - err, - ) - } - } - - return result, nil -} - -// ImageType returns the file format of the current image. -// This method implements the Image interface. -func (a *Avif) ImageType() string { - return AVIF -} diff --git a/pkg/files/images/bmp.go b/pkg/files/images/bmp.go index 92ac5f3..c3d66f1 100644 --- a/pkg/files/images/bmp.go +++ b/pkg/files/images/bmp.go @@ -20,7 +20,6 @@ func NewBmp() *Bmp { b := Bmp{ compatibleFormats: map[string][]string{ "Image": { - AVIF, JPG, JPEG, PNG, diff --git a/pkg/files/images/gif.go b/pkg/files/images/gif.go index c4ba1de..c23a35c 100644 --- a/pkg/files/images/gif.go +++ b/pkg/files/images/gif.go @@ -19,7 +19,6 @@ func NewGif() *Gif { g := Gif{ compatibleFormats: map[string][]string{ "Image": { - AVIF, JPG, JPEG, PNG, diff --git a/pkg/files/images/images.go b/pkg/files/images/images.go index b0d75ff..ca30bee 100644 --- a/pkg/files/images/images.go +++ b/pkg/files/images/images.go @@ -9,7 +9,6 @@ import ( "image/png" "strings" - "github.com/Kagami/go-avif" "github.com/chai2010/webp" "golang.org/x/image/bmp" "golang.org/x/image/tiff" @@ -23,7 +22,6 @@ const ( WEBP = "webp" TIFF = "tiff" BMP = "bmp" - AVIF = "avif" imageMimeType = "image/" imageType = "image" @@ -94,16 +92,6 @@ func toBMP(img image.Image) ([]byte, error) { return buf.Bytes(), nil } -func toAvif(img image.Image) ([]byte, error) { - buf := new(bytes.Buffer) - - if err := avif.Encode(buf, img, nil); err != nil { - return nil, err - } - - return buf.Bytes(), nil -} - func ParseMimeType(mimetype string) string { if !strings.Contains(mimetype, imageMimeType) { return mimetype @@ -147,11 +135,6 @@ func convertToImage(target string, img image.Image) ([]byte, error) { if err != nil { return nil, err } - case AVIF: - result, err = toAvif(img) - if err != nil { - return nil, err - } default: return nil, fmt.Errorf("file format to convert to not supported: %s", target) } diff --git a/pkg/files/images/images_test.go b/pkg/files/images/images_test.go index 399c745..ac16866 100644 --- a/pkg/files/images/images_test.go +++ b/pkg/files/images/images_test.go @@ -50,7 +50,6 @@ func TestConvertImage(t *testing.T) { mimetype: "image/jpeg", supportedFormats: map[string][]string{ "Image": { - images.AVIF, images.JPG, images.JPEG, images.GIF, @@ -74,7 +73,6 @@ func TestConvertImage(t *testing.T) { mimetype: "image/png", supportedFormats: map[string][]string{ "Image": { - images.AVIF, images.PNG, images.GIF, images.WEBP, @@ -97,7 +95,6 @@ func TestConvertImage(t *testing.T) { mimetype: "image/png", supportedFormats: map[string][]string{ "Image": { - images.AVIF, images.JPG, images.JPEG, images.PNG, @@ -121,7 +118,6 @@ func TestConvertImage(t *testing.T) { mimetype: "image/webp", supportedFormats: map[string][]string{ "Image": { - images.AVIF, images.JPG, images.JPEG, images.GIF, @@ -145,7 +141,6 @@ func TestConvertImage(t *testing.T) { mimetype: "image/tiff", supportedFormats: map[string][]string{ "Image": { - images.AVIF, images.JPG, images.JPEG, images.PNG, @@ -169,7 +164,6 @@ func TestConvertImage(t *testing.T) { mimetype: "image/png", supportedFormats: map[string][]string{ "Image": { - images.AVIF, images.JPG, images.JPEG, images.PNG, @@ -193,31 +187,6 @@ func TestConvertImage(t *testing.T) { mimetype: "image/bmp", supportedFormats: map[string][]string{ "Image": { - images.AVIF, - images.PNG, - images.GIF, - images.WEBP, - images.TIFF, - images.BMP, - }, - }, - }, - }, - { - name: "avif to png", - input: input{ - filename: "testdata/mr_robot.avif", - mimetype: "image/avif", - targetFileType: "Image", - targetFormat: "png", - imager: images.NewAvif(), - }, - expected: expected{ - mimetype: "image/png", - supportedFormats: map[string][]string{ - "Image": { - images.JPG, - images.JPEG, images.PNG, images.GIF, images.WEBP, diff --git a/pkg/files/images/jpeg.go b/pkg/files/images/jpeg.go index 152ac60..1d746c2 100644 --- a/pkg/files/images/jpeg.go +++ b/pkg/files/images/jpeg.go @@ -19,7 +19,6 @@ func NewJpeg() *Jpeg { j := Jpeg{ compatibleFormats: map[string][]string{ "Image": { - AVIF, PNG, GIF, WEBP, diff --git a/pkg/files/images/png.go b/pkg/files/images/png.go index 912d5b2..12ef33e 100644 --- a/pkg/files/images/png.go +++ b/pkg/files/images/png.go @@ -19,7 +19,6 @@ func NewPng() *Png { p := Png{ compatibleFormats: map[string][]string{ "Image": { - AVIF, JPG, JPEG, GIF, diff --git a/pkg/files/images/tiff.go b/pkg/files/images/tiff.go index 2b2cb86..632f3b6 100644 --- a/pkg/files/images/tiff.go +++ b/pkg/files/images/tiff.go @@ -20,7 +20,6 @@ func NewTiff() *Tiff { t := Tiff{ compatibleFormats: map[string][]string{ "Image": { - AVIF, JPG, JPEG, PNG, diff --git a/pkg/files/images/webp.go b/pkg/files/images/webp.go index 725b16c..4c65bae 100644 --- a/pkg/files/images/webp.go +++ b/pkg/files/images/webp.go @@ -20,7 +20,6 @@ func NewWebp() *Webp { w := Webp{ compatibleFormats: map[string][]string{ "Image": { - AVIF, JPG, JPEG, PNG,