001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers.files;
034:
035: import org.apache.commons.fileupload.FileItem;
036: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
037: import org.apache.commons.fileupload.servlet.ServletFileUpload;
038: import org.apache.commons.fileupload.servlet.ServletRequestContext;
039:
040: import org.libresource.Libresource;
041:
042: import org.libresource.core.FileDataImpl;
043:
044: import org.libresource.files.FileConstants;
045: import org.libresource.files.ejb.model.FileResourceValue;
046: import org.libresource.files.interfaces.LibresourceFilesService;
047:
048: import org.libresource.kernel.KernelConstants;
049: import org.libresource.kernel.LibresourceSecurityException;
050: import org.libresource.kernel.interfaces.KernelService;
051:
052: import org.libresource.web.Controller;
053: import org.libresource.web.config.Config;
054:
055: import java.io.File;
056: import java.io.FileOutputStream;
057: import java.io.InputStream;
058:
059: import java.net.URI;
060:
061: import java.util.Iterator;
062: import java.util.List;
063:
064: import javax.servlet.http.HttpServletRequest;
065: import javax.servlet.http.HttpServletResponse;
066:
067: public class EditFileController implements Controller {
068: public Object process(URI uri, HttpServletRequest request,
069: HttpServletResponse response) throws Exception {
070: if (!ServletFileUpload
071: .isMultipartContent(new ServletRequestContext(request))) {
072: KernelService kernelService = (KernelService) Libresource
073: .getService(KernelConstants.SERVICE);
074:
075: if (!kernelService.checkSecurity(uri,
076: KernelConstants.SECURITY_UPDATE)) {
077: throw new LibresourceSecurityException(uri,
078: KernelConstants.SECURITY_UPDATE);
079: }
080:
081: LibresourceFilesService libresourceFilesService = (LibresourceFilesService) Libresource
082: .getService(FileConstants.SERVICE);
083: FileResourceValue file = libresourceFilesService
084: .getFile(uri);
085: request.setAttribute("displayName", file.getDisplayName());
086: request.setAttribute("description", file.getDescription());
087: request.setAttribute("throwEvent", String.valueOf(file
088: .getThrowEvent()));
089: request.setAttribute("infos", file.getDownloadInfos());
090:
091: return "/pages/modules/files/editFile.jsp";
092: }
093:
094: // Create a new file upload handler
095: ServletFileUpload upload = new ServletFileUpload(
096: new DiskFileItemFactory());
097:
098: // Parse the request
099: List items = upload.parseRequest(request);
100:
101: String fileName = null;
102: String displayName = null;
103: String description = null;
104: String throwEvent = null;
105: FileDataImpl fileData = null;
106: boolean cancel = false;
107:
108: // Process the uploaded items
109: Iterator iter = items.iterator();
110:
111: while (iter.hasNext()) {
112: FileItem item = (FileItem) iter.next();
113:
114: if (item.isFormField()) {
115: if (item.getFieldName().equals("displayName")) {
116: displayName = item.getString();
117: }
118:
119: if (item.getFieldName().equals("description")) {
120: description = item.getString();
121: }
122:
123: if (item.getFieldName().equals("throwEvent")) {
124: throwEvent = item.getString();
125: }
126:
127: if (item.getFieldName().equals("cancel")) {
128: cancel = true;
129: }
130: } else {
131: if (item.getFieldName().equals("file")) {
132: if (item.getSize() > 0) {
133: fileName = item.getName();
134:
135: if (fileName.indexOf("\\") != -1) {
136: fileName = fileName.substring(fileName
137: .lastIndexOf("\\") + 1);
138: }
139:
140: File temp = File.createTempFile("file",
141: "upload", Config.getInstance(null)
142: .getLsWebBuffer());
143: item.write(temp);
144: fileData = new FileDataImpl(temp);
145: }
146: }
147: }
148: }
149:
150: if (fileData != null) {
151: fileData.setName(fileName);
152:
153: // remove thumbnail
154: LibresourceFilesService libresourceFilesService = (LibresourceFilesService) Libresource
155: .getService(FileConstants.SERVICE);
156: FileResourceValue file = libresourceFilesService
157: .getFile(uri);
158: File tn = new File(Config.getInstance(null)
159: .getLsWebBuffer()
160: + File.separator + file.getId() + "_tn");
161:
162: if (tn.exists()) {
163: tn.delete();
164: }
165: }
166:
167: if (cancel) {
168: return uri;
169: }
170:
171: LibresourceFilesService libresourceFilesService = (LibresourceFilesService) Libresource
172: .getService(FileConstants.SERVICE);
173:
174: if ((throwEvent != null) && (throwEvent.compareTo("on") == 0)) {
175: libresourceFilesService.editFile(uri, displayName,
176: description, fileData, true);
177: } else {
178: libresourceFilesService.editFile(uri, displayName,
179: description, fileData, false);
180: }
181:
182: return uri;
183: }
184: }
|