PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/jeecms/cms/manager/assist/impl/CmsResourceMngImpl.java

https://gitlab.com/MetadataDev/mcms
Java | 447 lines | 402 code | 34 blank | 11 comment | 63 complexity | e7fc4f9882d186a02f46915c15d674df MD5 | raw file
  1. package com.jeecms.cms.manager.assist.impl;
  2. import static com.jeecms.common.web.Constants.SPT;
  3. import static com.jeecms.common.web.Constants.UTF8;
  4. import static com.jeecms.core.web.util.FrontUtils.RES_EXP;
  5. import java.io.File;
  6. import java.io.FileFilter;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.FilenameFilter;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Enumeration;
  17. import java.util.List;
  18. import javax.servlet.http.HttpServletRequest;
  19. import org.apache.commons.io.FileUtils;
  20. import org.apache.commons.io.FilenameUtils;
  21. import org.apache.tools.zip.ZipEntry;
  22. import org.apache.tools.zip.ZipFile;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.web.multipart.MultipartFile;
  28. import com.jeecms.cms.entity.assist.CmsFile;
  29. import com.jeecms.cms.manager.assist.CmsFileMng;
  30. import com.jeecms.cms.manager.assist.CmsResourceMng;
  31. import com.jeecms.cms.staticpage.DistributionThread;
  32. import com.jeecms.common.file.FileWrap;
  33. import com.jeecms.common.file.FileWrap.FileComparator;
  34. import com.jeecms.common.util.Zipper.FileEntry;
  35. import com.jeecms.common.web.springmvc.RealPathResolver;
  36. import com.jeecms.core.entity.CmsSite;
  37. import com.jeecms.core.entity.Ftp;
  38. import com.jeecms.core.manager.FtpMng;
  39. import com.jeecms.core.web.util.CmsUtils;
  40. import com.jeecms.core.web.util.FrontUtils;
  41. @Service
  42. public class CmsResourceMngImpl implements CmsResourceMng {
  43. private static final Logger log = LoggerFactory
  44. .getLogger(CmsResourceMngImpl.class);
  45. public List<FileWrap> listFile(String path, boolean dirAndEditable) {
  46. File parent = new File(realPathResolver.get(path));
  47. if (parent.exists()) {
  48. File[] files;
  49. if (dirAndEditable) {
  50. files = parent.listFiles(filter);
  51. } else {
  52. files = parent.listFiles();
  53. }
  54. Arrays.sort(files, new FileComparator());
  55. List<FileWrap> list = new ArrayList<FileWrap>(files.length);
  56. for (File f : files) {
  57. list.add(new FileWrap(f, realPathResolver.get("")));
  58. }
  59. return list;
  60. } else {
  61. return new ArrayList<FileWrap>(0);
  62. }
  63. }
  64. public List<FileWrap> listFileValid(String path, boolean dirAndEditable) {
  65. File parent = new File(realPathResolver.get(path));
  66. if (parent.exists()) {
  67. File[] files;
  68. if (dirAndEditable) {
  69. files = parent.listFiles(filter);
  70. } else {
  71. files = parent.listFiles();
  72. }
  73. Arrays.sort(files, new FileComparator());
  74. List<FileWrap> list = new ArrayList<FileWrap>(files.length);
  75. CmsFile file;
  76. for (File f : files) {
  77. file=fileMng.findByPath(f.getName());
  78. if(file!=null){
  79. list.add(new FileWrap(f, realPathResolver.get(""),null,file.getFileIsvalid()));
  80. }else{
  81. list.add(new FileWrap(f, realPathResolver.get(""),null,false));
  82. }
  83. }
  84. return list;
  85. } else {
  86. return new ArrayList<FileWrap>(0);
  87. }
  88. }
  89. public List<FileWrap> queryFiles(String path, Boolean valid){
  90. File parent = new File(realPathResolver.get(path));
  91. if (parent.exists()) {
  92. File[] files;
  93. files = parent.listFiles();
  94. Arrays.sort(files, new FileComparator());
  95. List<FileWrap> list = new ArrayList<FileWrap>(files.length);
  96. CmsFile file;
  97. for (File f : files) {
  98. file=fileMng.findByPath(f.getName());
  99. if(valid!=null){
  100. if(file!=null){
  101. if(file.getFileIsvalid().equals(valid)){
  102. list.add(new FileWrap(f, realPathResolver.get(""),null,valid));
  103. }
  104. }else{
  105. if(valid.equals(false)){
  106. list.add(new FileWrap(f, realPathResolver.get(""),null,false));
  107. }
  108. }
  109. }else{
  110. if(file!=null){
  111. list.add(new FileWrap(f, realPathResolver.get(""),null,file.getFileIsvalid()));
  112. }else{
  113. list.add(new FileWrap(f, realPathResolver.get(""),null,false));
  114. }
  115. }
  116. }
  117. return list;
  118. } else {
  119. return new ArrayList<FileWrap>(0);
  120. }
  121. }
  122. public boolean createDir(String path, String dirName) {
  123. File parent = new File(realPathResolver.get(path));
  124. parent.mkdirs();
  125. File dir = new File(parent, dirName);
  126. return dir.mkdir();
  127. }
  128. public void saveFile(HttpServletRequest request,String path, MultipartFile file)
  129. throws IllegalStateException, IOException {
  130. File dest = new File(realPathResolver.get(path), file
  131. .getOriginalFilename());
  132. file.transferTo(dest);
  133. CmsSite site=CmsUtils.getSite(request);
  134. if(site.getResouceSync()){
  135. distributeFile(site, dest, path+"/"+dest.getName());
  136. }
  137. }
  138. public void createFile(HttpServletRequest request,String path, String filename, String data)
  139. throws IOException {
  140. File parent = new File(realPathResolver.get(path));
  141. parent.mkdirs();
  142. File file = new File(parent, filename);
  143. FileUtils.writeStringToFile(file, data, UTF8);
  144. CmsSite site=CmsUtils.getSite(request);
  145. if(site.getResouceSync()){
  146. distributeFile(site, file, path+"/"+file.getName());
  147. }
  148. }
  149. public String readFile(String name) throws IOException {
  150. File file = new File(realPathResolver.get(name));
  151. return FileUtils.readFileToString(file, UTF8);
  152. }
  153. public void updateFile(String name, String data) throws IOException {
  154. File file = new File(realPathResolver.get(name));
  155. FileUtils.writeStringToFile(file, data, UTF8);
  156. }
  157. public int delete(String[] names) {
  158. int count = 0;
  159. File f;
  160. for (String name : names) {
  161. f = new File(realPathResolver.get(name));
  162. if (FileUtils.deleteQuietly(f)) {
  163. count++;
  164. }
  165. }
  166. return count;
  167. }
  168. public void rename(String origName, String destName) {
  169. File orig = new File(realPathResolver.get(origName));
  170. File dest = new File(realPathResolver.get(destName));
  171. orig.renameTo(dest);
  172. }
  173. public void copyTplAndRes(CmsSite from, CmsSite to) throws IOException {
  174. String fromSol = from.getTplSolution();
  175. String toSol = to.getTplSolution();
  176. File tplFrom = new File(realPathResolver.get(from.getTplPath()),
  177. fromSol);
  178. File tplTo = new File(realPathResolver.get(to.getTplPath()), toSol);
  179. FileUtils.copyDirectory(tplFrom, tplTo);
  180. File resFrom = new File(realPathResolver.get(from.getResPath()),
  181. fromSol);
  182. if (resFrom.exists()) {
  183. File resTo = new File(realPathResolver.get(to.getResPath()), toSol);
  184. FileUtils.copyDirectory(resFrom, resTo);
  185. }
  186. }
  187. public void delTplAndRes(CmsSite site) throws IOException {
  188. File tpl = new File(realPathResolver.get(site.getTplPath()));
  189. File res = new File(realPathResolver.get(site.getResPath()));
  190. FileUtils.deleteDirectory(tpl);
  191. FileUtils.deleteDirectory(res);
  192. }
  193. public String[] getSolutions(String path) {
  194. File tpl = new File(realPathResolver.get(path));
  195. return tpl.list(new FilenameFilter() {
  196. public boolean accept(File dir, String name) {
  197. return dir.isDirectory();
  198. }
  199. });
  200. }
  201. public List<FileEntry> export(CmsSite site, String solution) {
  202. List<FileEntry> fileEntrys = new ArrayList<FileEntry>();
  203. File tpl = new File(realPathResolver.get(site.getTplPath()), solution);
  204. fileEntrys.add(new FileEntry("", "", tpl));
  205. File res = new File(realPathResolver.get(site.getResPath()), solution);
  206. if (res.exists()) {
  207. for (File r : res.listFiles()) {
  208. fileEntrys.add(new FileEntry(FrontUtils.RES_EXP, r));
  209. }
  210. }
  211. return fileEntrys;
  212. }
  213. @SuppressWarnings("unchecked")
  214. public void imoport(File file, CmsSite site) throws IOException {
  215. String resRoot = site.getResPath();
  216. String tplRoot = site.getTplPath();
  217. // 用默认编码或UTF-8编码解压会乱码!windows7的原因吗?
  218. ZipFile zip = new ZipFile(file, "GBK");
  219. ZipEntry entry;
  220. String name;
  221. String filename;
  222. File outFile;
  223. File pfile;
  224. byte[] buf = new byte[1024];
  225. int len;
  226. InputStream is = null;
  227. OutputStream os = null;
  228. String solution = null;
  229. Enumeration<ZipEntry> en = zip.getEntries();
  230. while (en.hasMoreElements()) {
  231. name = en.nextElement().getName();
  232. if (!name.startsWith(RES_EXP)) {
  233. solution = name.substring(0, name.indexOf("/"));
  234. break;
  235. }
  236. }
  237. if (solution == null) {
  238. return;
  239. }
  240. en = zip.getEntries();
  241. while (en.hasMoreElements()) {
  242. entry = en.nextElement();
  243. if (!entry.isDirectory()) {
  244. name = entry.getName();
  245. log.debug("unzip file:{}", name);
  246. // 模板还是资源
  247. if (name.startsWith(RES_EXP)) {
  248. filename = resRoot + "/" + solution
  249. + name.substring(RES_EXP.length());
  250. } else {
  251. filename = tplRoot + SPT + name;
  252. }
  253. log.debug("解压地址:{}", filename);
  254. outFile = new File(realPathResolver.get(filename));
  255. pfile = outFile.getParentFile();
  256. if (!pfile.exists()) {
  257. pfile.mkdirs();
  258. }
  259. try {
  260. is = zip.getInputStream(entry);
  261. os = new FileOutputStream(outFile);
  262. while ((len = is.read(buf)) != -1) {
  263. os.write(buf, 0, len);
  264. }
  265. } finally {
  266. if (is != null) {
  267. is.close();
  268. is = null;
  269. }
  270. if (os != null) {
  271. os.close();
  272. os = null;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. public void unZipFile(File file) throws IOException {
  279. // 用默认编码或UTF-8编码解压会乱码!windows7的原因吗?
  280. //解压之前要坚持是否冲突
  281. ZipFile zip = new ZipFile(file, "GBK");
  282. ZipEntry entry;
  283. String name;
  284. String filename;
  285. File outFile;
  286. File pfile;
  287. byte[] buf = new byte[1024];
  288. int len;
  289. InputStream is = null;
  290. OutputStream os = null;
  291. Enumeration<ZipEntry> en = zip.getEntries();
  292. while (en.hasMoreElements()) {
  293. entry = en.nextElement();
  294. name = entry.getName();
  295. if (!entry.isDirectory()) {
  296. name = entry.getName();
  297. filename = name;
  298. outFile = new File(realPathResolver.get(filename));
  299. if(outFile.exists()){
  300. break;
  301. }
  302. pfile = outFile.getParentFile();
  303. if (!pfile.exists()) {
  304. // pfile.mkdirs();
  305. createFolder(outFile);
  306. }
  307. try {
  308. is = zip.getInputStream(entry);
  309. os = new FileOutputStream(outFile);
  310. while ((len = is.read(buf)) != -1) {
  311. os.write(buf, 0, len);
  312. }
  313. } finally {
  314. if (is != null) {
  315. is.close();
  316. is = null;
  317. }
  318. if (os != null) {
  319. os.close();
  320. os = null;
  321. }
  322. }
  323. }
  324. }
  325. zip.close();
  326. }
  327. public void deleteZipFile(File file) throws IOException {
  328. //根据压缩包删除解压后的文件
  329. // 用默认编码或UTF-8编码解压会乱码!windows7的原因吗
  330. ZipFile zip = new ZipFile(file, "GBK");
  331. ZipEntry entry;
  332. String name;
  333. String filename;
  334. File directory;
  335. //删除文件
  336. Enumeration<ZipEntry> en = zip.getEntries();
  337. while (en.hasMoreElements()) {
  338. entry = en.nextElement();
  339. if (!entry.isDirectory()) {
  340. name = entry.getName();
  341. filename = name;
  342. directory = new File(realPathResolver.get(filename));
  343. if(directory.exists()){
  344. directory.delete();
  345. }
  346. }
  347. }
  348. //删除空文件夹
  349. en= zip.getEntries();
  350. while (en.hasMoreElements()) {
  351. entry = en.nextElement();
  352. if (entry.isDirectory()) {
  353. name = entry.getName();
  354. filename = name;
  355. directory = new File(realPathResolver.get(filename));
  356. if(!directoryHasFile(directory)){
  357. directory.delete();
  358. }
  359. }
  360. }
  361. zip.close();
  362. }
  363. private void distributeFile(CmsSite site,File f,String filename) throws FileNotFoundException{
  364. if(site.getSyncPageFtp()!=null){
  365. Ftp syncPageFtp=ftpMng.findById(site.getSyncPageFtp().getId());
  366. Thread thread = new Thread(new DistributionThread(syncPageFtp,filename, new FileInputStream(f)));
  367. thread.start();
  368. }
  369. }
  370. private void createFolder(File f){
  371. File parent=f.getParentFile();
  372. if(!parent.exists()){
  373. parent.mkdirs();
  374. createFolder(parent);
  375. }
  376. }
  377. //文件夹判断是否有文件
  378. private boolean directoryHasFile(File directory){
  379. File[] files = directory.listFiles();
  380. if(files!=null&&files.length>0){
  381. for(File f:files){
  382. if(directoryHasFile(f)){
  383. return true;
  384. }else{
  385. continue;
  386. }
  387. }
  388. return false;
  389. }else{
  390. return false;
  391. }
  392. }
  393. // 文件夹和可编辑文件则显示
  394. private FileFilter filter = new FileFilter() {
  395. public boolean accept(File file) {
  396. return file.isDirectory()
  397. || FileWrap.editableExt(FilenameUtils.getExtension(file
  398. .getName()));
  399. }
  400. };
  401. private RealPathResolver realPathResolver;
  402. private CmsFileMng fileMng;
  403. @Autowired
  404. private FtpMng ftpMng;
  405. @Autowired
  406. public void setRealPathResolver(RealPathResolver realPathResolver) {
  407. this.realPathResolver = realPathResolver;
  408. }
  409. @Autowired
  410. public void setFileMng(CmsFileMng fileMng) {
  411. this.fileMng = fileMng;
  412. }
  413. }