Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- import java.util.Optional;
- import java.util.function.Predicate;
- @Component
- @Qualifier(API.LOAD_IMAGE)
- public class LoadImageApi implements IApiAdapter {
- @Autowired
- private UserImageDAO userImageDAO;
- @Autowired
- private TokenService tokenService;
- @Override
- public Respond execute(Request request, Date time) {
- final String imageId = request.getImageId();
- final String imageKind = request.getImageKind();
- if (StringUtils.isNullOrEmpty(imageId) || StringUtils.isNullOrEmpty(imageKind)) {
- return new EntityRespond(ErrorCode.WRONG_DATA_FORMAT);
- }
- final String iasActive = request.getIasActive();
- if(iasActive == null) {
- final String token = request.getToken();
- boolean verifyToken = tokenService.verify(token.split(Pattern.quote(SPLIT_TOKEN_CHARACTER))[0]);
- if(!verifyToken) {
- return new EntityRespond(ErrorCode.INVALID_TOKEN);
- }
- }
- final String imageKindUpdated = imageKind.equals(Constant.IMAGE_KIND_VALUE.DEMO_IMAGE) ? Constant.IMAGE_KIND_VALUE.NEWS_BANNER : imageKind;
- final String imageIdUpdated = Optional.ofNullable(JsonUtils.parser(imageId))
- .map(json -> JsonUtils.getProperty(json, ParamKey.IMAGE_ID))
- .orElse(imageId);
- try {
- final String userId = request.getUserId();
- final int iasInt = Integer.parseInt(iasActive);
- if(iasInt == Constant.FLAG.ON) {
- final boolean supportImageKind = (imageKindUpdated.equals(Constant.IMAGE_KIND_VALUE.THUMBNAIL) || imageKindUpdated.equals(Constant.IMAGE_KIND_VALUE.ORIGINAL_IMAGE));
- if(!supportImageKind) {
- return new EntityRespond(ErrorCode.FILE_NOT_FOUND);
- }
- if(userImageDAO.imageExist(imageIdUpdated, userId)) {
- throw new IOException("Image had existed " + imageIdUpdated + userId);
- }
- }
- final String imageUrl = Helper.getImageUrl(imageIdUpdated, imageKind);
- if(imageUrl == null) {
- throw new FileNotFoundException("Image not found " + imageIdUpdated + imageKind);
- }
- final boolean denyImage = userImageDAO.deniedImageChat(imageIdUpdated, userId);
- if(denyImage) {
- return new EntityRespond(ErrorCode.FILE_NOT_FOUND);
- }
- ByteRespond respond = new ByteRespond();
- byte[] resultImageByte = Helper.getFile(imageUrl);
- if(resultImageByte == null) {
- String imageUrlS3;
- if(Constant.IMAGE_KIND_VALUE.THUMBNAIL.equals(imageKind)) {
- imageUrlS3 = FilesAndFolders.FOLDERS.THUMBNAIL_IMAGE_FOLDER_S3 + imageUrl.replace(FilesAndFolders.FOLDERS.THUMBNAIL_IMAGE_FOLDER, "");
- } else if(Constant.IMAGE_KIND_VALUE.ORIGINAL_IMAGE.equals(imageKind)) {
- imageUrlS3 = FilesAndFolders.FOLDERS.ORIGINAL_IMAGE_FOLDER_S3 + imageUrl.replace(FilesAndFolders.FOLDERS.ORIGINAL_IMAGE_FOLDER, "");
- } else {
- return new EntityRespond(ErrorCode.FILE_NOT_FOUND);
- }
- resultImageByte = HelperAWSS3.getByteData(imageUrlS3);
- if (resultImageByte == null) {
- throw new FileNotFoundException("Can not read image url" + imageUrl);
- }
- }
- respond.data = resultImageByte;
- return respond;
- } catch(IOException fnfe) {
- return new EntityRespond(ErrorCode.FILE_NOT_FOUND);
- } catch (ApplicationException ex) {
- return new ByteRespond(ex.getErrorCode());
- } catch (Exception ex) {
- Util.addErrorLog(ex);
- return new ByteRespond();
- }
- }
- public static final class StringUtils {
- private StringUtils() {throw new UnsupportedOperationException("Not allow creating instance");}
- public static boolean isNullOrEmpty(String input) {
- return input == null || input.length() == 0 ;
- }
- }
- public static final class JsonUtils {
- private JsonUtils() {throw new UnsupportedOperationException("Not allow creating instance");}
- /**
- * @return null if can not parse source
- */
- public static JSONObject parser(String source) {
- try {
- JSONParser parser = new JSONParser();
- return (JSONObject) parser.parse(source);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * @return null if {@code property} not found
- */
- public static String getProperty(JSONObject object, String property) {
- return (String) object.get(property);
- }
- }
- public interface TokenService {
- /**
- * @return true if valid, false otherwise (including case exception)
- */
- boolean verify(String token);
- }
- public static class JWTTokenService implements TokenService {
- @Override
- public boolean verify(String token) {
- try {
- return JWTCreator.getInstance().parseJWT(token) != null;
- } catch (Exception e) {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement