| | |
| | | /* |
| | | Dateiverwaltung - File management in your browser |
| | | Copyright (C) 2017 Ulrich Hilger, http://uhilger.de |
| | | |
| | | This program is free software: you can redistribute it and/or modify |
| | | it under the terms of the GNU Affero General Public License as |
| | | published by the Free Software Foundation, either version 3 of the |
| | | License, or (at your option) any later version. |
| | | |
| | | This program is distributed in the hope that it will be useful, |
| | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| | | GNU Affero General Public License for more details. |
| | | |
| | | You should have received a copy of the GNU Affero General Public License |
| | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| | | */ |
| | | |
| | | package de.uhilger.filecms.api; |
| | | |
| | | import static de.uhilger.filecms.api.FileMgr.HOME_DIR_NAME; |
| | | import static de.uhilger.filecms.api.FileMgr.HOME_DIR_PATH; |
| | | import static de.uhilger.filecms.api.FileMgr.PUB_DIR_NAME; |
| | | import static de.uhilger.filecms.api.FileMgr.PUB_DIR_PATH; |
| | | import de.uhilger.filecms.data.FileRef; |
| | | import de.uhilger.filecms.web.Initialiser; |
| | | import de.uhilger.filesystem.FileRef; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | |
| | | public static final String DESC = "file-cms uploader"; |
| | | |
| | | private static final Logger logger = Logger.getLogger(UploadServlet.class.getName()); |
| | | |
| | | private String targetDirStr; |
| | | |
| | | /** |
| | | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
| | |
| | | */ |
| | | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
| | | throws ServletException, IOException { |
| | | |
| | | //logger.log(Level.INFO, "start"); |
| | | boolean isMultipart = ServletFileUpload.isMultipartContent(request); |
| | | |
| | |
| | | |
| | | // Process the uploaded items |
| | | Iterator<FileItem> iter = items.iterator(); |
| | | FileItem fileItem = null; |
| | | while (iter.hasNext()) { |
| | | FileItem item = iter.next(); |
| | | |
| | |
| | | //File base = repository.getParentFile().getParentFile().getParentFile().getParentFile(); |
| | | //File webapps = new File(base, "webapps"); |
| | | //logger.info(webapps.getAbsolutePath()); |
| | | File targetDir = getTargetDir(request, PUB_DIR_NAME); |
| | | logger.log(Level.FINE, "rufe processUploadedFile"); |
| | | processUploadedFile(targetDir, item, true); |
| | | fileItem = item; |
| | | } |
| | | } |
| | | File targetDir = getTargetDir(request, targetDirStr); |
| | | if(targetDir != null && fileItem != null) { |
| | | logger.log(Level.FINER, "targetDir before upload: " + targetDir.getAbsolutePath()); |
| | | processUploadedFile(targetDir, fileItem, true); |
| | | } |
| | | } catch (Exception ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | |
| | | } |
| | | |
| | | private File getTargetDir(HttpServletRequest request, String relPath) { |
| | | logger.finer(relPath); |
| | | String targetPath = null; |
| | | if(relPath.startsWith(PUB_DIR_NAME)) { |
| | | targetPath = PUB_DIR_PATH + getUserName(request) + "/" + relPath.substring(PUB_DIR_NAME.length()); |
| | | } else if(relPath.startsWith(HOME_DIR_NAME)) { |
| | | targetPath = HOME_DIR_PATH + getUserName(request) + "/" + relPath.substring(HOME_DIR_NAME.length()); |
| | | } else { |
| | | // kann eigentlich nicht sein.. |
| | | File targetDir = null; |
| | | if(relPath != null) { |
| | | logger.finer(relPath); |
| | | String targetPath = null; |
| | | if(relPath.startsWith(PUB_DIR_NAME)) { |
| | | targetPath = PUB_DIR_PATH + getUserName(request) + relPath.substring(PUB_DIR_NAME.length()); |
| | | } else if(relPath.startsWith(HOME_DIR_NAME)) { |
| | | targetPath = HOME_DIR_PATH + getUserName(request) + relPath.substring(HOME_DIR_NAME.length()); |
| | | } else { |
| | | // kann eigentlich nicht sein.. |
| | | } |
| | | logger.finer(targetPath); |
| | | targetDir = new File(getBase().getAbsolutePath(), targetPath); |
| | | } |
| | | logger.finer(targetPath); |
| | | File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
| | | return targetDir; |
| | | } |
| | | |
| | |
| | | private FileRef getBase() { |
| | | FileRef base = null; |
| | | Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
| | | if (o instanceof FileRef) { |
| | | base = (FileRef) o; |
| | | if(o instanceof String) { |
| | | String baseStr = (String) o; |
| | | logger.fine(baseStr); |
| | | File file = new File(baseStr); |
| | | base = new FileRef(file.getAbsolutePath(), file.isDirectory()); |
| | | } |
| | | return base; |
| | | } |
| | |
| | | long sizeInBytes = item.getSize(); |
| | | // Process a file upload |
| | | if (writeToFile) { |
| | | //logger.log(Level.INFO, "writeToFile " + fileName); |
| | | File uploadedFile = new File(targetDir, fileName); |
| | | logger.log(Level.FINER, "writeToFile " + uploadedFile.getAbsolutePath()); |
| | | item.write(uploadedFile); |
| | | } else { |
| | | InputStream uploadedStream = item.getInputStream(); |
| | |
| | | if (item.isFormField()) { |
| | | String name = item.getFieldName(); |
| | | String value = item.getString(); |
| | | //logger.log(Level.INFO, "field name: " + name + ", value: " + value); |
| | | logger.log(Level.FINER, "field name: " + name + ", value: " + value); |
| | | //... |
| | | if(name.equals("pfad")) { |
| | | targetDirStr = value; |
| | | } |
| | | } |
| | | } |
| | | |