Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

1. Uploading images, video clips and other files

Code Block
languagejava
themeEmacs
titleupload
linenumberstrue
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
	static String ROOT_PATH = "C:\\workspace\\synapEditorsample\\out\\artifacts\\synapEditorsample_war_exploded";
	static String IMAGE_UPLOAD_DIR = "\\uploads";
    String fileName = file.getOriginalFilename();
    String ext = fileName.substring(fileName.lastIndexOf('.'));
    String saveFileName = UUID.randomUUID().toString() + ext;

    File uploadDir = new File(ROOT_PATH + IMAGE_UPLOAD_DIR);
    if(!uploadDir.exists()) {
        uploadDir.mkdirs();
    }

    byte[] bytes = file.getBytes();
    Path path = Paths.get(ROOT_PATH + IMAGE_UPLOAD_DIR + File.separator + saveFileName);
    Files.write(path, bytes);

    Map<String, Object> map = new HashMap<>();
    map.put("uploadPath", "uploads/" + saveFileName);

    return map;
}

2. Importing MS Word / LibreOffice documents

Code Block
languagejava
themeEmacs
titleimport
linenumberstrue
@RequestMapping(value = "/importDoc", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> importDoc(@RequestParam("file") MultipartFile importFile) throws IOException {
	static String ROOT_PATH = "C:\\workspace\\synapEditorsample\\out\\artifacts\\synapEditorsample_war_exploded";
	static String DOC_UPLOAD_DIR = "\\uploads\\docs";
	static String WORKS_DIR = "\\uploads\\tmp";
    String fileName = importFile.getOriginalFilename();

    File uploadDir = new File(DOC_UPLOAD_DIR);
    if(!uploadDir.exists()) {
        uploadDir.mkdirs();
    }

    byte[] bytes = new byte[0];
    try {
        bytes = importFile.getBytes();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Path path = Paths.get(DOC_UPLOAD_DIR + "\\" + fileName);

    try {
        Files.write(path, bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }

    File worksDir = new File(ROOT_PATH + WORKS_DIR);
    if(!worksDir.exists()) {
        worksDir.mkdirs();
    }

    executeConverter(DOC_UPLOAD_DIR + File.separator + fileName, ROOT_PATH + WORKS_DIR);
    Integer[] serializedData = serializePbData(WORKS_DIR + "\\document.word.pb");

    Map<String, Object> map = new HashMap<>();
    map.put("serializedData", serializedData);
    map.put("importPath", WORKS_DIR);

    return map;
}
 
public static void executeConverter(String inputFilePath, String outputFilePath) {
    String SEDOC_CONVERT_DIR = "C:\\sedocConverter\\sedocConverter.exe";
    String FONT_DIR = "C:\\sedocConverter\\sedocConverter\\fonts";
    String TEMP_DIR = "C:\\sedocConverter\\sedocConverter\\tmp";

    File tempDir = new File(TEMP_DIR);
    if(!tempDir.exists()) {
        tempDir.mkdirs();
    }

    File fontDir = new File(FONT_DIR);
    if(!fontDir.exists()) {
        fontDir.mkdirs();
    }

    Runtime rt = Runtime.getRuntime();
    Process process;

    try {
        process = rt.exec(new String[]{SEDOC_CONVERT_DIR, "-f", FONT_DIR, inputFilePath, outputFilePath, TEMP_DIR});
        process.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public Integer[] serializePbData(String pbFilePath) throws IOException {
    List<Integer> serializedData = new ArrayList<Integer>();
    FileInputStream fis = new FileInputStream(pbFilePath);

    Integer[] data = null;

    fis.skip(16);

    InflaterInputStream ifis = new InflaterInputStream(fis);

    byte[] buffer = new byte[1024];

    int len = -1;
    while ((len = ifis.read(buffer)) != -1) {
        for (int i = 0; i < len; i++) {
            serializedData.add(buffer[i] & 0xFF);
        }
    }

    data = serializedData.toArray(new Integer[serializedData.size()]);

    ifis.close();
    fis.close();

    return data;
}

...