package com.angkorteam.mbaas.server.bean;

import org.jooq.DSLContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.UUID;

public class System {

    private final DSLContext context;
    private final JdbcTemplate jdbcTemplate;

    public System(DSLContext context, JdbcTemplate jdbcTemplate) {
        this.context = context;
        this.jdbcTemplate = jdbcTemplate;
    }

    public synchronized String randomUUID() {
        return UUID.randomUUID().toString();
    }

}
@@ -36,16 +36,496 @@
 .bean;%0A%0A
+import com.angkorteam.mbaas.configuration.Constants;%0Aimport com.angkorteam.mbaas.model.entity.Tables;%0Aimport com.angkorteam.mbaas.model.entity.tables.FileTable;%0Aimport com.angkorteam.mbaas.model.entity.tables.records.FileRecord;%0Aimport org.apache.commons.configuration.XMLPropertiesConfiguration;%0Aimport org.apache.commons.io.FileUtils;%0Aimport org.apache.commons.io.FilenameUtils;%0Aimport org.apache.commons.lang3.StringUtils;%0Aimport org.apache.commons.lang3.time.DateFormatUtils;%0A
 import o
@@ -544,16 +544,16 @@
 ontext;%0A
-
 import o
@@ -596,16 +596,60 @@
 plate;%0A%0A
+import java.io.File;%0Aimport java.util.Date;%0A
 import j
@@ -721,24 +721,25 @@
 xt context;%0A
+%0A
     private 
@@ -962,24 +962,24 @@
 domUUID() %7B%0A
-
         retu
@@ -1018,9 +1018,2107 @@
 %0A    %7D%0A%0A
+    public String saveFile(File file) %7B%0A        XMLPropertiesConfiguration configuration = Constants.getXmlPropertiesConfiguration();%0A%0A        String patternFolder = configuration.getString(Constants.PATTERN_FOLDER);%0A        String repo = configuration.getString(Constants.RESOURCE_REPO);%0A        String fileRepo = DateFormatUtils.format(new Date(), patternFolder);%0A        File container = new File(repo, fileRepo);%0A        String extension = StringUtils.lowerCase(FilenameUtils.getExtension(file.getName()));%0A        String fileId = randomUUID();%0A        String name = fileId + %22.%22 + extension;%0A        container.mkdirs();%0A        try %7B%0A            FileUtils.copyFile(file, new File(container, name));%0A        %7D catch (Exception e) %7B%0A        %7D%0A%0A        long length = file.length();%0A        String path = fileRepo;%0A        String mime = parseMimeType(file.getName());%0A        String label = file.getName();%0A%0A        FileTable fileTable = Tables.FILE.as(%22fileTable%22);%0A%0A        FileRecord fileRecord = context.newRecord(fileTable);%0A        fileRecord.setFileId(fileId);%0A        fileRecord.setPath(path);%0A        fileRecord.setMime(mime);%0A        fileRecord.setExtension(extension);%0A        fileRecord.setLength((int) length);%0A        fileRecord.setLabel(label);%0A        fileRecord.setName(name);%0A        fileRecord.setDateCreated(new Date());%0A        fileRecord.store();%0A        return fileId;%0A    %7D%0A%0A    public String parseMimeType(String filename) %7B%0A        String extension = FilenameUtils.getExtension(filename);%0A        if (StringUtils.equalsIgnoreCase(%22png%22, extension)) %7B%0A            return %22image/png%22;%0A        %7D else if (StringUtils.equalsIgnoreCase(%22jpg%22, extension)) %7B%0A            return %22image/jpg%22;%0A        %7D else if (StringUtils.equalsIgnoreCase(%22gif%22, extension)) %7B%0A            return %22image/gif%22;%0A        %7D else if (StringUtils.equalsIgnoreCase(%22tiff%22, extension)) %7B%0A            return %22image/tiff%22;%0A        %7D else if (StringUtils.equalsIgnoreCase(%22txt%22, extension)) %7B%0A            return %22text/plain%22;%0A        %7D else %7B%0A            return %22application/octet-stream%22;%0A        %7D%0A    %7D%0A%0A
 %7D

add save file