From c8e3258dc7b6bef545d1ac7673c3158feab032c2 Mon Sep 17 00:00:00 2001 From: Aakash Prakash Date: Sat, 23 Feb 2019 18:38:38 +0530 Subject: [PATCH] Implement video upload --- Examples/WebContent/index.html | 26 +++++- .../examples/servlets/DeleteVideo.class | Bin 0 -> 1830 bytes .../examples/servlets/UploadVideo.class | Bin 0 -> 2440 bytes .../froala/examples/servlets/DeleteVideo.java | 51 ++++++++++ .../froala/examples/servlets/UploadImage.java | 20 +++- .../froala/examples/servlets/UploadVideo.java | 70 ++++++++++++++ Lib/bin/com/froala/editor/Video.class | Bin 0 -> 1616 bytes .../froala/editor/video/VideoOptions.class | Bin 0 -> 538 bytes .../froala/editor/video/VideoValidation.class | Bin 0 -> 947 bytes Lib/src/com/froala/editor/Video.java | 87 ++++++++++++++++++ .../froala/editor/file/FileValidation.java | 2 +- .../com/froala/editor/video/VideoOptions.java | 28 ++++++ .../froala/editor/video/VideoValidation.java | 53 +++++++++++ 13 files changed, 331 insertions(+), 6 deletions(-) create mode 100644 Examples/build/classes/com/froala/examples/servlets/DeleteVideo.class create mode 100644 Examples/build/classes/com/froala/examples/servlets/UploadVideo.class create mode 100644 Examples/src/com/froala/examples/servlets/DeleteVideo.java create mode 100644 Examples/src/com/froala/examples/servlets/UploadVideo.java create mode 100644 Lib/bin/com/froala/editor/Video.class create mode 100644 Lib/bin/com/froala/editor/video/VideoOptions.class create mode 100644 Lib/bin/com/froala/editor/video/VideoValidation.class create mode 100644 Lib/src/com/froala/editor/Video.java create mode 100644 Lib/src/com/froala/editor/video/VideoOptions.java create mode 100644 Lib/src/com/froala/editor/video/VideoValidation.java diff --git a/Examples/WebContent/index.html b/Examples/WebContent/index.html index 5c52342..cdd2e6d 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" @@ -121,6 +125,25 @@

Sample 1: Save to disk

console.log ('file delete problem: ' + JSON.stringify(err)); }) }) + // Catch video removal from the editor. + .on('froalaEditor.video.removed', function (e, editor, $vid) { + $.ajax({ + // Request method. + method: "POST", + // Request URL. + url: "/delete_video", + // Request params. + data: { + src: $vid.attr('src') + } + }) + .done (function (data) { + console.log ('video was deleted'); + }) + .fail (function (err) { + console.log ('video delete problem: ' + JSON.stringify(err)); + }) + }) }); @@ -264,7 +287,8 @@

Sample 4: Save to Amazon using signature version 4

