use of org.apache.commons.fileupload.FileItem in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method uploadWithPath.
@ApiOperation(value = "Upload a file to a store with a path", notes = "Must have write access to the store")
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, value = "/{name}/**")
public ResponseEntity<List<FileModel>> uploadWithPath(@ApiParam(value = "Valid File Store name", required = true, allowMultiple = false) @PathVariable("name") String name, @AuthenticationPrincipal User user, @RequestParam(required = false, defaultValue = "false") boolean overwrite, MultipartHttpServletRequest multipartRequest, HttpServletRequest request) throws IOException {
FileStoreDefinition def = ModuleRegistry.getFileStoreDefinition(name);
if (def == null)
throw new NotFoundRestException();
// Check Permissions
def.ensureStoreWritePermission(user);
String pathInStore = parsePath(request);
File root = def.getRoot().getCanonicalFile();
Path rootPath = root.toPath();
File outputDirectory = new File(root, pathInStore).getCanonicalFile();
if (!outputDirectory.toPath().startsWith(rootPath)) {
throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowRoot", pathInStore));
}
if (outputDirectory.exists() && !outputDirectory.isDirectory()) {
throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("filestore.cannotCreateDir", removeToRoot(root, outputDirectory), name));
}
if (!outputDirectory.exists()) {
if (!outputDirectory.mkdirs())
throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("filestore.cannotCreateDir", removeToRoot(root, outputDirectory), name));
}
// Put the file where it belongs
List<FileModel> fileModels = new ArrayList<>();
MultiValueMap<String, MultipartFile> filemap = multipartRequest.getMultiFileMap();
for (String nameField : filemap.keySet()) {
for (MultipartFile file : filemap.get(nameField)) {
String filename;
if (file instanceof CommonsMultipartFile) {
FileItem fileItem = ((CommonsMultipartFile) file).getFileItem();
filename = fileItem.getName();
} else {
filename = file.getName();
}
File newFile = findUniqueFileName(outputDirectory, filename, overwrite);
File parent = newFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try (OutputStream output = new FileOutputStream(newFile, false)) {
try (InputStream input = file.getInputStream()) {
StreamUtils.copy(input, output);
}
}
fileModels.add(fileToModel(newFile, root, request.getServletContext()));
}
}
return new ResponseEntity<>(fileModels, HttpStatus.OK);
}
use of org.apache.commons.fileupload.FileItem in project v7files by thiloplanz.
the class BucketsServlet method doFormPost.
private void doFormPost(HttpServletRequest request, HttpServletResponse response, BSONObject bucket) throws IOException {
ObjectId uploadId = new ObjectId();
BSONObject parameters = new BasicBSONObject();
List<FileItem> files = new ArrayList<FileItem>();
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
for (Object _file : upload.parseRequest(request)) {
FileItem file = (FileItem) _file;
if (file.isFormField()) {
String v = file.getString();
parameters.put(file.getFieldName(), v);
} else {
files.add(file);
}
}
} catch (FileUploadException e) {
throw new IOException(e);
}
} else {
for (Entry<String, String[]> param : request.getParameterMap().entrySet()) {
String[] v = param.getValue();
if (v.length == 1)
parameters.put(param.getKey(), v[0]);
else
parameters.put(param.getKey(), v);
}
}
BSONObject result = new BasicBSONObject("_id", uploadId);
BSONObject uploads = new BasicBSONObject();
for (FileItem file : files) {
DiskFileItem f = (DiskFileItem) file;
// inline until 10KB
if (f.isInMemory()) {
uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.get(), uploadId, f.getName(), f.getContentType()));
} else {
uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.getStoreLocation(), uploadId, f.getName(), f.getContentType()));
}
file.delete();
}
result.put("files", uploads);
result.put("parameters", parameters);
bucketCollection.update(new BasicDBObject("_id", bucket.get("_id")), new BasicDBObject("$push", new BasicDBObject("FormPost.data", result)));
String redirect = BSONUtils.getString(bucket, "FormPost.redirect");
// redirect mode
if (StringUtils.isNotBlank(redirect)) {
response.sendRedirect(redirect + "?v7_formpost_id=" + uploadId);
return;
}
// echo mode
// JSON does not work, see https://jira.mongodb.org/browse/JAVA-332
// response.setContentType("application/json");
// response.getWriter().write(JSON.serialize(result));
byte[] bson = BSON.encode(result);
response.getOutputStream().write(bson);
}
use of org.apache.commons.fileupload.FileItem in project jaggery by wso2.
the class RequestHostObject method parseMultipart.
private static void parseMultipart(RequestHostObject rho) throws ScriptException {
if (rho.files != null) {
return;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(rho.request);
} catch (FileUploadException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
// Process the uploaded items
String name;
rho.files = rho.context.newObject(rho);
for (Object obj : items) {
FileItem item = (FileItem) obj;
name = item.getFieldName();
if (item.isFormField()) {
ArrayList<FileItem> x = (ArrayList<FileItem>) rho.parameterMap.get(name);
if (x == null) {
ArrayList<FileItem> array = new ArrayList<FileItem>(1);
array.add(item);
rho.parameterMap.put(name, array);
} else {
x.add(item);
}
} else {
rho.files.put(item.getFieldName(), rho.files, rho.context.newObject(rho, "File", new Object[] { item }));
}
}
}
use of org.apache.commons.fileupload.FileItem in project jaggery by wso2.
the class RequestHostObject method jsFunction_getParameter.
public static Object jsFunction_getParameter(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "getParameter";
FileItem item;
int argsCount = args.length;
if (argsCount != 1 && argsCount != 2) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false);
}
if (argsCount == 2 && !(args[1] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "string", args[1], false);
}
String parameter = (String) args[0];
RequestHostObject rho = (RequestHostObject) thisObj;
if (!rho.isMultipart) {
return getParameter(parameter, rho.request, rho);
}
parseMultipart(rho);
if (rho.parameterMap.get(parameter) != null) {
item = rho.parameterMap.get(parameter).get(0);
} else {
return null;
}
if (argsCount == 1) {
return item.getString();
}
try {
return item.getString((String) args[1]);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
use of org.apache.commons.fileupload.FileItem in project jackrabbit by apache.
the class HttpMultipartPost method getFileParameterValues.
/**
* Returns an array of input streams for uploaded file parameters.
*
* @param name the name of the file parameter(s)
* @return an array of input streams or <code>null</code> if no file params
* with the given name exist.
* @throws IOException if an I/O error occurs
*/
InputStream[] getFileParameterValues(String name) throws IOException {
checkInitialized();
InputStream[] values = null;
if (fileParamNames.contains(name)) {
List<FileItem> l = nameToItems.get(name);
if (l != null && !l.isEmpty()) {
List<InputStream> ins = new ArrayList<InputStream>(l.size());
for (FileItem item : l) {
if (!item.isFormField()) {
ins.add(item.getInputStream());
}
}
values = ins.toArray(new InputStream[ins.size()]);
}
}
return values;
}
Aggregations