Skip to content

Commit a927e08

Browse files
authored
Improvements to image endpoints (#96)
- Removed `AltText` from Image definition since it is no longer used. - Added `Maps` to check for valid file types and content types to make use of O(1) checks for Sets/Maps.
1 parent 8e125a9 commit a927e08

File tree

5 files changed

+43
-30
lines changed

5 files changed

+43
-30
lines changed

admin-app/image.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ import (
66
"net/http"
77
"os"
88
"path/filepath"
9-
"slices"
109

1110
"github.com/fossoreslp/go-uuid-v4"
1211
"github.com/gin-gonic/gin"
1312
"github.com/matheusgomes28/urchin/common"
1413
"github.com/rs/zerolog/log"
1514
)
1615

16+
var allowed_extensions = map[string]bool{
17+
".jpeg": true, ".jpg": true, ".png": true,
18+
}
19+
20+
var allowed_content_types = map[string]bool{
21+
"image/jpeg": true, "image/png": true, "image/gif": true,
22+
}
23+
1724
// TODO : need these endpoints
1825
// r.POST("/images", postImageHandler(&database))
1926
// r.DELETE("/images", deleteImageHandler(&database))
@@ -38,9 +45,9 @@ func postImageHandler(app_settings common.AppSettings) func(*gin.Context) {
3845
}
3946

4047
file := file_array[0]
41-
allowed_types := []string{"image/jpeg", "image/png", "image/gif"}
4248
file_content_type := file.Header.Get("content-type")
43-
if !slices.Contains(allowed_types, file_content_type) {
49+
_, ok := allowed_content_types[file_content_type]
50+
if !ok {
4451
log.Error().Msgf("file type not supported")
4552
c.JSON(http.StatusBadRequest, common.MsgErrorRes("file type not supported"))
4653
return
@@ -60,10 +67,10 @@ func postImageHandler(app_settings common.AppSettings) func(*gin.Context) {
6067
return
6168
}
6269

63-
allowed_extensions := []string{"jpeg", "jpg", "png"}
6470
ext := filepath.Ext(file.Filename)
6571
// check ext is supported
66-
if ext == "" && slices.Contains(allowed_extensions, ext) {
72+
_, ok = allowed_extensions[ext]
73+
if ext == "" || !ok {
6774
log.Error().Msgf("file extension is not supported %v", err)
6875
c.JSON(http.StatusBadRequest, common.ErrorRes("file extension is not supported", err))
6976
return

app/image.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"os"
66
"path"
7-
"slices"
87
"strconv"
98

109
"github.com/gin-gonic/gin"
@@ -14,9 +13,18 @@ import (
1413
"github.com/rs/zerolog/log"
1514
)
1615

16+
// Since there are no builtin sets in go, we are using a map to improve the performance when checking for valid extensions
17+
// by creating a map with the valid extensions as keys and using an existence check.
18+
var valid_extensions = map[string]bool{
19+
".jpg": true,
20+
".jpeg": true,
21+
".png": true,
22+
".gif": true,
23+
}
24+
1725
func imagesHandler(c *gin.Context, app_settings common.AppSettings, database database.Database) ([]byte, error) {
1826
// TODO: Implement rendering.
19-
pageNum := 0 // Default to page 0
27+
pageNum := 1 // Default to page 0
2028
if pageNumQuery := c.Param("num"); pageNumQuery != "" {
2129
num, err := strconv.Atoi(pageNumQuery)
2230
if err == nil && num > 0 {
@@ -27,7 +35,7 @@ func imagesHandler(c *gin.Context, app_settings common.AppSettings, database dat
2735
}
2836

2937
limit := 10 // or whatever limit you want
30-
offset := max((pageNum-1)*limit, 0)
38+
offset := (pageNum - 1) * limit
3139

3240
// Get all the files inside the image directory
3341
files, err := os.ReadDir(app_settings.ImageDirectory)
@@ -38,9 +46,7 @@ func imagesHandler(c *gin.Context, app_settings common.AppSettings, database dat
3846

3947
// Filter all the non-images out of the list
4048
valid_images := make([]common.Image, 0)
41-
valid_extensions := []string{".jpg", ".jpeg", ".png", ".gif"}
4249
for n, file := range files {
43-
4450
// TODO : This is surely not the best way
4551
// to implement pagination in for loops
4652
if n >= limit {
@@ -53,16 +59,19 @@ func imagesHandler(c *gin.Context, app_settings common.AppSettings, database dat
5359

5460
filename := file.Name()
5561
ext := path.Ext(file.Name())
56-
if slices.Contains(valid_extensions, ext) {
57-
58-
image := common.Image{
59-
Uuid: filename[:len(filename)-len(ext)],
60-
Name: filename,
61-
AltText: "undefined", // TODO : perhaps remove this
62-
Ext: ext,
63-
}
64-
valid_images = append(valid_images, image)
62+
// Checking for the existence of a value in a map takes O(1) and therefore it's faster than
63+
// iterating over a string slice
64+
_, ok := valid_extensions[ext]
65+
if !ok {
66+
continue
67+
}
68+
69+
image := common.Image{
70+
Uuid: filename[:len(filename)-len(ext)],
71+
Name: filename,
72+
Ext: ext,
6573
}
74+
valid_images = append(valid_images, image)
6675
}
6776

6877
index_view := views.MakeImagesPage(valid_images, app_settings.AppNavbar.Links)
@@ -89,10 +98,9 @@ func imageHandler(c *gin.Context, app_settings common.AppSettings, database data
8998
name := filename[:len(filename)-len(ext)]
9099

91100
image := common.Image{
92-
Uuid: name,
93-
Name: filename,
94-
AltText: "undefined",
95-
Ext: ext,
101+
Uuid: name,
102+
Name: filename,
103+
Ext: ext,
96104
}
97105
index_view := views.MakeImagePage(image, app_settings.AppNavbar.Links)
98106
html_buffer := bytes.NewBuffer(nil)

common/image.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package common
22

33
type Image struct {
4-
Uuid string `json:"uuid"`
5-
Name string `json:"name"`
6-
AltText string `json:"alt_text"`
7-
Ext string `json:"extension"`
4+
Uuid string `json:"uuid"`
5+
Name string `json:"name"`
6+
Ext string `json:"extension"`
87
}

views/image.templ

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ templ MakeImagePage(image Image, links []Link) {
2323
<main>
2424
<article>
2525
<h3>{ image.Name }</h3>
26-
<img src={fmt.Sprintf("/images/data/%s", image.Name)} alt={image.AltText} />
27-
<p>{ image.AltText }</p>
26+
<img src={fmt.Sprintf("/images/data/%s", image.Name)} />
2827
</article>
2928
</main>
3029
<a href={templ.URL("/images")}>Back</a>

views/images.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ templ MakeImagesPage(images []Image, links []Link) {
4848
for _, image := range images {
4949
<a href={templ.URL("/images/" + image.Name)}>
5050
<div>
51-
<img src={fmt.Sprintf("/images/data/%s", image.Name)} alt={image.AltText} />
51+
<img src={fmt.Sprintf("/images/data/%s", image.Name)} />
5252
<span>{image.Name}</span>
5353
</div>
5454
</a>

0 commit comments

Comments
 (0)