Add delete method
This commit is contained in:
parent
ae1933c93b
commit
51ccc2f6a4
48
delete.go
Normal file
48
delete.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"github.com/zenazn/goji/web"
|
||||||
|
)
|
||||||
|
|
||||||
|
func deleteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
||||||
|
requestKey := r.Header.Get("X-Delete-Key")
|
||||||
|
|
||||||
|
filename := c.URLParams["name"]
|
||||||
|
filePath := path.Join(Config.filesDir, filename)
|
||||||
|
metaPath := path.Join(Config.metaDir, filename)
|
||||||
|
|
||||||
|
// Ensure requested file actually exists
|
||||||
|
if _, readErr := os.Stat(filePath); os.IsNotExist(readErr) {
|
||||||
|
notFoundHandler(c, w, r) // 404 - file doesn't exist
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure delete key is correct
|
||||||
|
deleteKey, err := metadataGetDeleteKey(filename)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
unauthorizedHandler(c, w, r) // 401 - no metadata available
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if deleteKey == requestKey {
|
||||||
|
fileDelErr := os.Remove(filePath)
|
||||||
|
metaDelErr := os.Remove(metaPath)
|
||||||
|
|
||||||
|
if (fileDelErr != nil) || (metaDelErr != nil) {
|
||||||
|
oopsHandler(c, w, r) // 500 - can't delete something
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
notFoundHandler(c, w, r) // 404 - file deleted
|
||||||
|
return
|
||||||
|
|
||||||
|
} else {
|
||||||
|
unauthorizedHandler(c, w, r) // 401 - wrong delete key
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,7 @@ func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||||||
fileName := c.URLParams["name"]
|
fileName := c.URLParams["name"]
|
||||||
filePath := path.Join(Config.filesDir, fileName)
|
filePath := path.Join(Config.filesDir, fileName)
|
||||||
|
|
||||||
if isFileExpired(fileName) {
|
if !fileExistsAndNotExpired(fileName) {
|
||||||
notFoundHandler(c, w, r)
|
notFoundHandler(c, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
2
meta.go
2
meta.go
@ -30,7 +30,7 @@ func metadataWrite(filename string, upload *Upload) error {
|
|||||||
|
|
||||||
// Return list of strings from a filename's metadata source
|
// Return list of strings from a filename's metadata source
|
||||||
func metadataRead(filename string) ([]string, error) {
|
func metadataRead(filename string) ([]string, error) {
|
||||||
file, err := os.Create(path.Join(Config.metaDir, filename))
|
file, err := os.Open(path.Join(Config.metaDir, filename))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
9
pages.go
9
pages.go
@ -23,8 +23,17 @@ func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(500)
|
||||||
err := Templates["oops.html"].ExecuteWriter(pongo2.Context{}, w)
|
err := Templates["oops.html"].ExecuteWriter(pongo2.Context{}, w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
oopsHandler(c, w, r)
|
oopsHandler(c, w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unauthorizedHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(401)
|
||||||
|
err := Templates["401.html"].ExecuteWriter(pongo2.Context{}, w)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -67,6 +67,7 @@ func setup() {
|
|||||||
// Routing setup
|
// Routing setup
|
||||||
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
|
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
|
||||||
selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
|
selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
|
||||||
|
selifIndexRe := regexp.MustCompile(`^/selif/$`)
|
||||||
|
|
||||||
goji.Get("/", indexHandler)
|
goji.Get("/", indexHandler)
|
||||||
|
|
||||||
@ -74,12 +75,14 @@ func setup() {
|
|||||||
goji.Post("/upload/", uploadPostHandler)
|
goji.Post("/upload/", uploadPostHandler)
|
||||||
goji.Put("/upload", uploadPutHandler)
|
goji.Put("/upload", uploadPutHandler)
|
||||||
goji.Put("/upload/:name", uploadPutHandler)
|
goji.Put("/upload/:name", uploadPutHandler)
|
||||||
|
goji.Delete("/:name", deleteHandler)
|
||||||
|
|
||||||
staticBox := rice.MustFindBox("static")
|
staticBox := rice.MustFindBox("static")
|
||||||
goji.Get("/static/*", http.StripPrefix("/static/",
|
goji.Get("/static/*", http.StripPrefix("/static/",
|
||||||
http.FileServer(staticBox.HTTPBox())))
|
http.FileServer(staticBox.HTTPBox())))
|
||||||
goji.Get(nameRe, fileDisplayHandler)
|
goji.Get(nameRe, fileDisplayHandler)
|
||||||
goji.Get(selifRe, fileServeHandler)
|
goji.Get(selifRe, fileServeHandler)
|
||||||
|
goji.Get(selifIndexRe, unauthorizedHandler)
|
||||||
goji.NotFound(notFoundHandler)
|
goji.NotFound(notFoundHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
static/images/401.jpg
Normal file
BIN
static/images/401.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 105 KiB |
@ -45,6 +45,7 @@ func populateTemplatesMap(tSet *pongo2.TemplateSet, tMap map[string]*pongo2.Temp
|
|||||||
templates := [...]string{
|
templates := [...]string{
|
||||||
"index.html",
|
"index.html",
|
||||||
"404.html",
|
"404.html",
|
||||||
|
"401.html",
|
||||||
"oops.html",
|
"oops.html",
|
||||||
|
|
||||||
"display/audio.html",
|
"display/audio.html",
|
||||||
|
5
templates/401.html
Normal file
5
templates/401.html
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<a href="/"><img style="border:0;" src='/static/images/401.jpg' width='400'></a>
|
||||||
|
{% endblock %}
|
Loading…
Reference in New Issue
Block a user