Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jh-boot
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jan Hrabal
jh-boot
Commits
cfc11eb8
Commit
cfc11eb8
authored
Oct 11, 2019
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
attachments
parent
d5e6d62e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
113 additions
and
24 deletions
+113
-24
.gitignore
.gitignore
+1
-0
AttachmentApiController.java
...a/com/jh/boot/attachment/api/AttachmentApiController.java
+8
-24
AttachmentContentProvider.java
...com/jh/boot/attachment/api/AttachmentContentProvider.java
+48
-0
Converter.java
src/main/java/com/jh/boot/web/Converter.java
+56
-0
No files found.
.gitignore
View file @
cfc11eb8
...
...
@@ -39,3 +39,4 @@ boot-react-common/.apt_generated
boot-react-common/.settings
boot-react-common/target
/.idea/
/jh-boot.iml
src/main/java/com/jh/boot/attachment/api/AttachmentApiController.java
View file @
cfc11eb8
package
com
.
jh
.
boot
.
attachment
.
api
;
import
java.io.InputStream
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOn
Bean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOn
Property
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
...
...
@@ -17,14 +13,16 @@ import org.springframework.web.bind.annotation.ResponseBody;
import
com.jh.boot.attachment.Attachment
;
import
com.jh.boot.attachment.AttachmentService
;
import
com.jh.boot.web.error.NotFoundException
;
@Controller
@ConditionalOn
Bean
(
AttachmentService
.
class
)
@ConditionalOn
Property
(
value
=
"attachments.api.enabled"
,
havingValue
=
"true"
,
matchIfMissing
=
true
)
public
class
AttachmentApiController
{
@Autowired
private
AttachmentService
service
;
@Autowired
private
AttachmentContentProvider
contentProvider
;
@GetMapping
(
"/attachments/{unitId}/{objectType}/{objectId}"
)
...
...
@@ -33,23 +31,9 @@ public class AttachmentApiController {
}
@GetMapping
(
"/attachments/{attachmentId}"
)
public
ResponseEntity
<
InputStreamResource
>
fetch
(
@PathVariable
(
"unitId"
)
Long
unitId
,
@PathVariable
(
"objectType"
)
String
objectType
,
@PathVariable
(
"objectId"
)
Long
objectId
,
@PathVariable
(
"attachmentId"
)
Long
attachmentId
)
{
Attachment
a
=
service
.
fetchAttachment
(
attachmentId
);
if
(
a
==
null
)
{
throw
new
NotFoundException
();
}
InputStream
is
=
service
.
fetchAttachmentBody
(
a
);
if
(
is
==
null
)
{
throw
new
NotFoundException
();
}
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_OCTET_STREAM
);
headers
.
set
(
"Content-Disposition"
,
"attachment; filename=\""
+
a
.
getName
()
+
"\""
);
return
new
ResponseEntity
<>(
content
,
headers
,
HttpStatus
.
OK
);
@GetMapping
(
"/attachments/{attachmentId}/data"
)
public
ResponseEntity
<
InputStreamResource
>
fetch
(
@PathVariable
(
"attachmentId"
)
Long
attachmentId
)
{
return
contentProvider
.
fetch
(
attachmentId
);
}
}
src/main/java/com/jh/boot/attachment/api/AttachmentContentProvider.java
0 → 100644
View file @
cfc11eb8
package
com
.
jh
.
boot
.
attachment
.
api
;
import
java.io.InputStream
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
com.jh.boot.attachment.Attachment
;
import
com.jh.boot.attachment.AttachmentService
;
import
com.jh.boot.web.error.NotFoundException
;
@Component
public
class
AttachmentContentProvider
{
private
AttachmentService
service
;
public
ResponseEntity
<
InputStreamResource
>
fetch
(
Long
attachmentId
)
{
Attachment
a
=
service
.
fetchAttachment
(
attachmentId
);
if
(
a
==
null
)
{
throw
new
NotFoundException
();
}
InputStream
is
=
service
.
fetchAttachmentBody
(
a
);
if
(
is
==
null
)
{
throw
new
NotFoundException
();
}
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
StringUtils
.
hasText
(
a
.
getMimeType
())
?
MediaType
.
parseMediaType
(
a
.
getMimeType
())
:
MediaType
.
APPLICATION_OCTET_STREAM
);
headers
.
set
(
"Content-Disposition"
,
"attachment; filename=\""
+
a
.
getFilename
()
+
"\""
);
return
new
ResponseEntity
<>(
new
InputStreamResource
(
is
),
headers
,
HttpStatus
.
OK
);
}
@Autowired
(
required
=
false
)
public
void
setService
(
AttachmentService
service
)
{
this
.
service
=
service
;
}
}
src/main/java/com/jh/boot/web/Converter.java
0 → 100644
View file @
cfc11eb8
package
com
.
jh
.
boot
.
web
;
import
com.jh.boot.jpa.AbstractIdEntity
;
import
com.jh.boot.web.list.Page
;
import
com.jh.boot.web.list.SortTrend
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.function.Function
;
import
java.util.stream.Collectors
;
public
class
Converter
{
private
Converter
()
{}
public
static
<
S
,
D
>
D
convert
(
S
source
,
Function
<
S
,
D
>
mappingFunction
)
{
if
(
source
==
null
||
mappingFunction
==
null
)
{
return
null
;
}
return
mappingFunction
.
apply
(
source
);
}
public
static
<
S
,
D
>
List
<
D
>
convertList
(
List
<
S
>
list
,
Function
<
S
,
D
>
mappingFunction
)
{
if
(
list
==
null
)
{
return
null
;
}
return
list
.
stream
().
map
(
mappingFunction
).
collect
(
Collectors
.
toList
());
}
public
static
<
S
,
D
>
Page
<
D
>
convertPage
(
Page
<
S
>
page
,
Function
<
S
,
D
>
mappingFunction
)
{
if
(
page
==
null
||
mappingFunction
==
null
)
{
return
null
;
}
List
<
D
>
content
=
new
ArrayList
<>();
if
(
page
.
getContent
()
!=
null
)
{
for
(
S
s
:
page
.
getContent
())
{
content
.
add
(
mappingFunction
.
apply
(
s
));
}
}
return
new
Page
<
D
>(
page
.
getPage
(),
page
.
getPagesCount
(),
page
.
getTotalElements
(),
content
,
page
.
getSortBy
(),
SortTrend
.
parse
(
page
.
getTrend
()));
}
/**
* Helper function to safely get id
*
* @param entity
* @return
*/
public
static
Long
getId
(
AbstractIdEntity
entity
)
{
return
entity
==
null
?
null
:
entity
.
getId
();
}
}
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