Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit 171732e

Browse files
committed
sha256はファイル名にいれる
1 parent 02ebcbb commit 171732e

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

webapp/go/user_handler.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"net/http"
1111
"os"
1212
"os/exec"
13+
"path/filepath"
14+
"strings"
1315
"time"
1416

1517
"github.com/google/uuid"
@@ -104,12 +106,20 @@ func getIconHandler(c echo.Context) error {
104106
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user: "+err.Error())
105107
}
106108

107-
_, err = os.Stat(fmt.Sprintf("../img/user-%d.jpg", user.ID))
109+
files, err := filepath.Glob(fmt.Sprintf("../img/user-%d-*.jpg", user.ID))
108110
if err != nil {
109-
// ないね
111+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to glob: "+err.Error())
112+
}
113+
for _, f := range files {
114+
if err := os.Remove(f); err != nil {
115+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to remove file: "+err.Error())
116+
}
117+
}
118+
119+
if len(files) == 0 {
110120
return c.File(fallbackImage)
111121
}
112-
image, err := os.ReadFile(fmt.Sprintf("../img/user-%d.jpg", user.ID))
122+
image, err := os.ReadFile(files[0])
113123
if err != nil {
114124
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user icon: "+err.Error())
115125
}
@@ -143,7 +153,19 @@ func postIconHandler(c echo.Context) error {
143153
return echo.NewHTTPError(http.StatusBadRequest, "failed to decode the request body as json")
144154
}
145155

146-
os.WriteFile(fmt.Sprintf("../img/user-%d.jpg", userID), req.Image, 0644)
156+
iconHash := sha256.Sum256(req.Image)
157+
158+
files, err := filepath.Glob(fmt.Sprintf("../img/user-%d-*.jpg", userID))
159+
if err != nil {
160+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to glob: "+err.Error())
161+
}
162+
for _, f := range files {
163+
if err := os.Remove(f); err != nil {
164+
return echo.NewHTTPError(http.StatusInternalServerError, "failed to remove file: "+err.Error())
165+
}
166+
}
167+
168+
os.WriteFile(fmt.Sprintf("../img/user-%d-%x.jpg", userID, iconHash), req.Image, 0644)
147169

148170
// tx, err := dbConn.BeginTxx(ctx, nil)
149171
// if err != nil {
@@ -415,7 +437,6 @@ func fillUserResponse(ctx context.Context, tx *sqlx.Tx, userModel UserModel) (Us
415437
return User{}, err
416438
}
417439

418-
var image []byte
419440
// if err := tx.GetContext(ctx, &image, "SELECT image FROM icons WHERE user_id = ?", userModel.ID); err != nil {
420441
// if !errors.Is(err, sql.ErrNoRows) {
421442
// return User{}, err
@@ -425,20 +446,19 @@ func fillUserResponse(ctx context.Context, tx *sqlx.Tx, userModel UserModel) (Us
425446
// return User{}, err
426447
// }
427448
// }
428-
_, err := os.Stat(fmt.Sprintf("../img/user-%d.jpg", userModel.ID))
449+
iconHashStr := ""
450+
files, err := filepath.Glob(fmt.Sprintf("../img/user-%d-*.jpg", userModel.ID))
429451
if err != nil {
430-
// ないね
431-
image, err = os.ReadFile(fallbackImage)
432-
if err != nil {
433-
return User{}, err
434-
}
435-
} else {
436-
image, err = os.ReadFile(fmt.Sprintf("../img/user-%d.jpg", userModel.ID))
437-
if err != nil {
438-
return User{}, err
439-
}
452+
return User{}, err
453+
}
454+
if len(files) == 0 {
455+
iconHashStr = "d9f8294e9d895f81ce62e73dc7d5dff862a4fa40bd4e0fecf53f7526a8edcac0"
456+
}
457+
for _, f := range files {
458+
ss := strings.Split(f, "-")
459+
iconHashStr = ss[1]
460+
break
440461
}
441-
iconHash := sha256.Sum256(image)
442462

443463
user := User{
444464
ID: userModel.ID,
@@ -449,7 +469,7 @@ func fillUserResponse(ctx context.Context, tx *sqlx.Tx, userModel UserModel) (Us
449469
ID: themeModel.ID,
450470
DarkMode: themeModel.DarkMode,
451471
},
452-
IconHash: fmt.Sprintf("%x", iconHash),
472+
IconHash: iconHashStr,
453473
}
454474

455475
return user, nil

0 commit comments

Comments
 (0)