Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
legobackend
/
lego-manage
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
01d450fd
authored
2024-10-24 22:33:49 +0800
by
村长大人
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
处理文件存储URL问题
1 parent
22e4a1ac
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
8 deletions
lego-system/src/main/java/com/lego/system/service/impl/SysOssServiceImpl.java
lego-system/src/main/java/com/lego/system/service/impl/SysOssServiceImpl.java
View file @
01d450f
...
...
@@ -4,9 +4,11 @@ import cn.hutool.core.bean.BeanUtil;
import
cn.hutool.core.convert.Convert
;
import
cn.hutool.core.io.IoUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.baomidou.mybatisplus.extension.plugins.pagination.Page
;
import
com.lego.common.core.service.ConfigService
;
import
com.lego.system.domain.bo.SysOssBo
;
import
com.lego.system.service.ISysOssService
;
import
com.lego.common.constant.CacheNames
;
...
...
@@ -51,6 +53,7 @@ import java.util.stream.Collectors;
public
class
SysOssServiceImpl
implements
ISysOssService
,
OssService
{
private
final
SysOssMapper
baseMapper
;
private
final
ConfigService
configService
;
@Override
public
TableDataInfo
<
SysOssVo
>
queryPageList
(
SysOssBo
bo
,
PageQuery
pageQuery
)
{
...
...
@@ -61,9 +64,28 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
return
TableDataInfo
.
build
(
result
);
}
private
void
repUrl
(
List
<
SysOssVo
>
list
)
{
String
baseUrl
=
configService
.
getConfigValue
(
"sys.oss.baseUrl"
);
for
(
SysOssVo
vo
:
list
)
{
String
url
=
vo
.
getUrl
();
if
(
StrUtil
.
isNotBlank
(
url
)
&&
StrUtil
.
isNotBlank
(
baseUrl
))
{
vo
.
setUrl
(
url
.
replace
(
"127.0.0.1"
,
baseUrl
));
}
}
}
private
void
repUrl
(
SysOssVo
vo
)
{
String
baseUrl
=
configService
.
getConfigValue
(
"sys.oss.baseUrl"
);
String
url
=
vo
.
getUrl
();
if
(
StrUtil
.
isNotBlank
(
url
)
&&
StrUtil
.
isNotBlank
(
baseUrl
))
{
vo
.
setUrl
(
url
.
replace
(
"127.0.0.1"
,
baseUrl
));
}
}
@Override
public
List
<
SysOssVo
>
listByIds
(
Collection
<
Long
>
ossIds
)
{
List
<
SysOssVo
>
list
=
new
ArrayList
<>();
String
baseUrl
=
configService
.
getConfigValue
(
"sys.oss.baseUrl"
);
for
(
Long
id
:
ossIds
)
{
SysOssVo
vo
=
SpringUtils
.
getAopProxy
(
this
).
getById
(
id
);
if
(
ObjectUtil
.
isNotNull
(
vo
))
{
...
...
@@ -72,8 +94,10 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
}
catch
(
Exception
ignored
)
{
// 如果oss异常无法连接则将数据直接返回
list
.
add
(
vo
);
}
}
}
}
}
repUrl
(
list
);
return
list
;
}
...
...
@@ -91,7 +115,14 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
}
}
}
return
String
.
join
(
StringUtils
.
SEPARATOR
,
list
);
String
baseUrl
=
configService
.
getConfigValue
(
"sys.oss.baseUrl"
);
List
<
String
>
repList
=
new
ArrayList
<>();
for
(
String
url
:
list
)
{
if
(
StrUtil
.
isNotBlank
(
url
)
&&
StrUtil
.
isNotBlank
(
baseUrl
))
{
repList
.
add
(
url
.
replace
(
"127.0.0.1"
,
baseUrl
));
}
}
return
String
.
join
(
StringUtils
.
SEPARATOR
,
repList
);
}
private
LambdaQueryWrapper
<
SysOss
>
buildQueryWrapper
(
SysOssBo
bo
)
{
...
...
@@ -111,7 +142,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
@Cacheable
(
cacheNames
=
CacheNames
.
SYS_OSS
,
key
=
"#ossId"
)
@Override
public
SysOssVo
getById
(
Long
ossId
)
{
return
baseMapper
.
selectVoById
(
ossId
);
SysOssVo
vo
=
baseMapper
.
selectVoById
(
ossId
);
repUrl
(
vo
);
return
vo
;
}
@Override
...
...
@@ -123,7 +156,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
FileUtils
.
setAttachmentResponseHeader
(
response
,
sysOss
.
getOriginalName
());
response
.
setContentType
(
MediaType
.
APPLICATION_OCTET_STREAM_VALUE
+
"; charset=UTF-8"
);
OssClient
storage
=
OssFactory
.
instance
(
sysOss
.
getService
());
try
(
InputStream
inputStream
=
storage
.
getObjectContent
(
sysOss
.
getUrl
()))
{
try
(
InputStream
inputStream
=
storage
.
getObjectContent
(
sysOss
.
getUrl
()))
{
int
available
=
inputStream
.
available
();
IoUtil
.
copy
(
inputStream
,
response
.
getOutputStream
(),
available
);
response
.
setContentLength
(
available
);
...
...
@@ -144,7 +177,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
throw
new
ServiceException
(
e
.
getMessage
());
}
// 保存文件信息
return
buildResultEntity
(
originalfileName
,
suffix
,
storage
.
getConfigKey
(),
uploadResult
);
SysOssVo
vo
=
buildResultEntity
(
originalfileName
,
suffix
,
storage
.
getConfigKey
(),
uploadResult
);
repUrl
(
vo
);
return
vo
;
}
@Override
...
...
@@ -154,7 +189,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
OssClient
storage
=
OssFactory
.
instance
();
UploadResult
uploadResult
=
storage
.
uploadSuffix
(
file
,
suffix
);
// 保存文件信息
return
buildResultEntity
(
originalfileName
,
suffix
,
storage
.
getConfigKey
(),
uploadResult
);
SysOssVo
vo
=
buildResultEntity
(
originalfileName
,
suffix
,
storage
.
getConfigKey
(),
uploadResult
);
repUrl
(
vo
);
return
vo
;
}
@Override
...
...
@@ -172,7 +209,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
baseMapper
.
insert
(
oss
);
SysOssVo
sysOssVo
=
new
SysOssVo
();
BeanCopyUtils
.
copy
(
oss
,
sysOssVo
);
return
this
.
matchingUrl
(
sysOssVo
);
SysOssVo
vo
=
this
.
matchingUrl
(
sysOssVo
);
repUrl
(
vo
);
return
vo
;
}
private
SysOssVo
buildResultEntity
(
String
originalfileName
,
String
suffix
,
String
configKey
,
UploadResult
uploadResult
)
{
...
...
@@ -184,7 +223,9 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
oss
.
setService
(
configKey
);
baseMapper
.
insert
(
oss
);
SysOssVo
sysOssVo
=
BeanUtil
.
toBean
(
oss
,
SysOssVo
.
class
);
return
this
.
matchingUrl
(
sysOssVo
);
SysOssVo
vo
=
this
.
matchingUrl
(
sysOssVo
);
repUrl
(
vo
);
return
vo
;
}
@Override
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment