diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..c229b99 --- /dev/null +++ b/.classpath @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..0222fad --- /dev/null +++ b/.project @@ -0,0 +1,31 @@ + + + wysiwyg-editor-java-sdk + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.wst.jsdt.core.jsNature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/.jsdtscope b/.settings/.jsdtscope new file mode 100644 index 0000000..cca691f --- /dev/null +++ b/.settings/.jsdtscope @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..f42de36 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..d5cd831 --- /dev/null +++ b/.settings/org.eclipse.wst.common.component @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..b46bc51 --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.container b/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000..49c8cd4 --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.JRE_CONTAINER \ No newline at end of file diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.name b/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000..11006e2 --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Global \ No newline at end of file diff --git a/Examples/WebContent/WEB-INF/web.xml b/Examples/WebContent/WEB-INF/web.xml index 7600143..60e4384 100644 --- a/Examples/WebContent/WEB-INF/web.xml +++ b/Examples/WebContent/WEB-INF/web.xml @@ -14,4 +14,4 @@ com.froala.examples.servlets.ContextListener - \ No newline at end of file + diff --git a/Examples/WebContent/index.html b/Examples/WebContent/index.html index 5c52342..862d364 100644 --- a/Examples/WebContent/index.html +++ b/Examples/WebContent/index.html @@ -79,6 +79,10 @@

Sample 1: Save to disk

fileUploadParams: { id: 'my_editor' }, + videoUploadURL: './upload_video', + videoUploadParams: { + id: 'my_editor' + }, imageManagerLoadURL: '/load_images', imageManagerDeleteURL: "/delete_image", imageManagerDeleteMethod: "POST" @@ -120,7 +124,23 @@

Sample 1: Save to disk