.done(function( data ) { $('#edit-amazon').froalaEditor({ imageUploadToS3: data, - fileUploadToS3: data + fileUploadToS3: data, + videoUploadToS3: data }) }); }); diff --git a/Examples/build/classes/com/froala/examples/servlets/DeleteVideo.class b/Examples/build/classes/com/froala/examples/servlets/DeleteVideo.class new file mode 100644 index 0000000000000000000000000000000000000000..4d44c12a39791bb30bfecdad08b65a4c5e8d964a GIT binary patch literal 1830 zcmb7EZC4vb6n-Y%upw+I4OFCW+FDA&+oDuOf@tNXrA+||1Z&kgY{sw{cH`~@@fZ1- zFRj#b`~m(R&+(i3%qDQ4_DIb+nL9gk@7#Nzd!PIJUoUUDC^Qy z`*OEYSFszbXs@nx?9M9+Rq?ATv|$Nc-;sNA|LES`*1F-|=TdGN2=vg@m-UK@Vm}Nv zmgWV-1A+5%VGwH>=!&d2Rhs_IPY86(_<^ry1=+MexR0{ zyIU$+ms@oukSm2A9Z0(2cl*%N+kPxCTzcJXigaBKSHoBftmRApoz|NAp{ZhBJX0-h zFill7nPeCCJ=M?zA-2)~FXY;Q9V^)w5a=~wxPIs^tsGxuaE0K+k=KQ5c+19hfx*{` z*1_AjA>h=MUX_vDWgR1d+pS#Px(sS=Sx0_QD;AQxP90_O4hC)966imPXw}zYsE!eQl?3Kbi71o}*#wJ@yJm0OF$z`e&Mi%E26@S%WRZhD@I zV{`J6gK3xs(%}Oty#(sY){gS@t3b`*V-iF*8g<{3rZe0fnwvB}aqubb3UtRxlPOvS zdi|#coBh(O%2ml=P9VLpzBo2ztQQ<+hu3Ftr}ok7sV_}Shn%#s1&EK9jq8n=^xE2L?vo5LtyAkZnP*Qkcm{Q9GpF8 z8np>!Sf6m?WZQDG7=%t-9Cr+h(_^q*YrC44T5<>_z4J&mlA&SaOQU5Ncj8qNw;VPj zPc8ap*z}wjfa6A2U|_8oXn&VOG4?rt?gl}qlWKDQ48D|*Jic@{)mEGH3@*KA#vmhd zTl9IlXC^FefK)eg5torh2c^xg6f*oXFm!Qscrr;TDAU8wasD7txxh6`HDCZ1&3m|W zT1vQx9KS5|qK_JvxbL8R18*je{mH!<$fc3oZi7u@_Aebd%}^$hvZi{g(% zjHPh!D<&U5$NRYto?&X*8qLidVm5`*+-HZFr#!?x!b6jX2lU*DZh9^-Arsp;g9mQ$ zbcpA9#!{gFVWvESn;63cHKyQ_1`~Klo|^b)87Jdv$8UD4_&mv}gf)~IcPHj>1?%+D zOD`L!Fv|&C#U{@zd_n60r2ggumJMO!nvHQAkIlBwL-Qw8NZ~7dP1((dzv2Js6Nnh! F{sZJS-sAuP literal 0 HcmV?d00001 diff --git a/Examples/build/classes/com/froala/examples/servlets/UploadVideo.class b/Examples/build/classes/com/froala/examples/servlets/UploadVideo.class new file mode 100644 index 0000000000000000000000000000000000000000..fccb9ec2ac5b3788fcef4c79324d47241bbddcbd GIT binary patch literal 2440 zcmb7FZBr9h6n-vjvKSUrL~XQEU#bR0SFv_zqO`t%Kv5|#L94A@l1o@zvg_^*V1GgX ziJ$#~rOvc7?YI71P0+;CmAt0)>jdW-o`n z<2kn6bk+h-Ms_5_bx*3uUI;wjsg~WU^i3E71FO!uv)R12SCk6u2`+Pqg1|nix{gpvloz2??kH7d|q7%U4IxBRNip%c9pa_AP= zJMGrxy~f&-4CkFCPYU!+`xQEH=!Wm#9HFM@nF}F#YeaX63Vn zj8th?u_z!+S<*;yW1AHjC?+8?arhsZOB1#uWFjwcK!;(wzCC$==b{To1-k5@vE;cG zJBMTV$i#ra;Z~$oSNWkGm&w9$oFKSHTseWM{{_zWoO;ckQ=wb0l?G$Hc0;+Ieb)&} zIh?|26CXDNjb(QNwQwdbSU_Dhr<}^u`Ov9I69s|&F|gRsgcGgMSPo~=+lBK285xFt zsBK@ca1l1qDnE%ppudm?(Hd7bF5`-cVbbcYUD~ur>Z@c@&|pzV65I)(`%9~`lGd;7 zq}6&Vl#=tn4>^2>l8GWo9lMD`NQ5k0!*#NC!*S`#C`=8OO?=L9bx~@*U-P70i~PEM zhf5Bl=+({$p5sZFhdH z#ngM-Vw47Z;vQyO#db`Pucvn-TAWrBL0iitSusIEBm*u+Jiik98_7h^&H0V6BFC9j zDz2_|JilZmt$juJ9@N z%$$3Lo|m!6UamQcf%DtI0QPZbB|_MbKE4?^fPP9G25 zIg9`iV~VgIKMiPn<3s`!C_O}Zn`f7Z=rR!x^W-Ym5fnL7C%Dd_ga;g=6qHfjaUR2?bhnrLWz<-v2^^*M z3Uj)`w`I88tqK-H-9Z-xDtZ)Z&C9v@bA|jgSz~AU=gVX>3 literal 0 HcmV?d00001 diff --git a/Examples/src/com/froala/examples/servlets/DeleteVideo.java b/Examples/src/com/froala/examples/servlets/DeleteVideo.java new file mode 100644 index 0000000..7bd5052 --- /dev/null +++ b/Examples/src/com/froala/examples/servlets/DeleteVideo.java @@ -0,0 +1,51 @@ +package com.froala.examples.servlets; + +import java.io.IOException; + +import javax.servlet.ServletException; +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 DeleteVideo + */ +@WebServlet("/delete_video") +public class DeleteVideo extends HttpServlet { + private static final long serialVersionUID = 1L; + + /** + * @see HttpServlet#HttpServlet() + */ + public DeleteVideo() { + super(); + } + + /** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse + * response) + */ + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + String src = request.getParameter("src"); + + try { + Video.delete(request, src); + } catch (Exception e) { + e.printStackTrace(); + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + return; + } + String jsonResponseData = new Gson().toJson("Success"); + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + response.getWriter().write(jsonResponseData); + } + +} diff --git a/Examples/src/com/froala/examples/servlets/UploadImage.java b/Examples/src/com/froala/examples/servlets/UploadImage.java index faac973..74a9176 100644 --- a/Examples/src/com/froala/examples/servlets/UploadImage.java +++ b/Examples/src/com/froala/examples/servlets/UploadImage.java @@ -49,10 +49,22 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) responseData = new HashMap(); responseData.put("error", e.toString()); } - String jsonResponseData = new Gson().toJson(responseData); - response.setContentType("application/json"); - response.setCharacterEncoding("UTF-8"); - response.getWriter().write(jsonResponseData); + // Wait for 5 secs for image upload + synchronized (responseData) { + try + { + responseData.wait(5000); + String jsonResponseData = new Gson().toJson(responseData); + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + response.getWriter().write(jsonResponseData); + } + catch ( InterruptedException e ) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } } } 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..ad9e345 --- /dev/null +++ b/Examples/src/com/froala/examples/servlets/UploadVideo.java @@ -0,0 +1,70 @@ +package com.froala.examples.servlets; + +import java.io.IOException; +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 UploadVideo + */ +@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 = "/public/"; + + Map responseData; + try { + responseData = Video.upload(request, fileRoute); // Use default + // video + // validation. + } catch (Exception e) { + e.printStackTrace(); + responseData = new HashMap(); + responseData.put("error", e.toString()); + } + // Wait for 5 secs for video upload + synchronized (responseData) { + try + { + responseData.wait(5000); + String jsonResponseData = new Gson().toJson(responseData); + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + response.getWriter().write(jsonResponseData); + } + catch ( InterruptedException e ) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + +} diff --git a/Lib/bin/com/froala/editor/Video.class b/Lib/bin/com/froala/editor/Video.class new file mode 100644 index 0000000000000000000000000000000000000000..b5c4b22010850586d3445559ed2dd32cbba12833 GIT binary patch literal 1616 zcmb_cTTc@~6#k}{U06!1REyvRR4lz9EBe^l7sU`$B%#p6$7MS$OP1ZTohAGde}|U` zh$g<8_@j(xW=l(KA4uh)(>-&}eCIpgnKQrteE$hx121*N80KyN*lY*BRj zrqklSjs(N4BkR;MUCZm3I|oPH77XbYZ(E@&cDlmxy&glpK8kQE5h9#^T!mq(YP*i- zh#Et@P;4?J-uNx9V}@bnIuHg7%%+gW6oXb(NYfyWDI}pWWa^H`KZM5zJlL}iT+T38 z_if85RAHcm2!WlRxwD^M=9j6^k2!xNIT|Mj4Bil_zA=6H8m%#+K$V2{ZOce59RE) zfZ@~R0!~!JwPntupktLGH+uQsQBAT1MF#Ii=}WOpiGDejqYcpg%gNSSobEg6TAiFj zv1uTW^%Tljqq*+|HVr|;4+EP^8f2(}TbGTaH;F>%ZKBDPGfiJfOhKm~^_i?PVzdG? zKVW=3N9r3gU&xH%Hmw>30gISZJ93v^ivGb@^e?^1(mArjBZk7uB7wW=SSkv_9kM|g zbC@SS_vkx8o(0@j&<_-s*W{6Grc33o$XwvzXXP9+DJsZZ6oh3wCcgxIf~N!~!I#E> zuPE?+@<<^vYrmiq{C26lc7bQZ6lvvtOT0Fz$Ssv>IXW^(mFcG<7?en!7(r7PRC1t3 hw&bss$`^S4uO@q>{Qi{!v;nFHQx(AQf=S6ot=i(xxVdlxv`+&1Nczl z9Sv=;GH@^Fa^|}~pI>hu08X&wphVd3=|R{}wMaxLW0mPNoTympFf#G>Ia7K#a$pno z7M*>S$ncs&?jv|=sIn{B7vm%&Sk0q|U|;H3I#?ncE+PueB9W+An1(K@@O=2F5Gv;e zdEtVv?4ynqLTx0oxtY*vb{4_2+2+o+nWo-RLwPeEbY*%kx``w-I=Uy4NTkZ>yl7{S zYD75rhg=vr-@EO?f=6)g^f>KFa}h#Nyx{SZmtnS=z((W z4zC^Q=z;XLJ28+D*u9xN)Q+;>(K1gJSeK6DUaCQ$@%F2K{zeVu)bRyMpITq!g|r$)05E)fy!e3i5{!dZ)a*wGLQR?o@oE2K)hD(2qa#+17*MxC@ia)$Whk#?74{o zC0?rD*hG;acQi^vVAVnrDHCe~>2S~>_ntr|3`55K*>S|HSTK+h$hY)FeVC4W$~%=k zM+p>LZeKbb>1mE5Fw6Z$Q*+sE3y+wdjV5JN7~iq5%XlHeXTwwP{%3tIP;cEdJ?u1a zcWyiRm^uz@${>!0g&NXCuifJKEl|C~tOI5>$&0o-_4|g8=0M4`=eP=v;6{;|EGt*rILY3XkUkxuDAm^|px|NwA0&Qm}_AMS`7* zx;>(u<&uqS)aN2$7kgB>x*(8T5NJjM@2MQyVl upload(HttpServletRequest req, String fileRoute) throws Exception { + + return upload(req, fileRoute, defaultOptions); + } + + /** + * Uploads a video to disk. + * + * @param req + * Servlet HTTP request. + * @param fileRoute + * Server route where the file 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 { + + if (options == null) { + options = defaultOptions; + } + + return File.upload(req, fileRoute, options); + } + + /** + * Delete a video from disk. + * + * @param req + * Used to get the servlet context. + * @param src + * Server file path. + */ + public static void delete(HttpServletRequest req, String src) { + File.delete(req, src); + } + +} diff --git a/Lib/src/com/froala/editor/file/FileValidation.java b/Lib/src/com/froala/editor/file/FileValidation.java index 2836b73..61ea6e1 100644 --- a/Lib/src/com/froala/editor/file/FileValidation.java +++ b/Lib/src/com/froala/editor/file/FileValidation.java @@ -106,7 +106,7 @@ public boolean check(String filePath, String mimeType) { return customValidation.validate(filePath, mimeType); } - return ArrayUtils.contains(allowedExts, FilenameUtils.getExtension(filePath)) + return ArrayUtils.contains(allowedExts, FilenameUtils.getExtension(filePath).toLowerCase()) && ArrayUtils.contains(allowedMimeTypes, mimeType.toLowerCase()); } } 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..2bac459 --- /dev/null +++ b/Lib/src/com/froala/editor/video/VideoOptions.java @@ -0,0 +1,28 @@ +package com.froala.editor.video; + +import com.froala.editor.file.FileOptions; + +/** + * Video Options used for uploading. + * + * @author florin@froala.com + */ +public class VideoOptions extends FileOptions { + + /** + * Init default video upload settings. + */ + @Override + protected void initDefault() { + setValidation(new VideoValidation()); + } + + /** + * Constructor. Uses default options: - fieldname "file" - validation + * default VideoValidation. To change them, use getters and setters. + */ + public VideoOptions() { + super(); + } + +} 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..1899b80 --- /dev/null +++ b/Lib/src/com/froala/editor/video/VideoValidation.java @@ -0,0 +1,53 @@ +package com.froala.editor.video; + +import com.froala.editor.file.FileValidation; + +/** + * Video Validation. + * + * @author florin@froala.com + */ +public class VideoValidation extends FileValidation { + + /** + * 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" }; + + /** + * Init default video validation settings. + */ + @Override + protected void initDefault() { + + allowedExts = allowedVideoExtsDefault; + allowedMimeTypes = allowedVideoMimeTypesDefault; + } + + /** + * Constructor. Validates default videos with: - allowed file extensions: + * ".mp4", ".webm", ".ogg" - allowed mime types: + * "video/mp4", "video/webm", "video/ogg" + */ + public VideoValidation() { + super(); + } + + /** + * Constructor. + * + * @param allowedExts + * Allowed validation video extensions. + * @param allowedMimeTypes + * Allowed validation video mimetypes. + */ + public VideoValidation(String[] allowedExts, String[] allowedMimeTypes) { + super(allowedExts, allowedMimeTypes); + } + +}