Skip to content

Commit

Permalink
Merge pull request #64 from Pomog/yurii_dev
Browse files Browse the repository at this point in the history
validations
  • Loading branch information
TartuDen authored Dec 21, 2023
2 parents 8abe762 + f4152a3 commit 0595569
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ getGitUdates.sh
# corrected fileName
getGitUpdates.sh
mainDB.db
*.db

*.db
6 changes: 6 additions & 0 deletions internal/handler/homeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func (m *Repository) HomeHandler(w http.ResponseWriter, r *http.Request) {
return
}

// checking text length
if len(thread.Subject) > 500 {
setErrorAndRedirect(w, r, "Only 500 symbols allowed", "/error-page")
return
}

id, err := m.DB.CreateThread(thread)
if err != nil {
setErrorAndRedirect(w, r, "Could not create a thread", "/error-page")
Expand Down
27 changes: 27 additions & 0 deletions internal/handler/registerHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ func (m *Repository) RegisterHandler(w http.ResponseWriter, r *http.Request) {
}
defer file.Close()

// Validate file size (1 MB limit)
if handler.Size > 1<<20 {
form.Errors.Add("avatar", "File size should be below 1 MB")
data := make(map[string]interface{})
data["registrationData"] = registrationData
renderer.RendererTemplate(w, "register.page.html", &models.TemplateData{
Form: form,
Data: data,
})
return
}

// Validate file type (must be an image)
contentType := handler.Header.Get("Content-Type")
if contentType != "image/jpeg" && contentType != "image/png" && contentType != "image/gif" {
form.Errors.Add("avatar", "Invalid file type. Only JPEG, PNG, and GIF images are allowed")
data := make(map[string]interface{})
data["registrationData"] = registrationData
renderer.RendererTemplate(w, "register.page.html", &models.TemplateData{
Form: form,
Data: data,
})
return
}

// Create a new file in the "static/ava" directory
newFilePath := filepath.Join("static/ava", handler.Filename)
newFile, errFileCreate := os.Create(newFilePath)
Expand All @@ -105,6 +130,7 @@ func (m *Repository) RegisterHandler(w http.ResponseWriter, r *http.Request) {
err := m.DB.CreateUser(registrationData)
if err != nil {
setErrorAndRedirect(w, r, "DB Error func CreateUser", "/error-page")
return
}
message := fmt.Sprintf("User %s is registered", registrationData.UserName)
helper.SendEmail(m.App.ServerEmail, message)
Expand All @@ -113,6 +139,7 @@ func (m *Repository) RegisterHandler(w http.ResponseWriter, r *http.Request) {

} else {
http.Error(w, "No such method", http.StatusMethodNotAllowed)
return
}

}
6 changes: 6 additions & 0 deletions internal/handler/themeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ func (m *Repository) ThemeHandler(w http.ResponseWriter, r *http.Request) {
setErrorAndRedirect(w, r, "Empty post can not be created", "/error-page")
return
}

// checking text length
if len(post.Content) > 500 {
setErrorAndRedirect(w, r, "Only 500 symbols allowed", "/error-page")
return
}


err = m.DB.CreatePost(post)
Expand Down
Binary file added static/ava/bobik.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions template/register.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ <h1>Register</h1>


<input type="file" class="form-control" name="avatar" id="avatar">
{{with .Form.Errors.Get "avatar"}}<label class="text-danger">{{.}}</label>{{end}}

</div>
<button type="submit" class="btn btn-light">Register</button>
</form>
Expand Down

0 comments on commit 0595569

Please sign in to comment.