diff --git a/Dockerfile b/Dockerfile index f082f29..bd6f145 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /go/src/shioriko COPY . . RUN go get -d -v ./... -RUN go build -o /shioriko +RUN CGO_ENABLED=0 GOOS=linux go build -o /shioriko RUN mkdir -p /web && cp -r web/static web/template /web FROM node:14-alpine AS node_build @@ -13,12 +13,11 @@ COPY . . WORKDIR /src/web/app RUN yarn install && yarn build -FROM alpine AS runtime +FROM scratch AS runtime -RUN mkdir -p /app/web WORKDIR /app -COPY --from=build /shioriko /app -COPY --from=node_build /src/web/static /app/web/static -COPY --from=node_build /src/web/template /app/web/template +COPY --from=build /shioriko /app/ +COPY --from=node_build /src/web/static/ /app/web/static/ +COPY --from=node_build /src/web/template/ /app/web/template/ ENTRYPOINT ["/app/shioriko"] diff --git a/pkg/app/blob_routes.go b/pkg/app/blob_routes.go index 1042565..db10f5f 100644 --- a/pkg/app/blob_routes.go +++ b/pkg/app/blob_routes.go @@ -1,6 +1,10 @@ package app import ( + "image" + _ "image/gif" + _ "image/jpeg" + _ "image/png" "net/http" "os" "path/filepath" @@ -31,7 +35,30 @@ func uploadBlob(c *gin.Context) { Code: http.StatusBadRequest, Message: err.Error(), }) + return } + fileObj, err := file.Open() + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse{ + Code: http.StatusBadRequest, + Message: err.Error(), + }) + return + } + + image, _, err := image.Decode(fileObj) + + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse{ + Code: http.StatusBadRequest, + Message: err.Error(), + }) + return + } + + width := image.Bounds().Dx() + height := image.Bounds().Dy() + id := uuid.NewString() folder1 := id[0:2] if _, err := os.Stat(filepath.Join(dataDir, folder1)); os.IsNotExist(err) { @@ -55,10 +82,14 @@ func uploadBlob(c *gin.Context) { blob := database.Blob{ ID: id, FilePath: filepath.Join(folder1, folder2, filename), + Width: width, + Height: height, } database.DB.Create(&blob) c.JSON(http.StatusOK, models.BlobResponse{ - ID: id, + ID: id, + Width: width, + Height: height, }) } diff --git a/pkg/app/post_routes.go b/pkg/app/post_routes.go index 4844267..bf76a81 100644 --- a/pkg/app/post_routes.go +++ b/pkg/app/post_routes.go @@ -86,6 +86,8 @@ func postGetOne(c *gin.Context) { ImagePath: "/data/" + post.Blob.FilePath, SourceURL: post.SourceURL, Tags: tagStrings, + Width: post.Blob.Width, + Height: post.Blob.Height, }) } diff --git a/pkg/database/blob.go b/pkg/database/blob.go index 3dbd3bb..58308aa 100644 --- a/pkg/database/blob.go +++ b/pkg/database/blob.go @@ -7,6 +7,8 @@ import ( type Blob struct { ID string `gorm:"size:36"` FilePath string + Width int + Height int CreatedAt time.Time UpdatedAt time.Time } diff --git a/pkg/models/read.go b/pkg/models/read.go index 6a48d19..7c30b47 100644 --- a/pkg/models/read.go +++ b/pkg/models/read.go @@ -5,4 +5,6 @@ type PostReadModel struct { ImagePath string `json:"image_path"` SourceURL string `json:"source_url"` Tags []string `json:"tags"` + Width int `json:"width"` + Height int `json:"height"` } diff --git a/pkg/models/responses.go b/pkg/models/responses.go index 816bb64..13ffe12 100644 --- a/pkg/models/responses.go +++ b/pkg/models/responses.go @@ -15,7 +15,9 @@ type UserProfileResponse struct { } type BlobResponse struct { - ID string `json:"id"` + ID string `json:"id"` + Width int `json:"width"` + Height int `json:"height"` } type PostPaginationResponse struct {