001: package com.openedit.modules.admin.filemanager;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.ArrayList;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.openedit.repository.ContentItem;
013: import org.openedit.repository.InputStreamItem;
014:
015: import com.openedit.OpenEditException;
016: import com.openedit.page.Page;
017: import com.openedit.page.manage.PageManager;
018: import com.openedit.users.User;
019: import com.openedit.util.PathUtilities;
020: import com.openedit.util.ZipUtil;
021:
022: public class UploadRequest {
023: private static final Log log = LogFactory
024: .getLog(UploadRequest.class);
025: protected List fieldUploadItems;
026: protected Map fieldProperties;
027: protected PageManager fieldPageManager;
028: protected File fieldRoot;
029: protected ZipUtil fieldZipUtil;
030:
031: public void addUploadItem(FileUploadItem inItem) {
032: getUploadItems().add(inItem);
033: }
034:
035: public List getUploadItems() {
036: if (fieldUploadItems == null) {
037: fieldUploadItems = new ArrayList();
038: }
039: return fieldUploadItems;
040: }
041:
042: public void setUploadItems(List inUploadItems) {
043: fieldUploadItems = inUploadItems;
044: }
045:
046: public Map getProperties() {
047: return fieldProperties;
048: }
049:
050: public void setProperties(Map inProperties) {
051: fieldProperties = inProperties;
052: }
053:
054: public String get(String inString) {
055: return (String) getProperties().get(inString);
056: }
057:
058: public String getPathFor(String home, FileUploadItem inItem)
059: throws OpenEditException {
060: String path = inItem.get("path");
061: if (path == null) {
062: path = get("path");
063: }
064:
065: if (path == null) {
066: log.error("No path specified in multipart form");
067: throw new OpenEditException(
068: "No path passed in with the upload");
069: }
070: if (home != null && home.length() > 0 && path.startsWith(home)) {
071: path = path.substring(home.length());
072: }
073:
074: String finalpath = null;
075: //TODO: Check to see if it is already a directory or ends with /
076: String name = inItem.getName();
077: name = name = name.replace('\\', '/');
078: name = PathUtilities.extractFileName(name);
079: name = name.replaceAll(" ", "");
080:
081: if (path.endsWith("/")) {
082: finalpath = path + name;
083: } else {
084: Page page = getPageManager().getPage(path);
085: if (page.isFolder()) //upload to an exising folder
086: {
087: finalpath = path + "/" + name;
088: } else {
089: finalpath = path;
090: }
091: }
092: return finalpath;
093: }
094:
095: public PageManager getPageManager() {
096: return fieldPageManager;
097: }
098:
099: public void setPageManager(PageManager inPageManager) {
100: fieldPageManager = inPageManager;
101: }
102:
103: public void saveFile(FileUploadItem inItem, String inHome,
104: User inUser) throws OpenEditException {
105: /**
106: * inPath is full filename
107: * @param inProps
108: */
109: // public Page saveFile(Map inProps, String inPath, WebPageRequest inContext) throws OpenEditException
110: //From now on we are going to assume people always upload to a directory
111: //now put the file there
112: String inPath = getPathFor(inHome, inItem);
113: if (inPath.indexOf("..") > -1) {
114: throw new OpenEditException("Illegal path name");
115: }
116: final String path = PathUtilities.resolveRelativePath(inPath,
117: "/");
118: saveFileAs(inItem, path, inUser);
119: }
120:
121: private void saveFileAs(FileUploadItem inItem, final String path,
122: User inUser) throws OpenEditException {
123: Page page = getPageManager().getPage(path);
124: InputStreamItem revision = new InputStreamItem();
125: // final Date lastModified = new Date();
126: if (inUser == null) {
127: throw new IllegalArgumentException("No user logged in");
128: }
129: revision.setAuthor(inUser.getUserName());
130: revision.setType(ContentItem.TYPE_ADDED);
131: revision.setMessage("Uploaded file");
132: revision.setPath(path);
133: try {
134: revision.setInputStream(inItem.getFileItem()
135: .getInputStream());
136: } catch (IOException ex) {
137: throw new OpenEditException(ex);
138: }
139: page.setContentItem(revision);
140: getPageManager().putPage(page);
141: inItem.setSavedPage(page);
142: }
143:
144: public void unzipFiles(boolean inForceUnzip)
145: throws OpenEditException {
146: for (Iterator iterator = getUploadItems().iterator(); iterator
147: .hasNext();) {
148: FileUploadItem item = (FileUploadItem) iterator.next();
149: Page page = item.getSavedPage();
150: if (page.getPath().toLowerCase().endsWith(".zip")) {
151: String ok = (String) item.get("unzip");
152: if (inForceUnzip || "checked".equals(ok)
153: || Boolean.parseBoolean(ok)) {
154: log.info("Unzipping " + page.getPath());
155: File in = new File(getRoot(), page.getContentItem()
156: .getPath());
157: try {
158: getZipUtil().unzip(in, in.getParentFile());
159: } catch (Exception ex) {
160: log.error("Could not unzip : "
161: + in.getAbsolutePath());
162: log.error(ex);
163: return;
164: }
165: getPageManager().removePage(page);
166: }
167: }
168: }
169: }
170:
171: public File getRoot() {
172: return fieldRoot;
173: }
174:
175: public void setRoot(File inRoot) {
176: fieldRoot = inRoot;
177: }
178:
179: public ZipUtil getZipUtil() {
180: if (fieldZipUtil == null) {
181: fieldZipUtil = new ZipUtil();
182: }
183: return fieldZipUtil;
184: }
185:
186: public void setZipUtil(ZipUtil inZipUtil) {
187: fieldZipUtil = inZipUtil;
188: }
189:
190: public Page saveFirstFileAs(String inPath, User inUser)
191: throws OpenEditException {
192: FileUploadItem item = getFirstItem();
193: saveFileAs(item, inPath, inUser);
194: return item.getSavedPage();
195: }
196:
197: public FileUploadItem getFirstItem() {
198: for (Iterator iterator = getUploadItems().iterator(); iterator
199: .hasNext();) {
200: FileUploadItem item = (FileUploadItem) iterator.next();
201: return item;
202: }
203: return null;
204: }
205:
206: public FileUploadItem getUploadItem(int inI) {
207: for (Iterator iterator = getUploadItems().iterator(); iterator
208: .hasNext();) {
209: FileUploadItem item = (FileUploadItem) iterator.next();
210: if (item.getCount() == inI) {
211: return item;
212: }
213: }
214: return null;
215: }
216: }
|