Search in sources :

Example 1 with FileNotFoundException

use of com.varsql.core.exception.FileNotFoundException in project varsql by varsqlinfo.

the class FileServiceUtils method getFileInfos.

/**
 * @method  : fileInfos
 * @desc : file info 구하기
 * @author   : ytkim
 * @date   : 2022. 1. 15.
 * @param FilePaths
 * @return
 * @throws IOException
 */
public static List<FileInfo> getFileInfos(String[] FilePaths) throws IOException {
    List<FileInfo> driverJarFiles = new ArrayList<>();
    for (String path : FilePaths) {
        Resource resource = ResourceUtils.getResource(path);
        if (resource != null) {
            FileInfo fi = new FileInfo();
            fi.setName(resource.getFile().getName());
            fi.setPath(resource.getFile().getAbsolutePath());
            fi.setExt(FileUtils.extension(resource.getFile().getName()));
            driverJarFiles.add(fi);
        } else {
            throw new FileNotFoundException("file path not found : " + path);
        }
    }
    return driverJarFiles;
}
Also used : FileInfo(com.varsql.core.common.beans.FileInfo) ArrayList(java.util.ArrayList) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) FileNotFoundException(com.varsql.core.exception.FileNotFoundException)

Example 2 with FileNotFoundException

use of com.varsql.core.exception.FileNotFoundException in project varsql by varsqlinfo.

the class UserPreferencesServiceFileImpl method zipFileDetail.

public ResponseResult zipFileDetail(String fileId, String fileName) {
    FileInfoEntity entity = fileInfoEntityRepository.findByFileId(fileId);
    if (entity == null) {
        new FileNotFoundException("fileId not found : " + fileId);
    }
    StringBuffer sb = new StringBuffer();
    try (ZipFile zipFile = new ZipFile(FileServiceUtils.getFileInfoToFile(entity))) {
        ZipEntry entry = zipFile.getEntry(fileName);
        if (entry.getName().equals(fileName)) {
            try (InputStreamReader isr = new InputStreamReader(zipFile.getInputStream(entry));
                BufferedReader br = new BufferedReader(isr)) {
                String read_data = "";
                int lineCount = 0;
                while ((read_data = br.readLine()) != null) {
                    if (lineCount >= MAX_LINE) {
                        break;
                    }
                    lineCount++;
                    sb.append(read_data).append(BlankConstants.NEW_LINE);
                }
                IOUtils.close(br);
                IOUtils.close(isr);
            }
        }
        IOUtils.close(zipFile);
    } catch (Exception e) {
        logger.error("detail fileId : {}", fileId, e);
    }
    return VarsqlUtils.getResponseResultItemOne(sb.toString());
}
Also used : ZipFile(java.util.zip.ZipFile) InputStreamReader(java.io.InputStreamReader) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(com.varsql.core.exception.FileNotFoundException) BufferedReader(java.io.BufferedReader) FileInfoEntity(com.varsql.web.model.entity.app.FileInfoEntity) FileNotFoundException(com.varsql.core.exception.FileNotFoundException)

Example 3 with FileNotFoundException

use of com.varsql.core.exception.FileNotFoundException in project varsql by varsqlinfo.

the class UserPreferencesServiceFileImpl method detail.

public ResponseResult detail(String fileId) {
    FileInfoEntity entity = fileInfoEntityRepository.findByFileId(fileId);
    if (entity == null) {
        new FileNotFoundException("fileId not found : " + fileId);
    }
    if (UploadFileType.EXPORT.equals(UploadFileType.getDivType(entity.getFileDiv()))) {
        Map item;
        List<Map> fileList = new ArrayList<>();
        try (ZipFile zipFile = new ZipFile(FileServiceUtils.getFileInfoToFile(entity))) {
            final Enumeration<? extends ZipEntry> entries = zipFile.entries();
            ZipEntry entry;
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
                item = new HashMap();
                item.put("fileName", entry.getName());
                item.put("fileSize", entry.getSize());
                item.put("compressFileSize", entry.getCompressedSize());
                item.put("updDt", DateUtils.dateformat(VarsqlConstants.DATE_TIME_FORMAT, entry.getLastModifiedTime().toMillis()));
                fileList.add(item);
            }
            IOUtils.close(zipFile);
        } catch (Exception e) {
            logger.error("detail fileId : {}", fileId, e);
        }
        return VarsqlUtils.getResponseResultItemList(fileList);
    } else if (UploadFileType.IMPORT.equals(UploadFileType.getDivType(entity.getFileDiv()))) {
        StringBuffer sb = new StringBuffer();
        Map item = new HashMap();
        try (InputStreamReader isr = new InputStreamReader(new FileInputStream(FileServiceUtils.getFileInfoToFile(entity)));
            BufferedReader br = new BufferedReader(isr)) {
            String read_data = "";
            int lineCount = 0;
            while ((read_data = br.readLine()) != null) {
                if (lineCount >= MAX_LINE) {
                    break;
                }
                lineCount++;
                sb.append(read_data).append(BlankConstants.NEW_LINE);
            }
            item.put("lineCount", lineCount);
            item.put("content", sb.toString());
            IOUtils.close(br);
            IOUtils.close(isr);
        } catch (Exception e) {
            logger.error("detail fileId : {}", fileId, e);
        }
        return VarsqlUtils.getResponseResultItemOne(item);
    }
    return VarsqlUtils.getResponseResultItemOne(null);
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(com.varsql.core.exception.FileNotFoundException) ArrayList(java.util.ArrayList) FileNotFoundException(com.varsql.core.exception.FileNotFoundException) FileInputStream(java.io.FileInputStream) ZipFile(java.util.zip.ZipFile) BufferedReader(java.io.BufferedReader) FileInfoEntity(com.varsql.web.model.entity.app.FileInfoEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with FileNotFoundException

use of com.varsql.core.exception.FileNotFoundException in project varsql by varsqlinfo.

the class FileServiceUtils method loadFileAsResource.

/**
 * @method  : loadFileAsResource
 * @desc : file nam
 * @author   : ytkim
 * @date   : 2021. 1. 3.
 * @param fileName
 * @return
 * @throws MalformedURLException
 */
public static Resource loadFileAsResource(String filePath) throws MalformedURLException {
    Path path = getUploadRootPath().resolve(filePath).normalize();
    Resource resource = new UrlResource(path.toUri());
    if (resource.exists()) {
        return resource;
    } else {
        throw new FileNotFoundException("File resource not found " + filePath);
    }
}
Also used : Path(java.nio.file.Path) UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) FileNotFoundException(com.varsql.core.exception.FileNotFoundException)

Aggregations

FileNotFoundException (com.varsql.core.exception.FileNotFoundException)4 FileInfoEntity (com.varsql.web.model.entity.app.FileInfoEntity)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 Resource (org.springframework.core.io.Resource)2 UrlResource (org.springframework.core.io.UrlResource)2 FileInfo (com.varsql.core.common.beans.FileInfo)1 FileInputStream (java.io.FileInputStream)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1