diff --git a/library/src/main/java/com/danikula/videocache/HttpUrlSource.java b/library/src/main/java/com/danikula/videocache/HttpUrlSource.java index 90599ba..994202c 100644 --- a/library/src/main/java/com/danikula/videocache/HttpUrlSource.java +++ b/library/src/main/java/com/danikula/videocache/HttpUrlSource.java @@ -1,13 +1,5 @@ package com.danikula.videocache; -import android.text.TextUtils; - -import com.danikula.videocache.sourcestorage.SourceInfoStorage; -import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -15,6 +7,12 @@ import java.net.HttpURLConnection; import java.net.URL; +import android.text.TextUtils; +import com.danikula.videocache.sourcestorage.SourceInfoStorage; +import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import static com.danikula.videocache.Preconditions.checkNotNull; import static com.danikula.videocache.ProxyCacheUtils.DEFAULT_BUFFER_SIZE; import static java.net.HttpURLConnection.HTTP_MOVED_PERM; @@ -125,7 +123,7 @@ private void fetchContentInfo() throws ProxyCacheException { HttpURLConnection urlConnection = null; InputStream inputStream = null; try { - urlConnection = openConnection(0, 10000); + urlConnection = openConnectionForHeader(10000); long length = getContentLength(urlConnection); String mime = urlConnection.getContentType(); inputStream = urlConnection.getInputStream(); @@ -141,7 +139,36 @@ private void fetchContentInfo() throws ProxyCacheException { } } } - + // only get http header information + private HttpURLConnection openConnectionForHeader(int timeout) throws IOException, ProxyCacheException { + HttpURLConnection connection; + boolean redirected; + int redirectCount = 0; + String url = this.sourceInfo.url; + do { + LOG.debug("Open connection for header to " + url); + connection = (HttpURLConnection) new URL(url).openConnection(); + if (timeout > 0) { + connection.setConnectTimeout(timeout); + connection.setReadTimeout(timeout); + } + //only get HEAD ,not need BODY,to get header faster information + connection.setRequestMethod("HEAD"); + int code = connection.getResponseCode(); + redirected = code == HTTP_MOVED_PERM || code == HTTP_MOVED_TEMP || code == HTTP_SEE_OTHER; + if (redirected) { + url = connection.getHeaderField("Location"); + LOG.debug("Redirect to:" + url); + redirectCount++; + connection.disconnect(); + LOG.debug("Redirect closed:" + url); + } + if (redirectCount > MAX_REDIRECTS) { + throw new ProxyCacheException("Too many redirects: " + redirectCount); + } + } while (redirected); + return connection; + } private HttpURLConnection openConnection(long offset, int timeout) throws IOException, ProxyCacheException { HttpURLConnection connection; boolean redirected; diff --git a/library/src/main/java/com/danikula/videocache/file/Files.java b/library/src/main/java/com/danikula/videocache/file/Files.java index 5f5234a..bf0e333 100644 --- a/library/src/main/java/com/danikula/videocache/file/Files.java +++ b/library/src/main/java/com/danikula/videocache/file/Files.java @@ -1,18 +1,17 @@ package com.danikula.videocache.file; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; -import java.util.Date; import java.util.LinkedList; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Utils for work with files. * @@ -48,13 +47,13 @@ static List getLruListFiles(File directory) { static void setLastModifiedNow(File file) throws IOException { if (file.exists()) { long now = System.currentTimeMillis(); - boolean modified = file.setLastModified(now); // on some devices (e.g. Nexus 5) doesn't work + boolean modified = file.setLastModified(now/1000*1000); // on some devices (e.g. Nexus 5) doesn't work if (!modified) { modify(file); - if (file.lastModified() < now) { - // NOTE: apparently this is a known issue (see: http://stackoverflow.com/questions/6633748/file-lastmodified-is-never-what-was-set-with-file-setlastmodified) - LOG.warn("Last modified date {} is not set for file {}", new Date(file.lastModified()), file.getAbsolutePath()); - } + //if (file.lastModified() < now) { + // // NOTE: apparently this is a known issue (see: http://stackoverflow.com/questions/6633748/file-lastmodified-is-never-what-was-set-with-file-setlastmodified) + // LOG.warn("Last modified date {} is not set for file {}", new Date(file.lastModified()), file.getAbsolutePath()); + //} } } }