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;
}
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;
}
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);
}
}
Aggregations