|
@@ -17,6 +17,8 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
import org.jsoup.nodes.Document;
|
|
|
+import org.jsoup.nodes.Element;
|
|
|
+import org.jsoup.select.Elements;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
|
@@ -65,7 +67,7 @@ import io.swagger.annotations.ApiParam;
|
|
|
@Api(tags = "公告类")
|
|
|
@RequestMapping("${$rmp.ctr.examwork}/notice")
|
|
|
public class NoticeController extends ControllerSupport {
|
|
|
- private static Pattern pattern = Pattern.compile("\\s*");
|
|
|
+ private static Pattern pattern = Pattern.compile("\\s*");
|
|
|
@Autowired
|
|
|
private NoticeService noticeService;
|
|
|
|
|
@@ -246,20 +248,60 @@ public class NoticeController extends ControllerSupport {
|
|
|
}
|
|
|
}
|
|
|
String content = addNoticeDomain.getContent();
|
|
|
- Document doc = Jsoup.parse(content);
|
|
|
- Matcher m = pattern.matcher(doc.text());
|
|
|
- String simpleText = m.replaceAll("").replaceAll(" ", "");
|
|
|
// 普通文本内容不允许超过500个字
|
|
|
- if (simpleText.length() > 500) {
|
|
|
+ if (getSimpleTextLength(content) > 500) {
|
|
|
throw new StatusException("500010", "通知内容不得超过500个字符");
|
|
|
}
|
|
|
- // 总大小不得超过5M
|
|
|
- if (content.getBytes().length > 5 * 1024 * 1024) {
|
|
|
+ // 图片总大小不得超过2M
|
|
|
+ int imgSize = getImageSize(content);
|
|
|
+ if (imgSize > (2 << 20)) {
|
|
|
DecimalFormat decimalFormat = new DecimalFormat("#.##");
|
|
|
String currentLength = decimalFormat
|
|
|
- .format(((double) content.getBytes().length / (double) 1024) / (double) 1024);
|
|
|
- throw new StatusException("500010", "通知内容总大小不得超过5MB,当前大小为:" + currentLength + "MB");
|
|
|
+ .format(((double) imgSize / (double) 1024 / (double) 1024));
|
|
|
+ throw new StatusException("500011", "图片大小不得超过2MB,当前大小为:" + currentLength + "MB");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private int getSimpleTextLength(String content) {
|
|
|
+ Document doc = Jsoup.parse(content);
|
|
|
+ Matcher m = pattern.matcher(doc.text());
|
|
|
+ return m.replaceAll("").replaceAll(" ", "").length();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取图片大小,单位:字节
|
|
|
+ *
|
|
|
+ * @param content
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private int getImageSize(String content) {
|
|
|
+ Document doc = Jsoup.parse(content);
|
|
|
+ Elements imgElements = doc.select("img[src]");
|
|
|
+ if (imgElements == null || imgElements.isEmpty()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ int totalSize = 0;
|
|
|
+ for (Element el : imgElements) {
|
|
|
+ String src = el.attr("src");
|
|
|
+ Integer beginIndex;
|
|
|
+ if (src.indexOf(",") > 0) {
|
|
|
+ beginIndex = src.indexOf(",") + 1;
|
|
|
+ } else {
|
|
|
+ beginIndex = 0;
|
|
|
+ }
|
|
|
+ Integer endIndex;
|
|
|
+ if (src.indexOf("=") > 0) {
|
|
|
+ endIndex = src.indexOf("=");
|
|
|
+ } else {
|
|
|
+ endIndex = src.length();
|
|
|
+ }
|
|
|
+ String imgOriginalData = src.substring(beginIndex, endIndex);
|
|
|
+ //原字节符流大小
|
|
|
+ int originalLength = imgOriginalData.length();
|
|
|
+ totalSize += originalLength - (originalLength / 8) * 2;//最终的文件大小(单位字节Byte)
|
|
|
}
|
|
|
+ return totalSize;
|
|
|
}
|
|
|
|
|
|
private void validateUpdateNotice(UpdateNoticeDomain updateNoticeDomain) {
|
|
@@ -271,19 +313,17 @@ public class NoticeController extends ControllerSupport {
|
|
|
}
|
|
|
}
|
|
|
String content = updateNoticeDomain.getContent();
|
|
|
- Document doc = Jsoup.parse(content);
|
|
|
- Matcher m = pattern.matcher(doc.text());
|
|
|
- String simpleText = m.replaceAll("").replaceAll(" ", "");
|
|
|
// 普通文本内容不允许超过500个字
|
|
|
- if (simpleText.length() > 500) {
|
|
|
+ if (getSimpleTextLength(content) > 500) {
|
|
|
throw new StatusException("500012", "通知内容不得超过500个字符");
|
|
|
}
|
|
|
- // 总大小不得超过5M
|
|
|
- if (content.getBytes().length > 5 * 1024 * 1024) {
|
|
|
+ // 图片总大小不得超过2M
|
|
|
+ int imgSize = getImageSize(content);
|
|
|
+ if (imgSize > (2 << 20)) {
|
|
|
DecimalFormat decimalFormat = new DecimalFormat("#.##");
|
|
|
String currentLength = decimalFormat
|
|
|
- .format(((double) content.getBytes().length / (double) 1024) / (double) 1024);
|
|
|
- throw new StatusException("500010", "通知内容总大小不得超过5MB,当前大小为:" + currentLength + "MB");
|
|
|
+ .format(((double) imgSize / (double) 1024 / (double) 1024));
|
|
|
+ throw new StatusException("500015", "图片总大小不得超过2MB,当前大小为:" + currentLength + "MB");
|
|
|
}
|
|
|
|
|
|
NoticeEntity notice = GlobalHelper.getEntity(noticeRepo, updateNoticeDomain.getId(),
|
|
@@ -371,4 +411,8 @@ public class NoticeController extends ControllerSupport {
|
|
|
return process.getMaxCommonUserId();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ }
|
|
|
}
|