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;
}
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());
}
}
}
}
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);
}
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;
}
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;
}
Aggregations