use of com.epam.ta.reportportal.entity.attachment.BinaryData in project commons-dao by reportportal.
the class UserBinaryDataServiceImpl method loadUserPhoto.
@Override
public BinaryData loadUserPhoto(User user, boolean loadThumbnail) {
Optional<String> fileId = ofNullable(loadThumbnail ? user.getAttachmentThumbnail() : user.getAttachment());
InputStream data;
String contentType;
try {
if (fileId.isPresent()) {
contentType = (String) user.getMetadata().getMetadata().get(ATTACHMENT_CONTENT_TYPE);
data = dataStoreService.load(fileId.get()).orElseThrow(() -> new ReportPortalException(ErrorType.UNABLE_TO_LOAD_BINARY_DATA, fileId.get()));
} else {
data = new ClassPathResource(DEFAULT_USER_PHOTO).getInputStream();
contentType = MimeTypeUtils.IMAGE_JPEG_VALUE;
}
return new BinaryData(contentType, (long) data.available(), data);
} catch (IOException e) {
LOGGER.error("Unable to load user photo", e);
throw new ReportPortalException(ErrorType.UNCLASSIFIED_REPORT_PORTAL_ERROR, "Unable to load user photo");
}
}
use of com.epam.ta.reportportal.entity.attachment.BinaryData in project service-authorization by reportportal.
the class GitHubUserReplicator method uploadAvatar.
private void uploadAvatar(GitHubClient gitHubClient, User user, String avatarUrl) {
if (null != avatarUrl) {
ResponseEntity<Resource> photoRs = gitHubClient.downloadResource(avatarUrl);
try (InputStream photoStream = photoRs.getBody().getInputStream()) {
BinaryData photo = new BinaryData(photoRs.getHeaders().getContentType().toString(), photoRs.getBody().contentLength(), photoStream);
uploadPhoto(user, photo);
} catch (IOException e) {
LOGGER.error("Unable to load photo for user {}", user.getLogin());
}
}
}
use of com.epam.ta.reportportal.entity.attachment.BinaryData in project commons-dao by reportportal.
the class UserCommonDataStoreServiceTest method saveUserPhoto.
@Test
void saveUserPhoto() throws IOException {
CommonsMultipartFile multipartFile = getMultipartFile("meh.jpg");
User user = userRepository.findByLogin("default").get();
userDataStoreService.saveUserPhoto(user, multipartFile);
user = userRepository.findByLogin("default").get();
BinaryData binaryData = userDataStoreService.loadUserPhoto(user, false);
assertThat(IOUtils.contentEquals(multipartFile.getInputStream(), binaryData.getInputStream())).isTrue();
BinaryData binaryDataThumbnail = userDataStoreService.loadUserPhoto(user, true);
assertThat(IOUtils.contentEquals(thumbnailator.createThumbnail(multipartFile.getInputStream()), binaryDataThumbnail.getInputStream())).isTrue();
}
use of com.epam.ta.reportportal.entity.attachment.BinaryData in project commons-dao by reportportal.
the class AttachmentBinaryDataServiceImpl method load.
@Override
public BinaryData load(Long fileId, ReportPortalUser.ProjectDetails projectDetails) {
try {
Attachment attachment = attachmentRepository.findById(fileId).orElseThrow(() -> new ReportPortalException(ErrorType.ATTACHMENT_NOT_FOUND, fileId));
InputStream data = dataStoreService.load(attachment.getFileId()).orElseThrow(() -> new ReportPortalException(ErrorType.UNABLE_TO_LOAD_BINARY_DATA, fileId));
expect(attachment.getProjectId(), Predicate.isEqual(projectDetails.getProjectId())).verify(ErrorType.ACCESS_DENIED, formattedSupplier("You are not assigned to project '{}'", projectDetails.getProjectName()));
return new BinaryData(attachment.getContentType(), (long) data.available(), data);
} catch (IOException e) {
LOGGER.error("Unable to load binary data", e);
throw new ReportPortalException(ErrorType.UNCLASSIFIED_REPORT_PORTAL_ERROR, "Unable to load binary data");
}
}
Aggregations