aboutsummaryrefslogtreecommitdiff
path: root/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/helpers/MediaFile.java
blob: b57ad0165c754cfb5d3febe4d849eb9ada6f6e01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package org.wordpress.android.util.helpers;

import android.text.TextUtils;
import android.webkit.MimeTypeMap;

import org.wordpress.android.util.MapUtils;
import org.wordpress.android.util.StringUtils;

import java.util.Date;
import java.util.Map;

public class MediaFile {
    protected int id;
    protected long postID;
    protected String filePath = null; //path of the file into disk
    protected String fileName = null; //name of the file into the server
    protected String title = null;
    protected String description = null;
    protected String caption = null;
    protected int horizontalAlignment; //0 = none, 1 = left, 2 = center, 3 = right
    protected boolean verticalAligment = false; //false = bottom, true = top
    protected int width = 500, height;
    protected String mimeType = "";
    protected String videoPressShortCode = null;
    protected boolean featured = false;
    protected boolean isVideo = false;
    protected boolean featuredInPost;
    protected String fileURL = null; // url of the file to download
    protected String thumbnailURL = null;  // url of the thumbnail to download
    private String blogId;
    private long dateCreatedGmt;
    private String uploadState = null;
    private String mediaId;

    public static String VIDEOPRESS_SHORTCODE_ID = "videopress_shortcode";

    public MediaFile(String blogId, Map<?, ?> resultMap, boolean isDotCom) {
        setBlogId(blogId);
        setMediaId(MapUtils.getMapStr(resultMap, "attachment_id"));
        setPostID(MapUtils.getMapLong(resultMap, "parent"));
        setTitle(MapUtils.getMapStr(resultMap, "title"));
        setCaption(MapUtils.getMapStr(resultMap, "caption"));
        setDescription(MapUtils.getMapStr(resultMap, "description"));
        setVideoPressShortCode(MapUtils.getMapStr(resultMap, VIDEOPRESS_SHORTCODE_ID));

        // get the file name from the link
        String link = MapUtils.getMapStr(resultMap, "link");
        setFileName(new String(link).replaceAll("^.*/([A-Za-z0-9_-]+)\\.\\w+$", "$1"));

        String fileType = new String(link).replaceAll(".*\\.(\\w+)$", "$1").toLowerCase();
        String fileMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileType);
        setMimeType(fileMimeType);

        // make the file urls be https://... so that we can get these images with oauth when the blogs are private
        // assume no https for images in self-hosted blogs
        String fileUrl = MapUtils.getMapStr(resultMap, "link");
        if (isDotCom) {
            fileUrl = fileUrl.replace("http:", "https:");
        }
        setFileURL(fileUrl);

        String thumbnailURL = MapUtils.getMapStr(resultMap, "thumbnail");
        if (thumbnailURL.startsWith("http")) {
            if (isDotCom) {
                thumbnailURL = thumbnailURL.replace("http:", "https:");
            }
            setThumbnailURL(thumbnailURL);
        }

        Date date = MapUtils.getMapDate(resultMap, "date_created_gmt");
        if (date != null) {
            setDateCreatedGMT(date.getTime());
        }

