forked from OrgGo/Tasks
upload file feature
This commit is contained in:
parent
81104bf5cb
commit
3a3adb35c3
|
@ -12,16 +12,18 @@
|
||||||
<h4 class="modal-title" id="newNoteLabel"><span class="glyphicon glyphicon-pencil"></span> New Task</h4>
|
<h4 class="modal-title" id="newNoteLabel"><span class="glyphicon glyphicon-pencil"></span> New Task</h4>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<form action="/add/" method="POST">
|
<form enctype="multipart/form-data" action="/add/" method="POST">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<!-- <label for="note-title" class="control-label">Title:</label> -->
|
<!-- <label for="note-title" class="control-label">Title:</label> -->
|
||||||
<input type="text" name="title" class="form-control" id="add-note-title" placeholder="Title" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
|
<input type="text" name="title" class="form-control" id="add-note-title" placeholder="Title" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
|
||||||
<input type="hidden" name="CSRFToken" value={{.CSRFToken}}>
|
<input type="hidden" name="CSRFToken" value={{.CSRFToken}}>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
<input type="file" name="uploadfile" />
|
||||||
<!-- <label for="note-content" class="control-label">Content:</label> -->
|
<!-- <label for="note-content" class="control-label">Content:</label> -->
|
||||||
<textarea class="form-control" name="content" id="add-note-content" placeholder="Content" rows="10" style="border:none;border-bottom:1px solid gray; box-shadow:none;"></textarea>
|
<textarea class="form-control" name="content" id="add-note-content" placeholder="Content" rows="10" style="border:none;border-bottom:1px solid gray; box-shadow:none;"></textarea>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
||||||
|
|
|
@ -9,8 +9,9 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
|
"time"
|
||||||
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
var homeTemplate *template.Template
|
var homeTemplate *template.Template
|
||||||
|
@ -64,10 +65,10 @@ func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if message != "" {
|
if message != "" {
|
||||||
context.Message = message
|
context.Message = message
|
||||||
}
|
}
|
||||||
context.CSRFToken = "abcde"
|
context.CSRFToken = "abcd"
|
||||||
message = ""
|
message = ""
|
||||||
expiration := time.Now().Add(365 * 24 * time.Hour)
|
expiration := time.Now().Add(365 * 24 * time.Hour)
|
||||||
cookie := http.Cookie{Name: "csrftoken",Value:"abcd",Expires:expiration}
|
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
|
||||||
http.SetCookie(w, &cookie)
|
http.SetCookie(w, &cookie)
|
||||||
homeTemplate.Execute(w, context)
|
homeTemplate.Execute(w, context)
|
||||||
} else {
|
} else {
|
||||||
|
@ -107,22 +108,44 @@ func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
//AddTaskFunc is used to handle the addition of new task, "/add" URL
|
//AddTaskFunc is used to handle the addition of new task, "/add" URL
|
||||||
func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
|
func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" { // Will work only for GET requests, will redirect to home
|
if r.Method == "POST" { // Will work only for POST requests, will redirect to home
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
file, handler, err := r.FormFile("uploadfile")
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
title := template.HTMLEscapeString(r.Form.Get("title"))
|
title := template.HTMLEscapeString(r.Form.Get("title"))
|
||||||
content := template.HTMLEscapeString(r.Form.Get("content"))
|
content := template.HTMLEscapeString(r.Form.Get("content"))
|
||||||
formToken := template.HTMLEscapeString(r.Form.Get("CSRFToken"))
|
formToken := template.HTMLEscapeString(r.Form.Get("CSRFToken"))
|
||||||
cookie, _ := r.Cookie("csrftoken")
|
|
||||||
log.Println(cookie)
|
cookie, _ := r.Cookie("csrftoken")
|
||||||
log.Println(formToken)
|
if formToken == cookie.Value {
|
||||||
if formToken == cookie.Value and title != nil and content!=nil{
|
if handler != nil {
|
||||||
|
r.ParseMultipartForm(32 << 20) //defined maximum size of file
|
||||||
|
defer file.Close()
|
||||||
|
f, err := os.OpenFile("./files/"+handler.Filename, 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/"+handler.Filename+">"+ handler.Filename+"</a>"
|
||||||
|
content = content + filelink
|
||||||
|
}
|
||||||
|
|
||||||
truth := db.AddTask(title, content)
|
truth := db.AddTask(title, content)
|
||||||
if truth != nil {
|
if truth != nil {
|
||||||
message = "Error adding task"
|
message = "Error adding task"
|
||||||
|
log.Println("error adding task to db")
|
||||||
} else {
|
} else {
|
||||||
message = "Task added"
|
message = "Task added"
|
||||||
|
log.Println("added task to db")
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
} else {
|
||||||
|
log.Fatal("CSRF mismatch")
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue