Save paths of deleted posts and show 410 error

This commit is contained in:
Jan-Lukas Else 2021-07-23 09:53:35 +02:00
parent 18a0bfd15b
commit c14ec0dcb5
4 changed files with 32 additions and 1 deletions

View File

@ -240,6 +240,16 @@ func migrateDb(db *sql.DB, logging bool) error {
return err
},
},
&migrator.Migration{
Name: "00020",
Func: func(tx *sql.Tx) error {
_, err := tx.Exec(`
create table deleted (path text primary key);
create index index_post_parameters_par_val_pat on post_parameters (parameter, value, path);
`)
return err
},
},
),
)
if err != nil {

View File

@ -17,6 +17,10 @@ func (a *goBlog) serve404(w http.ResponseWriter, r *http.Request) {
a.serveError(w, r, fmt.Sprintf("%s was not found", r.RequestURI), http.StatusNotFound)
}
func (a *goBlog) serve410(w http.ResponseWriter, r *http.Request) {
a.serveError(w, r, fmt.Sprintf("%s doesn't exist anymore", r.RequestURI), http.StatusGone)
}
func (a *goBlog) serveNotAllowed(w http.ResponseWriter, r *http.Request) {
a.serveError(w, r, "", http.StatusMethodNotAllowed)
}

View File

@ -493,6 +493,8 @@ func (a *goBlog) servePostsAliasesRedirects(pmh ...func(http.Handler) http.Handl
select 'post', status from posts where path = @path
union all
select 'alias', path from post_parameters where parameter = 'aliases' and value = @path
union all
select 'deleted', '' from deleted where path = @path
limit 1
`, sql.Named("path", path))
if err != nil {
@ -527,6 +529,12 @@ func (a *goBlog) servePostsAliasesRedirects(pmh ...func(http.Handler) http.Handl
http.Redirect(w, r, value, http.StatusFound)
}).ServeHTTP(w, r)
return
case "deleted":
// Is deleted, serve 410
alicePrivate.Append(a.cache.cacheMiddleware).ThenFunc(func(w http.ResponseWriter, r *http.Request) {
a.serve410(w, r)
}).ServeHTTP(w, r)
return
}
}
// No post, check regex redirects or serve 404 error

View File

@ -204,11 +204,20 @@ func (db *database) deletePost(path string) (*post, error) {
if path == "" {
return nil, nil
}
db.pcm.Lock()
defer db.pcm.Unlock()
p, err := db.getPost(path)
if err != nil {
return nil, err
}
_, err = db.exec("begin;delete from posts where path = ?;delete from post_parameters where path = ?;commit;", dbNoCache, p.Path, p.Path)
_, err = db.exec(
`begin;
delete from posts where path = ?;
delete from post_parameters where path = ?;
insert or ignore into deleted (path) values (?);
commit;`,
dbNoCache, p.Path, p.Path, p.Path,
)
if err != nil {
return nil, err
}