1
1
package com .innoventes .jukebox .controller .helper ;
2
2
3
+ import com .innoventes .jukebox .exceptions .JukeboxNotFoundException ;
3
4
import com .innoventes .jukebox .models .request .LoginRequest ;
4
5
import com .innoventes .jukebox .models .response .ErrorResponse ;
5
6
import com .innoventes .jukebox .models .response .JukeboxResponse ;
8
9
import com .innoventes .jukebox .security .UserDetailsImpl ;
9
10
import com .innoventes .jukebox .security .UserDetailsServiceImpl ;
10
11
import com .innoventes .jukebox .security .jwt .JwtUtils ;
12
+ import com .innoventes .jukebox .service .StorageService ;
11
13
import org .slf4j .Logger ;
12
14
import org .slf4j .LoggerFactory ;
13
15
import org .springframework .beans .factory .annotation .Autowired ;
16
+ import org .springframework .core .io .Resource ;
17
+ import org .springframework .http .HttpHeaders ;
14
18
import org .springframework .http .HttpStatus ;
19
+ import org .springframework .http .MediaType ;
15
20
import org .springframework .http .ResponseEntity ;
16
21
import org .springframework .security .authentication .AuthenticationManager ;
17
22
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
18
23
import org .springframework .security .core .Authentication ;
19
24
import org .springframework .security .crypto .password .PasswordEncoder ;
20
25
import org .springframework .stereotype .Component ;
21
26
27
+ import javax .servlet .http .HttpServletRequest ;
28
+ import java .io .IOException ;
29
+
22
30
@ Component
23
- public class AuthHelper {
31
+ public class PublicHelper {
24
32
25
33
@ Autowired
26
34
private UserDetailsServiceImpl userService ;
@@ -34,7 +42,10 @@ public class AuthHelper {
34
42
@ Autowired
35
43
private AuthenticationManager authenticationManager ;
36
44
37
- private static final Logger LOGGER = LoggerFactory .getLogger (AuthHelper .class );
45
+ @ Autowired
46
+ private StorageService storageService ;
47
+
48
+ private static final Logger LOGGER = LoggerFactory .getLogger (PublicHelper .class );
38
49
39
50
public ResponseEntity <JukeboxResponse > signInUser (LoginRequest loginRequest ){
40
51
try {
@@ -53,4 +64,28 @@ public ResponseEntity<JukeboxResponse> signInUser(LoginRequest loginRequest){
53
64
return ResponseEntity .status (HttpStatus .UNAUTHORIZED ).body (new ErrorResponse ("Unable to Login, Please check credentials" ));
54
65
}
55
66
}
67
+
68
+ public ResponseEntity <Resource > downloadFile (String fileName , HttpServletRequest request ) {
69
+ Resource resource = null ;
70
+ if (fileName != null && !fileName .isEmpty ()){
71
+ try {
72
+ resource = storageService .loadFileAsResource (fileName );
73
+ }catch (Exception ex ){
74
+ LOGGER .error (ex .getMessage ());
75
+ throw new JukeboxNotFoundException ("File named " + fileName + " not found" );
76
+ }
77
+ String contentType = null ;
78
+ try {
79
+ contentType = request .getServletContext ().getMimeType (resource .getFile ().getAbsolutePath ());
80
+ }catch (IOException ex ){
81
+ LOGGER .error ("Could not determine the file type" );
82
+ contentType = "application/octet-stream" ;
83
+ }
84
+ return ResponseEntity .ok ().contentType (MediaType .parseMediaType (contentType ))
85
+ .header (HttpHeaders .CONTENT_DISPOSITION , "attachment; filename=\" " + resource .getFilename () + "\" " )
86
+ .body (resource );
87
+ }else {
88
+ return ResponseEntity .notFound ().build ();
89
+ }
90
+ }
56
91
}
0 commit comments