CommonService.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.its.web.service.common;
  2. import com.its.web.dto.common.CodeDto;
  3. import com.its.web.dto.common.ConnStatisticsDto;
  4. import com.its.web.dto.common.TbWwwOrgDto;
  5. import com.its.web.dto.common.YearDto;
  6. import com.its.web.mapper.its.common.CommonMapper;
  7. import lombok.RequiredArgsConstructor;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.io.FileUtils;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.stereotype.Service;
  12. import javax.servlet.http.HttpServletRequest;
  13. import java.io.*;
  14. import java.net.URLDecoder;
  15. import java.util.*;
  16. @Slf4j
  17. @RequiredArgsConstructor
  18. @Service
  19. public class CommonService {
  20. private final CommonMapper mapper;
  21. @Value("${image-location}")
  22. String imageLocation;
  23. @Value("${video-location}")
  24. String videoLocation;
  25. /**
  26. * 공공기관 사이트 URL
  27. * @return List<TbWwwOrgDto>
  28. */
  29. public List<TbWwwOrgDto> findAllOrganization() {
  30. return this.mapper.findAllOrganization();
  31. }
  32. /**
  33. * Day Code 리스트
  34. * @return List<CodeDto>
  35. */
  36. public List<CodeDto> findDayList() {
  37. return this.mapper.findDayList();
  38. }
  39. /**
  40. * 접속자 조회 통계
  41. * @return List<ConnStatisticsDto>
  42. */
  43. public List<ConnStatisticsDto> getConnStatistics(ConnStatisticsDto.ConnStatisticsDtoReq req) {
  44. String type = req.getType();
  45. Map<String, String> paramMap = new HashMap<>();
  46. paramMap.put("type", type);
  47. paramMap.put("fromDate", req.getFromDate());
  48. paramMap.put("toDate", req.getToDate());
  49. if ("week".equals(type)) {
  50. return this.mapper.getConnWeekStatistics(paramMap);
  51. }
  52. else if ("month".equals(type)) {
  53. return this.mapper.getConnMonthStatistics(paramMap);
  54. }
  55. else if ("year".equals(type)) {
  56. return this.mapper.getConnYearStatistics(paramMap);
  57. }
  58. return this.mapper.getConnStatistics(paramMap);
  59. }
  60. /**
  61. * 접속자 이력 등록
  62. * @return DB 등록 수
  63. */
  64. public int insertConnHs() { return this.mapper.insertConnHs(); }
  65. public String imageUpload(HttpServletRequest req) {
  66. String uploadImageInfo = "";
  67. String fileName = req.getHeader("file-name");
  68. try {
  69. fileName = URLDecoder.decode(fileName, "UTF-8");
  70. }
  71. catch (UnsupportedEncodingException exception) {
  72. log.error("Can not Decoding UTF-8");
  73. }
  74. String filePath = imageLocation;
  75. File file = new File(filePath);
  76. boolean isMkdirs = true;
  77. if (!file.exists()) {
  78. file.setExecutable(false, true); /*파일 실행 권한 설정 (파라미터 - 일반, 소유자)*/
  79. file.setReadable(true); /*파일 읽기 권한 설정*/
  80. file.setWritable(false, true);
  81. isMkdirs = file.mkdirs();
  82. }
  83. if (isMkdirs) {
  84. String realFileNm;
  85. realFileNm = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."));
  86. String rlFileNm = filePath + File.separator + realFileNm;
  87. OutputStream os = null;
  88. try (
  89. InputStream is = req.getInputStream()
  90. ) {
  91. os = new FileOutputStream(rlFileNm);
  92. int numRead;
  93. byte[] byteArray = new byte[Integer.parseInt(req.getHeader("file-size"))];
  94. if (is != null) {
  95. while ((numRead = is.read(byteArray, 0, byteArray.length)) != -1) {
  96. os.write(byteArray, 0, numRead);
  97. }
  98. }
  99. os.flush();
  100. } catch (IOException e) {
  101. log.error("noticeImgupload() IO Exception");
  102. } catch (Exception e1) {
  103. log.error("noticeImgupload() Exception");
  104. }
  105. finally {
  106. if(os != null) {
  107. try{
  108. os.close();
  109. }
  110. catch (IOException e) {
  111. log.error("Cause IOException...");
  112. }
  113. }
  114. }
  115. uploadImageInfo += "&bNewLine=true";
  116. uploadImageInfo += "&sFileName=" + fileName;
  117. uploadImageInfo += "&sFileURL=" + "/api/common/upload/" + realFileNm;
  118. }
  119. return uploadImageInfo;
  120. }
  121. public byte[] upload(String imageName) {
  122. byte[] image = null;
  123. File file = new File(imageLocation, imageName);
  124. int fileSize = (int) file.length();
  125. if (fileSize > 0) {
  126. try {
  127. byte[] byteArray = FileUtils.readFileToByteArray(file);
  128. if (byteArray.length > 0) {
  129. int ii = 0;
  130. image = new byte[byteArray.length];
  131. for (byte b : byteArray) {
  132. image[ii++] = b;
  133. }
  134. }
  135. } catch (FileNotFoundException e) {
  136. log.error("Not Found File - {}", imageLocation + imageName);
  137. } catch (IOException e) {
  138. log.error("Can Not Send Image Cause IOException - {}", imageLocation + imageName);
  139. }
  140. }
  141. return image;
  142. }
  143. public List<YearDto> getYearList() { return this.mapper.getYearList(); }
  144. }