From 9ea64498a0752d261e45f15e45843ef09c506cf9 Mon Sep 17 00:00:00 2001 From: Ollie Phillips Date: Mon, 25 Mar 2019 22:51:15 +0000 Subject: delete physical upload from disk --- system/admin/filesystem.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'system/admin/filesystem.go') diff --git a/system/admin/filesystem.go b/system/admin/filesystem.go index 77c721e..21e9740 100644 --- a/system/admin/filesystem.go +++ b/system/admin/filesystem.go @@ -1,10 +1,38 @@ package admin import ( + "encoding/json" "net/http" "os" + "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 + } + + // use path to delete the physical file from disk + delPath := strings.Replace(upload.Path, "/api/", "./", 1) + err = os.Remove(delPath) + if err != nil { + return err + } + + return nil +} + func restrict(dir http.Dir) justFilesFilesystem { return justFilesFilesystem{dir} } -- cgit v1.2.3 From 9348a3e6cd0a1e2b0cd6611b1f7acb04307544e7 Mon Sep 17 00:00:00 2001 From: Ollie Phillips Date: Tue, 26 Mar 2019 09:28:27 +0000 Subject: use filepath.Join for best OS compatiblity --- system/admin/filesystem.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'system/admin/filesystem.go') diff --git a/system/admin/filesystem.go b/system/admin/filesystem.go index 21e9740..e556ec8 100644 --- a/system/admin/filesystem.go +++ b/system/admin/filesystem.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" "os" + "path/filepath" "strings" "github.com/ponzu-cms/ponzu/system/db" @@ -23,9 +24,11 @@ func deleteUploadFromDisk(target string) error { return err } + // split and rebuild path in OS friendly way // use path to delete the physical file from disk - delPath := strings.Replace(upload.Path, "/api/", "./", 1) - err = os.Remove(delPath) + pathSplit := strings.Split(upload.Path, "/") + pathJoin := filepath.Join(pathSplit[2:]...) + err = os.Remove(pathJoin) if err != nil { return err } -- cgit v1.2.3 From d207c7d0f01bf3debd7b675dec0cf9243dac8a2e Mon Sep 17 00:00:00 2001 From: Ollie Phillips Date: Tue, 26 Mar 2019 22:55:36 +0000 Subject: avoid feeding filepath.Join panicable slice --- system/admin/filesystem.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/admin/filesystem.go') diff --git a/system/admin/filesystem.go b/system/admin/filesystem.go index e556ec8..758966b 100644 --- a/system/admin/filesystem.go +++ b/system/admin/filesystem.go @@ -26,8 +26,8 @@ func deleteUploadFromDisk(target string) error { // split and rebuild path in OS friendly way // use path to delete the physical file from disk - pathSplit := strings.Split(upload.Path, "/") - pathJoin := filepath.Join(pathSplit[2:]...) + pathSplit := strings.Split(strings.TrimPrefix(upload.Path, "/api/"), "/") + pathJoin := filepath.Join(pathSplit...) err = os.Remove(pathJoin) if err != nil { return err -- cgit v1.2.3