33import cn .bukkit .sip .exception .RestException ;
44import cn .bukkit .sip .orm .entity .ImgEntity ;
55import cn .bukkit .sip .pojo .RestData ;
6+ import cn .bukkit .sip .pojo .ImgMetaDto ;
67import cn .bukkit .sip .security .CasdoorAuthenticationToken ;
78import cn .bukkit .sip .service .ImgService ;
89import com .baomidou .mybatisplus .core .toolkit .Wrappers ;
910import com .baomidou .mybatisplus .extension .plugins .pagination .Page ;
10- import lombok .SneakyThrows ;
1111import org .springframework .beans .factory .annotation .Value ;
1212import org .springframework .validation .annotation .Validated ;
13- import org .springframework .web .bind .annotation .PathVariable ;
14- import org .springframework .web .bind .annotation .RequestMapping ;
15- import org .springframework .web .bind .annotation .ResponseBody ;
16- import org .springframework .web .bind .annotation .RestController ;
13+ import org .springframework .web .bind .annotation .*;
1714
1815import javax .annotation .Resource ;
1916import javax .validation .constraints .DecimalMax ;
2017import javax .validation .constraints .DecimalMin ;
2118import javax .validation .constraints .NotNull ;
2219import java .time .LocalDateTime ;
20+ import java .util .HashMap ;
2321import java .util .Optional ;
22+ import java .util .stream .Collectors ;
2423
2524@ RequestMapping ("/img" )
2625@ RestController
@@ -34,53 +33,71 @@ public class ImgController {
3433
3534 // 显示图片
3635 @ RequestMapping (path = "/get/{id}" , produces = "image/*;charset=utf-8" )
37- @ ResponseBody
38- public byte [] get (@ PathVariable Long id ) {
39- byte [] img = imgService .loadImg (id );
36+ public byte [] get (@ PathVariable Long id , CasdoorAuthenticationToken authentication ) {
37+ ImgEntity imgEntity = imgService .getImgDaoService ().getById (id );
38+ byte [] img = null ;
39+ if (imgEntity != null &&
40+ ((authentication != null && this .imgService .checkPermission (imgEntity , authentication )) ||
41+ this .imgService .limitCheck (imgEntity ))) {
42+ this .imgService .addTimes (imgEntity .getId ());
43+ img = imgService .loadImg (id );
44+ }
4045 if (img == null || img .length == 0 ) img = imgService .loadUnknownImg ();
4146 return img ;
4247 }
4348
44-
45- @ RequestMapping (path = "/del/{id}" )
46- @ ResponseBody
49+ @ PostMapping (path = "/del/{id}" )
4750 public RestData del (@ PathVariable Long id , CasdoorAuthenticationToken authentication ) {
4851 ImgEntity imgEntity = imgService .getImgDaoService ().getById (id );
4952 if (imgEntity == null ) throw RestException .builder ().message ("图片不存在" ).build ();
50- if (imgEntity .getOwner ().isBlank ()
51- ||
52- !(authentication != null &&
53- (authentication .getPrincipal ().getRoles ().stream ().anyMatch (role -> role .getName ().contains ("admin" ))
54- || imgEntity .getOwner ().equalsIgnoreCase (authentication .getPrincipal ().getId ()))
55- )
56- )
53+ if (!this .imgService .checkPermission (imgEntity , authentication ))
5754 throw RestException .builder ().message ("无权操作" ).build ();
5855 if (!imgService .getImgDaoService ().removeById (id )) throw RestException .builder ().message ("删除失败" ).build ();
5956 return RestData .builder ().build ();
6057 }
6158
6259 @ RequestMapping (path = "/info/{id}" )
63- @ ResponseBody
64- @ SneakyThrows
6560 public RestData info (@ PathVariable Long id ) {
6661 ImgEntity imgEntity = imgService .getImgDaoService ().getById (id );
6762 if (imgEntity == null ) throw RestException .builder ().message ("图片不存在" ).build ();
68- if (imgEntity .getDateLimit ().isBefore (LocalDateTime .now ()) ||
69- (Optional .ofNullable (imgEntity .getTimesLimit ()).orElse (0 ) > 0 && this .imgService .getTimes (imgEntity .getId ()) >= imgEntity .getTimesLimit ()))
70- throw RestException .builder ().message ("图片已过期" ).build ();
71- this .imgService .addTimes (imgEntity .getId ());
72- return RestData .builder ().data (imgEntity ).build ();
63+ return RestData .builder ().data (new HashMap <>() {
64+ {
65+ put ("info" , imgEntity );
66+ put ("times" , imgService .getTimes (id ));
67+ }
68+ }).build ();
69+ }
70+
71+ @ PostMapping (path = "/edit/{id}" )
72+ public RestData edit (@ PathVariable Long id , ImgMetaDto dto , CasdoorAuthenticationToken authenticationToken ) {
73+ ImgEntity imgEntity = imgService .getImgDaoService ().getById (id );
74+ if (imgEntity == null ) throw RestException .builder ().message ("图片不存在" ).build ();
75+ if (!this .imgService .checkPermission (imgEntity , authenticationToken ))
76+ throw RestException .builder ().message ("无权操作" ).build ();
77+ if (dto .getIsPublic () != null ) imgEntity .setIsPublic (dto .getIsPublic ());
78+ if (dto .getDateLimit () != null && dto .getDateLimit () != 0 )
79+ imgEntity .setDateLimitFromTimestamp (dto .getDateLimit () / 1000 );
80+ else if (Optional .ofNullable (dto .getDateLimit ()).orElse (0L ) == 0L )
81+ imgEntity .setDateLimit (null );
82+ if (dto .getTimesLimit () != null ) imgEntity .setTimesLimit (dto .getTimesLimit ());
83+ this .imgService .getImgDaoService ().updateById (imgEntity );
84+ return RestData .builder ().build ();
7385 }
7486
7587
76- @ RequestMapping ("/list" )
77- @ ResponseBody
88+ @ PostMapping ("/list" )
7889 public RestData list (@ NotNull @ DecimalMin ("1" ) Integer current , @ NotNull @ DecimalMax ("20" ) Integer size ) {
7990 Page <ImgEntity > page = this .imgService .getImgDaoService ().page (
8091 new Page <>(current , size ),
8192 Wrappers .<ImgEntity >lambdaQuery ()
8293 .eq (ImgEntity ::getIsPublic , true )
94+ .and (i -> i .isNull (ImgEntity ::getDateLimit ).or ().gt (ImgEntity ::getDateLimit , LocalDateTime .now ()))
8395 .orderByDesc (ImgEntity ::getId ));
84- return RestData .builder ().data (page .getRecords ()).build ();
96+ return RestData .builder ().data (new HashMap <>() {
97+ {
98+ put ("img" , page .getRecords ().stream ().filter (r -> imgService .limitCheck (r )).collect (Collectors .toList ()));
99+ put ("hasNext" , page .getSize () >= size );
100+ }
101+ }).build ();
85102 }
86103}
0 commit comments