Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
SkuciSe
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tim 2 - 2022
SkuciSe
Commits
33943d4c
Commit
33943d4c
authored
Sep 01, 2022
by
Bogdan Andjelkovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
profile picture upload - solved
parent
8ac8ce0c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
33 additions
and
51 deletions
+33
-51
SkuciSe/src/main/java/com/example/SkuciSe/controller/AppController.java
+12
-4
SkuciSe/src/main/java/com/example/SkuciSe/model/korisnik/Korisnik.java
+15
-4
SkuciSe/src/main/java/com/example/SkuciSe/repository/KorisnikRepository.java
+5
-10
SkuciSe/src/main/java/com/example/SkuciSe/util/FileUploadUtil.java
+0
-30
SkuciSe/src/main/resources/templates/profile.html
+1
-3
No files found.
SkuciSe/src/main/java/com/example/SkuciSe/controller/AppController.java
View file @
33943d4c
...
@@ -10,6 +10,8 @@ import org.springframework.ui.Model;
...
@@ -10,6 +10,8 @@ import org.springframework.ui.Model;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
@Controller
@Controller
public
class
AppController
public
class
AppController
{
{
...
@@ -44,15 +46,21 @@ public class AppController
...
@@ -44,15 +46,21 @@ public class AppController
public
String
postRegisterProccess
(
@ModelAttribute
Korisnik
korisnik
,
@RequestParam
(
"image"
)
MultipartFile
multipartFile
)
public
String
postRegisterProccess
(
@ModelAttribute
Korisnik
korisnik
,
@RequestParam
(
"image"
)
MultipartFile
multipartFile
)
{
{
System
.
out
.
println
(
korisnik
);
System
.
out
.
println
(
korisnik
);
kRepo
.
insert
(
korisnik
,
multipartFile
);
try
{
kRepo
.
insert
(
korisnik
,
multipartFile
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
return
(
"redirect:/login"
);
return
(
"redirect:/login"
);
}
}
@GetMapping
(
"/profile/{korisnikid}"
)
@GetMapping
(
"/profile/{korisnikid}"
)
public
String
getProfile
(
@PathVariable
(
"korisnikid"
)
int
korisnikid
,
Model
model
,
@AuthenticationPrincipal
KorisnikDetails
korisnik
)
public
String
getProfile
(
@PathVariable
(
"korisnikid"
)
int
korisnikid
,
Model
model
,
@AuthenticationPrincipal
KorisnikDetails
loggedUser
)
{
{
model
.
addAttribute
(
"korisnik"
,
kRepo
.
findById
(
korisnikid
));
Korisnik
profile
=
kRepo
.
findById
(
korisnikid
);
model
.
addAttribute
(
"loggedUser"
,
korisnik
);
System
.
out
.
println
(
profile
.
toString
());
model
.
addAttribute
(
"korisnik"
,
profile
);
model
.
addAttribute
(
"loggedUser"
,
loggedUser
);
return
(
"profile"
);
return
(
"profile"
);
}
}
}
}
SkuciSe/src/main/java/com/example/SkuciSe/model/korisnik/Korisnik.java
View file @
33943d4c
...
@@ -5,12 +5,14 @@ import lombok.Getter;
...
@@ -5,12 +5,14 @@ import lombok.Getter;
import
lombok.NoArgsConstructor
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
import
lombok.Setter
;
import
java.io.IOException
;
import
java.util.Base64
;
@Setter
@Setter
@Getter
@Getter
@AllArgsConstructor
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor
public
class
Korisnik
public
class
Korisnik
{
{
int
korisnikId
;
int
korisnikId
;
String
ime
;
String
ime
;
String
prezime
;
String
prezime
;
...
@@ -18,7 +20,8 @@ public class Korisnik
...
@@ -18,7 +20,8 @@ public class Korisnik
String
sifra
;
String
sifra
;
String
telefon
;
String
telefon
;
int
tipId
;
int
tipId
;
String
slika
;
private
String
slika
;
@Override
@Override
public
String
toString
()
{
public
String
toString
()
{
...
@@ -30,6 +33,13 @@ public class Korisnik
...
@@ -30,6 +33,13 @@ public class Korisnik
", sifra='"
+
sifra
+
'\''
+
", sifra='"
+
sifra
+
'\''
+
", telefon='"
+
telefon
+
'\''
+
", telefon='"
+
telefon
+
'\''
+
", tipId="
+
tipId
+
", tipId="
+
tipId
+
", slika="
+
slika
+
'}'
;
'}'
;
}
}
}
/*public String getEncodedPic(){
String slika=null;
slika= Base64.getEncoder().encodeToString(getSlika());
return slika;
*/
}
\ No newline at end of file
SkuciSe/src/main/java/com/example/SkuciSe/repository/KorisnikRepository.java
View file @
33943d4c
package
com
.
example
.
SkuciSe
.
repository
;
package
com
.
example
.
SkuciSe
.
repository
;
import
com.example.SkuciSe.model.korisnik.Korisnik
;
import
com.example.SkuciSe.model.korisnik.Korisnik
;
import
com.example.SkuciSe.util.FileUploadUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
...
@@ -10,6 +8,7 @@ import org.springframework.web.multipart.MultipartFile;
...
@@ -10,6 +8,7 @@ import org.springframework.web.multipart.MultipartFile;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.sql.*
;
import
java.sql.*
;
import
java.util.Base64
;
import
java.util.Optional
;
import
java.util.Optional
;
@Component
@Component
...
@@ -27,16 +26,12 @@ public class KorisnikRepository
...
@@ -27,16 +26,12 @@ public class KorisnikRepository
}
}
}
}
public
void
insert
(
Korisnik
korisnik
,
MultipartFile
multipartFile
)
public
void
insert
(
Korisnik
korisnik
,
MultipartFile
multipartFile
)
throws
IOException
{
{
String
fileName
=
StringUtils
.
cleanPath
(
multipartFile
.
getOriginalFilename
());
String
fileName
=
StringUtils
.
cleanPath
(
multipartFile
.
getOriginalFilename
());
String
uploadDir
=
"user-photos"
;
String
slika
=
Base64
.
getEncoder
().
encodeToString
(
multipartFile
.
getBytes
());
try
{
System
.
out
.
println
(
slika
);
FileUploadUtil
.
saveFile
(
uploadDir
,
fileName
,
multipartFile
);
String
sql
=
"insert into korisnik( ime, prezime, telefon, email, sifra, tipId, slika) values('"
+
korisnik
.
getIme
()+
"','"
+
korisnik
.
getPrezime
()+
"','"
+
korisnik
.
getTelefon
()+
"','"
+
korisnik
.
getEmail
()+
"','"
+
new
BCryptPasswordEncoder
().
encode
(
korisnik
.
getSifra
())+
"',1, '"
+
slika
+
"')"
;
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
String
sql
=
"insert into korisnik( ime, prezime, telefon, email, sifra, tipId, slika) values('"
+
korisnik
.
getIme
()+
"','"
+
korisnik
.
getPrezime
()+
"','"
+
korisnik
.
getTelefon
()+
"','"
+
korisnik
.
getEmail
()+
"','"
+
new
BCryptPasswordEncoder
().
encode
(
korisnik
.
getSifra
())+
"',1, '"
+
fileName
+
"')"
;
System
.
out
.
println
(
sql
);
System
.
out
.
println
(
sql
);
try
{
try
{
statement
.
executeUpdate
(
sql
);
statement
.
executeUpdate
(
sql
);
...
...
SkuciSe/src/main/java/com/example/SkuciSe/util/FileUploadUtil.java
deleted
100644 → 0
View file @
8ac8ce0c
package
com
.
example
.
SkuciSe
.
util
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
public
class
FileUploadUtil
{
public
static
void
saveFile
(
String
uploadDir
,
String
fileName
,
MultipartFile
multipartFile
)
throws
IOException
{
Path
uploadPath
=
Paths
.
get
(
uploadDir
);
if
(!
Files
.
exists
(
uploadPath
))
{
Files
.
createDirectories
(
uploadPath
);
}
try
(
InputStream
inputStream
=
multipartFile
.
getInputStream
())
{
Path
filePath
=
uploadPath
.
resolve
(
fileName
);
Files
.
copy
(
inputStream
,
filePath
,
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
IOException
ioe
)
{
throw
new
IOException
(
"Could not save image file: "
+
fileName
,
ioe
);
}
}
}
\ No newline at end of file
SkuciSe/src/main/resources/templates/profile.html
View file @
33943d4c
...
@@ -60,9 +60,7 @@
...
@@ -60,9 +60,7 @@
<div
class=
"row"
>
<div
class=
"row"
>
<div
class=
"col-12 bg-white p-0 px-3 py-3 mb-3"
>
<div
class=
"col-12 bg-white p-0 px-3 py-3 mb-3"
>
<div
class=
"d-flex flex-column align-items-center"
>
<div
class=
"d-flex flex-column align-items-center"
>
<img
class=
"photo"
<img
class=
"photo"
th:src=
"@{'data:image/png;charset=utf-8;base64,'+${loggedUser.getKorisnik().getSlika()}}"
alt=
"slika korisnika"
>
src=
"https://images.unsplash.com/photo-1541647376583-8934aaf3448a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80"
alt=
""
>
<p
class=
"fw-bold h4 mt-3"
><span
th:text=
"${loggedUser.getKorisnik().getIme()}"
></span></p>
<p
class=
"fw-bold h4 mt-3"
><span
th:text=
"${loggedUser.getKorisnik().getIme()}"
></span></p>
<p
class=
"text-muted"
>
Korisnik
</p>
<p
class=
"text-muted"
>
Korisnik
</p>
<br>
<br>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment