Skip to content

Commit

Permalink
Merge pull request #71 from Pomog/yurii_dev
Browse files Browse the repository at this point in the history
Yurii dev
  • Loading branch information
TartuDen authored Dec 23, 2023
2 parents e99d3ee + 30aba01 commit 231586a
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 126 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ go.work
getGitUdates.sh
# corrected fileName
getGitUpdates.sh
getGitUdates.sh
mainDB.db
*.db
4 changes: 2 additions & 2 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func routes(a *config.AppConfig) http.Handler {

// registerStaticHandlers registers handlers for static content.
func registerStaticHandlers(mux *http.ServeMux) {
//Define a list of static directories (e.g., "css", "logo", "ava").
statics := []string{"css", "logo", "ava"}
//Define a list of static directories (e.g., "css", "logo", "ava", "post_images").
statics := []string{"css", "logo", "ava", "post_images"}

// Register handlers for static content.
for _, static := range statics {
Expand Down
38 changes: 0 additions & 38 deletions getGitUdates.sh

This file was deleted.

53 changes: 53 additions & 0 deletions internal/handler/themeHandler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package handler

import (
"io"
"net/http"
"os"
"path"
"path/filepath"
"strconv"

"github.com/Pomog/ForumFFF/internal/models"
Expand Down Expand Up @@ -72,11 +76,20 @@ func (m *Repository) ThemeHandler(w http.ResponseWriter, r *http.Request) {
setErrorAndRedirect(w, r, guestRestiction, "/error-page")
return
}

// Parse the form data, including files Need to Set Upper limit for DATA
err := r.ParseMultipartForm(1 << 20)
if err != nil {
setErrorAndRedirect(w, r, "Image is too large", "/error-page")
return
}

post := models.Post{
Subject: ShortenerOfSubject(mainThread.Subject),
Content: r.FormValue("post-text"),
UserID: visitorID,
ThreadId: mainThread.ID,
Image: r.FormValue("image"),
}

// checking if there is a text before thread creation
Expand All @@ -90,6 +103,45 @@ func (m *Repository) ThemeHandler(w http.ResponseWriter, r *http.Request) {
setErrorAndRedirect(w, r, "Only 500 symbols allowed", "/error-page")
return
}
// ADD IMAGE TO STATIC
// Get the file from the form data
file, handler, errFileGet := r.FormFile("image")
if errFileGet != nil {
setErrorAndRedirect(w, r, fileReceivingErrorMsg, "/error-page")
return
}
defer file.Close()

// Validate file size (1 MB limit)
if handler.Size > 1<<20 {
setErrorAndRedirect(w, r, "File size should be below 1 MB", "/error-page")
return
}

// Validate file type (must be an image)
contentType := handler.Header.Get("Content-Type")
if contentType != "image/jpeg" && contentType != "image/png" && contentType != "image/gif" {
setErrorAndRedirect(w, r, "Wrong File Formate, allowed jpeg, png, gif ", "/error-page")
return
}

// Create a new file in the "static/post_images" directory
newFilePath := filepath.Join("static/post_images", handler.Filename)
newFile, errFileCreate := os.Create(newFilePath)
if errFileCreate != nil {
setErrorAndRedirect(w, r, fileCreatingErrorMsg, "/error-page")
return
}
defer newFile.Close()

// Copy the uploaded file to the new file
_, err = io.Copy(newFile, file)
if err != nil {
setErrorAndRedirect(w, r, fileSavingErrorMsg, "/error-page")
return
}

post.Image = path.Join("/", newFilePath)

err = m.DB.CreatePost(post)
if err != nil {
Expand Down Expand Up @@ -125,6 +177,7 @@ func (m *Repository) ThemeHandler(w http.ResponseWriter, r *http.Request) {
info.Subject = post.Subject
info.Created = post.Created.Format("2006-01-02 15:04:05")
info.Content = post.Content
info.Image = post.Image
info.PictureUserWhoCreatedPost = user.Picture
info.UserNameWhoCreatedPost = user.UserName
info.UserIDWhoCreatedPost = user.ID
Expand Down
2 changes: 2 additions & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type PostDataForThemePage struct {
ID int
Subject string
Content string
Image string
Created string
UserNameWhoCreatedPost string
UserIDWhoCreatedPost int
Expand All @@ -55,6 +56,7 @@ type Post struct {
Created time.Time
ThreadId int
UserID int
Image string
}

type Votes struct {
Expand Down
7 changes: 4 additions & 3 deletions internal/repository/dbrepo/sqllite.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ func (m *SqliteBDRepo) CreatePost(post models.Post) error {
}

stmt := `insert into post
(subject, content, threadID, userID)
values ($1, $2, $3, $4
(subject, content, threadID, userID, postImage)
values ($1, $2, $3, $4, $5
)
`

Expand All @@ -212,6 +212,7 @@ func (m *SqliteBDRepo) CreatePost(post models.Post) error {
post.Content,
post.ThreadId,
post.UserID,
post.Image,
)

if err != nil {
Expand Down Expand Up @@ -311,7 +312,7 @@ func (m *SqliteBDRepo) GetAllPostsFromThread(threadID int) ([]models.Post, error

for rows.Next() {
var post models.Post
err := rows.Scan(&post.ID, &post.Subject, &post.Content, &post.Created, &post.ThreadId, &post.UserID)
err := rows.Scan(&post.ID, &post.Subject, &post.Content, &post.Created, &post.ThreadId, &post.UserID, &post.Image)
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions internal/repository/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ var sessionIdTable = `CREATE TABLE IF NOT EXISTS sessionId (
var guestUser = `INSERT INTO users (username, password, first_name, last_name, email)
VALUES ('guest', '123456', 'Guest', 'User', 'guest@gmail.com');`

var alterPostTable = `
ALTER TABLE post
ADD COLUMN postImage TEXT DEFAULT "";
`


func getQuerys() []string {
var sqlQuerys []string
sqlQuerys = append(sqlQuerys, userTable)
sqlQuerys = append(sqlQuerys, threadTable)
sqlQuerys = append(sqlQuerys, postTable)
sqlQuerys = append(sqlQuerys, votesTable)
sqlQuerys = append(sqlQuerys, sessionIdTable)
sqlQuerys = append(sqlQuerys, alterPostTable)
return sqlQuerys
}
5 changes: 5 additions & 0 deletions static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ section {
max-height: auto;
}

.post_img{
max-width: 40%;
max-height: auto;
}

.logo_ava{
max-width: 15%;
max-height: auto;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/post_images/mushroom.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
95 changes: 12 additions & 83 deletions template/theme.page.html
Original file line number Diff line number Diff line change
@@ -1,88 +1,6 @@
{{template "body" .}}

{{define "style"}}
<style>

.logo_img{

max-width: 3%;
max-height: auto;
}

.ava_img{

max-width: 50px;

max-height: auto;
}
.navbar-brand{
font-family: 'MedievalSharp', cursive;
font-size: 3em;
}
.underTable{
background-color: #232427;
}
#navbarTop {
height: 6em;
}

.whiteLink{
color: rgb(225, 225, 225);
}
.forumTable{
width: 100%;
}

.forumFirstColTheme{

width: 15%;
}

td {
padding: 10px; /* Adjust the value as needed */
}
th {
padding: 10px; /* Adjust the value as needed */
}

.custom-container{
max-width: 1800px;
}
.containerHead {
background-color: rgb(24, 22, 33)

}
.navBar{
background-color: rgb(24, 22, 33)
}
.containerBody {
background: linear-gradient(to bottom, rgb(36, 33, 49), rgb(24, 22, 33));
margin-top: 0;
}

.main-footer {
/* position: fixed; */
bottom: 0;
margin-left: 0;
width: 100%;
height: 5em;
padding: 3em;
background-color: rgb(2, 0, 7);
margin-top: 2em;
color: aliceblue;
}

#form-search{
margin-right: 5em;
}
.login_and_register{
color: aliceblue;
}

body{
background-color: rgb(13, 11, 20);
}
</style>

{{end}}
{{define "centralPart"}}
Expand Down Expand Up @@ -117,11 +35,17 @@ <h1 class="modal-title fs-5" id="createNewPostModalLabel">Type your post and cli
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="">
<form method="post" action="" enctype="multipart/form-data">
<div class="mb-3">
<label for="post-text" class="col-form-label">Type your topic here:</label>
<textarea class="form-control" id="post-text" name="post-text"></textarea>
</div>

<div class="mb-3">
<label for="image" class="col-form-label">Image: (Max 20 MB), allowed formats jpeg, png, gif.</label>
<input type="file" class="form-control" name="image" id="image">
</div>

<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-light">Post</button>
Expand Down Expand Up @@ -161,6 +85,11 @@ <h3>Topic:</h3>
</td>
<td>
<em>{{ .Subject}}</em> <br><br>

{{ if .Image }}
<img src="{{ .Image }}" class="post_img" alt="post_img">
{{ end }} <br><br>

{{ .Content }}&nbsp;

<form method="POST" action="/edit_topic?postID={{ .ID}}">
Expand Down

0 comments on commit 231586a

Please sign in to comment.