-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
f8a1540
commit 5686987
Showing
33 changed files
with
1,982 additions
and
1,418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
modules/portal/portal-impl/src/main/java/com/enonic/xp/portal/impl/MediaHashResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.enonic.xp.portal.impl; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.HexFormat; | ||
import java.util.Objects; | ||
|
||
import com.google.common.hash.HashCode; | ||
import com.google.common.hash.Hasher; | ||
import com.google.common.hash.Hashing; | ||
|
||
import com.enonic.xp.attachment.Attachment; | ||
import com.enonic.xp.content.Media; | ||
import com.enonic.xp.image.Cropping; | ||
import com.enonic.xp.image.FocalPoint; | ||
import com.enonic.xp.media.ImageOrientation; | ||
|
||
public final class MediaHashResolver | ||
{ | ||
public static String resolveLegacyImageHash( final Media media, final String hash ) | ||
{ | ||
return Hashing.sha1() | ||
.newHasher() | ||
.putString( String.valueOf( hash ), StandardCharsets.UTF_8 ) | ||
.putString( String.valueOf( media.getFocalPoint() ), StandardCharsets.UTF_8 ) | ||
.putString( String.valueOf( media.getCropping() ), StandardCharsets.UTF_8 ) | ||
.putString( String.valueOf( media.getOrientation() ), StandardCharsets.UTF_8 ) | ||
.hash() | ||
.toString(); | ||
} | ||
|
||
public static String resolveImageHash( final Media media, final String hash ) | ||
{ | ||
if ( hash == null ) | ||
{ | ||
return null; | ||
} | ||
|
||
final Hasher hasher = Hashing.sha512().newHasher(); | ||
|
||
hasher.putBytes( HashCode.fromString( hash ).asBytes() ); | ||
|
||
final FocalPoint focalPoint = Objects.requireNonNullElse( media.getFocalPoint(), FocalPoint.DEFAULT ); | ||
|
||
hasher.putDouble( focalPoint.xOffset() ); | ||
hasher.putDouble( focalPoint.yOffset() ); | ||
|
||
final Cropping cropping = Objects.requireNonNullElse( media.getCropping(), Cropping.DEFAULT ); | ||
|
||
hasher.putDouble( cropping.top() ); | ||
hasher.putDouble( cropping.left() ); | ||
hasher.putDouble( cropping.bottom() ); | ||
hasher.putDouble( cropping.right() ); | ||
hasher.putDouble( cropping.zoom() ); | ||
|
||
final ImageOrientation orientation = Objects.requireNonNullElse( media.getOrientation(), ImageOrientation.DEFAULT ); | ||
hasher.putInt( orientation.ordinal() ); | ||
|
||
return HexFormat.of().formatHex( hasher.hash().asBytes(), 0, 16 ); | ||
} | ||
|
||
public static String resolveImageHash( final Media media ) | ||
{ | ||
final Attachment attachment = media.getMediaAttachment(); | ||
|
||
if ( attachment == null || attachment.getSha512() == null ) | ||
{ | ||
return null; | ||
} | ||
|
||
return resolveImageHash( media, resolveAttachmentHash( attachment ) ); | ||
} | ||
|
||
public static String resolveAttachmentHash( final Attachment attachment ) | ||
{ | ||
return attachment == null || attachment.getSha512() == null ? null : attachment.getSha512().substring( 0, 32 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.