2016-01-31 14:20:01 +08:00
|
|
|
package views
|
|
|
|
|
2016-02-01 22:34:19 +08:00
|
|
|
/*
|
|
|
|
Holds the insert task related view handlers, includes the one for file upload
|
|
|
|
*/
|
2016-01-31 14:20:01 +08:00
|
|
|
import (
|
|
|
|
"crypto/md5"
|
2016-01-31 22:22:00 +08:00
|
|
|
"fmt"
|
2016-01-31 14:20:01 +08:00
|
|
|
"io"
|
2016-01-31 22:22:00 +08:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2016-02-06 14:58:00 +08:00
|
|
|
"strings"
|
2016-01-31 22:22:00 +08:00
|
|
|
"text/template"
|
|
|
|
"time"
|
2016-02-01 21:45:04 +08:00
|
|
|
|
|
|
|
"github.com/thewhitetulip/Tasks/db"
|
2016-01-31 14:20:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// UploadedFileHandler is used to handle the uploaded file related requests
|
|
|
|
func UploadedFileHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
token := r.URL.Path[len("/files/"):]
|
|
|
|
|
|
|
|
//file, err := db.GetFileName(token)
|
|
|
|
//if err != nil {
|
|
|
|
log.Println("serving file ./files/" + token)
|
|
|
|
http.ServeFile(w, r, "./files/"+token)
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//AddTaskFunc is used to handle the addition of new task, "/add" URL
|
|
|
|
func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "POST" { // Will work only for POST requests, will redirect to home
|
|
|
|
r.ParseForm()
|
|
|
|
file, handler, err := r.FormFile("uploadfile")
|
2016-02-02 23:38:35 +08:00
|
|
|
if err != nil && handler != nil {
|
|
|
|
//Case executed when file is uploaded and yet an error occurs
|
2016-01-31 14:20:01 +08:00
|
|
|
log.Println(err)
|
2016-02-01 21:45:04 +08:00
|
|
|
message = "Error uploading file"
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
taskPriority, priorityErr := strconv.Atoi(r.FormValue("priority"))
|
2016-02-02 23:38:35 +08:00
|
|
|
|
2016-01-31 14:20:01 +08:00
|
|
|
if priorityErr != nil {
|
|
|
|
log.Print(priorityErr)
|
2016-02-01 21:45:04 +08:00
|
|
|
message = "Bad task priority"
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
priorityList := []int{1, 2, 3}
|
|
|
|
found := false
|
|
|
|
for _, priority := range priorityList {
|
|
|
|
if taskPriority == priority {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//If someone gives us incorrect priority number, we give the priority
|
|
|
|
//to that task as 1 i.e. Low
|
|
|
|
if !found {
|
|
|
|
taskPriority = 1
|
|
|
|
}
|
2016-02-03 01:40:44 +08:00
|
|
|
|
|
|
|
category := r.FormValue("category")
|
2016-01-31 14:20:01 +08:00
|
|
|
title := template.HTMLEscapeString(r.Form.Get("title"))
|
|
|
|
content := template.HTMLEscapeString(r.Form.Get("content"))
|
|
|
|
formToken := template.HTMLEscapeString(r.Form.Get("CSRFToken"))
|
|
|
|
|
|
|
|
cookie, _ := r.Cookie("csrftoken")
|
|
|
|
if formToken == cookie.Value {
|
|
|
|
if handler != nil {
|
2016-02-02 23:38:35 +08:00
|
|
|
// this will be executed whenever a file is uploaded
|
2016-01-31 14:20:01 +08:00
|
|
|
r.ParseMultipartForm(32 << 20) //defined maximum size of file
|
|
|
|
defer file.Close()
|
|
|
|
randomFileName := md5.New()
|
|
|
|
io.WriteString(randomFileName, strconv.FormatInt(time.Now().Unix(), 10))
|
|
|
|
io.WriteString(randomFileName, handler.Filename)
|
|
|
|
token := fmt.Sprintf("%x", randomFileName.Sum(nil))
|
|
|
|
f, err := os.OpenFile("./files/"+token, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
io.Copy(f, file)
|
|
|
|
|
|
|
|
filelink := "<br> <a href=/files/" + token + ">" + handler.Filename + "</a>"
|
|
|
|
content = content + filelink
|
|
|
|
|
|
|
|
fileTruth := db.AddFile(handler.Filename, token)
|
|
|
|
if fileTruth != nil {
|
|
|
|
message = "Error adding filename in db"
|
|
|
|
log.Println("error adding task to db")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 01:40:44 +08:00
|
|
|
taskTruth := db.AddTask(title, content, category, taskPriority)
|
2016-01-31 14:20:01 +08:00
|
|
|
|
|
|
|
if taskTruth != nil {
|
|
|
|
message = "Error adding task"
|
|
|
|
log.Println("error adding task to db")
|
2016-02-01 21:45:04 +08:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
2016-01-31 14:20:01 +08:00
|
|
|
} else {
|
|
|
|
message = "Task added"
|
|
|
|
log.Println("added task to db")
|
2016-02-02 23:38:35 +08:00
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-01 21:45:04 +08:00
|
|
|
log.Println("CSRF mismatch")
|
|
|
|
message = "Server Error"
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
2016-01-31 14:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
message = "Method not allowed"
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 01:40:44 +08:00
|
|
|
|
|
|
|
//AddCategoryFunc used to add new categories to the database
|
|
|
|
func AddCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
category := r.Form.Get("category")
|
2016-02-06 14:58:00 +08:00
|
|
|
if strings.Trim(category, " ") != "" {
|
2016-02-03 01:40:44 +08:00
|
|
|
err := db.AddCategory(category)
|
|
|
|
if err != nil {
|
|
|
|
message = "Error adding category"
|
|
|
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
|
|
|
} else {
|
|
|
|
message = "Added category"
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 02:36:00 +08:00
|
|
|
|
|
|
|
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
|
|
|
|
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" {
|
|
|
|
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
|
|
|
} else {
|
|
|
|
task, err := db.GetTaskByID(id)
|
|
|
|
categories := db.GetCategories()
|
|
|
|
task.Categories = categories
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
task.Message = "Error fetching Tasks"
|
|
|
|
}
|
|
|
|
editTemplate.Execute(w, task)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message = "Method not allowed"
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|