예제 #1
0
//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 GET requests, will redirect to home
		r.ParseForm()
		title := r.Form.Get("title")
		content := r.Form.Get("content")
		truth := db.AddTask(title, content)
		if truth != nil {
			message = "Error adding task"
		} else {
			message = "Task added"
		}
		http.Redirect(w, r, "/", http.StatusFound)
	} else {
		message = "Method not allowed"
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
예제 #2
0
파일: addViews.go 프로젝트: Skarlso/Tasks
//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")
		if err != nil && handler != nil {
			//Case executed when file is uploaded and yet an error occurs
			log.Println(err)
			message = "Error uploading file"
			http.Redirect(w, r, "/", http.StatusInternalServerError)
		}

		taskPriority, priorityErr := strconv.Atoi(r.FormValue("priority"))

		if priorityErr != nil {
			log.Print(priorityErr)
			message = "Bad task priority"
			http.Redirect(w, r, "/", http.StatusInternalServerError)
		}
		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
		}

		category := r.FormValue("category")
		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 {
				// this will be executed whenever a file is uploaded
				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")
				}
			}

			taskTruth := db.AddTask(title, content, category, taskPriority)

			if taskTruth != nil {
				message = "Error adding task"
				log.Println("error adding task to db")
				http.Redirect(w, r, "/", http.StatusInternalServerError)
			} else {
				message = "Task added"
				log.Println("added task to db")
				http.Redirect(w, r, "/", http.StatusFound)
			}
		} else {
			log.Println("CSRF mismatch")
			message = "Server Error"
			http.Redirect(w, r, "/", http.StatusInternalServerError)
		}

	} else {
		message = "Method not allowed"
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
예제 #3
0
파일: api.go 프로젝트: thewhitetulip/Tasks
//AddTaskFuncAPI will add the tasks for the user
func AddTaskFuncAPI(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		token := r.Header["Token"][0]
		w.Header().Set("Content-Type", "application/json; charset=UTF-8")

		IsTokenValid, username := ValidateToken(token)
		//When the token is not valid show the default error JSON document
		if !IsTokenValid {
			status := types.Status{StatusCode: http.StatusInternalServerError, Message: message}
			w.WriteHeader(http.StatusInternalServerError)
			err = json.NewEncoder(w).Encode(status)

			if err != nil {
				panic(err)
			}
			return
		}

		log.Println("token is valid " + username + " is logged in")

		r.ParseForm()
		category := r.FormValue("category")
		title := template.HTMLEscapeString(r.Form.Get("title"))
		content := template.HTMLEscapeString(r.Form.Get("content"))
		taskPriority, priorityErr := strconv.Atoi(r.FormValue("priority"))

		if priorityErr != nil {
			log.Print(priorityErr)
			message = "Bad task priority"
		}
		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
		}
		var hidden int
		hideTimeline := r.FormValue("hide")
		if hideTimeline != "" {
			hidden = 1
		} else {
			hidden = 0
		}
		var taskErr bool

		if title != "" && content != "" {
			taskTruth := db.AddTask(title, content, category, taskPriority, username, hidden)
			if taskTruth != nil {
				taskErr = true
			}
		}

		var statusCode int
		var message string

		if !taskErr {
			statusCode = http.StatusInternalServerError
			message = "Error adding task to db"
		} else {
			statusCode = http.StatusOK
			message = "Task added to db"
		}
		status := types.Status{StatusCode: statusCode, Message: message}
		json.NewEncoder(w).Encode(status)
	} else {
		var statusCode int
		var message string

		statusCode = http.StatusBadRequest
		message = "Invalid request"
		status := types.Status{StatusCode: statusCode, Message: message}
		json.NewEncoder(w).Encode(status)

	}
}