package froala.editor;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import egovframework.rte.fdl.cmmn.exception.EgovBizException;
import froala.editor.video.VideoOptions;
/**
* Video functionality.
*
* @author florin@froala.com
*/
public final class Video {
public static final String multipartContentType = "multipart/form-data";
/**
* 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 EgovBizException
* @throws IOException
* @throws NoSuchAlgorithmException
*/
public static EditorFileVO upload (HttpServletRequest req, String fileRoute) throws EgovBizException, NoSuchAlgorithmException, IOException {
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 NoSuchAlgorithmException
* @throws EgovBizException
* @throws IOException
*/
public static EditorFileVO upload (HttpServletRequest req, String fileRoute, VideoOptions options) throws EgovBizException, NoSuchAlgorithmException, IOException {
if (options == null) {
options = defaultOptions;
}
if (req.getContentType () == null || req.getContentType ().toLowerCase ().indexOf (multipartContentType) == -1) {
throw new EgovBizException ("Invalid contentType. It must be " + multipartContentType);
}
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) req;
MultipartFile file = multipartHttpServletRequest.getFile (options.getFieldname ());
// servlet 3.0
//Part filePart = req.getPart(options.getFieldname());
if (file == null) {
throw new EgovBizException ("Fieldname is not correct. It must be: " + options.getFieldname ());
}
// Generate random name.
String file_extsn = FilenameUtils.getExtension (Utils.getFileName (file));
String extension = file_extsn;
extension = (extension != null && extension != "") ? "." + extension : extension;
String name = Utils.generateUniqueString () + extension;
String linkName = fileRoute + name;
InputStream fileContent = file.getInputStream ();
String absoluteServerPath = getAbsoluteServerPath (req, linkName);
// String absoluteServerPath = linkName;
java.io.File targetFile = null;
if (absoluteServerPath != null && !absoluteServerPath.equals("")) {
targetFile = new java.io.File(absoluteServerPath);
FileUtils.copyInputStreamToFile (fileContent, targetFile);
} else {
return null;
}
if (options.getValidation () != null && !options.getValidation ().check (absoluteServerPath, file.getContentType ())) {
delete (req, linkName);
throw new EgovBizException ("File does not meet the validation.");
}
// 정보 셋팅
EditorFileVO fileVO = new EditorFileVO ();
fileVO.setLink (options.getStreamingUrl ());
fileVO.setFullPath (linkName);
fileVO.setFileExtsn (file_extsn);
fileVO.setOriginFileNm (file.getOriginalFilename ());
fileVO.setSysFileNm (name);
fileVO.setUploadDir (fileRoute);
fileVO.setFileSize (file.getSize ());
fileVO.setMime (file.getContentType ());
return fileVO;
}
/**
* 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);
return relativePath;
}
/**
* 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);
}
}