use of com.epam.pipeline.entity.datastorage.DataStorageItemContent 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.DataStorageItemContent in project cloud-pipeline by epam.
the class S3Helper method downloadContent.
private DataStorageItemContent downloadContent(Long maxDownloadSize, S3Object objectPortion) {
try (InputStream objectData = objectPortion.getObjectContent()) {
DataStorageItemContent content = new DataStorageItemContent();
content.setContentType(objectPortion.getObjectMetadata().getContentType());
content.setTruncated(objectPortion.getObjectMetadata().getInstanceLength() > maxDownloadSize);
byte[] byteContent = IOUtils.toByteArray(objectData);
if (FileContentUtils.isBinaryContent(byteContent)) {
content.setMayBeBinary(true);
} else {
content.setContent(byteContent);
}
return content;
} catch (IOException e) {
throw new DataStorageException(e.getMessage(), e);
}
}
use of com.epam.pipeline.entity.datastorage.DataStorageItemContent in project cloud-pipeline by epam.
the class S3Helper method getFileContent.
public DataStorageItemContent getFileContent(AbstractDataStorage dataStorage, String path, String version, Long maxDownloadSize) {
try {
AmazonS3 client = getDefaultS3Client();
GetObjectRequest rangeObjectRequest = new GetObjectRequest(dataStorage.getPath(), path, version).withRange(0, maxDownloadSize - 1);
S3Object objectPortion = client.getObject(rangeObjectRequest);
return downloadContent(maxDownloadSize, objectPortion);
} catch (AmazonS3Exception e) {
if (e.getStatusCode() == NOT_FOUND) {
throw new DataStorageException(String.format("File '%s' doesn't exist", path));
} else if (e.getStatusCode() == INVALID_RANGE) {
// is thrown in case of en empty file
LOGGER.debug(e.getMessage(), e);
DataStorageItemContent content = new DataStorageItemContent();
content.setTruncated(false);
return content;
} else {
throw new DataStorageException(e.getMessage(), e);
}
}
}
Aggregations