Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ImageHandler.java « helper « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf59f1e5f4493860f236feb5b0f1cd57d15cb924 (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
/*
* Android ownCloud News
*
* @author David Luhmer
* @copyright 2013 David Luhmer david-dev@live.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package de.luhmer.owncloudnewsreader.helper;

import android.util.Log;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ImageHandler {
    private static final String TAG = "[ImageHandler]";
    private static final Pattern patternImg = Pattern.compile("<img[^>]*>");
    private static final Pattern patternImgSrcLink = Pattern.compile("src=\"(.*?)\"");

	public static List<String> getImageLinksFromText(String articleUrl, String text)
	{
		List<String> links = new ArrayList<>();

		Matcher matcher = patternImg.matcher(text);
	    // Check all occurrences
	    while (matcher.find()) {
	    	Matcher matcherSrcLink = patternImgSrcLink.matcher(matcher.group());
	    	if(matcherSrcLink.find()) {
                String link = matcherSrcLink.group(1);
                if(link != null && link.startsWith("//")) { //Maybe the text contains image urls without http or https prefix.
                    link = "https:" + link;
                }

                // the android universal image loader doesn't support svg images. Therefore we don't want to load them through UIL
                if(link.endsWith(".svg")) {
                    Log.d(TAG, "detected unsupported svg image in article: " + articleUrl + " -> " + link);
                } else {
                    links.add(link);
                }
	    	}
	    }
	    return links;
	}

    public static String fixBrokenImageLinksInArticle(String articleUrl, String text)
    {
        Matcher matcher = patternImg.matcher(text);
        // Check all occurrences
        while (matcher.find()) {
            Matcher matcherSrcLink = patternImgSrcLink.matcher(matcher.group());
            if(matcherSrcLink.find()) {
                String link = matcherSrcLink.group(1);
                String originalLink = link;
                String originalArticleUrl = articleUrl;
                if(link != null) {
                    if(link.startsWith("//")) { //Maybe the text contains image urls without http or https prefix.
                        link = "https:" + link;
                    } else if (link.startsWith("/")) { // detected absolute url
                        try {
                            URI uri = new URI(articleUrl);
                            String domain = uri.getHost();
                            link = uri.getScheme() + "://" + domain + link;
                        } catch (URISyntaxException e) {
                            e.printStackTrace();
                            Log.e(TAG, e.toString());
                        }
                    } else {
                        // check if we have relative urls such as
                        // ./abc.jpeg or ./../abc.jpeg, ../abc.jpeg or ../../abc.jpeg
                        boolean linkNeedsHost = false;
                        if(link.startsWith("./")) {
                            link = link.substring(2); // remove ./ from link
                            linkNeedsHost = true;
                        }

                        // if link is relative without anything else in front (e.g. pix/wow.svg)
                        if(!link.startsWith("http") && !link.startsWith(".")) {
                            linkNeedsHost = true;
                            if(!articleUrl.endsWith("/")) {
                                // remove last part of article url to get a relative url
                                articleUrl = sliceLastPathOfUrl(articleUrl);
                            }
                        }

                        // in case the article url is of type articles/matrix-vs-xmpp.html we need to remove the file plus the first part of the url
                        if(link.startsWith("../") && !articleUrl.endsWith("/")) {
                            linkNeedsHost = true;
                            articleUrl = sliceLastPathOfUrl(articleUrl);
                            articleUrl = sliceLastPathOfUrl(articleUrl);
                            link = link.substring(3); // remove ../ from link
                        }

                        // if the article urls ends with an / we can just remove it piece by piece
                        while(link.startsWith("../")) {
                            linkNeedsHost = true;
                            articleUrl = sliceLastPathOfUrl(articleUrl);
                            link = link.substring(3); // remove ../ from link
                        }

                        if(linkNeedsHost) {
                            link = articleUrl + "/" + link;
                        }
                    }
                }

                if(!originalLink.equals(link)) {
                    Log.d(TAG, "Fixed link from: " + originalArticleUrl + " and " + originalLink + " -> " + link);
                    // text = text.replaceAll(originalLink, link); // this causes OutOfMemoryExceptions (https://github.com/nextcloud/news-android/issues/1055)

                    Pattern URL_PATTERN = Pattern.compile(originalLink);
                    Matcher urlMatcher = URL_PATTERN.matcher(text);
                    return urlMatcher.replaceAll(link);

                }
            }
        }
        return text;
    }

    private static String sliceLastPathOfUrl(String url) {
	    int idx = url.lastIndexOf("/");
        int countOfSlashes = url.split("/").length - 1;
        // Log.d(TAG, url + " " + countOfSlashes);
        // make sure we don't count into the domain name (at least two slashes for ://)
	    if(idx > 0 && countOfSlashes > 2) {
            return url.substring(0, idx);
        } else {
	        return url;
        }
    }

    public static void clearCache()
    {
        if(ImageLoader.getInstance().isInited()) {
            ImageLoader.getInstance().clearDiskCache();
            ImageLoader.getInstance().clearMemoryCache();
        }
    }
}