summaryrefslogtreecommitdiff
path: root/system/admin/filesystem.go
blob: 21e9740528788f66a0f7364b8fcf5732beb13a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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}
}

// the code below removes the open directory listing when accessing a URL which
// normally would point to a directory. code from golang-nuts mailing list:
// https://groups.google.com/d/msg/golang-nuts/bStLPdIVM6w/hidTJgDZpHcJ
// credit: Brad Fitzpatrick (c) 2012

type justFilesFilesystem struct {
	fs http.FileSystem
}

func (fs justFilesFilesystem) Open(name string) (http.File, error) {
	f, err := fs.fs.Open(name)
	if err != nil {
		return nil, err
	}
	return neuteredReaddirFile{f}, nil
}

type neuteredReaddirFile struct {
	http.File
}

func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
	return nil, nil
}