Search in sources :

Example 6 with InternalException

use of com.baidu.hugegraph.exception.InternalException in project incubator-hugegraph-toolchain by apache.

the class FileUploadController method ensureLocationExist.

private void ensureLocationExist(String location, String connPath) {
    String path = Paths.get(location, connPath).toString();
    File locationDir = new File(path);
    if (!locationDir.exists()) {
        try {
            FileUtils.forceMkdir(locationDir);
        } catch (IOException e) {
            throw new InternalException("failed to create location dir", e);
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) InternalException(com.baidu.hugegraph.exception.InternalException)

Example 7 with InternalException

use of com.baidu.hugegraph.exception.InternalException in project incubator-hugegraph-toolchain by apache.

the class FileMappingService method deleteDiskFile.

public void deleteDiskFile(FileMapping mapping) {
    File file = new File(mapping.getPath());
    if (file.isDirectory()) {
        log.info("Prepare to delete directory {}", file);
        try {
            FileUtils.forceDelete(file);
        } catch (IOException e) {
            throw new InternalException("Failed to delete directory " + "corresponded to the file id %s, " + "please delete it manually", e, mapping.getId());
        }
    } else {
        File parentDir = file.getParentFile();
        log.info("Prepare to delete directory {}", parentDir);
        try {
            FileUtils.forceDelete(parentDir);
        } catch (IOException e) {
            throw new InternalException("Failed to delete parent directory " + "corresponded to the file id %s, " + "please delete it manually", e, mapping.getId());
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) InternalException(com.baidu.hugegraph.exception.InternalException)

Example 8 with InternalException

use of com.baidu.hugegraph.exception.InternalException in project incubator-hugegraph-toolchain by apache.

the class FileMappingService method tryMergePartFiles.

public boolean tryMergePartFiles(String dirPath, int total) {
    File dir = new File(dirPath);
    File[] partFiles = dir.listFiles();
    if (partFiles == null) {
        throw new InternalException("The part files can't be null");
    }
    if (partFiles.length != total) {
        return false;
    }
    File newFile = new File(dir.getPath() + ".all");
    File destFile = new File(dir.getPath());
    if (partFiles.length == 1) {
        try {
            // Rename file to dest file
            FileUtils.moveFile(partFiles[0], newFile);
        } catch (IOException e) {
            log.error("Failed to rename file from {} to {}", partFiles[0], newFile, e);
            throw new InternalException("load.upload.move-file.failed", e);
        }
    } else {
        Arrays.sort(partFiles, (o1, o2) -> {
            String file1Idx = StringUtils.substringAfterLast(o1.getName(), "-");
            String file2Idx = StringUtils.substringAfterLast(o2.getName(), "-");
            Integer idx1 = Integer.valueOf(file1Idx);
            Integer idx2 = Integer.valueOf(file2Idx);
            return idx1.compareTo(idx2);
        });
        try (OutputStream os = new FileOutputStream(newFile, true)) {
            for (int i = 0; i < partFiles.length; i++) {
                File partFile = partFiles[i];
                try (InputStream is = new FileInputStream(partFile)) {
                    IOUtils.copy(is, os);
                } catch (IOException e) {
                    log.error("Failed to copy file stream from {} to {}", partFile, newFile, e);
                    throw new InternalException("load.upload.merge-file.failed", e);
                }
            }
        } catch (IOException e) {
            log.error("Failed to copy all file-parts stream to {}", newFile, e);
            throw new InternalException("load.upload.merge-file.failed", e);
        }
    }
    // Delete origin directory
    try {
        FileUtils.forceDelete(dir);
    } catch (IOException e) {
        log.error("Failed to force delete file {}", dir, e);
        throw new InternalException("load.upload.delete-temp-dir.failed", e);
    }
    // Rename file to dest file
    if (!newFile.renameTo(destFile)) {
        throw new InternalException("load.upload.rename-file.failed");
    }
    return true;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInputStream(java.io.FileInputStream) InternalException(com.baidu.hugegraph.exception.InternalException)

Example 9 with InternalException

use of com.baidu.hugegraph.exception.InternalException in project incubator-hugegraph-toolchain by apache.

the class JobManagerController method update.

@PutMapping("{id}")
public JobManager update(@PathVariable("connId") int connId, @PathVariable("id") int id, @RequestBody JobManager newEntity) {
    Ex.check(newEntity.getJobName().length() <= 48, "job.manager.job-name.reached-limit");
    Ex.check(newEntity.getJobName() != null, () -> Constant.COMMON_NAME_PATTERN.matcher(newEntity.getJobName()).matches(), "job.manager.job-name.unmatch-regex");
    Ex.check(!StringUtils.isEmpty(newEntity.getJobRemarks()), () -> Constant.COMMON_REMARK_PATTERN.matcher(newEntity.getJobRemarks()).matches(), "job.manager.job-remarks.unmatch-regex");
    // Check exist Job Manager with this id
    JobManager entity = this.service.get(id);
    if (entity == null) {
        throw new ExternalException("job-manager.not-exist.id", id);
    }
    if (!newEntity.getJobName().equals(entity.getJobName()) && this.service.getTask(newEntity.getJobName(), connId) != null) {
        throw new InternalException("job.manager.job-name.repeated");
    }
    entity.setJobName(newEntity.getJobName());
    entity.setJobRemarks(newEntity.getJobRemarks());
    this.service.update(entity);
    return entity;
}
Also used : JobManager(com.baidu.hugegraph.entity.load.JobManager) ExternalException(com.baidu.hugegraph.exception.ExternalException) InternalException(com.baidu.hugegraph.exception.InternalException) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Aggregations

InternalException (com.baidu.hugegraph.exception.InternalException)9 File (java.io.File)6 IOException (java.io.IOException)6 MultipartFile (org.springframework.web.multipart.MultipartFile)5 MergeProperty (com.baidu.hugegraph.annotation.MergeProperty)1 HugeClient (com.baidu.hugegraph.driver.HugeClient)1 FileSetting (com.baidu.hugegraph.entity.load.FileSetting)1 JobManager (com.baidu.hugegraph.entity.load.JobManager)1 ExecuteHistory (com.baidu.hugegraph.entity.query.ExecuteHistory)1 ExternalException (com.baidu.hugegraph.exception.ExternalException)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Field (java.lang.reflect.Field)1 Pattern (java.util.regex.Pattern)1 Transactional (org.springframework.transaction.annotation.Transactional)1