        Object meta = resultMap.get("metadata");
        if (meta != null && meta instanceof Map) {
            Map<?, ?> metadata = (Map<?, ?>) meta;
            setWidth(MapUtils.getMapInt(metadata, "width"));
            setHeight(MapUtils.getMapInt(metadata, "height"));
        }
    }

    public MediaFile() {
        // default constructor
    }

    public MediaFile(MediaFile mediaFile) {
        this.id = mediaFile.id;
        this.postID = mediaFile.postID;
        this.filePath = mediaFile.filePath;
        this.fileName = mediaFile.fileName;
        this.title = mediaFile.title;
        this.description = mediaFile.description;
        this.caption = mediaFile.caption;
        this.horizontalAlignment = mediaFile.horizontalAlignment;
        this.verticalAligment = mediaFile.verticalAligment;
        this.width = mediaFile.width;
        this.height = mediaFile.height;
        this.mimeType = mediaFile.mimeType;
        this.videoPressShortCode = mediaFile.videoPressShortCode;
        this.featured = mediaFile.featured;
        this.isVideo = mediaFile.isVideo;
        this.featuredInPost = mediaFile.featuredInPost;
        this.fileURL = mediaFile.fileURL;
        this.thumbnailURL = mediaFile.thumbnailURL;
        this.blogId = mediaFile.blogId;
        this.dateCreatedGmt = mediaFile.dateCreatedGmt;
        this.uploadState = mediaFile.uploadState;
        this.mediaId = mediaFile.mediaId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMediaId() {
        return mediaId;
    }

    public void setMediaId(String id) {
        mediaId = id;
    }

    public boolean isFeatured() {
        return featured;
    }

    public void setFeatured(boolean featured) {
        this.featured = featured;
    }

    public long getPostID() {
        return postID;
    }

    public void setPostID(long postID) {
        this.postID = postID;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getFileURL() {
        return fileURL;
    }

    public void setFileURL(String fileURL) {
        this.fileURL = fileURL;
    }

    public String getThumbnailURL() {
        return thumbnailURL;
    }

    public void setThumbnailURL(String thumbnailURL) {
        this.thumbnailURL = thumbnailURL;
    }

    public boolean isVerticalAlignmentOnTop() {
        return verticalAligment;
    }

    public void setVerticalAlignmentOnTop(boolean verticalAligment) {
        this.verticalAligment = verticalAligment;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getMimeType() {
        return StringUtils.notNullStr(mimeType);
    }

    public void setMimeType(String type) {
        mimeType = StringUtils.notNullStr(type);
    }

    public String getVideoPressShortCode() {
        return videoPressShortCode;
    }

    public void setVideoPressShortCode(String videoPressShortCode) {
        this.videoPressShortCode = videoPressShortCode;
    }

    public int getHorizontalAlignment() {
        return horizontalAlignment;
    }

    public void setHorizontalAlignment(int horizontalAlignment) {
        this.horizontalAlignment = horizontalAlignment;
    }

    public boolean isVideo() {
        return isVideo;
    }

    public void setVideo(boolean isVideo) {
        this.isVideo = isVideo;
    }

    public boolean isFeaturedInPost() {
        return featuredInPost;
    }

    public void setFeaturedInPost(boolean featuredInPost) {
        this.featuredInPost = featuredInPost;
    }

    public String getBlogId() {
        return blogId;
    }

    public void setBlogId(String blogId) {
        this.blogId = blogId;
    }

    public void setDateCreatedGMT(long date_created_gmt) {
        this.dateCreatedGmt = date_created_gmt;
    }

    public long getDateCreatedGMT() {
        return dateCreatedGmt;
    }

    public void setUploadState(String uploadState) {
        this.uploadState = uploadState;
    }

    public String getUploadState() {
        return uploadState;
    }

    /**
     * Outputs the Html for an image
     * If a fullSizeUrl exists, a link will be created to it from the resizedPictureUrl
     */
    public String getImageHtmlForUrls(String fullSizeUrl, String resizedPictureURL, boolean shouldAddImageWidthCSS) {
        String alignment = "";
        switch (getHorizontalAlignment()) {
            case 0:
                alignment = "alignnone";
                break;
            case 1:
                alignment = "alignleft";
                break;
            case 2:
                alignment = "aligncenter";
                break;
            case 3:
                alignment = "alignright";
                break;
        }

        String alignmentCSS = "class=\"" + alignment + " size-full\" ";

        if (shouldAddImageWidthCSS) {
            alignmentCSS += "style=\"max-width: " + getWidth() + "px\" ";
        }

        // Check if we uploaded a featured picture that is not added to the Post content (normal case)
        if ((fullSizeUrl != null && fullSizeUrl.equalsIgnoreCase("")) ||
                (resizedPictureURL != null && resizedPictureURL.equalsIgnoreCase(""))) {
            return ""; // Not featured in Post. Do not add to the content.
        }

        if (fullSizeUrl == null && resizedPictureURL != null) {
            fullSizeUrl = resizedPictureURL;
        } else if (fullSizeUrl != null && resizedPictureURL == null) {
            resizedPictureURL = fullSizeUrl;
        }

        String mediaTitle = StringUtils.notNullStr(getTitle());

        String content = String.format("<a href=\"%s\"><img title=\"%s\" %s alt=\"image\" src=\"%s\" /></a>",
                fullSizeUrl, mediaTitle, alignmentCSS, resizedPictureURL);

        if (!TextUtils.isEmpty(getCaption())) {
            content = String.format("[caption id=\"\" align=\"%s\" width=\"%d\"]%s%s[/caption]",
                    alignment, getWidth(), content, TextUtils.htmlEncode(getCaption()));
        }

        return content;
    }
}