Search in sources :

Example 1 with BinaryData

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");
    }
}
Also used : ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BinaryData(com.epam.ta.reportportal.entity.attachment.BinaryData) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 2 with BinaryData

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());
        }
    }
}
Also used : InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) BinaryData(com.epam.ta.reportportal.entity.attachment.BinaryData) IOException(java.io.IOException)

Example 3 with BinaryData

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();
}
Also used : User(com.epam.ta.reportportal.entity.user.User) BinaryData(com.epam.ta.reportportal.entity.attachment.BinaryData) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) BaseTest(com.epam.ta.reportportal.BaseTest) Test(org.junit.jupiter.api.Test)

Example 4 with BinaryData

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");
    }
}
Also used : ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) Attachment(com.epam.ta.reportportal.entity.attachment.Attachment) BinaryData(com.epam.ta.reportportal.entity.attachment.BinaryData)

Aggregations

BinaryData (com.epam.ta.reportportal.entity.attachment.BinaryData)4 ReportPortalException (com.epam.ta.reportportal.exception.ReportPortalException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 BaseTest (com.epam.ta.reportportal.BaseTest)1 Attachment (com.epam.ta.reportportal.entity.attachment.Attachment)1 User (com.epam.ta.reportportal.entity.user.User)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Test (org.junit.jupiter.api.Test)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1 CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)1