1
1
package controllers
2
2
3
+ import java .nio .file .Files
3
4
import javax .inject .{ Inject , Singleton }
4
5
5
- import akka .stream .scaladsl .FileIO
6
- import play .api .Configuration
7
6
import play .api .http .HttpEntity
8
- import play .api .libs .Files .TemporaryFile
7
+ import play .api .libs .Files .DefaultTemporaryFileCreator
9
8
import play .api .mvc ._
9
+ import play .api .{ Configuration , mvc }
10
+
11
+ import akka .stream .scaladsl .FileIO
10
12
import net .lingala .zip4j .core .ZipFile
11
13
import net .lingala .zip4j .model .ZipParameters
12
14
import net .lingala .zip4j .util .Zip4jConstants
15
+
13
16
import org .elastic4play .Timed
14
- import org .elastic4play .services .{ AttachmentSrv , Role }
15
- import org .elastic4play .models .AttachmentAttributeFormat
17
+ import org .elastic4play .controllers .{ Authenticated , Renderer }
16
18
import org .elastic4play .models .AttachmentAttributeFormat
17
- import org .elastic4play .controllers .Authenticated
18
- import org .elastic4play .controllers .Renderer
19
+ import org .elastic4play .services .{ AttachmentSrv , Role }
19
20
20
21
/**
21
22
* Controller used to access stored attachments (plain or zipped)
22
23
*/
23
24
@ Singleton
24
25
class AttachmentCtrl (
25
26
password : String ,
27
+ tempFileCreator : DefaultTemporaryFileCreator ,
26
28
attachmentSrv : AttachmentSrv ,
27
29
authenticated : Authenticated ,
28
- renderer : Renderer ) extends Controller {
30
+ components : ControllerComponents ,
31
+ renderer : Renderer ) extends AbstractController (components) {
29
32
30
33
@ Inject () def this (
31
34
configuration : Configuration ,
35
+ tempFileCreator : DefaultTemporaryFileCreator ,
32
36
attachmentSrv : AttachmentSrv ,
33
37
authenticated : Authenticated ,
38
+ components : ControllerComponents ,
34
39
renderer : Renderer ) =
35
40
this (
36
- configuration.getString(" datastore.attachment.password" ).get,
41
+ configuration.get[String ](" datastore.attachment.password" ),
42
+ tempFileCreator,
37
43
attachmentSrv,
38
44
authenticated,
45
+ components,
39
46
renderer)
40
47
41
48
/**
@@ -47,8 +54,8 @@ class AttachmentCtrl(
47
54
def download (hash : String , name : Option [String ]): Action [AnyContent ] = authenticated(Role .read) { implicit request ⇒
48
55
if (hash.startsWith(" {{" )) // angularjs hack
49
56
NoContent
50
- else if (! name.getOrElse(" " ).intersect(AttachmentAttributeFormat .forbiddenChar).isEmpty() )
51
- BadRequest (" File name is invalid" )
57
+ else if (! name.getOrElse(" " ).intersect(AttachmentAttributeFormat .forbiddenChar).isEmpty)
58
+ mvc. Results . BadRequest (" File name is invalid" )
52
59
else
53
60
Result (
54
61
header = ResponseHeader (
@@ -66,12 +73,12 @@ class AttachmentCtrl(
66
73
*/
67
74
@ Timed (" controllers.AttachmentCtrl.downloadZip" )
68
75
def downloadZip (hash : String , name : Option [String ]): Action [AnyContent ] = authenticated(Role .read) { implicit request ⇒
69
- if (! name.getOrElse(" " ).intersect(AttachmentAttributeFormat .forbiddenChar).isEmpty() )
76
+ if (! name.getOrElse(" " ).intersect(AttachmentAttributeFormat .forbiddenChar).isEmpty)
70
77
BadRequest (" File name is invalid" )
71
78
else {
72
- val f = TemporaryFile (" zip" , hash).file
73
- f .delete()
74
- val zipFile = new ZipFile (f)
79
+ val f = tempFileCreator.create (" zip" , hash).path
80
+ Files .delete(f )
81
+ val zipFile = new ZipFile (f.toFile )
75
82
val zipParams = new ZipParameters
76
83
zipParams.setCompressionLevel(Zip4jConstants .DEFLATE_LEVEL_FASTEST )
77
84
zipParams.setEncryptFiles(true )
@@ -88,8 +95,8 @@ class AttachmentCtrl(
88
95
" Content-Disposition" → s """ attachment; filename=" ${name.getOrElse(hash)}.zip" """ ,
89
96
" Content-Type" → " application/zip" ,
90
97
" Content-Transfer-Encoding" → " binary" ,
91
- " Content-Length" → f.length .toString)),
92
- body = HttpEntity .Streamed (FileIO .fromPath(f.toPath ), Some (f.length ), Some (" application/zip" )))
98
+ " Content-Length" → Files .size(f) .toString)),
99
+ body = HttpEntity .Streamed (FileIO .fromPath(f), Some (Files .size(f) ), Some (" application/zip" )))
93
100
}
94
101
}
95
102
}
0 commit comments