Skip to content

Commit

Permalink
Merge pull request #2 from akki744/video-upload
Browse files Browse the repository at this point in the history
Implement video upload
  • Loading branch information
dheerajaccolite authored Feb 23, 2019
2 parents 6f149a9 + c8e3258 commit 345a944
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 6 deletions.
26 changes: 25 additions & 1 deletion Examples/WebContent/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ <h2>Sample 1: Save to disk</h2>
fileUploadParams: {
id: 'my_editor'
},
videoUploadURL: '/upload_video',
videoUploadParams: {
id: 'my_editor'
},
imageManagerLoadURL: '/load_images',
imageManagerDeleteURL: "/delete_image",
imageManagerDeleteMethod: "POST"
Expand Down Expand Up @@ -121,6 +125,25 @@ <h2>Sample 1: Save to disk</h2>
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));
})
})
});
</script>

Expand Down Expand Up @@ -264,7 +287,8 @@ <h2>Sample 4: Save to Amazon using signature version 4</h2>
.done(function( data ) {
$('#edit-amazon').froalaEditor({
imageUploadToS3: data,
fileUploadToS3: data
fileUploadToS3: data,
videoUploadToS3: data
})
});
});
Expand Down
Binary file not shown.
Binary file not shown.
51 changes: 51 additions & 0 deletions Examples/src/com/froala/examples/servlets/DeleteVideo.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
20 changes: 16 additions & 4 deletions Examples/src/com/froala/examples/servlets/UploadImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,22 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
responseData = new HashMap<Object, Object>();
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();
}
}
}

}
70 changes: 70 additions & 0 deletions Examples/src/com/froala/examples/servlets/UploadVideo.java
Original file line number Diff line number Diff line change
@@ -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<Object, Object> responseData;
try {
responseData = Video.upload(request, fileRoute); // Use default
// video
// validation.
} catch (Exception e) {
e.printStackTrace();
responseData = new HashMap<Object, Object>();
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();
}
}
}

}
Binary file added Lib/bin/com/froala/editor/Video.class
Binary file not shown.
Binary file added Lib/bin/com/froala/editor/video/VideoOptions.class
Binary file not shown.
Binary file not shown.
87 changes: 87 additions & 0 deletions Lib/src/com/froala/editor/Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.froala.editor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.froala.editor.video.VideoOptions;

/**
* Video functionality.
*
* @author [email protected]
*/
public final class Video {

/**
* Private constructor.
*/
private Video() {

}

/**
* File default options.
*/
public static final VideoOptions defaultOptions = new VideoOptions();

/**
* Uploads a video to disk.
*
* @param req
* Servlet HTTP request.
* @param fileRoute
* Route Server route where the file will be uploaded. This route
* must be public to be accesed by the editor.
* @return Object with link.
* @throws Exception
*/
public static Map<Object, Object> 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
* </br>
* Fieldname: "file" </br>
* Validation:
* <ul>
* <li>Extensions: ".mp4", ".webm", ".ogg"</li>
* <li>Mime Types: "video/mp4", "video/webm", "video/ogg"</li>
* </ul>
*
* @return Object with link.
* @throws Exception
*/
public static Map<Object, Object> 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);
}

}
2 changes: 1 addition & 1 deletion Lib/src/com/froala/editor/file/FileValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
28 changes: 28 additions & 0 deletions Lib/src/com/froala/editor/video/VideoOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.froala.editor.video;

import com.froala.editor.file.FileOptions;

/**
* Video Options used for uploading.
*
* @author [email protected]
*/
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();
}

}
53 changes: 53 additions & 0 deletions Lib/src/com/froala/editor/video/VideoValidation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.froala.editor.video;

import com.froala.editor.file.FileValidation;

/**
* Video Validation.
*
* @author [email protected]
*/
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);
}

}

0 comments on commit 345a944

Please sign in to comment.