1313package com .tinyengine .it .controller ;
1414
1515import com .tinyengine .it .common .base .Result ;
16+ import com .tinyengine .it .common .context .LoginUserContext ;
17+ import com .tinyengine .it .common .exception .ExceptionEnum ;
1618import com .tinyengine .it .common .log .SystemControllerLog ;
1719import com .tinyengine .it .common .utils .ImageThumbnailGenerator ;
1820import com .tinyengine .it .common .utils .Utils ;
2830import jakarta .servlet .http .HttpServletResponse ;
2931import jakarta .validation .Valid ;
3032import org .springframework .beans .factory .annotation .Autowired ;
33+ import org .springframework .util .StringUtils ;
3134import org .springframework .validation .annotation .Validated ;
3235import org .springframework .web .bind .annotation .DeleteMapping ;
3336import org .springframework .web .bind .annotation .GetMapping ;
3841import org .springframework .web .bind .annotation .RequestMapping ;
3942import org .springframework .web .bind .annotation .RequestParam ;
4043import org .springframework .web .bind .annotation .RestController ;
44+ import org .springframework .web .multipart .MultipartFile ;
4145
4246import java .io .OutputStream ;
4347import java .net .URLEncoder ;
@@ -61,6 +65,9 @@ public class ResourceController {
6165 @ Autowired
6266 private ResourceService resourceService ;
6367
68+ @ Autowired
69+ private LoginUserContext loginUserContext ;
70+
6471 /**
6572 * 查询表Resource信息
6673 *
@@ -69,7 +76,7 @@ public class ResourceController {
6976 @ Operation (summary = "查询表Resource信息" , description = "查询表Resource信息" ,
7077 responses = {
7178 @ ApiResponse (responseCode = "200" , description = "返回信息" ,
72- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
79+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
7380 @ ApiResponse (responseCode = "400" , description = "请求失败" )
7481 })
7582 @ SystemControllerLog (description = "查询表Resource信息" )
@@ -90,7 +97,7 @@ public Result<List<Resource>> getAllResource() {
9097 @ Parameter (name = "id" , description = "Resource主键id" )
9198 }, responses = {
9299 @ ApiResponse (responseCode = "200" , description = "返回信息" ,
93- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
100+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
94101 @ ApiResponse (responseCode = "400" , description = "请求失败" )
95102 })
96103 @ SystemControllerLog (description = "根据id查询表Resource信息" )
@@ -110,7 +117,7 @@ public Result<Resource> getResourceById(@PathVariable Integer id) {
110117 @ Parameter (name = "resourceGroupId" , description = "ResourceGroup主键id" )
111118 }, responses = {
112119 @ ApiResponse (responseCode = "200" , description = "返回信息" ,
113- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
120+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
114121 @ ApiResponse (responseCode = "400" , description = "请求失败" )
115122 })
116123 @ SystemControllerLog (description = "根据分组id和创建人查询表t_resource信息" )
@@ -132,7 +139,7 @@ public Result<List<Resource>> getResourceByResourceGroupId(@PathVariable Integer
132139 @ Parameter (name = "des" , description = "描述" )
133140 }, responses = {
134141 @ ApiResponse (responseCode = "200" , description = "返回信息" ,
135- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
142+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
136143 @ ApiResponse (responseCode = "400" , description = "请求失败" )
137144 })
138145 @ SystemControllerLog (description = "模糊查询表Resource信息列表" )
@@ -154,7 +161,7 @@ public Result<List<Resource>> getResourceById(@RequestParam String name, @Reques
154161 @ Parameter (name = "resource" , description = "Resource入参对象" )
155162 }, responses = {
156163 @ ApiResponse (responseCode = "200" , description = "返回信息" ,
157- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
164+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
158165 @ ApiResponse (responseCode = "400" , description = "请求失败" )
159166 })
160167 @ SystemControllerLog (description = "创建resource" )
@@ -167,20 +174,36 @@ public Result<Resource> createResource(@Valid @RequestBody Resource resource) th
167174 /**
168175 * 上传图片
169176 *
170- * @param resource the resource
177+ * @param file the file
171178 * @return Resource信息 result
172179 */
173180 @ Operation (summary = "上传图片" , description = "上传图片" ,
174- parameters = {
175- @ Parameter (name = "resource " , description = "Resource入参对象 " )
176- }, responses = {
181+ parameters = {
182+ @ Parameter (name = "file " , description = "图片 " )
183+ }, responses = {
177184 @ ApiResponse (responseCode = "200" , description = "返回信息" ,
178- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
185+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
179186 @ ApiResponse (responseCode = "400" , description = "请求失败" )
180187 })
181188 @ SystemControllerLog (description = "上传图片" )
182- @ PostMapping ("/resource/uoload" )
183- public Result <Resource > resourceUoload (@ Valid @ RequestBody Resource resource ) throws Exception {
189+ @ PostMapping ("/resource/upload" )
190+ public Result <Resource > resourceUpload (@ RequestParam MultipartFile file ) throws Exception {
191+ // 获取文件的原始名称
192+ String fileName = StringUtils .cleanPath (java .util .Optional .ofNullable (file .getOriginalFilename ()).orElse ("image" ));
193+
194+ if (!ImageThumbnailGenerator .validateByImageIO (file )){
195+ return Result .failed (ExceptionEnum .CM325 );
196+ }
197+ if (fileName .contains (".." )) {
198+ return Result .failed (ExceptionEnum .CM325 );
199+ }
200+ // 将文件转为 Base64
201+ String base64 = ImageThumbnailGenerator .convertToBase64 (file );
202+ Resource resource = new Resource ();
203+ resource .setName (fileName );
204+ resource .setResourceData (base64 );
205+ resource .setAppId (loginUserContext .getAppId ());
206+ resource .setCategory ("image" );
184207 Resource result = resourceService .resourceUpload (resource );
185208 return Result .success (result );
186209 }
@@ -196,7 +219,7 @@ public Result<Resource> resourceUoload(@Valid @RequestBody Resource resource) th
196219 @ Parameter (name = "resources" , description = "Resource入参对象" )
197220 }, responses = {
198221 @ ApiResponse (responseCode = "200" , description = "返回信息" ,
199- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
222+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
200223 @ ApiResponse (responseCode = "400" , description = "请求失败" )
201224 })
202225 @ SystemControllerLog (description = "批量创建Resource" )
@@ -218,7 +241,7 @@ public Result<List<Resource>> createResource(@Valid @RequestBody List<Resource>
218241 @ Parameter (name = "id" , description = "id" ),
219242 @ Parameter (name = "Resource" , description = "入参对象" )}, responses = {
220243 @ ApiResponse (responseCode = "200" , description = "返回信息" ,
221- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
244+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
222245 @ ApiResponse (responseCode = "400" , description = "请求失败" )
223246 })
224247 @ SystemControllerLog (description = "修改单个Resource信息" )
@@ -258,7 +281,7 @@ public Result<Resource> deleteResource(@PathVariable Integer id) {
258281 parameters = {
259282 @ Parameter (name = "id" , description = "id" )}, responses = {
260283 @ ApiResponse (responseCode = "200" , description = "返回信息" ,
261- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
284+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
262285 @ ApiResponse (responseCode = "400" , description = "请求失败" )
263286 })
264287 @ SystemControllerLog (description = "获取resource信息详情" )
@@ -277,7 +300,7 @@ public Result<Resource> detail(@PathVariable Integer id) {
277300 parameters = {
278301 @ Parameter (name = "data" , description = "base64编码数据" )}, responses = {
279302 @ ApiResponse (responseCode = "200" , description = "图片流数据" ,
280- content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
303+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = Resource .class ))),
281304 @ ApiResponse (responseCode = "400" , description = "请求失败" )
282305 })
283306 @ SystemControllerLog (description = "获取资源" )
0 commit comments