Search in sources :

Example 1 with FileStorageException

use of io.jmix.core.FileStorageException in project jmix by jmix-framework.

the class LocalFileStorage method saveStream.

public long saveStream(FileRef fileRef, InputStream inputStream) {
    Path relativePath = getRelativePath(fileRef.getPath());
    Path[] roots = getStorageRoots();
    // Store to primary storage
    checkStorageDefined(roots, fileRef.getFileName());
    checkPrimaryStorageAccessible(roots, fileRef.getFileName());
    Path path = roots[0].resolve(relativePath);
    if (!path.getParent().toFile().exists() && !path.getParent().toFile().mkdirs()) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, "Cannot create directory: " + path.getParent().toAbsolutePath().toString());
    }
    checkFileExists(path);
    long size;
    try (OutputStream outputStream = Files.newOutputStream(path, CREATE_NEW)) {
        size = IOUtils.copyLarge(inputStream, outputStream);
        outputStream.flush();
    // writeLog(path, false);
    } catch (IOException e) {
        FileUtils.deleteQuietly(path.toFile());
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, path.toAbsolutePath().toString(), e);
    }
    // Copy file to secondary storages asynchronously
    for (int i = 1; i < roots.length; i++) {
        if (!roots[i].toFile().exists()) {
            log.error("Error saving {} into {} : directory doesn't exist", fileRef.getFileName(), roots[i]);
            continue;
        }
        Path pathCopy = roots[i].resolve(relativePath);
        writeExecutor.submit(() -> {
            try {
                FileUtils.copyFile(path.toFile(), pathCopy.toFile(), true);
            } catch (Exception e) {
                log.error("Error saving {} into {} : {}", fileRef.getFileName(), pathCopy, e.getMessage());
            }
        });
    }
    return size;
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) IOException(java.io.IOException) FileStorageException(io.jmix.core.FileStorageException) IOException(java.io.IOException) FileStorageException(io.jmix.core.FileStorageException)

Example 2 with FileStorageException

use of io.jmix.core.FileStorageException in project jmix by jmix-framework.

the class LocalFileStorage method removeFile.

@Override
public void removeFile(FileRef reference) {
    Path[] roots = getStorageRoots();
    if (roots.length == 0) {
        log.error("No storage directories defined");
        return;
    }
    Path relativePath = getRelativePath(reference.getPath());
    for (Path root : roots) {
        Path filePath = root.resolve(relativePath);
        File file = filePath.toFile();
        if (file.exists()) {
            if (!file.delete()) {
                throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, "Unable to delete file " + file.getAbsolutePath());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) FileStorageException(io.jmix.core.FileStorageException) File(java.io.File)

Example 3 with FileStorageException

use of io.jmix.core.FileStorageException in project jmix by jmix-framework.

the class TemporaryStorageImpl method createFileInternal.

protected FileInfo createFileInternal() {
    UUID uuid = UuidProvider.createUuid();
    File dir = new File(tempDir);
    if (!dir.exists() && !dir.mkdirs()) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, "Cannot create temp directory: " + dir.getAbsolutePath());
    }
    File file = new File(dir, uuid.toString());
    if (file.exists()) {
        throw new FileStorageException(FileStorageException.Type.FILE_ALREADY_EXISTS, file.getAbsolutePath());
    }
    try {
        if (file.createNewFile())
            tempFiles.put(uuid, file);
        else
            throw new FileStorageException(FileStorageException.Type.FILE_ALREADY_EXISTS, file.getAbsolutePath());
    } catch (IOException ex) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, file.getAbsolutePath());
    }
    return new FileInfo(file, uuid);
}
Also used : IOException(java.io.IOException) UUID(java.util.UUID) FileStorageException(io.jmix.core.FileStorageException) File(java.io.File)

Example 4 with FileStorageException

use of io.jmix.core.FileStorageException in project jmix by jmix-framework.

the class TemporaryStorageImpl method putFileIntoStorage.

@Override
public FileRef putFileIntoStorage(UUID fileId, String fileName, FileStorage fileStorage) {
    File file = getFile(fileId);
    if (file == null) {
        throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileName);
    }
    FileRef fileRef;
    try (InputStream io = new FileInputStream(file)) {
        fileRef = fileStorage.saveStream(fileName, io);
    } catch (FileNotFoundException e) {
        throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, "Temp file is not found " + file.getAbsolutePath());
    } catch (IOException e) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileName);
    }
    deleteFile(fileId);
    return fileRef;
}
Also used : FileRef(io.jmix.core.FileRef) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileStorageException(io.jmix.core.FileStorageException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with FileStorageException

use of io.jmix.core.FileStorageException in project jmix by jmix-framework.

the class TemporaryStorageImpl method saveFile.

@Override
public UUID saveFile(InputStream stream, UploadProgressListener listener) {
    checkNotNullArgument(stream, "Null input stream for save file");
    UUID uuid = UuidProvider.createUuid();
    File dir = new File(tempDir);
    if (!dir.exists() && !dir.mkdirs()) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, "Cannot create temp directory: " + dir.getAbsolutePath());
    }
    File file = new File(dir, uuid.toString());
    if (file.exists()) {
        throw new FileStorageException(FileStorageException.Type.FILE_ALREADY_EXISTS, file.getAbsolutePath());
    }
    try {
        boolean failed = false;
        try (FileOutputStream fileOutput = new FileOutputStream(file)) {
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead;
            int totalBytes = 0;
            while ((bytesRead = stream.read(buffer)) > 0) {
                fileOutput.write(buffer, 0, bytesRead);
                totalBytes += bytesRead;
                if (listener != null)
                    listener.progressChanged(uuid, totalBytes);
            }
        } catch (Exception ex) {
            failed = true;
            throw ex;
        } finally {
            if (!failed)
                tempFiles.put(uuid, file);
        }
    } catch (Exception ex) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, file.getAbsolutePath(), ex);
    }
    return uuid;
}
Also used : FileOutputStream(java.io.FileOutputStream) UUID(java.util.UUID) FileStorageException(io.jmix.core.FileStorageException) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileStorageException(io.jmix.core.FileStorageException)

Aggregations

FileStorageException (io.jmix.core.FileStorageException)13 IOException (java.io.IOException)8 File (java.io.File)5 InputStream (java.io.InputStream)4 Path (java.nio.file.Path)4 UUID (java.util.UUID)3 SdkException (software.amazon.awssdk.core.exception.SdkException)3 S3Client (software.amazon.awssdk.services.s3.S3Client)3 FileRef (io.jmix.core.FileRef)2 BufferedInputStream (java.io.BufferedInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 MailSSLSocketFactory (com.sun.mail.util.MailSSLSocketFactory)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 MessagingException (javax.mail.MessagingException)1 CompleteMultipartUploadRequest (software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)1 CompletedMultipartUpload (software.amazon.awssdk.services.s3.model.CompletedMultipartUpload)1