Add delete method

This commit is contained in:
Matt Hazinski 2015-09-28 23:46:43 -04:00
parent ae1933c93b
commit 51ccc2f6a4
8 changed files with 68 additions and 2 deletions

48
delete.go Normal file
View 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
}
}

View File

@ -12,7 +12,7 @@ func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
fileName := c.URLParams["name"]
filePath := path.Join(Config.filesDir, fileName)
if isFileExpired(fileName) {
if !fileExistsAndNotExpired(fileName) {
notFoundHandler(c, w, r)
return
}

View File

@ -30,7 +30,7 @@ func metadataWrite(filename string, upload *Upload) error {
// Return list of strings from a filename's metadata source
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 {
return nil, err

View File

@ -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) {
w.WriteHeader(500)
err := Templates["oops.html"].ExecuteWriter(pongo2.Context{}, w)
if err != nil {
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)
}
}

View File

@ -67,6 +67,7 @@ func setup() {
// Routing setup
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
selifIndexRe := regexp.MustCompile(`^/selif/$`)
goji.Get("/", indexHandler)
@ -74,12 +75,14 @@ func setup() {
goji.Post("/upload/", uploadPostHandler)
goji.Put("/upload", uploadPutHandler)
goji.Put("/upload/:name", uploadPutHandler)
goji.Delete("/:name", deleteHandler)
staticBox := rice.MustFindBox("static")
goji.Get("/static/*", http.StripPrefix("/static/",
http.FileServer(staticBox.HTTPBox())))
goji.Get(nameRe, fileDisplayHandler)
goji.Get(selifRe, fileServeHandler)
goji.Get(selifIndexRe, unauthorizedHandler)
goji.NotFound(notFoundHandler)
}

BIN
static/images/401.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

View File

@ -45,6 +45,7 @@ func populateTemplatesMap(tSet *pongo2.TemplateSet, tMap map[string]*pongo2.Temp
templates := [...]string{
"index.html",
"404.html",
"401.html",
"oops.html",
"display/audio.html",

5
templates/401.html Normal file
View 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 %}