파일 다운로드 수정
parent
93ea2ffdf9
commit
1a01c2887f
@ -0,0 +1,164 @@
|
||||
package cokr.xit.interfaces.saeol.service.bean;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URLConnection;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import cokr.xit.base.file.FileInfo;
|
||||
import cokr.xit.foundation.web.WebClient;
|
||||
import cokr.xit.interfaces.saeol.SOHNN00011;
|
||||
|
||||
public class EPeople2 extends EPeople {
|
||||
@Override
|
||||
public List<FileInfo> download(SOHNN00011.Resp00011 resp) {
|
||||
SOHNN00011.Item item = resp.getResult();
|
||||
String phoneNo = ifEmpty(item.getTelno_m(), item::getHpno_m);
|
||||
|
||||
return resp.getAllFileinfo().stream()
|
||||
.map(file -> {
|
||||
FileInfo fileInfo = downloadFrom(file.getFile_path(), phoneNo);
|
||||
if (fileInfo != null) {
|
||||
String file_type = file.getFile_type();
|
||||
fileInfo.setSubType(file_type);
|
||||
fileInfo.setSubCode("M".equals(file_type) ? file.getFile_id_m() : file.getFile_id_p());
|
||||
}
|
||||
return fileInfo;
|
||||
})
|
||||
.filter(fileInfo -> fileInfo != null)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public FileInfo downloadFrom(String filepath, String phoneNo) {
|
||||
try {
|
||||
Info info = getInfo(filepath);
|
||||
|
||||
addCookie("fileDownloadToken", "TRUE");
|
||||
addCookie("XSRF-TOKEN", info.csrf);
|
||||
|
||||
// compare(info, phoneNo);
|
||||
return download(info);
|
||||
} catch (Exception e) {
|
||||
throw runtimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Info getInfo(String url) throws Exception {
|
||||
HttpResponse<String> hresp = hclient().get(req ->
|
||||
req.uri(url.replace("http://www.epeople.go.kr", HOST))
|
||||
.noCache()
|
||||
);
|
||||
Info info = Info.parse(hresp.body());
|
||||
log().debug("EPeople.Info:\n{}", info.toString().replace(", ", "\n"));
|
||||
return info;
|
||||
}
|
||||
|
||||
private void compare(Info info, String phoneNo) throws Exception {
|
||||
HttpResponse<String> hresp = hclient().post(req ->
|
||||
req.uri(HOST + "/paid/syscmmn/fileDwldCompare.npaid")
|
||||
.noCache()
|
||||
.contentType(WebClient.Request.ContentType.FORM)
|
||||
.header("Accept", "application/json, text/javascript, */*; q=0.01")
|
||||
.header("X-Requested-With", "XMLHttpRequest")
|
||||
.header("AJAX", "true")
|
||||
.header("X-CSRF-TOKEN", info.csrf)
|
||||
|
||||
.data(info.asParams())
|
||||
.data("encryptValue", phoneNo)
|
||||
);
|
||||
log().debug(hresp.body());
|
||||
}
|
||||
|
||||
private FileInfo download(Info info) throws Exception {
|
||||
HttpResponse<InputStream> hresp = hclient().post(req ->
|
||||
req.uri(HOST + "/paid/syscmmn/fileDwld.npaid")
|
||||
.noCache()
|
||||
.contentType(WebClient.Request.ContentType.FORM)
|
||||
.header("X-CSRF-TOKEN", info.csrf)
|
||||
|
||||
.data(info.asParams())
|
||||
|
||||
.download(true)
|
||||
);
|
||||
InputStream downloaded = hresp.body();
|
||||
if (WebClient.Request.SUCCESS != hresp.statusCode()) {
|
||||
if (info.retry()) {
|
||||
log().debug("Retrying to download the attachment");
|
||||
return download(info);
|
||||
}
|
||||
|
||||
log().debug(hclient().toString(downloaded).trim());
|
||||
return null;
|
||||
}
|
||||
|
||||
String filename = info.filename;
|
||||
Optional<String> header = hresp.headers().firstValue("content-length");
|
||||
long length = header.isPresent() ? Long.parseLong(header.get()) : 0;
|
||||
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
fileInfo.setName(filename);
|
||||
fileInfo.setInputStream(downloaded);
|
||||
|
||||
String mimeType = URLConnection.guessContentTypeFromName(filename);
|
||||
if (isEmpty(mimeType))
|
||||
mimeType = URLConnection.guessContentTypeFromStream(downloaded);
|
||||
fileInfo.setMimeType(mimeType);
|
||||
|
||||
if (length > 0)
|
||||
fileInfo.setSize(length);
|
||||
|
||||
log().debug("{} downloaded", filename);
|
||||
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
public static class Info {
|
||||
private String
|
||||
csrf,
|
||||
atchFileGrpId,
|
||||
atchFileId,
|
||||
oldAtchFileId,
|
||||
doc_id,
|
||||
filename;
|
||||
private int attempt = 1;
|
||||
|
||||
public static Info parse(String str) {
|
||||
Info info = new Info();
|
||||
|
||||
info.csrf = parseValue(str, "\"_csrf\" content=\"", "\"").trim();
|
||||
info.atchFileGrpId = parseValue(str, "atchFileGrpId\" value=\"", "\"").trim();
|
||||
info.atchFileId = parseValue(str, "atchFileId\" value=\"", "\"").trim();
|
||||
info.oldAtchFileId = parseValue(str, "oldAtchFileId\" value=\"", "\"").trim();
|
||||
info.doc_id = parseValue(str, "doc_id\" value=\"", "\"").trim();
|
||||
info.filename = parseValue(str, "filename\" value=\"", "\"").trim();
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
public Map<String, String> asParams() {
|
||||
return Map.of(
|
||||
"atchFileGrpId", atchFileGrpId,
|
||||
"atchFileId", atchFileId,
|
||||
"oldAtchFileId", "oldAtchFileId",
|
||||
"doc_id", doc_id,
|
||||
"filename", filename,
|
||||
"_csrf", csrf
|
||||
);
|
||||
}
|
||||
|
||||
public boolean retry() {
|
||||
++attempt;
|
||||
return attempt < 6;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format(
|
||||
"csrf: %s, atchFileGrpId: %s, atchFileId: %s, oldAtchFileId: %s, doc_id: %s, filename: %s",
|
||||
csrf, atchFileGrpId, atchFileId, oldAtchFileId, doc_id, filename
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
{ "enabled": true,
|
||||
"cryptoDefs": [
|
||||
{ "name": "xit-aria",
|
||||
"class": "cokr.xit.base.crypto.bean.XitAria",
|
||||
"settings": {
|
||||
"version": 1,
|
||||
"charset": "EUC-KR",
|
||||
"key": "3AsrGqpYYnkHIu3D",
|
||||
"keySize": 128
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue