Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Litemall
Commits
b4942f18
Commit
b4942f18
authored
Apr 19, 2018
by
Junling Bu
Browse files
update[litemall-os-api]: 对象存储服务可以在线查看图片。
parent
0bb04413
Changes
1
Hide whitespace changes
Inline
Side-by-side
litemall-os-api/src/main/java/org/linlinjava/litemall/os/web/StorageController.java
View file @
b4942f18
...
...
@@ -9,6 +9,7 @@ import org.linlinjava.litemall.os.config.ObjectStorageConfig;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
...
...
@@ -34,15 +35,18 @@ public class StorageController {
private
ObjectStorageConfig
osConfig
;
private
String
generateUrl
(
String
key
){
return
"http://"
+
osConfig
.
getAddress
()
+
":"
+
osConfig
.
getPort
()
+
"/storage/storage/fetch
?key=
"
+
key
;
return
"http://"
+
osConfig
.
getAddress
()
+
":"
+
osConfig
.
getPort
()
+
"/storage/storage/fetch
/
"
+
key
;
}
private
final
String
generateKey
(){
private
String
generateKey
(
String
originalFilename
){
int
index
=
originalFilename
.
lastIndexOf
(
'.'
);
String
suffix
=
originalFilename
.
substring
(
index
);
String
key
=
null
;
LitemallStorage
storageInfo
=
null
;
do
{
key
=
CharUtil
.
getRandomString
(
20
);
key
=
CharUtil
.
getRandomString
(
20
)
+
suffix
;
storageInfo
=
litemallStorageService
.
findByKey
(
key
);
}
while
(
storageInfo
!=
null
);
...
...
@@ -74,7 +78,7 @@ public class StorageController {
e
.
printStackTrace
();
return
ResponseUtil
.
badArgumentValue
();
}
String
key
=
generateKey
();
String
key
=
generateKey
(
originalFilename
);
storageService
.
store
(
inputStream
,
key
);
String
url
=
generateUrl
(
key
);
...
...
@@ -116,25 +120,36 @@ public class StorageController {
return
ResponseUtil
.
ok
();
}
@GetMapping
(
"/fetch"
)
public
ResponseEntity
<
Resource
>
fetch
(
String
key
)
{
@GetMapping
(
"/fetch/{key:.+}"
)
public
ResponseEntity
<
Resource
>
fetch
(
@PathVariable
String
key
)
{
LitemallStorage
litemallStorage
=
litemallStorageService
.
findByKey
(
key
);
if
(
key
==
null
){
ResponseEntity
.
notFound
();
}
String
type
=
litemallStorage
.
getType
();
MediaType
mediaType
=
MediaType
.
parseMediaType
(
type
);
Resource
file
=
storageService
.
loadAsResource
(
key
);
if
(
file
==
null
)
{
ResponseEntity
.
notFound
();
}
return
ResponseEntity
.
ok
().
body
(
file
);
return
ResponseEntity
.
ok
().
contentType
(
mediaType
).
body
(
file
);
}
@GetMapping
(
"/download"
)
public
ResponseEntity
<
Resource
>
download
(
String
key
)
{
@GetMapping
(
"/download/{key:.+}"
)
public
ResponseEntity
<
Resource
>
download
(
@PathVariable
String
key
)
{
LitemallStorage
litemallStorage
=
litemallStorageService
.
findByKey
(
key
);
if
(
key
==
null
){
ResponseEntity
.
notFound
();
}
String
type
=
litemallStorage
.
getType
();
MediaType
mediaType
=
MediaType
.
parseMediaType
(
type
);
Resource
file
=
storageService
.
loadAsResource
(
key
);
if
(
file
==
null
)
{
ResponseEntity
.
notFound
();
}
return
ResponseEntity
.
ok
().
header
(
HttpHeaders
.
CONTENT_DISPOSITION
,
return
ResponseEntity
.
ok
().
contentType
(
mediaType
).
header
(
HttpHeaders
.
CONTENT_DISPOSITION
,
"attachment; filename=\""
+
file
.
getFilename
()
+
"\""
).
body
(
file
);
}
...
...
Write
Preview
Supports
Markdown
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