5
5
import java .io .FileOutputStream ;
6
6
import java .io .IOException ;
7
7
import java .io .InputStream ;
8
+ import java .nio .file .Files ;
8
9
import java .nio .file .Path ;
10
+ import java .nio .file .Paths ;
11
+ import org .apache .commons .io .FilenameUtils ;
9
12
import org .springframework .beans .factory .annotation .Autowired ;
13
+ import org .springframework .security .core .context .SecurityContextHolder ;
14
+ import org .springframework .security .core .userdetails .UserDetails ;
10
15
import org .springframework .stereotype .Service ;
11
16
import org .springframework .transaction .annotation .Transactional ;
17
+ import org .springframework .web .multipart .MultipartFile ;
18
+ import lombok .extern .slf4j .Slf4j ;
12
19
import servie .track_servie .entity .User ;
13
20
import servie .track_servie .exceptions .ResourceNotFoundException ;
14
21
import servie .track_servie .repository .UserRepository ;
15
22
16
23
@ Service
24
+ @ Slf4j
17
25
public class UserService
18
26
{
19
27
@ Autowired
20
28
private UserRepository userRepository ;
21
29
private static final String profilePicsDirectory = "/home/aakkiieezz/Coding/track_servie/profilePics/" ;
22
30
23
31
@ Transactional
24
- public void profileImgUpload (Path fileNamePath ) throws IOException
32
+ public String profileImgUpload (MultipartFile file ) throws IOException
25
33
{
26
- User user = userRepository .findById (1 ).orElseThrow (() -> new RuntimeException ("User not found" ));
34
+ UserDetails userDetails = (UserDetails ) SecurityContextHolder .getContext ().getAuthentication ().getPrincipal ();
35
+ User user = userRepository .findByUsername (userDetails .getUsername ()).orElseThrow (() -> new ResourceNotFoundException ("User" , "Username" , userDetails .getUsername ()));
36
+ File directory = new File (profilePicsDirectory );
37
+ if (!directory .exists ())
38
+ directory .mkdir ();
39
+ String newFileName = String .format ("UserId-%s.%s" , user .getId (), FilenameUtils .getExtension (file .getOriginalFilename ()));
40
+ Path fileNamePath = Paths .get (profilePicsDirectory , newFileName );
41
+ Files .write (fileNamePath , file .getBytes ());
27
42
File fileObject = fileNamePath .toFile ();
28
43
byte [] imageData = new byte [(int ) fileObject .length ()];
29
44
try (InputStream inputStream = new FileInputStream (fileObject ))
@@ -32,6 +47,7 @@ public void profileImgUpload(Path fileNamePath) throws IOException
32
47
}
33
48
user .setProfileImg (imageData );
34
49
userRepository .save (user );
50
+ return newFileName ;
35
51
}
36
52
37
53
public String checkGenerateProfileUrlIfProfileImgExist (Integer userId )
@@ -53,17 +69,18 @@ public String checkGenerateProfileUrlIfProfileImgExist(Integer userId)
53
69
}
54
70
catch (IOException e )
55
71
{
56
- System . err . println ("Error while creating the file: " +e .getMessage ());
72
+ log . error ("Error while creating the file: " +e .getMessage ());
57
73
}
58
74
return null ;
59
75
}
60
76
61
- public void profileImgDelete (Integer userId )
77
+ public void profileImgDelete ()
62
78
{
63
- User user = userRepository .findById (userId ).orElseThrow (() -> new ResourceNotFoundException ("User" , "Id" , userId .toString ()));
79
+ UserDetails userDetails = (UserDetails ) SecurityContextHolder .getContext ().getAuthentication ().getPrincipal ();
80
+ User user = userRepository .findByUsername (userDetails .getUsername ()).orElseThrow (() -> new ResourceNotFoundException ("User" , "Username" , userDetails .getUsername ()));
64
81
user .setProfileImg (null );
65
82
userRepository .save (user );
66
- File file = new File (profilePicsDirectory +"UserId-" +userId +".jpg" );
83
+ File file = new File (profilePicsDirectory +"UserId-" +user . getId () +".jpg" );
67
84
file .delete ();
68
85
}
69
86
}
0 commit comments