page_image_detail.go 2.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package main

import (
	"fmt"
	_ "github.com/lib/pq"
	"net/http"
	"os"
	"path"
)

type ImageDetailData struct {
	User   UserInfo
	Image  Image
	IsMine bool
}

func pageImageDetail(ctx PageContext) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := parseUser(r)
		_, imageId := path.Split(r.URL.Path)
21

22 23 24 25 26 27 28 29 30 31 32 33 34
		result, err := ctx.Database.Query(`
			SELECT
				id,
				owner,
				coalesce(title,  ''),
				coalesce(description, ''),
        		coalesce(created_at, to_timestamp(0)),
				coalesce(original_name, ''),
				coalesce(type, '')
			FROM images
			WHERE id = $1
			`, imageId)
		if err != nil {
35 36 37
			fmt.Printf("An error occured: %s", err.Error())
			_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
			return
38 39 40
		}

		var info Image
41

42 43 44 45
		if result.Next() {
			var owner string
			err := result.Scan(&info.Id, &owner, &info.Title, &info.Description, &info.CreatedAt, &info.OriginalName, &info.MimeType)
			if err != nil {
46 47 48
				fmt.Printf("An error occured: %s", err.Error())
				_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
				return
49 50 51 52 53 54 55 56 57 58 59 60
			}

			switch r.PostFormValue("action") {
			case "update":
				_, err = ctx.Database.Exec(
					"UPDATE images SET title = $1, description = $2 WHERE id = $3 AND owner = $4",
					r.PostFormValue("title"),
					r.PostFormValue("description"),
					info.Id,
					user.Id,
				)
				if err != nil {
61 62 63
					fmt.Printf("An error occured: %s", err.Error())
					_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
					return
64
				}
Janne Koschinski's avatar
Janne Koschinski committed
65
				if r.PostFormValue("from_js") == "true" {
66
					_ = returnJson(w, true)
Janne Koschinski's avatar
Janne Koschinski committed
67 68 69
				} else {
					http.Redirect(w, r, r.URL.Path, http.StatusFound)
				}
70 71 72 73
				return
			case "delete":
				_, err = ctx.Database.Exec("DELETE FROM images WHERE id = $1 AND owner = $2", info.Id, user.Id)
				if err != nil {
74 75 76
					fmt.Printf("An error occured: %s", err.Error())
					_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
					return
77 78
				}
				for _, definition := range ctx.Config.Sizes {
Janne Koschinski's avatar
Janne Koschinski committed
79
					err := os.Remove(path.Join(ctx.Config.TargetFolder, fmt.Sprintf("%s%s", info.Id, definition.Suffix)))
Janne Koschinski's avatar
Janne Koschinski committed
80
					if err != nil && !os.IsNotExist(err) {
81 82 83
						fmt.Printf("An error occured: %s", err.Error())
						_ = returnError(w, http.StatusInternalServerError, "Internal Server Error")
						return
Janne Koschinski's avatar
Janne Koschinski committed
84
					}
85
				}
Janne Koschinski's avatar
Janne Koschinski committed
86
				http.Redirect(w, r, "/me/images", http.StatusFound)
87 88
				return
			}
Janne Koschinski's avatar
Janne Koschinski committed
89 90 91 92 93 94 95 96 97

			if err = formatTemplate(w, "image_detail.html", ImageDetailData{
				user,
				info,
				owner == user.Id,
			}); err != nil {
				panic(err)
			}
			return
98 99
		}

100
		_ = returnError(w, http.StatusNotFound, "Image Not Found")
101 102
	})
}