stores file object with original filename and update references

This commit is contained in:
Ronnie A. Baez Sesto 2018-10-02 09:49:37 -04:00
parent 8dbf1ceb0a
commit 752f8356ff
1 changed files with 7 additions and 6 deletions

View File

@ -90,11 +90,12 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
// this will be executed whenever a file is uploaded // this will be executed whenever a file is uploaded
r.ParseMultipartForm(32 << 20) //defined maximum size of file r.ParseMultipartForm(32 << 20) //defined maximum size of file
defer file.Close() defer file.Close()
htmlFilename := strings.Replace(handler.Filename, " ", "-", -1)
randomFileName := md5.New() randomFileName := md5.New()
io.WriteString(randomFileName, strconv.FormatInt(time.Now().Unix(), 10)) 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)) 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 { if err != nil {
log.Println(err) log.Println(err)
return return
@ -102,14 +103,14 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
defer f.Close() defer f.Close()
io.Copy(f, file) io.Copy(f, file)
if strings.HasSuffix(handler.Filename, ".png") || strings.HasSuffix(handler.Filename, ".jpg") { if strings.HasSuffix(htmlFilename, ".png") || strings.HasSuffix(htmlFilename, ".jpg") {
filelink = "<br> <img src='/files/" + token + "'/>" filelink = "<br> <img src='/files/" + htmlFilename + "'/>"
} else { } else {
filelink = "<br> <a href=/files/" + token + ">" + handler.Filename + "</a>" filelink = "<br> <a href=/files/" + htmlFilename + ">" + htmlFilename + "</a>"
} }
content = content + filelink content = content + filelink
fileTruth := db.AddFile(handler.Filename, token, username) fileTruth := db.AddFile(htmlFilename, token, username)
if fileTruth != nil { if fileTruth != nil {
message = "Error adding filename in db" message = "Error adding filename in db"
log.Println("error adding task to db") log.Println("error adding task to db")