mahmudkuet

Image Helper for Infolady project

Feb 5th, 2017
180
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Image {
  2.    
  3.     private String baseUrl = "http://stage.isocial.com.bd/";
  4.     private String imageUrl = baseUrl + "uploads/";
  5.     private String memberImageUrl = imageUrl + "member/";
  6.     private String placeholderImage = "http://placehold.it/350x150";
  7.    
  8.     private String domain;
  9.     private String size = "t";
  10.     private String imageId;
  11.    
  12.     public Image setDomain(String domain){
  13.         this.domain = domain;
  14.         return this;
  15.     }
  16.    
  17.     public Image setSize(String size){
  18.         this.size = size;
  19.         return this;
  20.     }
  21.    
  22.     public Image setId(String imageId){
  23.         this.imageId = imageId;
  24.         return this;
  25.     }
  26.    
  27.     public Image setPlaceholder(String placeholder){
  28.         this.placeholderImage = placeholder;
  29.         return this;
  30.     }
  31.    
  32.     public String get(){
  33.         Pattern pattern = Pattern.compile("(\\d+)_(\\d+)_([\\d\\-]+.[a-z]+)");
  34.         Matcher matcher = pattern.matcher(this.imageId);
  35.         String url = "";
  36.         if(matcher.find()){
  37.             String year = matcher.group(1);
  38.             String month = matcher.group(2);
  39.             String id = matcher.group(3);
  40.            
  41.             String relUrl = "";
  42.            
  43.             if(this.domain.equalsIgnoreCase("member")){
  44.                 relUrl = this.memberImageUrl;
  45.             }
  46.            
  47.             url = relUrl + year + "/" + month + "/" + this.size + id;
  48.            
  49.         }else{
  50.             url = this.placeholderImage;
  51.         }
  52.         return url;
  53.     }
  54.    
  55. }
  56.  
  57.  
  58.  
  59.  
  60. // Usage
  61.  
  62. public class TestApp{
  63.  
  64.     /**
  65.      * @param args the command line arguments
  66.      */
  67.     public static void main(String[] args) {
  68.         System.out.println(new Image().setDomain("member").setId("2017_02_1486274285-8933.jpg").get());
  69.         System.out.println(new Image().setDomain("member").setPlaceholder("http://placehold.it/350x150").setId("2017_02_1486274285-8933.jpg").get());
  70.     }
  71.    
  72. }
RAW Paste Data