use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.
the class DataStorageController method uploadFile.
@RequestMapping(value = "/datastorage/{id}/list/upload", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "Uploads a file to data storage.", notes = "Uploads a file to data storage.", produces = MediaType.APPLICATION_JSON_VALUE)
public List<UploadFileMetadata> uploadFile(@PathVariable(value = ID) Long id, @RequestParam(value = PATH, required = false) final String folder, HttpServletRequest request) throws FileUploadException {
MultipartFile file = consumeMultipartFile(request);
LinkedList<UploadFileMetadata> uploadedFiles = new LinkedList<>();
UploadFileMetadata fileMeta = new UploadFileMetadata();
fileMeta.setFileName(FilenameUtils.getName(file.getOriginalFilename()));
fileMeta.setFileSize(file.getSize() / BYTES_IN_KB + " Kb");
fileMeta.setFileType(file.getContentType());
try {
fileMeta.setBytes(file.getBytes());
uploadedFiles.add(fileMeta);
} catch (IOException e) {
throw new DataStorageException("Failed to upload file to datastorage.", e);
}
dataStorageApiService.createDataStorageFile(id, folder, fileMeta.getFileName(), fileMeta.getBytes());
return uploadedFiles;
}
use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.
the class NFSStorageProvider method getFile.
@Override
public DataStorageItemContent getFile(NFSDataStorage dataStorage, String path, String version, Long maxDownloadSize) {
File mntDir = mount(dataStorage);
File file = new File(mntDir, path);
try (FileInputStream fis = new FileInputStream(file)) {
DataStorageItemContent content = new DataStorageItemContent();
long bytesToRead = file.length();
if (file.length() > maxDownloadSize) {
content.setTruncated(true);
bytesToRead = maxDownloadSize;
}
byte[] contentBytes = IOUtils.toByteArray(fis, bytesToRead);
if (FileContentUtils.isBinaryContent(contentBytes)) {
content.setMayBeBinary(true);
} else {
content.setContent(contentBytes);
}
return content;
} catch (IOException e) {
throw new DataStorageException(e);
}
}
use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.
the class NFSStorageProvider method getStream.
@Override
public DataStorageStreamingContent getStream(NFSDataStorage dataStorage, String path, String version) {
File mntDir = mount(dataStorage);
File file = new File(mntDir, path);
try {
return new DataStorageStreamingContent(file);
} catch (FileNotFoundException e) {
throw new DataStorageException(e);
}
}
use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.
the class NFSStorageProvider method createFile.
@Override
public DataStorageFile createFile(NFSDataStorage dataStorage, String path, InputStream dataStream) throws DataStorageException {
File dataStorageDir = mount(dataStorage);
File file = new File(dataStorageDir, path);
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) {
IOUtils.copy(dataStream, outputStream);
setUmask(file);
} catch (IOException e) {
throw new DataStorageException(e);
}
return new DataStorageFile(path, file);
}
use of com.epam.pipeline.entity.datastorage.DataStorageException in project cloud-pipeline by epam.
the class NFSStorageProvider method unmountNFSIfEmpty.
private synchronized void unmountNFSIfEmpty(AbstractDataStorage storage) {
String storagePath = storage.getPath();
File mntDir = Paths.get(rootMountPoint, getMountDirName(storagePath)).toFile();
List<AbstractDataStorage> remaining = dataStorageDao.loadDataStoragesByNFSPath(getNfsRootPath(storagePath));
LOGGER.debug("Remaining NFS: " + remaining.stream().map(AbstractDataStorage::getPath).collect(Collectors.joining(";")) + " related with current root path");
if (mntDir.exists() && isStorageOnlyOnNFS(storage, remaining)) {
try {
String umountCmd = String.format(NFS_UNMOUNT_CMD_PATTERN, mntDir.getAbsolutePath());
cmdExecutor.executeCommand(umountCmd);
FileUtils.deleteDirectory(mntDir);
} catch (IOException e) {
throw new DataStorageException(e);
}
}
}
Aggregations