@@ -41,6 +41,7 @@ type UserModel struct {
4141 Description string `db:"description"`
4242 HashedPassword string `db:"password"`
4343 DarkMode bool `db:"dark_mode"`
44+ IconHash string `db:"icon_hash"`
4445}
4546
4647type User struct {
@@ -112,26 +113,33 @@ func getIconHandler(c echo.Context) error {
112113 return echo .NewHTTPError (http .StatusInternalServerError , "failed to get user: " + err .Error ())
113114 }
114115
115- files , err := filepath .Glob (fmt .Sprintf ("../img/user-%d-*.jpg" , user .ID ))
116- if err != nil {
117- return echo .NewHTTPError (http .StatusInternalServerError , "failed to glob: " + err .Error ())
116+ if ifNoneMatchValue != "" && ifNoneMatchValue == user .IconHash {
117+ return c .NoContent (http .StatusNotModified )
118118 }
119- if len (files ) == 0 {
120- if ifNoneMatchValue == "d9f8294e9d895f81ce62e73dc7d5dff862a4fa40bd4e0fecf53f7526a8edcac0" {
121- return c .NoContent (http .StatusNotModified )
122- }
123- // c.Response().Header().Set("x-accel-redirect", fmt.Sprintf("/img/%s", filepath.Base(fallbackImage)))
124- // return c.NoContent(http.StatusOK)
125119
126- return c .File (fallbackImage )
127- }
120+ // files, err := filepath.Glob(fmt.Sprintf("../img/user-%d-*.jpg", user.ID))
121+ // if err != nil {
122+ // return echo.NewHTTPError(http.StatusInternalServerError, "failed to glob: "+err.Error())
123+ // }
124+ // if len(files) == 0 {
125+ // if ifNoneMatchValue == "d9f8294e9d895f81ce62e73dc7d5dff862a4fa40bd4e0fecf53f7526a8edcac0" {
126+ // return c.NoContent(http.StatusNotModified)
127+ // }
128+ // // c.Response().Header().Set("x-accel-redirect", fmt.Sprintf("/img/%s", filepath.Base(fallbackImage)))
129+ // // return c.NoContent(http.StatusOK)
128130
129- iconHashStr := strings .Split (files [0 ], "-" )[2 ]
130- if iconHashStr == ifNoneMatchValue {
131- return c .NoContent (http .StatusNotModified )
132- }
131+ // return c.File(fallbackImage)
132+ // }
133+
134+ // iconHashStr := strings.Split(files[0], "-")[2]
135+ // if iconHashStr == ifNoneMatchValue {
136+ // return c.NoContent(http.StatusNotModified)
137+ // }
133138
134- image , err := os .ReadFile (files [0 ])
139+ if user .IconHash == "d9f8294e9d895f81ce62e73dc7d5dff862a4fa40bd4e0fecf53f7526a8edcac0" {
140+ return c .File (fallbackImage )
141+ }
142+ image , err := os .ReadFile (fmt .Sprintf ("../img/user-%d-%s-p.jpg" , user .ID , user .IconHash ))
135143 if err != nil {
136144 return echo .NewHTTPError (http .StatusInternalServerError , "failed to get user icon: " + err .Error ())
137145 }
@@ -151,7 +159,7 @@ func getIconHandler(c echo.Context) error {
151159}
152160
153161func postIconHandler (c echo.Context ) error {
154- //_ := c.Request().Context()
162+ ctx := c .Request ().Context ()
155163
156164 if err := verifyUserSession (c ); err != nil {
157165 // echo.NewHTTPErrorが返っているのでそのまま出力
@@ -182,11 +190,11 @@ func postIconHandler(c echo.Context) error {
182190
183191 os .WriteFile (fmt .Sprintf ("../img/user-%d-%x-p.jpg" , userID , iconHash ), req .Image , 0644 )
184192
185- // tx, err := dbConn.BeginTxx(ctx, nil)
186- // if err != nil {
187- // return echo.NewHTTPError(http.StatusInternalServerError, "failed to begin transaction: "+err.Error())
188- // }
189- // defer tx.Rollback()
193+ tx , err := dbConn .BeginTxx (ctx , nil )
194+ if err != nil {
195+ return echo .NewHTTPError (http .StatusInternalServerError , "failed to begin transaction: " + err .Error ())
196+ }
197+ defer tx .Rollback ()
190198
191199 // if _, err := tx.ExecContext(ctx, "DELETE FROM icons WHERE user_id = ?", userID); err != nil {
192200 // return echo.NewHTTPError(http.StatusInternalServerError, "failed to delete old user icon: "+err.Error())
@@ -197,14 +205,19 @@ func postIconHandler(c echo.Context) error {
197205 // return echo.NewHTTPError(http.StatusInternalServerError, "failed to insert new user icon: "+err.Error())
198206 // }
199207
208+ _ , err = tx .ExecContext (ctx , "UPDATE users SET icon_hash = ? WHERE id = ?" , iconHash , userID )
209+ if err != nil {
210+ return echo .NewHTTPError (http .StatusInternalServerError , "failed to insert new user icon: " + err .Error ())
211+ }
212+
200213 // // iconID, err := rs.LastInsertId()
201214 // // if err != nil {
202215 // // return echo.NewHTTPError(http.StatusInternalServerError, "failed to get last inserted icon id: "+err.Error())
203216 // // }
204217
205- // if err := tx.Commit(); err != nil {
206- // return echo.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
207- // }
218+ if err := tx .Commit (); err != nil {
219+ return echo .NewHTTPError (http .StatusInternalServerError , "failed to commit: " + err .Error ())
220+ }
208221
209222 return c .JSON (http .StatusCreated , & PostIconResponse {
210223 ID : 32 ,
@@ -484,20 +497,20 @@ func fillUserResponse(ctx context.Context, tx *sqlx.Tx, userModel UserModel) (Us
484497 // return User{}, err
485498 // }
486499 // }
487- iconHashStr := ""
488- files , err := filepath .Glob (fmt .Sprintf ("../img/user-%d-*.jpg" , userModel .ID ))
489- if err != nil {
490- return User {}, err
491- }
492- if len (files ) == 0 {
493- iconHashStr = "d9f8294e9d895f81ce62e73dc7d5dff862a4fa40bd4e0fecf53f7526a8edcac0"
494- }
495- for _ , f := range files {
496- ss := strings .Split (f , "-" )
497- iconHashStr = ss [2 ]
500+ // iconHashStr := ""
501+ // files, err := filepath.Glob(fmt.Sprintf("../img/user-%d-*.jpg", userModel.ID))
502+ // if err != nil {
503+ // return User{}, err
504+ // }
505+ // if len(files) == 0 {
506+ // iconHashStr = "d9f8294e9d895f81ce62e73dc7d5dff862a4fa40bd4e0fecf53f7526a8edcac0"
507+ // }
508+ // for _, f := range files {
509+ // ss := strings.Split(f, "-")
510+ // iconHashStr = ss[2]
498511
499- break
500- }
512+ // break
513+ // }
501514
502515 user := User {
503516 ID : userModel .ID ,
@@ -508,7 +521,7 @@ func fillUserResponse(ctx context.Context, tx *sqlx.Tx, userModel UserModel) (Us
508521 ID : userModel .ID ,
509522 DarkMode : userModel .DarkMode ,
510523 },
511- IconHash : iconHashStr ,
524+ IconHash : userModel . IconHash ,
512525 }
513526
514527 return user , nil
0 commit comments