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

Commit

Permalink
iconをファイルに
Browse files Browse the repository at this point in the history
  • Loading branch information
acidlemon committed Nov 25, 2023
1 parent dd2916b commit 0f3e4cb
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions webapp/go/user_handler.go
Expand Up @@ -104,20 +104,29 @@ func getIconHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user: "+err.Error())
}

var image []byte
if err := tx.GetContext(ctx, &image, "SELECT image FROM icons WHERE user_id = ?", user.ID); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return c.File(fallbackImage)
} else {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user icon: "+err.Error())
}
_, err = os.Stat(fmt.Sprintf("../img/user-%d.jpg", user.ID))
if err != nil {
// ないね
return c.File(fallbackImage)
}
image, err := os.ReadFile(fmt.Sprintf("../img/user-%d.jpg", user.ID))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user icon: "+err.Error())
}

// if err := tx.GetContext(ctx, &image, "SELECT image FROM icons WHERE user_id = ?", user.ID); err != nil {
// if errors.Is(err, sql.ErrNoRows) {
// return c.File(fallbackImage)
// } else {
// return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user icon: "+err.Error())
// }
// }

return c.Blob(http.StatusOK, "image/jpeg", image)
}

func postIconHandler(c echo.Context) error {
ctx := c.Request().Context()
//_ := c.Request().Context()

if err := verifyUserSession(c); err != nil {
// echo.NewHTTPErrorが返っているのでそのまま出力
Expand All @@ -134,29 +143,31 @@ func postIconHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, "failed to decode the request body as json")
}

tx, err := dbConn.BeginTxx(ctx, nil)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to begin transaction: "+err.Error())
}
defer tx.Rollback()
os.WriteFile(fmt.Sprintf("../img/user-%d.jpg", userID), req.Image, 0644)

if _, err := tx.ExecContext(ctx, "DELETE FROM icons WHERE user_id = ?", userID); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to delete old user icon: "+err.Error())
}
// tx, err := dbConn.BeginTxx(ctx, nil)
// if err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, "failed to begin transaction: "+err.Error())
// }
// defer tx.Rollback()

_, err = tx.ExecContext(ctx, "INSERT INTO icons (user_id, image) VALUES (?, ?)", userID, req.Image)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to insert new user icon: "+err.Error())
}
// if _, err := tx.ExecContext(ctx, "DELETE FROM icons WHERE user_id = ?", userID); err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, "failed to delete old user icon: "+err.Error())
// }

// iconID, err := rs.LastInsertId()
// _, err = tx.ExecContext(ctx, "INSERT INTO icons (user_id, image) VALUES (?, ?)", userID, req.Image)
// if err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, "failed to get last inserted icon id: "+err.Error())
// return echo.NewHTTPError(http.StatusInternalServerError, "failed to insert new user icon: "+err.Error())
// }

if err := tx.Commit(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
}
// // iconID, err := rs.LastInsertId()
// // if err != nil {
// // return echo.NewHTTPError(http.StatusInternalServerError, "failed to get last inserted icon id: "+err.Error())
// // }

// if err := tx.Commit(); err != nil {
// return echo.NewHTTPError(http.StatusInternalServerError, "failed to commit: "+err.Error())
// }

return c.JSON(http.StatusCreated, &PostIconResponse{
ID: 32,
Expand Down

0 comments on commit 0f3e4cb

Please sign in to comment.