-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from akki744/video-upload
Implement video upload
- Loading branch information
Showing
13 changed files
with
331 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+1.79 KB
Examples/build/classes/com/froala/examples/servlets/DeleteVideo.class
Binary file not shown.
Binary file added
BIN
+2.38 KB
Examples/build/classes/com/froala/examples/servlets/UploadVideo.class
Binary file not shown.
51 changes: 51 additions & 0 deletions
51
Examples/src/com/froala/examples/servlets/DeleteVideo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
Examples/src/com/froala/examples/servlets/UploadVideo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |