📄 templateact.java
字号:
package com.jeecms.core.action;
import static com.jeecms.core.Constants.FILE_SPT;
import static com.jeecms.core.Constants.SPT;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.jeecms.core.JeeCoreAction;
import com.jeecms.core.util.FileWrap;
import com.jeecms.core.util.UploadRule;
import com.ponyjava.common.util.ComUtils;
import com.ponyjava.common.util.Zipper;
import com.ponyjava.common.util.Zipper.FileEntry;
@SuppressWarnings("serial")
@Scope("prototype")
@Controller("core.templateAct")
public class TemplateAct extends JeeCoreAction {
private static final Logger log = LoggerFactory
.getLogger(TemplateAct.class);
public String doResMain() {
return MAIN;
}
@SuppressWarnings("unchecked")
public String doLeft() {
String path = getWeb().getTplRootReal(contextPvd.getAppRoot())
.toString().replace(SPT, FILE_SPT);
File tplFile = new File(path);
treeRoot = new FileWrap(tplFile, path, new FileFilter() {
@Override
public boolean accept(File f) {
if (f.getName().startsWith(".") || f.getName().startsWith("$")) {
return false;
} else {
return true;
}
}
});
return LEFT;
}
public String doResLeft() {
String resPath = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.toString());
treeRoot = new FileWrap(new File(resPath), resPath, new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory() || f.getName().endsWith(".js")
|| f.getName().endsWith(".css")
|| f.getName().endsWith(".html")
|| f.getName().endsWith(".txt")) {
return true;
} else {
return false;
}
}
});
return LEFT;
}
public String doRight() {
return RIGHT;
}
public String doList() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).toString());
File dir = new File(path);
subDir = new FileWrap(dir).getChild();
return LIST;
}
public String doResList() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).toString());
File dir = new File(path);
subDir = new FileWrap(dir).getChild();
return LIST;
}
public String doAdd() {
// 设置上传规则
UploadRule rule = new UploadRule(getWeb().getResRoot(), "",
false);
contextPvd.setSessionAttr(UploadRule.KEY, rule);
return ADD;
}
public String doResAdd() {
return ADD;
}
private void saveFile(String path) {
File tpl = new File(path + relPath + FILE_SPT + tplName);
try {
FileUtils.writeStringToFile(tpl, tplContent);
} catch (IOException e) {
log.error("写入模板失败!", e);
addActionError("写入模板失败!");
}
}
public String doSave() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
.toString());
saveFile(path);
return doList();
}
public String doResSave() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.toString());
saveFile(path);
return doResList();
}
private void editFile(String path) {
File tpl = new File(path);
tplName = tpl.getName();
parentPath = relPath.substring(0, relPath.lastIndexOf(FILE_SPT));
try {
tplContent = FileUtils.readFileToString(tpl);
} catch (IOException e) {
log.error("读取模板文件失败", e);
addActionError("读取模板文件失败!");
}
}
public String doEdit() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).toString());
editFile(path);
// 设置上传规则
UploadRule rule = new UploadRule(getWeb().getResRoot(), "", false);
contextPvd.setSessionAttr(UploadRule.KEY, rule);
return EDIT;
}
public String doResEdit() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).toString());
editFile(path);
return EDIT;
}
private void updateFile(String path) {
File tpl = new File(path);
File newFile = tpl;
if (!tpl.getName().equals(tplName)) {
newFile = new File(tpl.getParent() + FILE_SPT + tplName);
tpl.renameTo(newFile);
}
try {
FileUtils.writeStringToFile(newFile, tplContent);
} catch (IOException e) {
log.error("写文件失败", e);
jsonRoot.put("success", false);
jsonRoot.put("msg", "写文件失败!");
}
jsonRoot.put("success", true);
jsonRoot.put("msg", "保存成功");
}
public String doUpdate() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).toString());
updateFile(path);
return SUCCESS;
}
public String doResUpdate() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).toString());
updateFile(path);
return SUCCESS;
}
private void renameFile(String path) {
File tpl = new File(path);
if (!origName.equals(tplName)) {
boolean b = tpl.renameTo(new File(tpl.getParent() + FILE_SPT
+ tplName));
if (!b) {
jsonRoot.put("success", false);
}
}
jsonRoot.put("success", true);
}
public String doRename() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).append(SPT).append(origName).toString());
renameFile(path);
return SUCCESS;
}
public String doResRename() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).append(SPT).append(origName).toString());
renameFile(path);
return SUCCESS;
}
private void deleteFile(String path) {
File tpl = new File(path);
if (FileUtils.deleteQuietly(tpl)) {
addActionError("删除成功!");
} else {
addActionError("删除失败!");
}
}
public String doDelete() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).append(SPT).append(tplName).toString());
deleteFile(path);
return doList();
}
public String doResDelete() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).append(SPT).append(tplName).toString());
deleteFile(path);
return doResList();
}
public String doCreateDir() {
String path = contextPvd.getAppRealPath(getWeb().getTplRoot().append(
relPath).append(SPT).append(dirName).toString());
File dir = new File(path);
dir.mkdir();
return doList();
}
public String doResCreateDir() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).append(SPT).append(dirName).toString());
File dir = new File(path);
dir.mkdir();
return doResList();
}
public String doResUpload() {
return "upload";
}
public String doResUploadSubmit() {
String path = contextPvd.getAppRealPath(getWeb().getResRootBuf()
.append(relPath).append(SPT).toString());
if (resFile != null) {
try {
for (int i = 0; i < resFile.length; i++) {
FileUtils.copyFile(resFile[i], new File(path + FILE_SPT
+ resFileFileName[i]));
}
addActionMessage("上传成功!");
} catch (IOException e) {
addActionError("上传失败!" + e.getMessage());
}
}
return doResList();
}
public String doSolutionEdit() {
// 数据库和目录中都有的模板套件才显示
// 数据库中模板方案
solMap = getWeb().getSolutions();
// 模板目录中模板方案
dirMap = new LinkedHashMap<String, String[]>();
String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
.toString());
File[] tplFiles = new File(path).listFiles(ComUtils.DIR_FILE_FILTER);
for (File f : tplFiles) {
if (solMap.containsKey(f.getName())) {
dirMap.put(f.getName(), f.list(ComUtils.DIR_FILE_FILTER));
}
}
return "solution";
}
public String doSolutionUpdate() {
Map<String, String> smap = getWeb().getSolutions();
for (String key : smap.keySet()) {
String s = solMap.get(key);
if (s != null) {
smap.put(key, s);
}
}
addActionMessage("操作成功");
return doSolutionEdit();
}
public String doExport() {
solSet = new LinkedHashSet<String>();
solMap = getWeb().getSolutions();
String path = contextPvd.getAppRealPath(getWeb().getTplRoot()
.toString());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -