diff --git a/server/system.go b/server/system.go index 6c08a3b..6d49369 100644 --- a/server/system.go +++ b/server/system.go @@ -148,4 +148,26 @@ func (s *Server) registerSystemRoutes(g *echo.Group) { } return nil }) + + g.POST("/system/vacuum", func(c echo.Context) error { + ctx := c.Request().Context() + userID, ok := c.Get(getUserIDContextKey()).(int) + if !ok { + return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session") + } + user, err := s.Store.FindUser(ctx, &api.UserFind{ + ID: &userID, + }) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err) + } + if user == nil || user.Role != api.Host { + return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized") + } + if err := s.Store.Vacuum(ctx); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to vacuum database").SetInternal(err) + } + c.Response().WriteHeader(http.StatusOK) + return nil + }) } diff --git a/web/src/components/Settings/SystemSection.tsx b/web/src/components/Settings/SystemSection.tsx index 8184f17..94c0505 100644 --- a/web/src/components/Settings/SystemSection.tsx +++ b/web/src/components/Settings/SystemSection.tsx @@ -60,6 +60,23 @@ const SystemSection = () => { }); }; + const handleVacuumBtnClick = async () => { + try { + await api.vacuumDatabase(); + const { data: status } = (await api.getSystemStatus()).data; + setState({ + dbSize: status.dbSize, + allowSignUp: status.allowSignUp, + additionalStyle: status.additionalStyle, + additionalScript: status.additionalScript, + }); + } catch (error) { + console.error(error); + return; + } + toastHelper.success("Succeed to vacuum database"); + }; + const handleSaveAdditionalStyle = async () => { try { await api.upsertSystemSetting({ @@ -96,9 +113,14 @@ const SystemSection = () => { return (

{t("common.basic")}

-

- {t("setting.system-section.database-file-size")}: {formatBytes(state.dbSize)} -

+

{t("sidebar.setting")}