66import android .webkit .MimeTypeMap ;
77
88import org .wordpress .android .WordPress ;
9+ import org .wordpress .android .util .MapUtils ;
910
1011public class MediaFile {
1112
@@ -31,54 +32,19 @@ public class MediaFile {
3132 private String uploadState = null ;
3233 private String mediaId ;
3334
34- /*
35- * wrappers for extracting values from the resultMap passed to the MediaFile constructor
36- * getMapStr() is guaranteed to return "" if value doesn't exist (never returns null)
37- * getMapInt() & getMapLong() are guaranteed to return 0 if value doesn't exist or isn't a number
38- * getMapDate() DOES return null if value doesn't exist or isn't a date
39- */
40- private static String getMapStr (final Map <?, ?> map , final String key ) {
41- if (map ==null || key ==null || !map .containsKey (key ))
42- return "" ;
43- return map .get (key ).toString ();
44- }
45- private static int getMapInt (final Map <?, ?> map , final String key ) {
46- try {
47- return Integer .parseInt (getMapStr (map , key ));
48- } catch (NumberFormatException e ) {
49- return 0 ;
50- }
51- }
52- private static long getMapLong (final Map <?, ?> map , final String key ) {
53- try {
54- return Long .parseLong (getMapStr (map , key ));
55- } catch (NumberFormatException e ) {
56- return 0 ;
57- }
58- }
59- private static Date getMapDate (final Map <?, ?> map , final String key ) {
60- if (map ==null || key ==null || !map .containsKey (key ))
61- return null ;
62- try {
63- return (Date ) map .get (key );
64- } catch (ClassCastException e ) {
65- return null ;
66- }
67- }
68-
6935 public MediaFile (String blogId , Map <?, ?> resultMap ) {
7036
7137 boolean isDotCom = (WordPress .getCurrentBlog () != null && WordPress .getCurrentBlog ().isDotcomFlag ());
7238
7339 setBlogId (blogId );
74- setMediaId (getMapStr (resultMap , "attachment_id" ));
75- setPostID (getMapLong (resultMap , "parent" ));
76- setTitle (getMapStr (resultMap , "title" ));
77- setCaption (getMapStr (resultMap , "caption" ));
78- setDescription (getMapStr (resultMap , "description" ));
40+ setMediaId (MapUtils . getMapStr (resultMap , "attachment_id" ));
41+ setPostID (MapUtils . getMapLong (resultMap , "parent" ));
42+ setTitle (MapUtils . getMapStr (resultMap , "title" ));
43+ setCaption (MapUtils . getMapStr (resultMap , "caption" ));
44+ setDescription (MapUtils . getMapStr (resultMap , "description" ));
7945
8046 // get the file name from the link
81- String link = getMapStr (resultMap , "link" );
47+ String link = MapUtils . getMapStr (resultMap , "link" );
8248 setFileName (new String (link ).replaceAll ("^.*/([A-Za-z0-9_-]+)\\ .\\ w+$" , "$1" ));
8349
8450 String fileType = new String (link ).replaceAll (".*\\ .(\\ w+)$" , "$1" ).toLowerCase ();
@@ -87,27 +53,27 @@ public MediaFile(String blogId, Map<?, ?> resultMap) {
8753
8854 // make the file urls be https://... so that we can get these images with oauth when the blogs are private
8955 // assume no https for images in self-hosted blogs
90- String fileUrl = getMapStr (resultMap , "link" );
56+ String fileUrl = MapUtils . getMapStr (resultMap , "link" );
9157 if (isDotCom )
9258 fileUrl = fileUrl .replace ("http:" , "https:" );
9359 setFileURL (fileUrl );
9460
95- String thumbnailURL = getMapStr (resultMap , "thumbnail" );
61+ String thumbnailURL = MapUtils . getMapStr (resultMap , "thumbnail" );
9662 if (thumbnailURL .startsWith ("http" )) {
9763 if (isDotCom )
9864 thumbnailURL = thumbnailURL .replace ("http:" , "https:" );
9965 setThumbnailURL (thumbnailURL );
10066 }
10167
102- Date date = getMapDate (resultMap , "date_created_gmt" );
68+ Date date = MapUtils . getMapDate (resultMap , "date_created_gmt" );
10369 if (date != null )
10470 setDateCreatedGMT (date .getTime ());
10571
10672 Object meta = resultMap .get ("metadata" );
10773 if (meta != null && meta instanceof Map ) {
10874 Map <?, ?> metadata = (Map <?, ?>) meta ;
109- setWidth (getMapInt (metadata , "width" ));
110- setHeight (getMapInt (metadata , "height" ));
75+ setWidth (MapUtils . getMapInt (metadata , "width" ));
76+ setHeight (MapUtils . getMapInt (metadata , "height" ));
11177 }
11278 }
11379
0 commit comments