Browse Source

Merge pull request #12 from matthazinski/delete

Add delete method
pull/14/head
Andrei Marcu 9 years ago
parent
commit
c7e679039a
  1. 48
      delete.go
  2. 2
      fileserve.go
  3. 2
      meta.go
  4. 9
      pages.go
  5. 3
      server.go
  6. 1
      templates.go
  7. 5
      templates/401.html

48
delete.go

@ -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
}
}

2
fileserve.go

@ -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

@ -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

@ -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)
}
}

3
server.go

@ -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)
} }

1
templates.go

@ -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

@ -0,0 +1,5 @@
{% extends "base.html" %}
{% block content %}
401 Unauthorized
{% endblock %}
Loading…
Cancel
Save