@@ -2,23 +2,21 @@ package org.thp.cortex.controllers
22
33import java .net .URLEncoder
44import java .nio .file .Files
5- import javax .inject .{Inject , Singleton }
6-
7- import play .api .http .HttpEntity
8- import play .api .libs .Files .DefaultTemporaryFileCreator
9- import play .api .mvc ._
10- import play .api .{mvc , Configuration }
115
126import akka .stream .scaladsl .FileIO
7+ import javax .inject .{Inject , Singleton }
138import net .lingala .zip4j .core .ZipFile
149import net .lingala .zip4j .model .ZipParameters
1510import net .lingala .zip4j .util .Zip4jConstants
16- import org .thp .cortex .models .Roles
17-
1811import org .elastic4play .Timed
19- import org .elastic4play .controllers .{ Authenticated , Renderer }
12+ import org .elastic4play .controllers .Authenticated
2013import org .elastic4play .models .AttachmentAttributeFormat
21- import org .elastic4play .services .AttachmentSrv
14+ import org .elastic4play .services .{AttachmentSrv , ExecutionContextSrv }
15+ import org .thp .cortex .models .Roles
16+ import play .api .http .HttpEntity
17+ import play .api .libs .Files .DefaultTemporaryFileCreator
18+ import play .api .mvc ._
19+ import play .api .{mvc , Configuration }
2220
2321/**
2422 * Controller used to access stored attachments (plain or zipped)
@@ -30,7 +28,7 @@ class AttachmentCtrl(
3028 attachmentSrv : AttachmentSrv ,
3129 authenticated : Authenticated ,
3230 components : ControllerComponents ,
33- renderer : Renderer
31+ executionContextSrv : ExecutionContextSrv
3432) extends AbstractController (components) {
3533
3634 @ Inject () def this (
@@ -39,9 +37,9 @@ class AttachmentCtrl(
3937 attachmentSrv : AttachmentSrv ,
4038 authenticated : Authenticated ,
4139 components : ControllerComponents ,
42- renderer : Renderer
40+ executionContextSrv : ExecutionContextSrv
4341 ) =
44- this (configuration.get[String ](" datastore.attachment.password" ), tempFileCreator, attachmentSrv, authenticated, components, renderer )
42+ this (configuration.get[String ](" datastore.attachment.password" ), tempFileCreator, attachmentSrv, authenticated, components, executionContextSrv )
4543
4644 /**
4745 * Download an attachment, identified by its hash, in plain format
@@ -50,16 +48,25 @@ class AttachmentCtrl(
5048 */
5149 @ Timed (" controllers.AttachmentCtrl.download" )
5250 def download (hash : String , name : Option [String ]): Action [AnyContent ] = authenticated(Roles .read) { _ =>
53- if (hash.startsWith(" {{" )) // angularjs hack
54- NoContent
55- else if (! name.getOrElse(" " ).intersect(AttachmentAttributeFormat .forbiddenChar).isEmpty)
56- mvc.Results .BadRequest (" File name is invalid" )
57- else
58- Result (
59- header = ResponseHeader (200 , Map (" Content-Disposition" -> s """ attachment; filename=" ${URLEncoder
60- .encode(name.getOrElse(hash), " utf-8" )}" """ , " Content-Transfer-Encoding" -> " binary" )),
61- body = HttpEntity .Streamed (attachmentSrv.source(hash), None , None )
62- )
51+ executionContextSrv.withDefault { implicit ec =>
52+ if (hash.startsWith(" {{" )) // angularjs hack
53+ NoContent
54+ else if (! name.getOrElse(" " ).intersect(AttachmentAttributeFormat .forbiddenChar).isEmpty)
55+ mvc.Results .BadRequest (" File name is invalid" )
56+ else
57+ Result (
58+ header = ResponseHeader (
59+ 200 ,
60+ Map (
61+ " Content-Disposition" ->
62+ s """ attachment; filename=" ${URLEncoder
63+ .encode(name.getOrElse(hash), " utf-8" )}" """ ,
64+ " Content-Transfer-Encoding" -> " binary"
65+ )
66+ ),
67+ body = HttpEntity .Streamed (attachmentSrv.source(hash), None , None )
68+ )
69+ }
6370 }
6471
6572 /**
@@ -69,33 +76,35 @@ class AttachmentCtrl(
6976 */
7077 @ Timed (" controllers.AttachmentCtrl.downloadZip" )
7178 def downloadZip (hash : String , name : Option [String ]): Action [AnyContent ] = authenticated(Roles .read) { _ =>
72- if (! name.getOrElse(" " ).intersect(AttachmentAttributeFormat .forbiddenChar).isEmpty)
73- BadRequest (" File name is invalid" )
74- else {
75- val f = tempFileCreator.create(" zip" , hash).path
76- Files .delete(f)
77- val zipFile = new ZipFile (f.toFile)
78- val zipParams = new ZipParameters
79- zipParams.setCompressionLevel(Zip4jConstants .DEFLATE_LEVEL_FASTEST )
80- zipParams.setEncryptFiles(true )
81- zipParams.setEncryptionMethod(Zip4jConstants .ENC_METHOD_STANDARD )
82- zipParams.setPassword(password)
83- zipParams.setFileNameInZip(name.getOrElse(hash))
84- zipParams.setSourceExternalStream(true )
85- zipFile.addStream(attachmentSrv.stream(hash), zipParams)
79+ executionContextSrv.withDefault { implicit ec =>
80+ if (! name.getOrElse(" " ).intersect(AttachmentAttributeFormat .forbiddenChar).isEmpty)
81+ BadRequest (" File name is invalid" )
82+ else {
83+ val f = tempFileCreator.create(" zip" , hash).path
84+ Files .delete(f)
85+ val zipFile = new ZipFile (f.toFile)
86+ val zipParams = new ZipParameters
87+ zipParams.setCompressionLevel(Zip4jConstants .DEFLATE_LEVEL_FASTEST )
88+ zipParams.setEncryptFiles(true )
89+ zipParams.setEncryptionMethod(Zip4jConstants .ENC_METHOD_STANDARD )
90+ zipParams.setPassword(password)
91+ zipParams.setFileNameInZip(name.getOrElse(hash))
92+ zipParams.setSourceExternalStream(true )
93+ zipFile.addStream(attachmentSrv.stream(hash), zipParams)
8694
87- Result (
88- header = ResponseHeader (
89- 200 ,
90- Map (
91- " Content-Disposition" -> s """ attachment; filename=" ${URLEncoder .encode(name.getOrElse(hash), " utf-8" )}.zip" """ ,
92- " Content-Type" -> " application/zip" ,
93- " Content-Transfer-Encoding" -> " binary" ,
94- " Content-Length" -> Files .size(f).toString
95- )
96- ),
97- body = HttpEntity .Streamed (FileIO .fromPath(f), Some (Files .size(f)), Some (" application/zip" ))
98- )
95+ Result (
96+ header = ResponseHeader (
97+ 200 ,
98+ Map (
99+ " Content-Disposition" -> s """ attachment; filename=" ${URLEncoder .encode(name.getOrElse(hash), " utf-8" )}.zip" """ ,
100+ " Content-Type" -> " application/zip" ,
101+ " Content-Transfer-Encoding" -> " binary" ,
102+ " Content-Length" -> Files .size(f).toString
103+ )
104+ ),
105+ body = HttpEntity .Streamed (FileIO .fromPath(f), Some (Files .size(f)), Some (" application/zip" ))
106+ )
107+ }
99108 }
100109 }
101110}
0 commit comments