Skip to content

Commit 1ca3def

Browse files
committed
fix video preview
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 71b9587 commit 1ca3def

2 files changed

Lines changed: 62 additions & 38 deletions

File tree

app/src/main/java/com/nextcloud/utils/extensions/OwnCloudClientExtensions.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ fun OwnCloudClient.getPreviewEndpoint(localFileId: Long, x: Int, y: Int): String
2828
"&x=" + (x / 2) + "&y=" + (y / 2) +
2929
"&a=1&mode=cover&forceIcon=0"
3030

31-
fun OwnCloudClient.getVideoPreviewEndpoint(localFileId: Long): String = baseUri
31+
fun OwnCloudClient.getVideoPreviewEndpoint(localFileId: Long, size: Int): String = baseUri
3232
.toString() +
3333
"/index.php/core/preview?fileId=" +
3434
localFileId +
35-
"&x=" + VIDEO_PREVIEW_SIZE + "&y=" + VIDEO_PREVIEW_SIZE +
35+
"&x=" + size + "&y=" + size +
3636
"&a=1&forceIcon=0"
37-
38-
private const val VIDEO_PREVIEW_SIZE = 1024

app/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
*/
9090
public final class ThumbnailsCacheManager {
9191
private static final int READ_TIMEOUT = 40000;
92+
private static final int[] VIDEO_PREVIEW_SIZES = {1024, 512, 256, 128, 64};
9293
private static final int CONNECTION_TIMEOUT = 5000;
9394

9495
/** Cache key prefix {@code <prefix><remoteId>}; sized from {@link #getScreenDimension()}. */
@@ -1271,6 +1272,52 @@ public static void generateThumbnailFromOCFile(OCFile file, User user, Context c
12711272
}
12721273
}
12731274

1275+
private static Bitmap downloadVideoPreview(OCFile file) {
1276+
for (int size : VIDEO_PREVIEW_SIZES) {
1277+
Bitmap preview = downloadPreview(file,
1278+
OwnCloudClientExtensionsKt.getVideoPreviewEndpoint(mClient,
1279+
file.getLocalId(),
1280+
size));
1281+
if (preview != null) {
1282+
return preview;
1283+
}
1284+
}
1285+
1286+
return null;
1287+
}
1288+
1289+
private static Bitmap downloadPreview(OCFile file, String uri) {
1290+
Log_OC.d(TAG, "generating resized image: " + file.getFileName() + " URI: " + uri);
1291+
1292+
GetMethod getMethod = null;
1293+
1294+
try {
1295+
getMethod = new GetMethod(uri);
1296+
getMethod.getParams().setSoTimeout(READ_TIMEOUT);
1297+
1298+
int status = mClient.executeMethod(getMethod);
1299+
if (status != HttpStatus.SC_OK) {
1300+
Log_OC.e(TAG, "cannot generate thumbnail not supported file type, status: " + status
1301+
+ " file: " + file.getRemotePath());
1302+
mClient.exhaustResponse(getMethod.getResponseBodyAsStream());
1303+
return null;
1304+
}
1305+
1306+
try (InputStream inputStream = getMethod.getResponseBodyAsStream()) {
1307+
Bitmap preview = BitmapFactory.decodeStream(inputStream);
1308+
Log_OC.d(TAG, "resized image generated");
1309+
return preview;
1310+
}
1311+
} catch (Exception e) {
1312+
Log_OC.e(TAG, "doResizedBitmap: ", e);
1313+
return null;
1314+
} finally {
1315+
if (getMethod != null) {
1316+
getMethod.releaseConnection();
1317+
}
1318+
}
1319+
}
1320+
12741321
@VisibleForTesting
12751322
public static void clearCache() {
12761323
synchronized (mThumbnailsDiskCacheLock) {
@@ -1310,41 +1357,20 @@ public static Bitmap doResizedImageInBackground(OCFile file, FileDataStorageMana
13101357
file.setUpdateThumbnailNeeded(false);
13111358
}
13121359
} else if (mClient != null) {
1313-
GetMethod getMethod = null;
1314-
1315-
try {
1316-
String uri = MimeTypeUtil.isVideo(file)
1317-
? OwnCloudClientExtensionsKt.getVideoPreviewEndpoint(mClient, file.getLocalId())
1318-
: OwnCloudClientExtensionsKt.getPreviewEndpoint(mClient, file.getLocalId(), pxW, pxH);
1319-
Log_OC.d(TAG, "generating resized image: " + file.getFileName() + " URI: " + uri);
1320-
1321-
getMethod = new GetMethod(uri);
1322-
getMethod.getParams().setSoTimeout(READ_TIMEOUT);
1323-
1324-
int status = mClient.executeMethod(getMethod);
1325-
if (status == HttpStatus.SC_OK) {
1326-
try (InputStream inputStream = getMethod.getResponseBodyAsStream()) {
1327-
thumbnail = BitmapFactory.decodeStream(inputStream);
1328-
Log_OC.d(TAG, "resized image generated");
1329-
}
1330-
} else {
1331-
Log_OC.e(TAG, "cannot generate thumbnail not supported file type, status: " + status + " file: " + file.getRemotePath());
1332-
mClient.exhaustResponse(getMethod.getResponseBodyAsStream());
1333-
}
1334-
1335-
if (thumbnail != null && PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) {
1336-
thumbnail = handlePNG(thumbnail, thumbnail.getWidth(), thumbnail.getHeight());
1337-
}
1360+
thumbnail = MimeTypeUtil.isVideo(file)
1361+
? downloadVideoPreview(file)
1362+
: downloadPreview(file,
1363+
OwnCloudClientExtensionsKt.getPreviewEndpoint(mClient,
1364+
file.getLocalId(),
1365+
pxW,
1366+
pxH));
1367+
1368+
if (thumbnail != null && PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) {
1369+
thumbnail = handlePNG(thumbnail, thumbnail.getWidth(), thumbnail.getHeight());
1370+
}
13381371

1339-
if (thumbnail != null) {
1340-
addBitmapToCache(imageKey, thumbnail);
1341-
}
1342-
} catch (Exception e) {
1343-
Log_OC.e(TAG, "doResizedBitmap: ", e);
1344-
} finally {
1345-
if (getMethod != null) {
1346-
getMethod.releaseConnection();
1347-
}
1372+
if (thumbnail != null) {
1373+
addBitmapToCache(imageKey, thumbnail);
13481374
}
13491375
}
13501376

0 commit comments

Comments
 (0)