Commit 906b219e authored by Liang Ding's avatar Liang Ding

新增 Markdown zip 导入方式 #128

parent 7791dba1
......@@ -232,16 +232,14 @@ public class AdminConsole {
* @param context the specified context
*/
public void importMarkdownZip(final RequestContext context) {
context.renderJSON();
final Request request = context.getRequest();
final FileUpload file = request.getFileUpload("file");
final String fileName = file.getFilename();
String suffix = StringUtils.substringAfterLast(fileName, ".");
if (StringUtils.isBlank(suffix)) {
// TODO
return;
}
if (!StringUtils.equalsIgnoreCase(suffix, "zip")) {
// TODO
context.renderMsg(langPropsService.get("allowZipOnlyLabel"));
return;
}
......@@ -255,12 +253,22 @@ public class AdminConsole {
final String unzipPath = tmpDir + File.separator + "solo-import-" + date;
final File unzipDir = new File(unzipPath);
ZipUtil.unzip(zipFile, unzipDir);
importService.importMarkdownDir(unzipDir);
final JSONObject result = importService.importMarkdownDir(unzipDir);
final int succCount = result.optInt("succCount");
final int failCount = result.optInt("failCount");
FileUtils.deleteQuietly(zipFile);
FileUtils.deleteQuietly(unzipDir);
context.renderJSON(true);
String msg = langPropsService.get("importSuccLabel");
msg = msg.replace("${succCount}", succCount + "");
if (0 < failCount) {
msg = langPropsService.get("importFailLabel");
msg = msg.replace("${failCount}", failCount + "");
}
context.renderMsg(msg);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Imports markdown file failed", e);
return;
context.renderMsg(langPropsService.get("importFailedSeeLogLabel"));
}
}
......
......@@ -87,13 +87,19 @@ public class ImportService {
* Imports markdown files under the specified markdown files dir.
*
* @param markdownsDir the specified markdown files dir
* @return <pre>
* {
* "failCount": int,
* "succCnt": int
* }
* </pre>
*/
public void importMarkdownDir(final File markdownsDir) {
public JSONObject importMarkdownDir(final File markdownsDir) {
LOGGER.debug("Import directory [" + markdownsDir.getPath() + "]");
final JSONObject admin = userQueryService.getAdmin();
if (null == admin) { // Not init yet
return;
return null;
}
final String adminId = admin.optString(Keys.OBJECT_ID);
......@@ -102,7 +108,7 @@ public class ImportService {
final Set<String> failSet = new TreeSet<>();
final Collection<File> mds = FileUtils.listFiles(markdownsDir, new String[]{"md"}, true);
if (mds.isEmpty()) {
return;
return null;
}
for (final File md : mds) {
......@@ -132,7 +138,7 @@ public class ImportService {
}
if (0 == succCnt && 0 == failCnt) {
return;
return null;
}
final StringBuilder logBuilder = new StringBuilder();
......@@ -147,6 +153,7 @@ public class ImportService {
logBuilder.append(" :p");
}
LOGGER.info(logBuilder.toString());
return new JSONObject().put("failCount", failCnt).put("succCount", succCnt);
}
private JSONObject parseArticle(final String fileName, String fileContent) {
......
......@@ -18,6 +18,11 @@
# Author: Dongxu Wang
#
importSuccLabel=After importing, a total of $ {succCount} articles have been successfully imported
importFailLabel=After importing, $ {failCount} articles failed to be imported, please check the log for details
allowZipOnlyLabel=Only supports Zip file
importFailedSeeLogLabel=Import failed, please check the log for details
uploadMarkdownZipLabel=Upload Markdown Zip
editorModeSVLabel=Split View
editorModeIRLabel=Instant Rendering
editorModeWYSIWYGLabel=WYSIWYG
......
......@@ -18,6 +18,11 @@
# Author: Dongxu Wang
#
importSuccLabel=\u5BFC\u5165\u5B8C\u6BD5\uFF0C\u4E00\u5171\u6210\u529F\u5BFC\u5165 ${succCount} \u7BC7\u6587\u7AE0
importFailLabel=\u5BFC\u5165\u5B8C\u6BD5\uFF0C\u5176\u4E2D ${failCount} \u7BC7\u6587\u7AE0\u5BFC\u5165\u5931\u8D25\uFF0C\u8BF7\u67E5\u770B\u65E5\u5FD7\u4E86\u89E3\u62A5\u9519\u8BE6\u60C5
allowZipOnlyLabel=\u4EC5\u652F\u6301 Zip \u6587\u4EF6
importFailedSeeLogLabel=\u5BFC\u5165\u5931\u8D25\uFF0C\u8BF7\u67E5\u770B\u65E5\u5FD7\u4E86\u89E3\u62A5\u9519\u8BE6\u60C5
uploadMarkdownZipLabel=\u4E0A\u4F20 Markdown Zip \u5305
editorModeSVLabel=\u5206\u5C4F\u9884\u89C8
editorModeIRLabel=\u5373\u65F6\u6E32\u67D3
editorModeWYSIWYGLabel=\u6240\u89C1\u5373\u6240\u5F97
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment