Search in sources :

Example 1 with Media

use of io.gravitee.repository.media.model.Media in project gravitee-management-rest-api by gravitee-io.

the class MediaServiceImpl method findAllWithoutContent.

@Override
public List<MediaEntity> findAllWithoutContent(List<PageMediaEntity> pageMediaEntities, String api) {
    List<MediaEntity> result = new ArrayList<>();
    if (pageMediaEntities != null && !pageMediaEntities.isEmpty()) {
        for (PageMediaEntity pme : pageMediaEntities) {
            final Optional<Media> foundMedia = mediaRepository.findByHashAndApi(pme.getMediaHash(), api, false);
            if (foundMedia.isPresent()) {
                MediaEntity me = this.convert(foundMedia.get());
                me.setFileName(pme.getMediaName());
                me.setUploadDate(pme.getAttachedAt());
                result.add(me);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Media(io.gravitee.repository.media.model.Media) PageMediaEntity(io.gravitee.rest.api.model.PageMediaEntity) PageMediaEntity(io.gravitee.rest.api.model.PageMediaEntity) MediaEntity(io.gravitee.rest.api.model.MediaEntity)

Example 2 with Media

use of io.gravitee.repository.media.model.Media in project gravitee-management-rest-api by gravitee-io.

the class MediaServiceImpl method convert.

private static Media convert(MediaEntity imageEntity) {
    Media media = new Media();
    media.setFileName(imageEntity.getFileName());
    media.setSize(imageEntity.getSize());
    media.setType(imageEntity.getType());
    media.setSubType(imageEntity.getSubType());
    media.setId(imageEntity.getId());
    // media.setData(new ByteArrayInputStream(imageEntity.getData()));
    return media;
}
Also used : Media(io.gravitee.repository.media.model.Media)

Example 3 with Media

use of io.gravitee.repository.media.model.Media in project gravitee-management-rest-api by gravitee-io.

the class MediaServiceImpl method saveApiMedia.

@Override
public String saveApiMedia(String api, MediaEntity mediaEntity) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] hash = digest.digest(mediaEntity.getData());
        String hashString = DatatypeConverter.printHexBinary(hash);
        String id = mediaEntity.getId() != null && UUID.fromString(mediaEntity.getId()) != null ? mediaEntity.getId() : UuidString.generateRandom();
        Optional<Media> checkMedia = null;
        if (api != null) {
            checkMedia = mediaRepository.findByHashAndApiAndType(hashString, api, mediaEntity.getType());
        } else {
            checkMedia = mediaRepository.findByHashAndType(hashString, mediaEntity.getType());
        }
        if (checkMedia.isPresent()) {
            return checkMedia.get().getHash();
        } else {
            Media media = convert(mediaEntity);
            media.setId(id);
            media.setHash(hashString);
            media.setSize((long) mediaEntity.getData().length);
            media.setApi(api);
            media.setData(mediaEntity.getData());
            mediaRepository.create(media);
            return hashString;
        }
    } catch (TechnicalException | NoSuchAlgorithmException ex) {
        LOGGER.error("An error occurs while trying to create {}", mediaEntity, ex);
        throw new TechnicalManagementException("An error occurs while trying create " + mediaEntity, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Media(io.gravitee.repository.media.model.Media) UuidString(io.gravitee.rest.api.service.common.UuidString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

Media (io.gravitee.repository.media.model.Media)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 MediaEntity (io.gravitee.rest.api.model.MediaEntity)1 PageMediaEntity (io.gravitee.rest.api.model.PageMediaEntity)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1