first commit

This commit is contained in:
tchivert committed 2023-06-28 11:18:39 +02:00
commit 2f768fd3da
6 files changed
+199

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
# Build container
FROM golang:alpine AS build
RUN apk add build-base make
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN CGO_ENABLED=1 GOOS=linux go build -o imgcdn .
# Final container
FROM golang:alpine
WORKDIR /app
COPY --from=build /app/imgcdn ./
EXPOSE 8080
ENTRYPOINT ["/app/imgcdn"]
+29
View File
@@ -0,0 +1,29 @@
NAME = imgcdn
USER = razian
REGISTRY = git.rznet.fr
all: build run
build:
docker build . -t ${REGISTRY}/${USER}/${NAME}:latest
run:
touch ./database.db
docker compose up -d
re:
docker compose restart
logs:
docker compose logs
clean: stop rm
stop:
docker compose stop
rm:
docker compose rm -f
push: build
docker push ${REGISTRY}/${USER}/${NAME}:latest
+8
View File
@@ -0,0 +1,8 @@
version: '3'
services:
imgcdn:
image: git.rznet.fr/razian/imgcdn
container_name: imgcdn
ports:
- "8080:8080"
restart: always
+8
View File
@@ -0,0 +1,8 @@
module imgcdn
go 1.20
require (
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
golang.org/x/image v0.8.0
)
+35
View File
@@ -0,0 +1,35 @@
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.8.0 h1:agUcRXV/+w6L9ryntYYsF2x9fQTMd4T8fiiYXAVW6Jg=
golang.org/x/image v0.8.0/go.mod h1:PwLxp3opCYg4WR2WO9P0L6ESnsD6bLTWcw8zanLMVFM=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+95
View File
@@ -0,0 +1,95 @@
package main
import (
"fmt"
"image"
"image/jpeg"
"image/png"
"net/http"
"strconv"
"strings"
"log"
"github.com/nfnt/resize"
"golang.org/x/image/webp"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
referrer := r.Referer()
if referrer == "" {
log.Println("Referrer not found")
http.Error(w, "Referrer not found", http.StatusBadRequest)
return
}
params := r.URL.Query()
width, err := strconv.Atoi(params.Get("w"))
if err != nil {
width = 0
}
height, err := strconv.Atoi(params.Get("h"))
if err != nil {
height = 0
}
percent, err := strconv.Atoi(params.Get("p"))
if err != nil {
percent = 0
}
path := r.URL.Path[1:]
url := referrer + "/" + path
ext := getExt(path)
resp, err := http.Get(url)
if err != nil {
log.Printf("Error getting image: %s\n", err.Error())
http.Redirect(w, r, url, http.StatusSeeOther)
return
}
defer resp.Body.Close()
var img image.Image
switch ext {
case "png":
img, err = png.Decode(resp.Body)
case "webp":
img, err = webp.Decode(resp.Body)
default:
img, _, err = image.Decode(resp.Body)
}
if err != nil {
log.Printf("Error decoding image: %s\n", err.Error())
http.Error(w, "Error decoding image", http.StatusInternalServerError)
return
}
var resizedImg image.Image
if percent > 0 {
resizedImg = resize.Resize(uint(img.Bounds().Dx()*percent/100), uint(img.Bounds().Dy()*percent/100), img, resize.Lanczos3)
} else if width > 0 || height > 0 {
resizedImg = resize.Resize(uint(width), uint(height), img, resize.Lanczos3)
} else {
resizedImg = resize.Resize(0, 500, img, resize.Lanczos3)
}
w.Header().Set("Content-Type", "image/jpeg")
jpeg.Encode(w, resizedImg, nil)
})
fmt.Println("Server started on port 8080")
http.ListenAndServe(":8080", nil)
}
func getExt(s string) (string) {
parts := strings.Split(s, ".")
return parts[len(parts)-1]
}