@ -6,6 +6,8 @@ import com.xit.biz.ctgy.entity.MinInfoBoard680;
import com.xit.biz.ctgy.service.ICtgyFileService ;
import com.xit.biz.ctgy.service.ICtgyFileService ;
import com.xit.core.api.IRestResponse ;
import com.xit.core.api.IRestResponse ;
import com.xit.core.api.RestResponse ;
import com.xit.core.api.RestResponse ;
import com.xit.core.constant.ErrorCode ;
import com.xit.core.exception.CustomBaseException ;
import com.xit.core.util.AssertUtils ;
import com.xit.core.util.AssertUtils ;
import com.xit.core.util.Checks ;
import com.xit.core.util.Checks ;
import io.swagger.v3.oas.annotations.Operation ;
import io.swagger.v3.oas.annotations.Operation ;
@ -14,9 +16,9 @@ import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FileUtils ;
import org.apache.commons.io.FileUtils ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.core.env.Environment ;
import org.springframework.core.env.Environment ;
import org.springframework. http.HttpStatus ;
import org.springframework. core.io.InputStreamResource ;
import org.springframework. http.MediaTyp e;
import org.springframework. core.io.Resourc e;
import org.springframework.http. ResponseEntity ;
import org.springframework.http. * ;
import org.springframework.lang.NonNull ;
import org.springframework.lang.NonNull ;
import org.springframework.web.bind.annotation.* ;
import org.springframework.web.bind.annotation.* ;
@ -26,6 +28,9 @@ import java.io.File;
import java.io.IOException ;
import java.io.IOException ;
import java.net.URLEncoder ;
import java.net.URLEncoder ;
import java.nio.charset.StandardCharsets ;
import java.nio.charset.StandardCharsets ;
import java.nio.file.Files ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.List ;
import java.util.List ;
@ -48,15 +53,15 @@ public class CtgyFileMgtController {
private final ICtgyFileService service ;
private final ICtgyFileService service ;
@Operation ( summary = "파일 조회" , description = "등록된 파일 조회" )
@Operation ( summary = "파일 조회" , description = "등록된 파일 조회" )
@GetMapping ( "/{id}" )
@GetMapping ( "/{id}" )
public ResponseEntity < ? extends IRestResponse > findFiles ( @PathVariable ( "inCode" ) @NonNull final Long inCode ) {
public ResponseEntity < ? extends IRestResponse > findFiles ( @PathVariable ( "inCode" ) @NonNull final Long inCode ) {
AssertUtils . isTrue ( ! Checks . isEmpty ( inCode ) , "대상 게시글[inCode]을 선택해 주세요." ) ;
AssertUtils . isTrue ( ! Checks . isEmpty ( inCode ) , "대상 게시글[inCode]을 선택해 주세요." ) ;
return RestResponse . of ( service . findFiles ( inCode ) ) ;
return RestResponse . of ( service . findFiles ( inCode ) ) ;
}
}
@Operation ( summary = "공지사항 저장" , description = "공지사항 저장" )
@Operation ( summary = "공지사항 저장" , description = "공지사항 저장" )
@PostMapping ( value = "/pboard" , consumes = MediaType . MULTIPART_FORM_DATA_VALUE )
@PostMapping ( value = "/pboard" , consumes = MediaType . MULTIPART_FORM_DATA_VALUE )
public ResponseEntity < ? extends IRestResponse > savePublicBoardFiles ( @Nonnull MinInfoBoard680Dto dto ) {
public ResponseEntity < ? extends IRestResponse > savePublicBoardFiles ( @Nonnull MinInfoBoard680Dto dto ) {
AssertUtils . isTrue ( ! Checks . isEmpty ( dto ) , "파일 정보가 존재하지 않습니다." ) ;
AssertUtils . isTrue ( ! Checks . isEmpty ( dto ) , "파일 정보가 존재하지 않습니다." ) ;
@ -70,8 +75,8 @@ public class CtgyFileMgtController {
return RestResponse . of ( service . saveFiles ( minInfoBoard680 , dto . getFiles ( ) ) ) ;
return RestResponse . of ( service . saveFiles ( minInfoBoard680 , dto . getFiles ( ) ) ) ;
}
}
@Operation ( summary = "공지사항 삭제" , description = "공지사항 삭제" )
@Operation ( summary = "공지사항 삭제" , description = "공지사항 삭제" )
@PostMapping ( value = "/pboard/{inCode}" )
@PostMapping ( value = "/pboard/{inCode}" )
public ResponseEntity < ? extends IRestResponse > removePublicBoardFile ( @PathVariable @Nonnull final Long inCode ) {
public ResponseEntity < ? extends IRestResponse > removePublicBoardFile ( @PathVariable @Nonnull final Long inCode ) {
AssertUtils . isTrue ( ! Checks . isEmpty ( inCode ) , "공지 사항이 선택되지 않았습니다." ) ;
AssertUtils . isTrue ( ! Checks . isEmpty ( inCode ) , "공지 사항이 선택되지 않았습니다." ) ;
service . removePublicBoardFile ( inCode ) ;
service . removePublicBoardFile ( inCode ) ;
@ -80,29 +85,90 @@ public class CtgyFileMgtController {
}
}
@GetMapping ( "/download/{inCode}" )
@GetMapping ( "/download/{inCode}" )
public void download ( @PathVariable Long inCode , HttpServletResponse response ) throws IOException {
public void download 2 ( @PathVariable Long inCode , HttpServletResponse response ) {
MinInfoBoard680 entity = service . findFiles ( inCode ) ;
MinInfoBoard680 entity = service . findFiles ( inCode ) ;
String absFile = "" ;
String absFile = "" ;
if ( Arrays . asList ( env . getActiveProfiles ( ) ) . contains ( "prod" ) )
if ( Arrays . asList ( env . getActiveProfiles ( ) ) . contains ( "prod" ) )
absFile = entity . getInFileurl ( ) + File . separator + entity . getInFilename ( ) ;
absFile = entity . getInFileurl ( ) + File . separator + entity . getInFilename ( ) ;
else
else
absFile = rootPath + entity . getInFileurl ( ) . split ( serviceUrl ) [ 1 ] + File . separator + entity . getInFilename ( ) ;
absFile = rootPath + entity . getInFileurl ( ) . split ( serviceUrl ) [ 1 ] + File . separator + entity . getInFilename ( ) ;
Path path = Paths . get ( absFile ) ;
String contentType = null ;
try {
contentType = Files . probeContentType ( path ) ;
} catch ( IOException e ) {
throw new CustomBaseException ( ErrorCode . FILE_NOT_FOUND ) ;
}
File file = new File ( absFile ) ;
byte [ ] fileByte = new byte [ 0 ] ;
try {
fileByte = FileUtils . readFileToByteArray ( file ) ;
} catch ( IOException e ) {
throw new CustomBaseException ( ErrorCode . FILE_NOT_FOUND ) ;
}
response . setContentType ( contentType ) ;
response . setHeader ( HttpHeaders . CONTENT_TYPE , contentType ) ;
response . setHeader ( HttpHeaders . ACCESS_CONTROL_ALLOW_CREDENTIALS , "true" ) ;
response . setHeader ( HttpHeaders . CONTENT_DISPOSITION , "attachment; filename=\"" + URLEncoder . encode ( entity . getInFilename ( ) , StandardCharsets . UTF_8 ) + "\";" ) ;
//response.setHeader(HttpHeaders.CONTENT_ENCODING, "binary");
response . setHeader ( HttpHeaders . CONTENT_LENGTH , String . valueOf ( entity . getInFilesize ( ) ) ) ;
try {
response . getOutputStream ( ) . write ( fileByte ) ;
response . getOutputStream ( ) . flush ( ) ;
response . getOutputStream ( ) . close ( ) ;
} catch ( IOException e ) {
throw new CustomBaseException ( ErrorCode . FILE_NOT_FOUND ) ;
}
}
@GetMapping ( "/download2/{inCode}" )
public ResponseEntity < Resource > download ( @PathVariable Long inCode , HttpServletResponse response ) {
byte [ ] fileByte = FileUtils . readFileToByteArray ( new File ( absFile ) ) ;
MinInfoBoard680 entity = service . findFiles ( inCode ) ;
String absFile = "" ;
response . setContentType ( "application/octet-stream" ) ;
if ( Arrays . asList ( env . getActiveProfiles ( ) ) . contains ( "prod" ) )
//response.setHeader("Access-Control-Allow-Credentials", "true");
absFile = entity . getInFileurl ( ) + File . separator + entity . getInFilename ( ) ;
response . setHeader ( "Content-Disposition" , "attachment; filename=\"" + URLEncoder . encode ( entity . getInFilename ( ) , StandardCharsets . UTF_8 ) + "\";" ) ;
else
response . setHeader ( "Content-Transfer-Encoding" , "binary" ) ;
absFile = rootPath + entity . getInFileurl ( ) . split ( serviceUrl ) [ 1 ] + File . separator + entity . getInFilename ( ) ;
response . setHeader ( "Content-Length" , String . valueOf ( entity . getInFilesize ( ) ) ) ;
response . getOutputStream ( ) . write ( fileByte ) ;
Path path = Paths . get ( absFile ) ;
response . getOutputStream ( ) . flush ( ) ;
String contentType = null ;
response . getOutputStream ( ) . close ( ) ;
try {
contentType = Files . probeContentType ( path ) ;
} catch ( IOException e ) {
throw new CustomBaseException ( ErrorCode . FILE_NOT_FOUND ) ;
}
// File file = new File(absFile);
// try {
// byte[] fileByte = FileUtils.readFileToByteArray(file);
// } catch (IOException e) {
// throw new CustomBaseException(ErrorCode.FILE_NOT_FOUND);
// }
HttpHeaders headers = new HttpHeaders ( ) ;
headers . add ( HttpHeaders . CONTENT_TYPE , contentType ) ;
headers . setContentDisposition ( ContentDisposition . builder ( "attachment" )
. filename ( entity . getInFilename ( ) , StandardCharsets . UTF_8 )
//.filename(URLEncoder.encode(entity.getInFilename(), StandardCharsets.UTF_8))
. build ( ) ) ;
headers . add ( HttpHeaders . CONTENT_LENGTH , String . valueOf ( entity . getInFilesize ( ) ) ) ;
Resource resource = null ;
try {
resource = new InputStreamResource ( Files . newInputStream ( path ) ) ;
} catch ( IOException e ) {
throw new CustomBaseException ( ErrorCode . FILE_NOT_FOUND ) ;
}
return new ResponseEntity < > ( resource , headers , HttpStatus . OK ) ;
}
}
}
}