diff options
author | Steve Manuel <nilslice@gmail.com> | 2019-03-28 11:07:22 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-28 11:07:22 -0600 |
commit | 8882d90229ec0fd8db592e15585e0627dca379d4 (patch) | |
tree | 320ab1e01086bd346c6fb6d5f8a74fe6ffdd5823 | |
parent | 565462ac1fd7b94caada20cb25baca77c659cf79 (diff) | |
parent | d207c7d0f01bf3debd7b675dec0cf9243dac8a2e (diff) |
Merge pull request #301 from olliephillips/ponzu-dev
adds physical file deletion, when deleting an uploaded file from Ponzu
-rw-r--r-- | system/admin/filesystem.go | 31 | ||||
-rw-r--r-- | system/admin/handlers.go | 13 |
2 files changed, 43 insertions, 1 deletions
diff --git a/system/admin/filesystem.go b/system/admin/filesystem.go index 77c721e..758966b 100644 --- a/system/admin/filesystem.go +++ b/system/admin/filesystem.go @@ -1,10 +1,41 @@ package admin import ( + "encoding/json" "net/http" "os" + "path/filepath" + "strings" + + "github.com/ponzu-cms/ponzu/system/db" + "github.com/ponzu-cms/ponzu/system/item" ) +func deleteUploadFromDisk(target string) error { + // get data on file + data, err := db.Upload(target) + if err != nil { + return err + } + + // unmarshal data + upload := item.FileUpload{} + if err = json.Unmarshal(data, &upload); err != nil { + return err + } + + // split and rebuild path in OS friendly way + // use path to delete the physical file from disk + pathSplit := strings.Split(strings.TrimPrefix(upload.Path, "/api/"), "/") + pathJoin := filepath.Join(pathSplit...) + err = os.Remove(pathJoin) + if err != nil { + return err + } + + return nil +} + func restrict(dir http.Dir) justFilesFilesystem { return justFilesFilesystem{dir} } diff --git a/system/admin/handlers.go b/system/admin/handlers.go index d0818ec..8850f03 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -2200,7 +2200,18 @@ func deleteUploadHandler(res http.ResponseWriter, req *http.Request) { return } - err = db.DeleteUpload(t + ":" + id) + dbTarget := t + ":" + id + + // delete from file system, if good, we continue to delete + // from database, if bad error 500 + err = deleteUploadFromDisk(dbTarget) + if err != nil { + log.Println(err) + res.WriteHeader(http.StatusInternalServerError) + return + } + + err = db.DeleteUpload(dbTarget) if err != nil { log.Println(err) res.WriteHeader(http.StatusInternalServerError) |