2015-09-28 23:46:43 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-04 02:25:00 -04:00
|
|
|
"fmt"
|
2015-09-28 23:46:43 -04:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func deleteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
|
2015-10-06 00:31:09 -04:00
|
|
|
requestKey := r.Header.Get("Linx-Delete-Key")
|
2015-09-28 23:46:43 -04:00
|
|
|
|
|
|
|
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) {
|
2015-10-04 12:47:20 -04:00
|
|
|
oopsHandler(c, w, r, RespPLAIN, "Could not delete")
|
2015-09-28 23:46:43 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-04 02:25:00 -04:00
|
|
|
fmt.Fprintf(w, "DELETED")
|
2015-09-28 23:46:43 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
} else {
|
|
|
|
unauthorizedHandler(c, w, r) // 401 - wrong delete key
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|