Skip to content

Commit 345a944

Browse files
Merge pull request #2 from akki744/video-upload
Implement video upload
2 parents 6f149a9 + c8e3258 commit 345a944

File tree

13 files changed

+331
-6
lines changed

13 files changed

+331
-6
lines changed

Examples/WebContent/index.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ <h2>Sample 1: Save to disk</h2>
7979
fileUploadParams: {
8080
id: 'my_editor'
8181
},
82+
videoUploadURL: '/upload_video',
83+
videoUploadParams: {
84+
id: 'my_editor'
85+
},
8286
imageManagerLoadURL: '/load_images',
8387
imageManagerDeleteURL: "/delete_image",
8488
imageManagerDeleteMethod: "POST"
@@ -121,6 +125,25 @@ <h2>Sample 1: Save to disk</h2>
121125
console.log ('file delete problem: ' + JSON.stringify(err));
122126
})
123127
})
128+
// Catch video removal from the editor.
129+
.on('froalaEditor.video.removed', function (e, editor, $vid) {
130+
$.ajax({
131+
// Request method.
132+
method: "POST",
133+
// Request URL.
134+
url: "/delete_video",
135+
// Request params.
136+
data: {
137+
src: $vid.attr('src')
138+
}
139+
})
140+
.done (function (data) {
141+
console.log ('video was deleted');
142+
})
143+
.fail (function (err) {
144+
console.log ('video delete problem: ' + JSON.stringify(err));
145+
})
146+
})
124147
});
125148
</script>
126149

@@ -264,7 +287,8 @@ <h2>Sample 4: Save to Amazon using signature version 4</h2>
264287
.done(function( data ) {
265288
$('#edit-amazon').froalaEditor({
266289
imageUploadToS3: data,
267-
fileUploadToS3: data
290+
fileUploadToS3: data,
291+
videoUploadToS3: data
268292
})
269293
});
270294
});
Binary file not shown.
Binary file not shown.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.froala.examples.servlets;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
import com.froala.editor.Video;
12+
import com.google.gson.Gson;
13+
14+
/**
15+
* Servlet implementation class DeleteVideo
16+
*/
17+
@WebServlet("/delete_video")
18+
public class DeleteVideo extends HttpServlet {
19+
private static final long serialVersionUID = 1L;
20+
21+
/**
22+
* @see HttpServlet#HttpServlet()
23+
*/
24+
public DeleteVideo() {
25+
super();
26+
}
27+
28+
/**
29+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
30+
* response)
31+
*/
32+
@Override
33+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
34+
throws ServletException, IOException {
35+
36+
String src = request.getParameter("src");
37+
38+
try {
39+
Video.delete(request, src);
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
43+
return;
44+
}
45+
String jsonResponseData = new Gson().toJson("Success");
46+
response.setContentType("application/json");
47+
response.setCharacterEncoding("UTF-8");
48+
response.getWriter().write(jsonResponseData);
49+
}
50+
51+
}

Examples/src/com/froala/examples/servlets/UploadImage.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,22 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
4949
responseData = new HashMap<Object, Object>();
5050
responseData.put("error", e.toString());
5151
}
52-
String jsonResponseData = new Gson().toJson(responseData);
53-
response.setContentType("application/json");
54-
response.setCharacterEncoding("UTF-8");
55-
response.getWriter().write(jsonResponseData);
52+
// Wait for 5 secs for image upload
53+
synchronized (responseData) {
54+
try
55+
{
56+
responseData.wait(5000);
57+
String jsonResponseData = new Gson().toJson(responseData);
58+
response.setContentType("application/json");
59+
response.setCharacterEncoding("UTF-8");
60+
response.getWriter().write(jsonResponseData);
61+
}
62+
catch ( InterruptedException e )
63+
{
64+
// TODO Auto-generated catch block
65+
e.printStackTrace();
66+
}
67+
}
5668
}
5769

5870
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.froala.examples.servlets;
2+
3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import javax.servlet.ServletException;
8+
import javax.servlet.annotation.MultipartConfig;
9+
import javax.servlet.annotation.WebServlet;
10+
import javax.servlet.http.HttpServlet;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
import com.froala.editor.Video;
15+
import com.google.gson.Gson;
16+
17+
/**
18+
* Servlet implementation class UploadVideo
19+
*/
20+
@WebServlet("/upload_video")
21+
@MultipartConfig
22+
public class UploadVideo extends HttpServlet {
23+
private static final long serialVersionUID = 1L;
24+
25+
/**
26+
* @see HttpServlet#HttpServlet()
27+
*/
28+
public UploadVideo() {
29+
super();
30+
}
31+
32+
/**
33+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
34+
* response)
35+
*/
36+
@Override
37+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
38+
throws ServletException, IOException {
39+
40+
String fileRoute = "/public/";
41+
42+
Map<Object, Object> responseData;
43+
try {
44+
responseData = Video.upload(request, fileRoute); // Use default
45+
// video
46+
// validation.
47+
} catch (Exception e) {
48+
e.printStackTrace();
49+
responseData = new HashMap<Object, Object>();
50+
responseData.put("error", e.toString());
51+
}
52+
// Wait for 5 secs for video upload
53+
synchronized (responseData) {
54+
try
55+
{
56+
responseData.wait(5000);
57+
String jsonResponseData = new Gson().toJson(responseData);
58+
response.setContentType("application/json");
59+
response.setCharacterEncoding("UTF-8");
60+
response.getWriter().write(jsonResponseData);
61+
}
62+
catch ( InterruptedException e )
63+
{
64+
// TODO Auto-generated catch block
65+
e.printStackTrace();
66+
}
67+
}
68+
}
69+
70+
}

