Compare commits
	
		
			4 Commits
		
	
	
		
			6d6aff50b3
			...
			file_uploa
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					37391190fb | ||
| 
						 | 
					1fb8fdef81 | ||
| 
						 | 
					baef0cbe78 | ||
| 
						 | 
					d0da1a9114 | 
@@ -25,6 +25,11 @@ type Configuration struct {
 | 
				
			|||||||
	Template struct {
 | 
						Template struct {
 | 
				
			||||||
		BaseName string `json:"BaseTemplateName"`
 | 
							BaseName string `json:"BaseTemplateName"`
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						Upload struct {
 | 
				
			||||||
 | 
							BaseName string `json:"UploadDirectoryName"`
 | 
				
			||||||
 | 
							MaxSize  int64  `json:"MaxUploadSize"`
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// LoadConfig loads and returns a configuration struct
 | 
					// LoadConfig loads and returns a configuration struct
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -61,6 +61,13 @@ func (getController *GetController) ShowLogin(w http.ResponseWriter, r *http.Req
 | 
				
			|||||||
	templating.RenderTemplate(getController.App, w, "templates/pages/login.html", data)
 | 
						templating.RenderTemplate(getController.App, w, "templates/pages/login.html", data)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (getController *GetController) ShowFile(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
						// GET /uploads?name=file.jpg
 | 
				
			||||||
 | 
						// will serve file.jpg
 | 
				
			||||||
 | 
						name := r.URL.Query().Get("name")
 | 
				
			||||||
 | 
						http.ServeFile(w, r, getController.App.Config.Upload.BaseName+name)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (getController *GetController) Logout(w http.ResponseWriter, r *http.Request) {
 | 
					func (getController *GetController) Logout(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	models.LogoutUser(getController.App, w, r)
 | 
						models.LogoutUser(getController.App, w, r)
 | 
				
			||||||
	http.Redirect(w, r, "/", http.StatusFound)
 | 
						http.Redirect(w, r, "/", http.StatusFound)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,8 +4,11 @@ import (
 | 
				
			|||||||
	"GoWeb/app"
 | 
						"GoWeb/app"
 | 
				
			||||||
	"GoWeb/models"
 | 
						"GoWeb/models"
 | 
				
			||||||
	"GoWeb/security"
 | 
						"GoWeb/security"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
						"mime/multipart"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -69,3 +72,60 @@ func (postController *PostController) Register(w http.ResponseWriter, r *http.Re
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	http.Redirect(w, r, "/login", http.StatusFound)
 | 
						http.Redirect(w, r, "/login", http.StatusFound)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (postController *PostController) FileUpload(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
						max := postController.App.Config.Upload.MaxSize
 | 
				
			||||||
 | 
						err := r.ParseMultipartForm(max)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// FormFile returns the first file for the given key `file`
 | 
				
			||||||
 | 
						// it also returns the FileHeader, so we can get the Filename,
 | 
				
			||||||
 | 
						// the Header and the size of the file
 | 
				
			||||||
 | 
						file, handler, err := r.FormFile("file")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Println("Error Retrieving the File")
 | 
				
			||||||
 | 
							log.Println(err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer func(file multipart.File) {
 | 
				
			||||||
 | 
							err := file.Close()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								log.Println(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}(file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if handler.Size > max {
 | 
				
			||||||
 | 
							log.Println("User tried uploading a file which is too large.")
 | 
				
			||||||
 | 
							http.Redirect(w, r, "/", http.StatusRequestHeaderFieldsTooLarge)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create a temporary file within upload directory
 | 
				
			||||||
 | 
						tempFile, err := os.Create(postController.App.Config.Upload.BaseName + handler.Filename)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Println(err)
 | 
				
			||||||
 | 
							http.Redirect(w, r, "/", http.StatusNotAcceptable)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer func(tempFile *os.File) {
 | 
				
			||||||
 | 
							err := tempFile.Close()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								log.Println(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}(tempFile)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// read all the contents of our uploaded file into a
 | 
				
			||||||
 | 
						// byte array
 | 
				
			||||||
 | 
						fileBytes, err := io.ReadAll(file)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Println(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err = tempFile.Write(fileBytes)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Println(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						http.Redirect(w, r, "/", http.StatusFound)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,5 +13,9 @@
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
  "Template": {
 | 
					  "Template": {
 | 
				
			||||||
    "BaseTemplateName": "templates/base.html"
 | 
					    "BaseTemplateName": "templates/base.html"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "Upload": {
 | 
				
			||||||
 | 
					      "UploadDirectoryName": "goweb-uploads/",
 | 
				
			||||||
 | 
					      "MaxUploadSize": 10485760
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										9
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								main.go
									
									
									
									
									
								
							@@ -8,6 +8,7 @@ import (
 | 
				
			|||||||
	"GoWeb/routes"
 | 
						"GoWeb/routes"
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"embed"
 | 
						"embed"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
@@ -43,6 +44,14 @@ func main() {
 | 
				
			|||||||
	file, err := os.OpenFile("logs/"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
 | 
						file, err := os.OpenFile("logs/"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
 | 
				
			||||||
	log.SetOutput(file)
 | 
						log.SetOutput(file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create upload directory if it doesn't exist
 | 
				
			||||||
 | 
						uploadPath := appLoaded.Config.Upload.BaseName
 | 
				
			||||||
 | 
						if _, err := os.Stat(uploadPath); errors.Is(err, os.ErrNotExist) {
 | 
				
			||||||
 | 
							if err := os.MkdirAll(uploadPath, os.ModePerm); err != nil {
 | 
				
			||||||
 | 
								log.Fatal(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Connect to database and run migrations
 | 
						// Connect to database and run migrations
 | 
				
			||||||
	appLoaded.Db = database.ConnectDB(&appLoaded)
 | 
						appLoaded.Db = database.ConnectDB(&appLoaded)
 | 
				
			||||||
	if appLoaded.Config.Db.AutoMigrate {
 | 
						if appLoaded.Config.Db.AutoMigrate {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,4 +30,7 @@ func GetRoutes(app *app.App) {
 | 
				
			|||||||
	http.HandleFunc("/login", getController.ShowLogin)
 | 
						http.HandleFunc("/login", getController.ShowLogin)
 | 
				
			||||||
	http.HandleFunc("/register", getController.ShowRegister)
 | 
						http.HandleFunc("/register", getController.ShowRegister)
 | 
				
			||||||
	http.HandleFunc("/logout", getController.Logout)
 | 
						http.HandleFunc("/logout", getController.Logout)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Files
 | 
				
			||||||
 | 
						http.HandleFunc("/uploads", getController.ShowFile)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,4 +16,5 @@ func PostRoutes(app *app.App) {
 | 
				
			|||||||
	// User authentication
 | 
						// User authentication
 | 
				
			||||||
	http.HandleFunc("/register-handle", postController.Register)
 | 
						http.HandleFunc("/register-handle", postController.Register)
 | 
				
			||||||
	http.HandleFunc("/login-handle", postController.Login)
 | 
						http.HandleFunc("/login-handle", postController.Login)
 | 
				
			||||||
 | 
						http.HandleFunc("/upload-handle", postController.FileUpload)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,25 @@
 | 
				
			|||||||
{{ define "pageTitle" }}Home{{ end }}
 | 
					{{ define "pageTitle" }}Home{{ end }}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{{ define "file-upload" }}
 | 
				
			||||||
 | 
					<form
 | 
				
			||||||
 | 
						enctype="multipart/form-data"
 | 
				
			||||||
 | 
						action="/upload-handle"
 | 
				
			||||||
 | 
						method="post"
 | 
				
			||||||
 | 
						>
 | 
				
			||||||
 | 
						<input type="file" accept="*/*" name="file" />
 | 
				
			||||||
 | 
						<input type="submit" value="upload" />
 | 
				
			||||||
 | 
					</form>
 | 
				
			||||||
 | 
					{{ end }}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
{{ define "content" }}
 | 
					{{ define "content" }}
 | 
				
			||||||
{{ .Test }}
 | 
					{{ .Test }}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<!-- Uncomment below to demo file upload system -->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<!-- {{ template "file-upload" . }} -->
 | 
				
			||||||
 | 
					<!-- <p>Upload an image called test.jpg to test the file upload system</p> -->
 | 
				
			||||||
 | 
					<!-- <img src="/uploads?name=test.jpg" alt=""> -->
 | 
				
			||||||
 | 
					{{ end }}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{{ define "content" }}
 | 
				
			||||||
{{ end }}
 | 
					{{ end }}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user