| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
package net.sourceforge.pebble.web.action; |
| 33 |
|
|
| 34 |
|
import net.sourceforge.pebble.Constants; |
| 35 |
|
import net.sourceforge.pebble.PebbleContext; |
| 36 |
|
import net.sourceforge.pebble.domain.FileManager; |
| 37 |
|
import net.sourceforge.pebble.domain.FileMetaData; |
| 38 |
|
import net.sourceforge.pebble.domain.Blog; |
| 39 |
|
import net.sourceforge.pebble.domain.BlogManager; |
| 40 |
|
import net.sourceforge.pebble.web.view.RedirectView; |
| 41 |
|
import net.sourceforge.pebble.web.view.View; |
| 42 |
|
import net.sourceforge.pebble.web.view.impl.FileTooLargeView; |
| 43 |
|
import net.sourceforge.pebble.web.view.impl.NotEnoughSpaceView; |
| 44 |
|
import org.apache.commons.fileupload.DiskFileUpload; |
| 45 |
|
import org.apache.commons.fileupload.FileItem; |
| 46 |
|
import org.apache.commons.fileupload.FileUpload; |
| 47 |
|
import org.apache.commons.fileupload.FileUploadBase; |
| 48 |
|
import org.apache.commons.logging.Log; |
| 49 |
|
import org.apache.commons.logging.LogFactory; |
| 50 |
|
|
| 51 |
|
import javax.servlet.ServletException; |
| 52 |
|
import javax.servlet.http.HttpServletRequest; |
| 53 |
|
import javax.servlet.http.HttpServletResponse; |
| 54 |
|
import java.io.File; |
| 55 |
|
import java.util.Iterator; |
| 56 |
|
import java.util.List; |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
@author |
| 62 |
|
|
|
|
|
| 0% |
Uncovered Elements: 75 (75) |
Complexity: 19 |
Complexity Density: 0,36 |
|
| 63 |
|
public abstract class UploadFileAction extends AbstractFileAction { |
| 64 |
|
|
| 65 |
|
private static final Log log = LogFactory.getLog(UploadFileAction.class); |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
@param |
| 71 |
|
@param |
| 72 |
|
@return |
| 73 |
|
|
|
|
|
| 0% |
Uncovered Elements: 69 (69) |
Complexity: 18 |
Complexity Density: 0,37 |
|
| 74 |
0
|
public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {... |
| 75 |
0
|
Blog blog = (Blog)getModel().get(Constants.BLOG_KEY); |
| 76 |
|
|
| 77 |
0
|
String type = getType(); |
| 78 |
0
|
String path = ""; |
| 79 |
0
|
String[] filenames = new String[10]; |
| 80 |
|
|
| 81 |
0
|
FileManager fileManager = new FileManager(blog, type); |
| 82 |
|
|
| 83 |
0
|
try { |
| 84 |
0
|
boolean isMultipart = FileUpload.isMultipartContent(request); |
| 85 |
|
|
| 86 |
0
|
if (isMultipart) { |
| 87 |
0
|
DiskFileUpload upload = new DiskFileUpload(); |
| 88 |
0
|
long sizeInBytes = PebbleContext.getInstance().getConfiguration().getFileUploadSize() * 1024; |
| 89 |
0
|
upload.setSizeMax(sizeInBytes); |
| 90 |
0
|
upload.setSizeThreshold((int)sizeInBytes/4); |
| 91 |
0
|
upload.setRepositoryPath(System.getProperty("java.io.tmpdir")); |
| 92 |
|
|
| 93 |
0
|
List items; |
| 94 |
0
|
try { |
| 95 |
0
|
items = upload.parseRequest(request); |
| 96 |
|
} catch (FileUploadBase.SizeLimitExceededException e) { |
| 97 |
0
|
return new FileTooLargeView(); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
0
|
Iterator it = items.iterator(); |
| 102 |
0
|
while (it.hasNext()) { |
| 103 |
0
|
FileItem item = (FileItem)it.next(); |
| 104 |
0
|
if (item.isFormField() && item.getFieldName().startsWith("filename")) { |
| 105 |
0
|
int index = Integer.parseInt(item.getFieldName().substring(item.getFieldName().length()-1)); |
| 106 |
0
|
filenames[index] = item.getString(); |
| 107 |
0
|
log.debug("index is " + index + ", filename is " + filenames[index]); |
| 108 |
0
|
} else if (item.isFormField() && item.getFieldName().equals("path")) { |
| 109 |
0
|
path = item.getString(); |
| 110 |
|
} |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
0
|
it = items.iterator(); |
| 115 |
0
|
while (it.hasNext()) { |
| 116 |
0
|
FileItem item = (FileItem)it.next(); |
| 117 |
|
|
| 118 |
0
|
if (!item.isFormField() && item.getSize() > 0 && item.getFieldName().startsWith("file")) { |
| 119 |
0
|
int index = Integer.parseInt(item.getFieldName().substring(item.getFieldName().length()-1)); |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
0
|
if (filenames[index] == null || filenames[index].length() == 0) { |
| 124 |
0
|
filenames[index] = item.getName(); |
| 125 |
|
} |
| 126 |
|
|
| 127 |
0
|
File destinationDirectory = fileManager.getFile(path); |
| 128 |
0
|
File file = new File(destinationDirectory, filenames[index]); |
| 129 |
0
|
if (!fileManager.isUnderneathRootDirectory(file)) { |
| 130 |
0
|
response.setStatus(HttpServletResponse.SC_FORBIDDEN); |
| 131 |
0
|
return null; |
| 132 |
|
} |
| 133 |
|
|
| 134 |
0
|
long itemSize = item.getSize()/1024; |
| 135 |
0
|
if (FileManager.hasEnoughSpace(blog, itemSize)) { |
| 136 |
0
|
log.debug("Writing file " + filenames[index] + ", size is " + item.getSize()); |
| 137 |
0
|
writeFile(fileManager, path, filenames[index], item); |
| 138 |
|
|
| 139 |
|
|
| 140 |
0
|
if (type.equals(FileMetaData.THEME_FILE)) { |
| 141 |
0
|
writeFile(new FileManager(blog, FileMetaData.BLOG_DATA), "/theme" + path, filenames[index], item); |
| 142 |
|
} |
| 143 |
|
} else { |
| 144 |
0
|
return new NotEnoughSpaceView(); |
| 145 |
|
} |
| 146 |
|
} |
| 147 |
|
} |
| 148 |
|
} |
| 149 |
|
|
| 150 |
0
|
blog.info("Files uploaded."); |
| 151 |
|
} catch (Exception e) { |
| 152 |
0
|
throw new ServletException(e); |
| 153 |
|
} |
| 154 |
|
|
| 155 |
0
|
FileMetaData directory = fileManager.getFileMetaData(path); |
| 156 |
|
|
| 157 |
0
|
return new RedirectView(blog.getUrl() + directory.getUrl()); |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
@param |
| 164 |
|
@param |
| 165 |
|
@param |
| 166 |
|
@param |
| 167 |
|
@throws |
| 168 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
| 169 |
0
|
private void writeFile(FileManager fileManager, String path, String filename, FileItem item) throws Exception {... |
| 170 |
0
|
File destinationDirectory = fileManager.getFile(path); |
| 171 |
0
|
destinationDirectory.mkdirs(); |
| 172 |
|
|
| 173 |
0
|
File file = new File(destinationDirectory, filename); |
| 174 |
0
|
item.write(file); |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
@return |
| 181 |
|
@see |
| 182 |
|
|
| 183 |
|
protected abstract String getType(); |
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
@return |
| 189 |
|
|
| 190 |
|
public abstract String[] getRoles(HttpServletRequest request); |
| 191 |
|
|
| 192 |
|
} |