Lib/bin/com/froala/editor/Video.class

1.58 KB
Binary file not shown.
538 Bytes
Binary file not shown.
Binary file not shown.

Lib/src/com/froala/editor/Video.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.froala.editor;
2+
3+
import java.util.Map;
4+
5+
import javax.servlet.http.HttpServletRequest;
6+
7+
import com.froala.editor.video.VideoOptions;
8+
9+
/**
10+
* Video functionality.
11+
*
12+
13+
*/
14+
public final class Video {
15+
16+
/**
17+
* Private constructor.
18+
*/
19+
private Video() {
20+
21+
}
22+
23+
/**
24+
* File default options.
25+
*/
26+
public static final VideoOptions defaultOptions = new VideoOptions();
27+
28+
/**
29+
* Uploads a video to disk.
30+
*
31+
* @param req
32+
* Servlet HTTP request.
33+
* @param fileRoute
34+
* Route Server route where the file will be uploaded. This route
35+
* must be public to be accesed by the editor.
36+
* @return Object with link.
37+
* @throws Exception
38+
*/
39+
public static Map<Object, Object> upload(HttpServletRequest req, String fileRoute) throws Exception {
40+
41+
return upload(req, fileRoute, defaultOptions);
42+
}
43+
44+
/**
45+
* Uploads a video to disk.
46+
*
47+
* @param req
48+
* Servlet HTTP request.
49+
* @param fileRoute
50+
* Server route where the file will be uploaded. This route must
51+
* be public to be accesed by the editor.
52+
* @param options
53+
* Video options. Defaults to {@link #defaultOptions} which has
54+
* </br>
55+
* Fieldname: "file" </br>
56+
* Validation:
57+
* <ul>
58+
* <li>Extensions: ".mp4", ".webm", ".ogg"</li>
59+
* <li>Mime Types: "video/mp4", "video/webm", "video/ogg"</li>
60+
* </ul>
61+
*
62+
* @return Object with link.
63+
* @throws Exception
64+
*/
65+
public static Map<Object, Object> upload(HttpServletRequest req, String fileRoute, VideoOptions options)
66+
throws Exception {
67+
68+
if (options == null) {
69+
options = defaultOptions;
70+
}
71+
72+
return File.upload(req, fileRoute, options);
73+
}
74+
75+
/**
76+
* Delete a video from disk.
77+
*
78+
* @param req
79+
* Used to get the servlet context.
80+
* @param src
81+
* Server file path.
82+
*/
83+
public static void delete(HttpServletRequest req, String src) {
84+
File.delete(req, src);
85+
}
86+
87+
}

Lib/src/com/froala/editor/file/FileValidation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public boolean check(String filePath, String mimeType) {
106106
return customValidation.validate(filePath, mimeType);
107107
}
108108

109-
return ArrayUtils.contains(allowedExts, FilenameUtils.getExtension(filePath))
109+
return ArrayUtils.contains(allowedExts, FilenameUtils.getExtension(filePath).toLowerCase())
110110
&& ArrayUtils.contains(allowedMimeTypes, mimeType.toLowerCase());
111111
}
112112
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.froala.editor.video;
2+
3+
import com.froala.editor.file.FileOptions;
4+
5+
/**
6+
* Video Options used for uploading.
7+
*
8+
9+
*/
10+
public class VideoOptions extends FileOptions {
11+
12+
/**
13+
* Init default video upload settings.
14+
*/
15+
@Override
16+
protected void initDefault() {
17+
setValidation(new VideoValidation());
18+
}
19+
20+
/**
21+
* Constructor. Uses default options: - fieldname "file" - validation
22+
* default VideoValidation. To change them, use getters and setters.
23+
*/
24+
public VideoOptions() {
25+
super();
26+
}
27+
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.froala.editor.video;
2+
3+
import com.froala.editor.file.FileValidation;
4+
5+
/**
6+
* Video Validation.
7+
*
8+
9+
*/
10+
public class VideoValidation extends FileValidation {
11+
12+
/**
13+
* Allowed video validation default extensions.
14+
*/
15+
public static final String[] allowedVideoExtsDefault = new String[] { "mp4", "webm", "ogg" };
16+
17+
/**
18+
* Allowed video validation default mimetypes.
19+
*/
20+
public static final String[] allowedVideoMimeTypesDefault = new String[] { "video/mp4", "video/webm", "video/ogg" };
21+
22+
/**
23+
* Init default video validation settings.
24+
*/
25+
@Override
26+
protected void initDefault() {
27+
28+
allowedExts = allowedVideoExtsDefault;
29+
allowedMimeTypes = allowedVideoMimeTypesDefault;
30+
}
31+
32+
/**
33+
* Constructor. Validates default videos with: - allowed file extensions:
34+
* ".mp4", ".webm", ".ogg" - allowed mime types:
35+
* "video/mp4", "video/webm", "video/ogg"
36+
*/
37+
public VideoValidation() {
38+
super();
39+
}
40+
41+
/**
42+
* Constructor.
43+
*
44+
* @param allowedExts
45+
* Allowed validation video extensions.
46+
* @param allowedMimeTypes
47+
* Allowed validation video mimetypes.
48+
*/
49+
public VideoValidation(String[] allowedExts, String[] allowedMimeTypes) {
50+
super(allowedExts, allowedMimeTypes);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)