.fail (function (err) { console.log ('file delete problem: ' + JSON.stringify(err)); }) - }) + }), + $('#edit').on('froalaEditor.image.uploaded', function (e, editor, resp) { + console.log (resp) + var json = JSON.parse(resp); + console.log (json.link); + var img = new Image(); + img.onerror = function () { + console.log ('error'); + } + img.src = json.link; + }) + $('#edit').on('froalaEditor.image.error', function (e, editor, error,response) { + console.log('this is e:', e); + console.log('this is editor:', editor); + console.log('this is error:', error); + console.log('this is error:', response); + }); }); diff --git a/Examples/build/classes/.gitignore b/Examples/build/classes/.gitignore new file mode 100644 index 0000000..c2d9872 --- /dev/null +++ b/Examples/build/classes/.gitignore @@ -0,0 +1 @@ +/com/ diff --git a/Examples/build/classes/com/froala/examples/servlets/ContextListener.class b/Examples/build/classes/com/froala/examples/servlets/ContextListener.class index 7d5db15..6cf9c50 100644 Binary files a/Examples/build/classes/com/froala/examples/servlets/ContextListener.class and b/Examples/build/classes/com/froala/examples/servlets/ContextListener.class differ diff --git a/Examples/build/classes/com/froala/examples/servlets/FileServlet.class b/Examples/build/classes/com/froala/examples/servlets/FileServlet.class new file mode 100644 index 0000000..84e0f37 Binary files /dev/null and b/Examples/build/classes/com/froala/examples/servlets/FileServlet.class differ diff --git a/Examples/build/classes/com/froala/examples/servlets/LoadImage.class b/Examples/build/classes/com/froala/examples/servlets/LoadImage.class index 76b2a77..2a6d2c4 100644 Binary files a/Examples/build/classes/com/froala/examples/servlets/LoadImage.class and b/Examples/build/classes/com/froala/examples/servlets/LoadImage.class differ diff --git a/Examples/build/classes/com/froala/examples/servlets/UploadFile.class b/Examples/build/classes/com/froala/examples/servlets/UploadFile.class index f9e2319..f9508ab 100644 Binary files a/Examples/build/classes/com/froala/examples/servlets/UploadFile.class and b/Examples/build/classes/com/froala/examples/servlets/UploadFile.class differ diff --git a/Examples/build/classes/com/froala/examples/servlets/UploadFileValidation.class b/Examples/build/classes/com/froala/examples/servlets/UploadFileValidation.class index 259ae93..6d822ba 100644 Binary files a/Examples/build/classes/com/froala/examples/servlets/UploadFileValidation.class and b/Examples/build/classes/com/froala/examples/servlets/UploadFileValidation.class differ diff --git a/Examples/build/classes/com/froala/examples/servlets/UploadImage.class b/Examples/build/classes/com/froala/examples/servlets/UploadImage.class index 108b395..826d2ac 100644 Binary files a/Examples/build/classes/com/froala/examples/servlets/UploadImage.class and b/Examples/build/classes/com/froala/examples/servlets/UploadImage.class differ diff --git a/Examples/build/classes/com/froala/examples/servlets/UploadImageResize.class b/Examples/build/classes/com/froala/examples/servlets/UploadImageResize.class index 6092a09..4d6f992 100644 Binary files a/Examples/build/classes/com/froala/examples/servlets/UploadImageResize.class and b/Examples/build/classes/com/froala/examples/servlets/UploadImageResize.class differ diff --git a/Examples/build/classes/com/froala/examples/servlets/UploadImageValidation.class b/Examples/build/classes/com/froala/examples/servlets/UploadImageValidation.class index 64d55a6..72caf09 100644 Binary files a/Examples/build/classes/com/froala/examples/servlets/UploadImageValidation.class and b/Examples/build/classes/com/froala/examples/servlets/UploadImageValidation.class differ diff --git a/Examples/src/com/froala/examples/servlets/ContextListener.java b/Examples/src/com/froala/examples/servlets/ContextListener.java index a2f4e25..a8ef3b2 100644 --- a/Examples/src/com/froala/examples/servlets/ContextListener.java +++ b/Examples/src/com/froala/examples/servlets/ContextListener.java @@ -30,7 +30,5 @@ public void contextInitialized(ServletContextEvent event) { if (!path.exists()) { path.mkdirs(); } - - System.out.println("Public folder is located here: " + publicFolderPath); } } diff --git a/Examples/src/com/froala/examples/servlets/FileServlet.java b/Examples/src/com/froala/examples/servlets/FileServlet.java new file mode 100644 index 0000000..3d44771 --- /dev/null +++ b/Examples/src/com/froala/examples/servlets/FileServlet.java @@ -0,0 +1,37 @@ +package com.froala.examples.servlets; + +import java.io.File; +import java.io.IOException; +import java.net.URLDecoder; +import java.nio.file.Files; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + + + +@WebServlet("/files/*") +public class FileServlet extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException + { + String filename = URLDecoder.decode(request.getPathInfo().substring(1), "UTF-8"); + File file = new File("/PATH TO/YOUR PROJECT/WORKSPACE/WEBCONTENT/WEB-INF/SOME FOLDER/", filename); + response.setHeader("Content-Type", getServletContext().getMimeType(filename)); + response.setHeader("Content-Length", String.valueOf(file.length())); + response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\""); + Files.copy(file.toPath(), response.getOutputStream()); + } + +} \ No newline at end of file diff --git a/Examples/src/com/froala/examples/servlets/LoadImage.java b/Examples/src/com/froala/examples/servlets/LoadImage.java index 19bb5821..3e2147d 100644 --- a/Examples/src/com/froala/examples/servlets/LoadImage.java +++ b/Examples/src/com/froala/examples/servlets/LoadImage.java @@ -35,7 +35,7 @@ public LoadImage() { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String fileRoute = "/public/"; + String fileRoute = "/PATH TO/YOUR PROJECT/WORKSPACE/WEBCONTENT/WEB-INF/SOME FOLDER/"; ArrayList responseData; try { responseData = Image.list(request, fileRoute); diff --git a/Examples/src/com/froala/examples/servlets/UploadFile.java b/Examples/src/com/froala/examples/servlets/UploadFile.java index c128e6b..680b5a9 100644 --- a/Examples/src/com/froala/examples/servlets/UploadFile.java +++ b/Examples/src/com/froala/examples/servlets/UploadFile.java @@ -38,7 +38,7 @@ public UploadFile() { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String fileRoute = "/public/"; + String fileRoute = "/PATH TO/YOUR PROJECT/WORKSPACE/WEBCONTENT/WEB-INF/SOME FOLDER/"; // No validation. FileOptions options = new FileOptions(); diff --git a/Examples/src/com/froala/examples/servlets/UploadFileValidation.java b/Examples/src/com/froala/examples/servlets/UploadFileValidation.java index b955cbb..6b92bfd 100644 --- a/Examples/src/com/froala/examples/servlets/UploadFileValidation.java +++ b/Examples/src/com/froala/examples/servlets/UploadFileValidation.java @@ -41,7 +41,7 @@ public UploadFileValidation() { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String fileRoute = "/public/"; + String fileRoute = "/PATH TO/YOUR PROJECT/WORKSPACE/WEBCONTENT/WEB-INF/SOME FOLDER/"; FileOptions options = new FileOptions(); options.setFieldname("myFile"); diff --git a/Examples/src/com/froala/examples/servlets/UploadImage.java b/Examples/src/com/froala/examples/servlets/UploadImage.java index faac973..8a5d7a7 100644 --- a/Examples/src/com/froala/examples/servlets/UploadImage.java +++ b/Examples/src/com/froala/examples/servlets/UploadImage.java @@ -1,6 +1,7 @@ package com.froala.examples.servlets; import java.io.IOException; +import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; @@ -36,23 +37,36 @@ public UploadImage() { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + String fileRoute = "/PATH TO/YOUR PROJECT/WORKSPACE/WEBCONTENT/WEB-INF/SOME FOLDER/"; + final PrintWriter writer = response.getWriter(); - String fileRoute = "/public/"; - - Map responseData; + Map responseData = null; try { - responseData = Image.upload(request, fileRoute); // Use default + + responseData = Image.upload(request, fileRoute); // Use default // image // validation. + } catch (Exception e) { + e.printStackTrace(); responseData = new HashMap(); responseData.put("error", e.toString()); + + } finally { + + String jsonResponseData = new Gson().toJson(responseData); + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + response.getWriter().write(jsonResponseData); + + if (writer != null) { + writer.close(); + } + } - String jsonResponseData = new Gson().toJson(responseData); - response.setContentType("application/json"); - response.setCharacterEncoding("UTF-8"); - response.getWriter().write(jsonResponseData); + } } diff --git a/Examples/src/com/froala/examples/servlets/UploadImageResize.java b/Examples/src/com/froala/examples/servlets/UploadImageResize.java index 6e0161c..2d3b80e 100644 --- a/Examples/src/com/froala/examples/servlets/UploadImageResize.java +++ b/Examples/src/com/froala/examples/servlets/UploadImageResize.java @@ -38,7 +38,7 @@ public UploadImageResize() { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String fileRoute = "/public/"; + String fileRoute = "/PATH TO/YOUR PROJECT/WORKSPACE/WEBCONTENT/WEB-INF/SOME FOLDER/"; // Use default validation with image resize; ImageOptions options = new ImageOptions(); diff --git a/Examples/src/com/froala/examples/servlets/UploadImageValidation.java b/Examples/src/com/froala/examples/servlets/UploadImageValidation.java index 5d73286..77cf5b3 100644 --- a/Examples/src/com/froala/examples/servlets/UploadImageValidation.java +++ b/Examples/src/com/froala/examples/servlets/UploadImageValidation.java @@ -42,7 +42,7 @@ public UploadImageValidation() { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String fileRoute = "/public/"; + String fileRoute = "/PATH TO/YOUR PROJECT/WORKSPACE/WEBCONTENT/WEB-INF/SOME FOLDER/"; ImageOptions options = new ImageOptions(); options.setFieldname("myImage"); diff --git a/Examples/src/com/froala/examples/servlets/UploadVideo.java b/Examples/src/com/froala/examples/servlets/UploadVideo.java new file mode 100644 index 0000000..40726d5 --- /dev/null +++ b/Examples/src/com/froala/examples/servlets/UploadVideo.java @@ -0,0 +1,72 @@ +package com.froala.examples.servlets; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.annotation.MultipartConfig; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.froala.editor.Video; +import com.google.gson.Gson; + +/** + * Servlet implementation class UploadImage + */ +@WebServlet("/upload_video") +@MultipartConfig + +public class UploadVideo extends HttpServlet { + private static final long serialVersionUID = 1L; + +/** + * @see HttpServlet#HttpServlet() + */ + public UploadVideo() { + super(); + } +/** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse + * response) + */ + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + String fileRoute = "/PATH TO/YOUR PROJECT/WORKSPACE/WEBCONTENT/WEB-INF/SOME FOLDER/"; + final PrintWriter writer = response.getWriter(); + + Map responseData = null; + try { + + responseData = Video.upload(request, fileRoute); // Use default + // image + // validation. + + } catch (Exception e) { + + e.printStackTrace(); + responseData = new HashMap(); + responseData.put("error", e.toString()); + + } finally { + + String jsonResponseData = new Gson().toJson(responseData); + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + response.getWriter().write(jsonResponseData); + + if (writer != null) { + System.out.println("writer is diff from null"); + writer.close(); + } + + } + + } +} diff --git a/Lib/.classpath b/Lib/.classpath index f92a1a8..97e5ee6 100644 --- a/Lib/.classpath +++ b/Lib/.classpath @@ -12,7 +12,7 @@ - + diff --git a/Lib/.settings/org.eclipse.jdt.core.prefs b/Lib/.settings/org.eclipse.jdt.core.prefs index eb1cb4c..d17b672 100644 --- a/Lib/.settings/org.eclipse.jdt.core.prefs +++ b/Lib/.settings/org.eclipse.jdt.core.prefs @@ -1,12 +1,12 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/Lib/bin/.gitignore b/Lib/bin/.gitignore deleted file mode 100644 index 0fb5df8..0000000 --- a/Lib/bin/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/com/ -/META-INF/ diff --git a/Lib/bin/com/froala/editor/CustomValidation.class b/Lib/bin/com/froala/editor/CustomValidation.class index 4c3f099..6a92d74 100644 Binary files a/Lib/bin/com/froala/editor/CustomValidation.class and b/Lib/bin/com/froala/editor/CustomValidation.class differ diff --git a/Lib/bin/com/froala/editor/File.class b/Lib/bin/com/froala/editor/File.class index 8cfa516..a0a51cb 100644 Binary files a/Lib/bin/com/froala/editor/File.class and b/Lib/bin/com/froala/editor/File.class differ diff --git a/Lib/bin/com/froala/editor/Image.class b/Lib/bin/com/froala/editor/Image.class index a14b26b..a5f82ec 100644 Binary files a/Lib/bin/com/froala/editor/Image.class and b/Lib/bin/com/froala/editor/Image.class differ diff --git a/Lib/bin/com/froala/editor/S3.class b/Lib/bin/com/froala/editor/S3.class index 7642fba..b0db083 100644 Binary files a/Lib/bin/com/froala/editor/S3.class and b/Lib/bin/com/froala/editor/S3.class differ diff --git a/Lib/bin/com/froala/editor/Utils.class b/Lib/bin/com/froala/editor/Utils.class index bfc3187..2efdd10 100644 Binary files a/Lib/bin/com/froala/editor/Utils.class and b/Lib/bin/com/froala/editor/Utils.class differ diff --git a/Lib/bin/com/froala/editor/Video.class b/Lib/bin/com/froala/editor/Video.class new file mode 100644 index 0000000..a32c2fa Binary files /dev/null and b/Lib/bin/com/froala/editor/Video.class differ diff --git a/Lib/bin/com/froala/editor/file/FileOptions.class b/Lib/bin/com/froala/editor/file/FileOptions.class index cfd3aa9..b43104b 100644 Binary files a/Lib/bin/com/froala/editor/file/FileOptions.class and b/Lib/bin/com/froala/editor/file/FileOptions.class differ diff --git a/Lib/bin/com/froala/editor/file/FileValidation.class b/Lib/bin/com/froala/editor/file/FileValidation.class index c5686da..234bb84 100644 Binary files a/Lib/bin/com/froala/editor/file/FileValidation.class and b/Lib/bin/com/froala/editor/file/FileValidation.class differ diff --git a/Lib/bin/com/froala/editor/image/ImageOptions$ResizeOptions.class b/Lib/bin/com/froala/editor/image/ImageOptions$ResizeOptions.class index 301074c..97ff342 100644 Binary files a/Lib/bin/com/froala/editor/image/ImageOptions$ResizeOptions.class and b/Lib/bin/com/froala/editor/image/ImageOptions$ResizeOptions.class differ diff --git a/Lib/bin/com/froala/editor/image/ImageOptions.class b/Lib/bin/com/froala/editor/image/ImageOptions.class index 21041aa..74e8d69 100644 Binary files a/Lib/bin/com/froala/editor/image/ImageOptions.class and b/Lib/bin/com/froala/editor/image/ImageOptions.class differ diff --git a/Lib/bin/com/froala/editor/image/ImageValidation.class b/Lib/bin/com/froala/editor/image/ImageValidation.class index def820f..39f7d13 100644 Binary files a/Lib/bin/com/froala/editor/image/ImageValidation.class and b/Lib/bin/com/froala/editor/image/ImageValidation.class differ diff --git a/Lib/bin/com/froala/editor/s3/S3Config.class b/Lib/bin/com/froala/editor/s3/S3Config.class index 3c5c0ed..53cc585 100644 Binary files a/Lib/bin/com/froala/editor/s3/S3Config.class and b/Lib/bin/com/froala/editor/s3/S3Config.class differ diff --git a/Lib/bin/com/froala/editor/video/VideoOptions.class b/Lib/bin/com/froala/editor/video/VideoOptions.class new file mode 100644 index 0000000..d6e6026 Binary files /dev/null and b/Lib/bin/com/froala/editor/video/VideoOptions.class differ diff --git a/Lib/bin/com/froala/editor/video/VideoValidation.class b/Lib/bin/com/froala/editor/video/VideoValidation.class new file mode 100644 index 0000000..cf52235 Binary files /dev/null and b/Lib/bin/com/froala/editor/video/VideoValidation.class differ diff --git a/Lib/src/com/froala/editor/File.java b/Lib/src/com/froala/editor/File.java index ff75693..b2da25c 100644 --- a/Lib/src/com/froala/editor/File.java +++ b/Lib/src/com/froala/editor/File.java @@ -1,13 +1,13 @@ package com.froala.editor; import java.io.InputStream; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; +import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Part; - -import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import com.froala.editor.file.FileOptions; @@ -79,7 +79,9 @@ public static Map upload(HttpServletRequest req, String fileRout */ public static Map upload(HttpServletRequest req, String fileRoute, FileOptions options) throws Exception { - + + String name = null; + if (options == null) { options = defaultOptions; } @@ -88,9 +90,9 @@ public static Map upload(HttpServletRequest req, String fileRout throw new Exception("Invalid contentType. It must be " + multipartContentType); } - + Part filePart = req.getPart(options.getFieldname()); - + if (filePart == null) { throw new Exception("Fieldname is not correct. It must be: " + options.getFieldname()); } @@ -98,14 +100,14 @@ public static Map upload(HttpServletRequest req, String fileRout // Generate random name. String extension = FilenameUtils.getExtension(Utils.getFileName(filePart)); extension = (extension != null && extension != "") ? "." + extension : extension; - String name = Utils.generateUniqueString() + extension; - - String linkName = fileRoute + name; + name = UUID.randomUUID().toString() + extension ; + + String linkName = "files/" + name; InputStream fileContent = filePart.getInputStream(); String absoluteServerPath = getAbsoluteServerPath(req, linkName); - java.io.File targetFile = new java.io.File(absoluteServerPath); + // Resize image. if (options instanceof ImageOptions && ((ImageOptions) options).getResizeOptions() != null) { @@ -123,10 +125,22 @@ public static Map upload(HttpServletRequest req, String fileRout thumbnailsBuilder = thumbnailsBuilder.forceSize(newWidth, newHeight); } - thumbnailsBuilder.toFile(targetFile); + thumbnailsBuilder.toFile(fileRoute + name); } else { - FileUtils.copyInputStreamToFile(fileContent, targetFile); - } + + // Save the file on server. + java.io.File file = new java.io.File(fileRoute, name); + + try (InputStream input = filePart.getInputStream()) { + Files.copy(input, file.toPath()); + } + + catch (Exception e) { + + System.out.println("
ERROR: " + e); + } + + } if (options.getValidation() != null && !options.getValidation().check(absoluteServerPath, filePart.getContentType())) { @@ -137,6 +151,7 @@ public static Map upload(HttpServletRequest req, String fileRout Map linkObject = new HashMap(); linkObject.put("link", linkName); + return linkObject; } @@ -170,4 +185,14 @@ public static void delete(HttpServletRequest req, String src) { file.delete(); } } + public static String getFileName(final Part part) { + for (String content : part.getHeader("content-disposition").split(";")) { + if (content.trim().startsWith("filename")) { + return content.substring( + content.indexOf('=') + 1).trim().replace("\"", ""); + } + } + return null; + } + } diff --git a/Lib/src/com/froala/editor/Image.java b/Lib/src/com/froala/editor/Image.java index 354ccf5..91d9ca6 100644 --- a/Lib/src/com/froala/editor/Image.java +++ b/Lib/src/com/froala/editor/Image.java @@ -42,7 +42,7 @@ private Image() { * @throws Exception */ public static Map upload(HttpServletRequest req, String fileRoute) throws Exception { - + return upload(req, fileRoute, defaultOptions); } diff --git a/Lib/src/com/froala/editor/Video.java b/Lib/src/com/froala/editor/Video.java new file mode 100644 index 0000000..84885a6 --- /dev/null +++ b/Lib/src/com/froala/editor/Video.java @@ -0,0 +1,166 @@ +package com.froala.editor; + +import java.io.InputStream; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.Part; + +import org.apache.commons.io.FilenameUtils; + +import com.froala.editor.video.VideoOptions; + +public class Video { + + /** + * Content type string used in http multipart. + */ + public static final String multipartContentType = "multipart/form-data"; + + /** + * Private constructor. + */ + private Video() { + + } + + /** + * Video default options. + */ + public static final VideoOptions defaultOptions = new VideoOptions(); + + /** + * Uploads a Video to disk. + * + * @param req + * Servlet HTTP request. + * @param VideoRoute + * Route Server route where the Video will be uploaded. This route + * must be public to be accesed by the editor. + * @return Object with link. + * @throws Exception + */ + public static Map upload(HttpServletRequest req, String VideoRoute) throws Exception { + + return upload(req, VideoRoute, defaultOptions); + } + + /** + * Uploads a Video to disk. + * + * @param req + * Servlet HTTP request. + * @param VideoRoute + * Server route where the Video will be uploaded. This route must + * be public to be accesed by the editor. + * @param options + * Video options. Defaults to {@link #defaultOptions} which has + *
+ * Fieldname: "File"
+ * Validation: + *
    + *
  • Extensions: "mp4", "webm", "ogg"
  • + *
  • Mime Types: "video/mp4", "video/webm", "video/ogg"
  • + *
+ * @return Object with link. + * @throws Exception + */ + public static Map upload(HttpServletRequest req, String fileRoute, VideoOptions options) + throws Exception { + + String name = null; + + if (options == null) { + options = defaultOptions; + } + + if (req.getContentType() == null || req.getContentType().toLowerCase().indexOf(multipartContentType) == -1) { + + throw new Exception("Invalid contentType. It must be " + multipartContentType); + } + + Part filePart = req.getPart(options.getFieldname()); + + if (filePart == null) { + throw new Exception("Fieldname is not correct. It must be: " + options.getFieldname()); + } + + // Generate random name. + String extension = FilenameUtils.getExtension(Utils.getFileName(filePart)); + extension = (extension != null && extension != "") ? "." + extension : extension; + name = UUID.randomUUID().toString() + extension ; + + String linkName = "files/" + name; + + String absoluteServerPath = getAbsoluteServerPath(req, linkName); + + // Save the file on server. + java.io.File file = new java.io.File(fileRoute, name); + + try (InputStream input = filePart.getInputStream()) { + Files.copy(input, file.toPath()); + } + + catch (Exception e) { + + System.out.println("
ERROR: " + e); + } + + if (options.getValidation() != null + && !options.getValidation().check(absoluteServerPath, filePart.getContentType())) { + + delete(req, linkName); + throw new Exception("File does not meet the validation."); + } + + Map linkObject = new HashMap(); + linkObject.put("link", linkName); + + return linkObject; + } + + /** + * Get absolute server path. + * + * @param req + * Used to get the servlet context. + * @param relativePath + * Relative path. + * @return Absolute path. + */ + public static String getAbsoluteServerPath(HttpServletRequest req, String relativePath) { + return req.getServletContext().getRealPath(relativePath); + } + + /** + * Delete a file from disk. + * + * @param req + * Used to get the servlet context. + * @param src + * Server file path. + */ + public static void delete(HttpServletRequest req, String src) { + + String filePath = getAbsoluteServerPath(req, src); + + java.io.File file = new java.io.File(filePath); + if (file.exists()) { + file.delete(); + } + } + public static String getFileName(final Part part) { + for (String content : part.getHeader("content-disposition").split(";")) { + if (content.trim().startsWith("filename")) { + return content.substring( + content.indexOf('=') + 1).trim().replace("\"", ""); + } + } + return null; + } + + +} diff --git a/Lib/src/com/froala/editor/file/FileOptions.java b/Lib/src/com/froala/editor/file/FileOptions.java index 9437cf6..e2af01c 100644 --- a/Lib/src/com/froala/editor/file/FileOptions.java +++ b/Lib/src/com/froala/editor/file/FileOptions.java @@ -13,19 +13,19 @@ public class FileOptions { public static final String fieldnameDefault = "file"; /** - * Tag name that points to the file. + * Tag name that points to the File. */ private String fieldname; /** - * Get tag name that points to the file. + * Get tag name that points to the File. */ public String getFieldname() { return fieldname; } /** - * Set tag name that points to the file. + * Set tag name that points to the File. */ public void setFieldname(String fieldname) { this.fieldname = fieldname; @@ -37,35 +37,35 @@ public void setFieldname(String fieldname) { private FileValidation validation; /** - * Get file validation. + * Get File validation. */ public FileValidation getValidation() { return validation; } /** - * Set file validation. + * Set File validation. */ public void setValidation(FileValidation validation) { this.validation = validation; } /** - * Init default file upload settings. + * Init default File upload settings. */ protected void initDefault() { setValidation(new FileValidation()); } /** - * Constructor. Uses default options: - fieldname "file" - validation + * Constructor. Uses default options: - fieldname "File" - validation * default FileValidation. To change them, use getters and setters. */ public FileOptions() { - + // Set default fieldname. setFieldname(fieldnameDefault); - + // Init default settings. initDefault(); } diff --git a/Lib/src/com/froala/editor/video/VideoOptions.java b/Lib/src/com/froala/editor/video/VideoOptions.java new file mode 100644 index 0000000..4ed3429 --- /dev/null +++ b/Lib/src/com/froala/editor/video/VideoOptions.java @@ -0,0 +1,69 @@ +package com.froala.editor.video; + +import com.froala.editor.video.VideoValidation; + +public class VideoOptions { + + /** + * Default Froala Editor tag name that points to the file. + */ + public static final String fieldnameDefault = "file"; + + /** + * Tag name that points to the File. + */ + private String fieldname; + + /** + * Get tag name that points to the Video. + */ + public String getFieldname() { + return fieldname; + } + + /** + * Set tag name that points to the Video. + */ + public void setFieldname(String fieldname) { + this.fieldname = fieldname; + } + + /** + * Video validation. + */ + private VideoValidation validation; + + /** + * Get Video validation. + */ + public VideoValidation getValidation() { + return validation; + } + + /** + * Set Video validation. + */ + public void setValidation(VideoValidation validation) { + this.validation = validation; + } + + /** + * Init default Video upload settings. + */ + protected void initDefault() { + setValidation(new VideoValidation()); + } + + /** + * Constructor. Uses default options: - fieldname "Video" - validation + * default VideoValidation. To change them, use getters and setters. + */ + public VideoOptions() { + + // Set default fieldname. + setFieldname(fieldnameDefault); + + // Init default settings. + initDefault(); + } +} diff --git a/Lib/src/com/froala/editor/video/VideoValidation.java b/Lib/src/com/froala/editor/video/VideoValidation.java new file mode 100644 index 0000000..16203ca --- /dev/null +++ b/Lib/src/com/froala/editor/video/VideoValidation.java @@ -0,0 +1,105 @@ +package com.froala.editor.video; + +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang.ArrayUtils; + +import com.froala.editor.CustomValidation; + +public class VideoValidation { + + /** + * Allowed Video validation default extensions. + */ + public static final String[] allowedVideoExtsDefault = new String[] { "mp4", "webm", "ogg" }; + /** + * Allowed Video validation default mimetypes. + */ + public static final String[] allowedVideoMimeTypesDefault = new String[] { "video/mp4", "video/webm", "video/ogg" }; + + /** + * Allowed validation extensions. + */ + protected String[] allowedExts; + + /** + * Allowed validation mimetypes. + */ + protected String[] allowedMimeTypes; + + /** + * Custom Video validation. + */ + protected CustomValidation customValidation; + + /** + * Init default Video validation settings. + */ + protected void initDefault() { + + allowedExts = allowedVideoExtsDefault; + allowedMimeTypes = allowedVideoMimeTypesDefault; + } + + /** + * Constructor. Validates default Videos with: - allowed Video extensions: + * "mp4", "webm", "ogg" - allowed mime types: "text/plain", + * "video/mp4", "video/webm", "video/ogg" + */ + public VideoValidation() { + + initDefault(); + } + + /** + * Constructor. + * + * @param allowedExts + * Allowed validation Video extensions. + * @param allowedMimeTypes + * Allowed validation Video mimetypes. + */ + public VideoValidation(String[] allowedExts, String[] allowedMimeTypes) { + + initDefault(); + + if (allowedExts != null) { + this.allowedExts = allowedExts; + } + + if (allowedMimeTypes != null) { + this.allowedMimeTypes = allowedMimeTypes; + } + } + + /** + * Constructor. + * + * @param customValidation + * Custom validation. + */ + public VideoValidation(CustomValidation customValidation) { + + initDefault(); + this.customValidation = customValidation; + } + + /** + * Check if Video is valid. Use only the custom function if provided. Else + * check if the Video has an allowed extension and mimetype. + * + * @param VideoPath + * Video path. + * @param mimeType + * Video mimetype + * @return + */ + public boolean check(String VideoPath, String mimeType) { + + if (customValidation != null) { + return customValidation.validate(VideoPath, mimeType); + } + + return ArrayUtils.contains(allowedExts, FilenameUtils.getExtension(VideoPath)) + && ArrayUtils.contains(allowedMimeTypes, mimeType.toLowerCase()); + } +} diff --git a/WebContent/META-INF/MANIFEST.MF b/WebContent/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5e94951 --- /dev/null +++ b/WebContent/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: +