Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
T
tanpu-community
Project
Project
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
探普后端
tanpu-community
Commits
9f9f90f5
Commit
9f9f90f5
authored
Aug 02, 2021
by
张辰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
oss更改目录
parent
5d339557
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
66 additions
and
27 deletions
+66
-27
OssDirEnum.java
...c/main/java/com/tanpu/community/api/enums/OssDirEnum.java
+31
-0
FileController.java
...n/java/com/tanpu/community/controller/FileController.java
+17
-6
FileManager.java
...rc/main/java/com/tanpu/community/manager/FileManager.java
+13
-8
TopicManager.java
...c/main/java/com/tanpu/community/manager/TopicManager.java
+1
-0
OSSFileService.java
...main/java/com/tanpu/community/service/OSSFileService.java
+4
-13
No files found.
community-api/src/main/java/com/tanpu/community/api/enums/OssDirEnum.java
0 → 100644
View file @
9f9f90f5
package
com
.
tanpu
.
community
.
api
.
enums
;
public
enum
OssDirEnum
{
Others
(
"others/"
,
"其他"
),
Theme_Pic
(
"pic/theme/"
,
"主题下的图片"
),
;
public
String
dir
;
public
String
type
;
public
String
getDir
()
{
return
dir
;
}
public
void
setDir
(
String
dir
)
{
this
.
dir
=
dir
;
}
public
String
getType
()
{
return
type
;
}
public
void
setType
(
String
type
)
{
this
.
type
=
type
;
}
OssDirEnum
(
String
dir
,
String
type
)
{
this
.
dir
=
dir
;
this
.
type
=
type
;
}
}
community-service/src/main/java/com/tanpu/community/controller/FileController.java
View file @
9f9f90f5
...
...
@@ -2,9 +2,12 @@ package com.tanpu.community.controller;
import
com.fasterxml.jackson.core.type.TypeReference
;
import
com.tanpu.common.api.CommonResp
;
import
com.tanpu.common.auth.AuthLogin
;
import
com.tanpu.common.auth.UserHolder
;
import
com.tanpu.common.util.JsonUtil
;
import
com.tanpu.community.api.beans.qo.ThemeQo
;
import
com.tanpu.community.api.beans.resp.FileUploadResp
;
import
com.tanpu.community.api.enums.OssDirEnum
;
import
com.tanpu.community.cache.RedisCache
;
import
com.tanpu.community.manager.FileManager
;
import
lombok.extern.slf4j.Slf4j
;
...
...
@@ -13,6 +16,7 @@ import org.springframework.cache.caffeine.CaffeineCacheManager;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.annotation.Resource
;
import
java.util.Arrays
;
import
java.util.List
;
...
...
@@ -24,16 +28,23 @@ public class FileController {
@Autowired
private
FileManager
fileManager
;
@Autowired
private
RedisCache
redisCache
;
@Autowired
private
CaffeineCacheManager
localCache
;
@Resource
private
UserHolder
userHolder
;
@PostMapping
(
"/uploadFile"
)
@ResponseBody
@AuthLogin
public
CommonResp
<
FileUploadResp
>
uploadToRemote
(
@RequestParam
(
value
=
"file"
)
MultipartFile
file
)
{
return
CommonResp
.
success
(
fileManager
.
uploadFile
(
file
));
String
userId
=
userHolder
.
getUserId
();
return
CommonResp
.
success
(
fileManager
.
uploadFile
(
file
,
OssDirEnum
.
Others
,
userId
));
}
@PostMapping
(
"/uploadThemePic"
)
@ResponseBody
@AuthLogin
public
CommonResp
<
FileUploadResp
>
uploadThemePic
(
@RequestParam
(
value
=
"file"
)
MultipartFile
file
)
{
String
userId
=
userHolder
.
getUserId
();
return
CommonResp
.
success
(
fileManager
.
uploadFile
(
file
,
OssDirEnum
.
Theme_Pic
,
userId
));
}
@GetMapping
(
"/test"
)
...
...
community-service/src/main/java/com/tanpu/community/manager/FileManager.java
View file @
9f9f90f5
...
...
@@ -3,14 +3,15 @@ package com.tanpu.community.manager;
import
com.tanpu.common.constant.ErrorCodeConstant
;
import
com.tanpu.common.exception.BizException
;
import
com.tanpu.community.api.beans.resp.FileUploadResp
;
import
com.tanpu.community.api.enums.OssDirEnum
;
import
com.tanpu.community.dao.entity.community.FileRecordEntity
;
import
com.tanpu.community.service.OSSFileService
;
import
com.tanpu.community.util.ConvertUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.annotation.Cacheable
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
...
...
@@ -25,7 +26,7 @@ public class FileManager {
@Autowired
private
OSSFileService
ossFileService
;
public
FileUploadResp
uploadFile
(
MultipartFile
file
)
{
public
FileUploadResp
uploadFile
(
MultipartFile
file
,
OssDirEnum
dirEnum
,
String
userId
)
{
if
(
file
==
null
)
{
throw
new
BizException
(
ErrorCodeConstant
.
FILE_UPLOAD_FAIL
);
}
...
...
@@ -42,7 +43,16 @@ public class FileManager {
if
(
data
.
length
==
0
||
StringUtils
.
isEmpty
(
originalName
))
{
throw
new
BizException
(
"上传文件为空"
);
}
FileRecordEntity
fileRecordEntity
=
ossFileService
.
uploadFile
(
data
,
originalName
);
String
dirPrefix
=
""
;
if
(
dirEnum
==
null
)
{
dirPrefix
=
dirPrefix
+
dirEnum
.
dir
;
}
if
(
StringUtils
.
isNotBlank
(
userId
))
{
dirPrefix
=
dirPrefix
+
userId
+
"/"
;
}
FileRecordEntity
fileRecordEntity
=
ossFileService
.
uploadFile
(
data
,
originalName
,
dirPrefix
);
return
ConvertUtil
.
fileRecordEntity2Resp
(
fileRecordEntity
);
}
...
...
@@ -51,9 +61,4 @@ public class FileManager {
.
collect
(
Collectors
.
toMap
(
FileRecordEntity:
:
getFileId
,
FileRecordEntity:
:
getUrl
));
}
@Cacheable
(
value
=
"local"
,
key
=
"#id"
)
public
String
getId
(
String
id
)
{
System
.
out
.
println
(
"cache "
+
id
);
return
"haha "
+
id
;
}
}
community-service/src/main/java/com/tanpu/community/manager/TopicManager.java
View file @
9f9f90f5
...
...
@@ -43,6 +43,7 @@ public class TopicManager {
}
// 话题详情页
public
TopicRankQo
getDetail
(
String
topicId
)
{
//TODO 临时埋点,接入新埋点后删除
visitLogService
.
addPageView
(
userHolder
.
getUserId
(),
topicId
,
COMM_VISIT_TOPIC_DETAIL
);
...
...
community-service/src/main/java/com/tanpu/community/service/OSSFileService.java
View file @
9f9f90f5
...
...
@@ -58,27 +58,19 @@ public class OSSFileService {
@Transactional
public
FileRecordEntity
uploadFile
(
byte
[]
data
,
String
fileName
)
{
public
FileRecordEntity
uploadFile
(
byte
[]
data
,
String
fileName
,
String
dirPrefix
)
{
String
fileSuffix
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
'.'
)
+
1
);
return
uploadFile
(
data
,
fileName
,
fileSuffix
,
null
,
OssRelType
.
None
);
return
uploadFile
(
data
,
fileName
,
fileSuffix
,
dirPrefix
);
}
@Transactional
public
FileRecordEntity
uploadFile
(
byte
[]
data
,
String
fileName
,
String
relId
,
OssRelType
relType
)
{
String
fileSuffix
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
'.'
)
+
1
);
return
uploadFile
(
data
,
fileName
,
fileSuffix
,
relId
,
relType
);
}
@Transactional
public
FileRecordEntity
uploadFile
(
byte
[]
data
,
String
fileName
,
String
fileSuffix
,
String
relId
,
OssRelType
relType
)
{
// todo uniqueLong
public
FileRecordEntity
uploadFile
(
byte
[]
data
,
String
fileName
,
String
fileSuffix
,
String
dirPrefix
)
{
String
[]
arr
=
StringUtils
.
split
(
fileName
,
"."
);
String
suffix
=
arr
[
arr
.
length
-
1
];
if
(
FileTypeEnum
.
imageTypeSet
.
contains
(
suffix
))
{
//上传
String
id
=
uuidGenHelper
.
getUuidStr
();
String
key
=
CommunityConstant
.
OSS_PREFIX_FOLDER
+
id
+
"."
+
suffix
;
String
key
=
CommunityConstant
.
OSS_PREFIX_FOLDER
+
dirPrefix
+
id
+
"."
+
suffix
;
ossHelper
.
writeFile
(
bucketName
,
key
,
data
,
fileSuffix
);
//落库
...
...
@@ -93,7 +85,6 @@ public class OSSFileService {
HashMap
<
String
,
Integer
>
attr
=
getStringIntegerHashMap
(
data
);
record
.
setExtInfo
(
JsonUtil
.
toJson
(
attr
));
fileRecordMapper
.
insert
(
record
);
return
record
;
...
...
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