Delete media files/directories along with corresponding posts/users

pull/734/head
Alborz Jafari 1 year ago
parent 6fbb51eb63
commit 7ee1aec3b7
  1. 12
      admin.go
  2. 19
      posts.go

@ -12,12 +12,14 @@ package writefreely
import (
"database/sql"
"path/filepath"
"fmt"
"net/http"
"runtime"
"strconv"
"strings"
"time"
"os"
"github.com/gorilla/mux"
"github.com/writeas/impart"
@ -341,10 +343,20 @@ func handleAdminDeleteUser(app *App, u *User, w http.ResponseWriter, r *http.Req
return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not delete user account for '%s': %v", username, err)}
}
deleteMediaFilesOfUser(app, username)
_ = addSessionFlash(app, w, r, fmt.Sprintf("User \"%s\" was deleted successfully.", username), nil)
return impart.HTTPError{http.StatusFound, "/admin/users"}
}
func deleteMediaFilesOfUser(app *App, username string) {
mediaDirectoryPath := filepath.Join(app.cfg.Server.MediaParentDir, mediaDir,
username)
err := os.RemoveAll(mediaDirectoryPath)
if err != nil {
log.Error("Deleting media directory of %s failed: %v", username, err)
}
}
func handleAdminToggleUserStatus(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
username := vars["username"]

@ -13,6 +13,7 @@ package writefreely
import (
"database/sql"
"encoding/json"
"path/filepath"
"errors"
"fmt"
"html/template"
@ -21,6 +22,7 @@ import (
"regexp"
"strings"
"time"
"os"
"github.com/gorilla/mux"
"github.com/guregu/null"
@ -799,6 +801,13 @@ func deletePost(app *App, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
friendlyID := vars["post"]
editToken := r.FormValue("token")
var err error
user := getUserSession(app, r)
slug, err := getSlugFromActionId(app, friendlyID)
if err == nil {
deleteMediaFilesOfPost(app, user.Username, slug)
}
var ownerID int64
var u *User
@ -812,7 +821,6 @@ func deletePost(app *App, w http.ResponseWriter, r *http.Request) error {
var res sql.Result
var t *sql.Tx
var err error
var collID sql.NullInt64
var coll *Collection
var pp *PublicPost
@ -911,6 +919,15 @@ func deletePost(app *App, w http.ResponseWriter, r *http.Request) error {
return impart.HTTPError{Status: http.StatusNoContent}
}
func deleteMediaFilesOfPost(app *App, username, slug string) {
mediaDirectoryPath := filepath.Join(app.cfg.Server.MediaParentDir, mediaDir,
username, slug)
err := os.RemoveAll(mediaDirectoryPath)
if err != nil {
log.Error("Deleting media directory of %s failed: %v", username, err)
}
}
// addPost associates a post with the authenticated user.
func addPost(app *App, w http.ResponseWriter, r *http.Request) error {
var ownerID int64

Loading…
Cancel
Save