|
1 | 1 | package com.danikula.videocache; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
| 4 | +import android.net.Uri; |
4 | 5 |
|
5 | 6 | import com.danikula.videocache.file.DiskUsage; |
6 | 7 | import com.danikula.videocache.file.FileNameGenerator; |
@@ -84,7 +85,37 @@ private HttpProxyCacheServer(Config config) { |
84 | 85 | } |
85 | 86 | } |
86 | 87 |
|
| 88 | + /** |
| 89 | + * Returns url that wrap original url and should be used for client (MediaPlayer, ExoPlayer, etc). |
| 90 | + * <p> |
| 91 | + * If file for this url is fully cached (it means method {@link #isCached(String)} returns {@code true}) |
| 92 | + * then file:// uri to cached file will be returned. |
| 93 | + * <p> |
| 94 | + * Calling this method has same effect as calling {@link #getProxyUrl(String, boolean)} with 2nd parameter set to {@code true}. |
| 95 | + * |
| 96 | + * @param url a url to file that should be cached. |
| 97 | + * @return a wrapped by proxy url if file is not fully cached or url pointed to cache file otherwise. |
| 98 | + */ |
87 | 99 | public String getProxyUrl(String url) { |
| 100 | + return getProxyUrl(url, true); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns url that wrap original url and should be used for client (MediaPlayer, ExoPlayer, etc). |
| 105 | + * <p> |
| 106 | + * If parameter {@code allowCachedFileUri} is {@code true} and file for this url is fully cached |
| 107 | + * (it means method {@link #isCached(String)} returns {@code true}) then file:// uri to cached file will be returned. |
| 108 | + * |
| 109 | + * @param url a url to file that should be cached. |
| 110 | + * @param allowCachedFileUri {@code true} if allow to return file:// uri if url is fully cached |
| 111 | + * @return a wrapped by proxy url if file is not fully cached or url pointed to cache file otherwise (if {@code allowCachedFileUri} is {@code true}). |
| 112 | + */ |
| 113 | + public String getProxyUrl(String url, boolean allowCachedFileUri) { |
| 114 | + if (allowCachedFileUri && isCached(url)) { |
| 115 | + File cacheFile = getCacheFile(url); |
| 116 | + touchFileSafely(cacheFile); |
| 117 | + return Uri.fromFile(cacheFile).toString(); |
| 118 | + } |
88 | 119 | return isAlive() ? appendToProxyUrl(url) : url; |
89 | 120 | } |
90 | 121 |
|
@@ -127,10 +158,7 @@ public void unregisterCacheListener(CacheListener cacheListener) { |
127 | 158 | */ |
128 | 159 | public boolean isCached(String url) { |
129 | 160 | checkNotNull(url, "Url can't be null!"); |
130 | | - File cacheDir = config.cacheRoot; |
131 | | - String fileName = config.fileNameGenerator.generate(url); |
132 | | - File cacheFile = new File(cacheDir, fileName); |
133 | | - return cacheFile.exists(); |
| 161 | + return getCacheFile(url).exists(); |
134 | 162 | } |
135 | 163 |
|
136 | 164 | public void shutdown() { |
@@ -158,6 +186,20 @@ private String appendToProxyUrl(String url) { |
158 | 186 | return String.format(Locale.US, "http://%s:%d/%s", PROXY_HOST, port, ProxyCacheUtils.encode(url)); |
159 | 187 | } |
160 | 188 |
|
| 189 | + private File getCacheFile(String url) { |
| 190 | + File cacheDir = config.cacheRoot; |
| 191 | + String fileName = config.fileNameGenerator.generate(url); |
| 192 | + return new File(cacheDir, fileName); |
| 193 | + } |
| 194 | + |
| 195 | + private void touchFileSafely(File cacheFile) { |
| 196 | + try { |
| 197 | + config.diskUsage.touch(cacheFile); |
| 198 | + } catch (IOException e) { |
| 199 | + LOG.error("Error touching file " + cacheFile, e); |
| 200 | + } |
| 201 | + } |
| 202 | + |
161 | 203 | private void shutdownClients() { |
162 | 204 | synchronized (clientsLock) { |
163 | 205 | for (HttpProxyCacheServerClients clients : clientsMap.values()) { |
|
0 commit comments