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 /system/admin/filesystem.go | |
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
Diffstat (limited to 'system/admin/filesystem.go')
-rw-r--r-- | system/admin/filesystem.go | 31 |
1 files changed, 31 insertions, 0 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} } |