feat: vacuum database in setting (#694)

* feat: vacuum database in setting

* update

* update

* update

* update
This commit is contained in:
Zeng1998 2022-12-07 22:45:47 +08:00 committed by GitHub
parent f48226d4f2
commit 147185309c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 65 additions and 10 deletions

View File

@ -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
})
}

View File

@ -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 (
<div className="section-container system-section-container">
<p className="title-text">{t("common.basic")}</p>
<p className="text-value">
{t("setting.system-section.database-file-size")}: <span className="font-mono font-medium">{formatBytes(state.dbSize)}</span>
</p>
<label className="form-label">
<span className="normal-text">
{t("setting.system-section.database-file-size")}: <span className="font-mono font-medium">{formatBytes(state.dbSize)}</span>
</span>
<Button size="sm" onClick={handleVacuumBtnClick}>
{t("common.vacuum")}
</Button>
</label>
<p className="title-text">{t("sidebar.setting")}</p>
<label className="form-label">
<span className="normal-text">{t("setting.system-section.allow-user-signup")}</span>

View File

@ -14,6 +14,10 @@ export function upsertSystemSetting(systemSetting: SystemSetting) {
return axios.post<ResponseObject<SystemSetting>>("/api/system/setting", systemSetting);
}
export function vacuumDatabase() {
return axios.post("/api/system/vacuum");
}
export function signin(username: string, password: string) {
return axios.post<ResponseObject<User>>("/api/auth/signin", {
username,

View File

@ -45,7 +45,8 @@
"fold": "Falten",
"expand": "Erweitern",
"image": "Bild",
"link": "Link"
"link": "Link",
"vacuum": "Vacuum"
},
"slogan": "Eine quelloffene, selbst gehostete Sammlung an Memos zur Wissensverwaltung und Vernetzung.",
"auth": {

View File

@ -45,7 +45,8 @@
"fold": "Fold",
"expand": "Expand",
"image": "Image",
"link": "Link"
"link": "Link",
"vacuum": "Vacuum"
},
"slogan": "An open-source, self-hosted memo hub with knowledge management and socialization.",
"auth": {

View File

@ -45,7 +45,8 @@
"fold": "Plier",
"expand": "Développer",
"image": "Image",
"link": "Link"
"link": "Link",
"vacuum": "Vacuum"
},
"slogan": "An open-source, self-hosted memo hub with knowledge management and socialization.",
"auth": {

View File

@ -41,7 +41,8 @@
"changed": "gewijzigd",
"update-on": "Update op",
"fold": "Invouwen",
"expand": "Uitklappen"
"expand": "Uitklappen",
"vacuum": "Vacuum"
},
"slogan": "Een open-source, zelf-beheerd memo applicatie voor kennismanagement en collaboratie.",
"auth": {

View File

@ -45,7 +45,8 @@
"fold": "Vik ihop",
"expand": "Expandera",
"image": "Bild",
"link": "Länk"
"link": "Länk",
"vacuum": "Vacuum"
},
"slogan": "En öppen källkod, self-hosted antecknings hubb med kunskapshantering och socialisering",
"auth": {

View File

@ -45,7 +45,8 @@
"fold": "Fold",
"expand": "Expand",
"image": "Image",
"link": "Link"
"link": "Link",
"vacuum": "Vacuum"
},
"slogan": "An open-source, self-hosted memo hub with knowledge management and socialization.",
"auth": {

View File

@ -45,7 +45,8 @@
"fold": "折叠",
"expand": "展开",
"image": "图片",
"link": "链接"
"link": "链接",
"vacuum": "清理"
},
"slogan": "An open-source, self-hosted memo hub with knowledge management and socialization.",
"auth": {