Skip to content

Commit 8e6fa9d

Browse files
committed
Remove utils file that was in wrong place
1 parent 68054cd commit 8e6fa9d

File tree

3 files changed

+64
-71
lines changed

3 files changed

+64
-71
lines changed

pkg/repo/utils.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

pkg/routes/profile_photo.go

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package routes
33
import (
44
"errors"
55
"fmt"
6+
"mime/multipart"
67
"net/http"
8+
"path/filepath"
9+
"strings"
10+
"time"
711

812
"github.com/mikestefanello/pagoda/ent"
913
"github.com/mikestefanello/pagoda/pkg/context"
1014
"github.com/mikestefanello/pagoda/pkg/controller"
11-
"github.com/mikestefanello/pagoda/pkg/repo"
1215
"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
1316
storagerepo "github.com/mikestefanello/pagoda/pkg/repos/storage"
1417
"github.com/mikestefanello/pagoda/templates/layouts"
@@ -73,13 +76,13 @@ func (p *currProfilePhoto) Post(ctx echo.Context) error {
7376
}
7477

7578
// Validate and process the image
76-
err = repo.ValidateAndProcessImage(file)
79+
err = ValidateAndProcessImage(file)
7780
ctx.Logger().Error(err)
7881
if err != nil {
7982
// Handle specific errors returned by ValidateAndProcessImage
80-
if errors.Is(err, repo.ErrInvalidMimeType) || errors.Is(err, repo.ErrInvalidFileExtension) {
83+
if errors.Is(err, ErrInvalidMimeType) || errors.Is(err, ErrInvalidFileExtension) {
8184
return echo.NewHTTPError(http.StatusBadRequest, "Invalid file type")
82-
} else if errors.Is(err, repo.ErrImageProcessing) {
85+
} else if errors.Is(err, ErrImageProcessing) {
8386
return echo.NewHTTPError(http.StatusInternalServerError, "Error processing image")
8487
} else {
8588
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
@@ -110,3 +113,57 @@ func (p *currProfilePhoto) Post(ctx echo.Context) error {
110113

111114
return p.ctr.RenderJSON(ctx, nil)
112115
}
116+
117+
var (
118+
ErrInvalidMimeType = errors.New("invalid MIME type")
119+
ErrInvalidFileExtension = errors.New("invalid file extension")
120+
ErrImageProcessing = errors.New("error processing image")
121+
)
122+
123+
func ValidateAndProcessImage(fileHeader *multipart.FileHeader) error {
124+
// TODO: need to do many other checks for file upload security: https://portswigger.net/web-security/file-upload
125+
126+
// Define allowed MIME types
127+
allowedMimeTypes := map[string]bool{
128+
"image/jpeg": true,
129+
"image/webp": true,
130+
"image/png": true,
131+
"image/gif": true,
132+
}
133+
134+
// Define allowed file extensions
135+
allowedExtensions := map[string]bool{
136+
".jpg": true,
137+
".jpeg": true,
138+
".webp": true,
139+
".png": true,
140+
".gif": true,
141+
}
142+
143+
// Check MIME type
144+
contentType := fileHeader.Header.Get("Content-Type")
145+
if !allowedMimeTypes[contentType] {
146+
return ErrInvalidMimeType
147+
}
148+
149+
// Check file extension
150+
extension := strings.ToLower(filepath.Ext(fileHeader.Filename))
151+
if !allowedExtensions[extension] {
152+
return ErrInvalidFileExtension
153+
}
154+
155+
// Open the file
156+
file, err := fileHeader.Open()
157+
if err != nil {
158+
return err
159+
}
160+
defer file.Close()
161+
162+
// TODO: verify that the file actually contains an image
163+
return nil
164+
}
165+
166+
// daysAgo returns a time.Time object for x days ago.
167+
func daysAgo(x int) time.Time {
168+
return time.Now().UTC().AddDate(0, 0, -x)
169+
}

pkg/routes/upload_photo.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/mikestefanello/pagoda/ent"
1010
"github.com/mikestefanello/pagoda/pkg/context"
1111
"github.com/mikestefanello/pagoda/pkg/controller"
12-
"github.com/mikestefanello/pagoda/pkg/repo"
1312
"github.com/mikestefanello/pagoda/pkg/repos/profilerepo"
1413
storagerepo "github.com/mikestefanello/pagoda/pkg/repos/storage"
1514
"github.com/mikestefanello/pagoda/templates/layouts"
@@ -74,12 +73,12 @@ func (p *uploadPhoto) Post(ctx echo.Context) error {
7473
}
7574

7675
// Validate and process the image
77-
err = repo.ValidateAndProcessImage(file)
76+
err = ValidateAndProcessImage(file)
7877
if err != nil {
7978
// Handle specific errors returned by ValidateAndProcessImage
80-
if errors.Is(err, repo.ErrInvalidMimeType) || errors.Is(err, repo.ErrInvalidFileExtension) {
79+
if errors.Is(err, ErrInvalidMimeType) || errors.Is(err, ErrInvalidFileExtension) {
8180
return echo.NewHTTPError(http.StatusBadRequest, "Invalid file type")
82-
} else if errors.Is(err, repo.ErrImageProcessing) {
81+
} else if errors.Is(err, ErrImageProcessing) {
8382
return echo.NewHTTPError(http.StatusInternalServerError, "Error processing image")
8483
} else {
8584
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())

0 commit comments

Comments
 (0)