123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package com.its.web.service.common;
- import com.its.web.dto.common.CodeDto;
- import com.its.web.dto.common.ConnStatisticsDto;
- import com.its.web.dto.common.TbWwwOrgDto;
- import com.its.web.mapper.its.common.CommonMapper;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.io.FileUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import javax.servlet.http.HttpServletRequest;
- import java.io.*;
- import java.net.URLDecoder;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
- @Slf4j
- @RequiredArgsConstructor
- @Service
- public class CommonService {
- private final CommonMapper mapper;
- @Value("${image-location}")
- String imageLocation;
- @Value("${video-location}")
- String videoLocation;
- /**
- * 공공기관 사이트 URL
- * @return List<TbWwwOrgDto>
- */
- public List<TbWwwOrgDto> findAllOrganization() {
- return this.mapper.findAllOrganization();
- }
- /**
- * Day Code 리스트
- * @return List<CodeDto>
- */
- public List<CodeDto> findDayList() {
- return this.mapper.findDayList();
- }
- /**
- * 접속자 조회 통계
- * @return List<ConnStatisticsDto>
- */
- public List<ConnStatisticsDto> getConnStatistics(Map<String, String> paramMap) {
- String type = paramMap.get("type");
- if ("week".equals(type)) {
- return this.mapper.getConnWeekStatistics(paramMap);
- }
- else if ("month".equals(type)) {
- return this.mapper.getConnMonthStatistics(paramMap);
- }
- return this.mapper.getConnStatistics(paramMap);
- }
- /**
- * 접속자 이력 등록
- * @return DB 등록 수
- */
- public int insertConnHs() { return this.mapper.insertConnHs(); }
- public String imageUpload(HttpServletRequest req) {
- String uploadImageInfo = "";
- String fileName = req.getHeader("file-name");
- try {
- fileName = URLDecoder.decode(fileName, "UTF-8");
- }
- catch (UnsupportedEncodingException exception) {
- log.error("Can not Decoding UTF-8");
- }
- String filePath = imageLocation;
- File file = new File(filePath);
- boolean isMkdirs = true;
- if (!file.exists()) {
- isMkdirs = file.mkdirs();
- }
- if (isMkdirs) {
- String realFileNm;
- realFileNm = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."));
- String rlFileNm = filePath + File.separator + realFileNm;
- try (
- InputStream is = req.getInputStream();
- OutputStream os = new FileOutputStream(rlFileNm);
- ) {
- int numRead;
- byte[] byteArray = new byte[Integer.parseInt(req.getHeader("file-size"))];
- if (is != null) {
- while ((numRead = is.read(byteArray, 0, byteArray.length)) != -1) {
- os.write(byteArray, 0, numRead);
- }
- }
- os.flush();
- } catch (IOException e) {
- log.error("noticeImgupload() IO Exception");
- } catch (Exception e1) {
- log.error("noticeImgupload() Exception");
- }
- uploadImageInfo += "&bNewLine=true";
- uploadImageInfo += "&sFileName=" + fileName;
- uploadImageInfo += "&sFileURL=" + "/api/common/upload/" + realFileNm;
- }
- return uploadImageInfo;
- }
- // public byte[] upload(String imageName, String type) {
- public byte[] upload(String imageName) {
- byte[] image = null;
- File file = new File(imageLocation, imageName);
- // if (type.equals("video")) {
- // file = new File(videoLocation, imageName);
- // }
- int fileSize = (int) file.length();
- if (fileSize > 0) {
- try {
- byte[] byteArray = FileUtils.readFileToByteArray(file);
- if (byteArray.length > 0) {
- int ii = 0;
- image = new byte[byteArray.length];
- for (byte b : byteArray) {
- image[ii++] = b;
- }
- }
- } catch (FileNotFoundException e) {
- log.error("Not Found File - {}", imageLocation + imageName);
- } catch (IOException e) {
- log.error("Can Not Send Image Cause IOException - {}", imageLocation + imageName);
- }
- }
- return image;
- }
- }
|