mirror of
https://github.com/vbatts/imgsrv.git
synced 2025-01-12 23:20:06 +00:00
Issue #4 Now there is a link and flow to deleting images, not just
through REST
This commit is contained in:
parent
7190667271
commit
6683ae4498
2 changed files with 99 additions and 8 deletions
56
layouts.go
56
layouts.go
|
@ -65,6 +65,34 @@ var tailTemplateHTML = `
|
||||||
</html>
|
</html>
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var formDeleteFileTemplate = template.Must(template.New("formDeleteFile").Parse(formDeleteFileTemplateHTML))
|
||||||
|
var formDeleteFileTemplateHTML = `
|
||||||
|
<div class="span9">
|
||||||
|
<div class="hero-unit">
|
||||||
|
<h3>Get file from URL</h3>
|
||||||
|
{{if .}}
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<b>Are you sure?</b>
|
||||||
|
</tr>
|
||||||
|
<br/>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<a href="/v/{{.}}">no!</a>
|
||||||
|
<br/>
|
||||||
|
<a href="/f/{{.}}?delete=true&confirm=true">yes! delete!</a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
{{else}}
|
||||||
|
<p>
|
||||||
|
<b>ERROR: No File provided!</b>
|
||||||
|
</p>
|
||||||
|
{{end}}
|
||||||
|
</div>{{/* hero-unit */}}
|
||||||
|
</div>{{/* span9 */}}
|
||||||
|
`
|
||||||
|
|
||||||
var formGetUrlTemplate = template.Must(template.New("formGetUrl").Parse(formGetUrlTemplateHTML))
|
var formGetUrlTemplate = template.Must(template.New("formGetUrl").Parse(formGetUrlTemplateHTML))
|
||||||
var formGetUrlTemplateHTML = `
|
var formGetUrlTemplateHTML = `
|
||||||
<div class="span9">
|
<div class="span9">
|
||||||
|
@ -180,9 +208,37 @@ var fileViewInfoTemplateHTML = `
|
||||||
[size: {{.Length}}]
|
[size: {{.Length}}]
|
||||||
<br/>
|
<br/>
|
||||||
[UploadDate: {{.Metadata.TimeStamp}}]
|
[UploadDate: {{.Metadata.TimeStamp}}]
|
||||||
|
<br/>
|
||||||
|
[<a href="/f/{{.Filename}}?delete=true">Delete</a>]
|
||||||
{{end}}
|
{{end}}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
func DeleteFilePage(w io.Writer, filename string) (err error) {
|
||||||
|
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: delete"})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = navbarTemplate.Execute(w, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = containerBeginTemplate.Execute(w, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = formDeleteFileTemplate.Execute(w, &filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func UrliePage(w io.Writer) (err error) {
|
func UrliePage(w io.Writer) (err error) {
|
||||||
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: URLie"})
|
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: URLie"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
51
server.go
51
server.go
|
@ -27,6 +27,7 @@ func runServer(ip, port string) {
|
||||||
defer mongo_session.Close()
|
defer mongo_session.Close()
|
||||||
|
|
||||||
http.HandleFunc("/", routeRoot)
|
http.HandleFunc("/", routeRoot)
|
||||||
|
http.HandleFunc("/favicon.ico", routeFavIcon)
|
||||||
http.HandleFunc("/assets/", routeAssets)
|
http.HandleFunc("/assets/", routeAssets)
|
||||||
http.HandleFunc("/upload", routeUpload)
|
http.HandleFunc("/upload", routeUpload)
|
||||||
http.HandleFunc("/urlie", routeGetFromUrl)
|
http.HandleFunc("/urlie", routeGetFromUrl)
|
||||||
|
@ -109,7 +110,7 @@ func LogRequest(r *http.Request, statusCode int) {
|
||||||
addr,
|
addr,
|
||||||
time.Now(),
|
time.Now(),
|
||||||
r.Method,
|
r.Method,
|
||||||
r.URL.Path,
|
r.URL.String(),
|
||||||
user_agent,
|
user_agent,
|
||||||
statusCode,
|
statusCode,
|
||||||
r.ContentLength)
|
r.ContentLength)
|
||||||
|
@ -149,6 +150,8 @@ func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
||||||
*/
|
*/
|
||||||
// Show a page of most recent images, and tags, and uploaders ...
|
// Show a page of most recent images, and tags, and uploaders ...
|
||||||
func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var err error
|
||||||
|
|
||||||
uriChunks := chunkURI(r.URL.Path)
|
uriChunks := chunkURI(r.URL.Path)
|
||||||
if len(uriChunks) > 2 {
|
if len(uriChunks) > 2 {
|
||||||
LogRequest(r, 404)
|
LogRequest(r, 404)
|
||||||
|
@ -156,6 +159,27 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
serverErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the Request got here by a delete request, confirm it
|
||||||
|
if (len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true") && (len(r.Form["confirm"]) > 0 && r.Form["confirm"][0] == "true") {
|
||||||
|
LogRequest(r, 200)
|
||||||
|
routeFilesDELETE(w, r)
|
||||||
|
return
|
||||||
|
} else if len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true" {
|
||||||
|
LogRequest(r, 200)
|
||||||
|
err = DeleteFilePage(w, uriChunks[1])
|
||||||
|
if err != nil {
|
||||||
|
serverErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if len(uriChunks) == 2 && len(uriChunks[1]) > 0 {
|
if len(uriChunks) == 2 && len(uriChunks[1]) > 0 {
|
||||||
log.Printf("Searching for [%s] ...", uriChunks[1])
|
log.Printf("Searching for [%s] ...", uriChunks[1])
|
||||||
query := gfs.Find(bson.M{"filename": uriChunks[1]})
|
query := gfs.Find(bson.M{"filename": uriChunks[1]})
|
||||||
|
@ -185,7 +209,6 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
io.Copy(w, file) // send the contents of the file in the body
|
io.Copy(w, file) // send the contents of the file in the body
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// no filename given, show them the full listing
|
// no filename given, show them the full listing
|
||||||
http.Redirect(w, r, "/all", 302)
|
http.Redirect(w, r, "/all", 302)
|
||||||
|
@ -316,29 +339,31 @@ func routeFilesPUT(w http.ResponseWriter, r *http.Request) {
|
||||||
// update the file by the name in the path and/or parameter?
|
// update the file by the name in the path and/or parameter?
|
||||||
// update/add keywords from the parameters
|
// update/add keywords from the parameters
|
||||||
// look for an image in the r.Body
|
// look for an image in the r.Body
|
||||||
LogRequest(r, 200)
|
LogRequest(r, 418)
|
||||||
}
|
}
|
||||||
|
|
||||||
func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
||||||
uriChunks := chunkURI(r.URL.Path)
|
uriChunks := chunkURI(r.URL.Path)
|
||||||
if len(uriChunks) > 2 {
|
if (len(uriChunks) > 2) || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
||||||
LogRequest(r, 404)
|
LogRequest(r, 400)
|
||||||
http.NotFound(w, r)
|
http.Error(w, "Bad Syntax", 400)
|
||||||
return
|
return
|
||||||
} else if len(uriChunks) == 2 && len(uriChunks[1]) == 0 {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exists, err := HasFileByFilename(uriChunks[1])
|
exists, err := HasFileByFilename(uriChunks[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
err = gfs.Remove(uriChunks[1])
|
err = gfs.Remove(uriChunks[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
LogRequest(r, 200)
|
LogRequest(r, 302)
|
||||||
|
http.Redirect(w, r, "/", 302)
|
||||||
} else {
|
} else {
|
||||||
LogRequest(r, 404)
|
LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
|
@ -518,6 +543,16 @@ func routeIPs(w http.ResponseWriter, r *http.Request) {
|
||||||
LogRequest(r, 200)
|
LogRequest(r, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
GET /favicon.ico
|
||||||
|
|
||||||
|
Set an unruly cache on this path, so the browser does not constantly ask for it
|
||||||
|
*/
|
||||||
|
func routeFavIcon(w http.ResponseWriter, r *http.Request) {
|
||||||
|
LogRequest(r, 200)
|
||||||
|
w.Header().Set("Cache-Control", "max-age=315360000")
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
GET /urlie
|
GET /urlie
|
||||||
POST /urlie
|
POST /urlie
|
||||||
|
|
Loading…
Reference in a new issue