From 752f8356ff1e40db2624ef5489438d3edf5d8a43 Mon Sep 17 00:00:00 2001 From: "Ronnie A. Baez Sesto" Date: Tue, 2 Oct 2018 09:49:37 -0400 Subject: [PATCH] stores file object with original filename and update references --- views/addViews.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/views/addViews.go b/views/addViews.go index 977f198..71d634c 100644 --- a/views/addViews.go +++ b/views/addViews.go @@ -90,11 +90,12 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) { // this will be executed whenever a file is uploaded r.ParseMultipartForm(32 << 20) //defined maximum size of file defer file.Close() + htmlFilename := strings.Replace(handler.Filename, " ", "-", -1) randomFileName := md5.New() io.WriteString(randomFileName, strconv.FormatInt(time.Now().Unix(), 10)) - io.WriteString(randomFileName, handler.Filename) + io.WriteString(randomFileName, htmlFilename) token := fmt.Sprintf("%x", randomFileName.Sum(nil)) - f, err := os.OpenFile("./files/"+token, os.O_WRONLY|os.O_CREATE, 0666) + f, err := os.OpenFile("./files/"+htmlFilename, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { log.Println(err) return @@ -102,14 +103,14 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) { defer f.Close() io.Copy(f, file) - if strings.HasSuffix(handler.Filename, ".png") || strings.HasSuffix(handler.Filename, ".jpg") { - filelink = "
" + if strings.HasSuffix(htmlFilename, ".png") || strings.HasSuffix(htmlFilename, ".jpg") { + filelink = "
" } else { - filelink = "
" + handler.Filename + "" + filelink = "
" + htmlFilename + "" } content = content + filelink - fileTruth := db.AddFile(handler.Filename, token, username) + fileTruth := db.AddFile(htmlFilename, token, username) if fileTruth != nil { message = "Error adding filename in db" log.Println("error adding task to db")