Skip to content

Commit

Permalink
Merge pull request #546 from nextcloud/backport/545/stable-2.4
Browse files Browse the repository at this point in the history
[stable-2.4] Limit gallery search to only own files for now
  • Loading branch information
tobiasKaminsky authored Nov 24, 2020
2 parents 53e5a58 + 897c379 commit 2fee764
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import android.net.Uri;
import android.os.Bundle;

import androidx.test.platform.app.InstrumentationRegistry;

import com.owncloud.android.AbstractIT;
import com.owncloud.android.lib.common.OwnCloudBasicCredentials;
import com.owncloud.android.lib.common.OwnCloudClient;
Expand All @@ -40,8 +42,6 @@

import java.io.IOException;

import androidx.test.platform.app.InstrumentationRegistry;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;

Expand Down Expand Up @@ -310,18 +310,19 @@ public void testGallerySearch() throws IOException {
String filePath = createFile("image" + i);
String remotePath = "/image" + i + ".jpg";
assertTrue(new UploadFileRemoteOperation(filePath, remotePath, "image/jpg", "123")
.execute(client).isSuccess());
.execute(client).isSuccess());
}
String videoPath = createFile("video");
assertTrue(new UploadFileRemoteOperation(videoPath, "/video.mp4", "video/mpeg", "123")
.execute(client).isSuccess());
.execute(client).isSuccess());

String filePath = createFile("pdf");
assertTrue(new UploadFileRemoteOperation(filePath, "/pdf.pdf", "application/pdf", "123")
.execute(client).isSuccess());
.execute(client).isSuccess());

SearchRemoteOperation sut = new SearchRemoteOperation("image/%", SearchRemoteOperation.SearchType.GALLERY_SEARCH,
false);
SearchRemoteOperation sut = new SearchRemoteOperation("image/%",
SearchRemoteOperation.SearchType.GALLERY_SEARCH,
false);

RemoteOperationResult result = sut.execute(client);
assertTrue(result.isSuccess());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
*/
package com.owncloud.android.lib.resources.files;

import com.owncloud.android.lib.common.network.WebdavEntry;

import org.apache.jackrabbit.webdav.search.SearchInfo;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand All @@ -44,9 +42,12 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import static com.owncloud.android.lib.common.network.WebdavEntry.NAMESPACE_OC;

public class NcSearchMethod extends org.apache.jackrabbit.webdav.client.methods.SearchMethod {
private static final String HEADER_CONTENT_TYPE_VALUE = "text/xml";
private static final String DAV_NAMESPACE = "DAV:";
private static final String OC_NAMESPACE = "oc:";

private SearchRemoteOperation.SearchType searchType;
private long timestamp;
Expand Down Expand Up @@ -98,10 +99,10 @@ private Document createQuery(String searchQuery) {
Element etagElement = query.createElementNS(DAV_NAMESPACE, "d:getetag");
Element quotaUsedElement = query.createElementNS(DAV_NAMESPACE, "d:quota-used-bytes");
Element quotaAvailableElement = query.createElementNS(DAV_NAMESPACE, "d:quota-available-bytes");
Element permissionsElement = query.createElementNS(WebdavEntry.NAMESPACE_OC, "oc:permissions");
Element remoteIdElement = query.createElementNS(WebdavEntry.NAMESPACE_OC, "oc:id");
Element sizeElement = query.createElementNS(WebdavEntry.NAMESPACE_OC, "oc:size");
Element favoriteElement = query.createElementNS(WebdavEntry.NAMESPACE_OC, "oc:favorite");
Element permissionsElement = query.createElementNS(NAMESPACE_OC, "oc:permissions");
Element remoteIdElement = query.createElementNS(NAMESPACE_OC, "oc:id");
Element sizeElement = query.createElementNS(NAMESPACE_OC, "oc:size");
Element favoriteElement = query.createElementNS(NAMESPACE_OC, "oc:favorite");

selectPropsElement.appendChild(displayNameElement);
selectPropsElement.appendChild(contentTypeElement);
Expand Down Expand Up @@ -166,15 +167,15 @@ private Document createQuery(String searchQuery) {
break;

case FAVORITE_SEARCH:
queryElement = query.createElementNS(WebdavEntry.NAMESPACE_OC, "oc:favorite");
queryElement = query.createElementNS(NAMESPACE_OC, "oc:favorite");
break;

case RECENTLY_MODIFIED_SEARCH:
queryElement = query.createElementNS(DAV_NAMESPACE, "d:getlastmodified");
break;

case FILE_ID_SEARCH:
queryElement = query.createElementNS(WebdavEntry.NAMESPACE_OC, "oc:fileid");
queryElement = query.createElementNS(NAMESPACE_OC, "oc:fileid");
break;

default:
Expand Down Expand Up @@ -276,7 +277,24 @@ private Document createQuery(String searchQuery) {
and.appendChild(equalsElement);
whereElement.appendChild(and);
} else {
whereElement.appendChild(equalsElement);
if (searchType == SearchRemoteOperation.SearchType.GALLERY_SEARCH) {
Element and = query.createElementNS(DAV_NAMESPACE, "d:and");
Element lessThan = query.createElementNS(DAV_NAMESPACE, "d:eq");
Element lastModified = query.createElementNS(NAMESPACE_OC, "oc:owner-id");
Element literal = query.createElementNS(DAV_NAMESPACE, "d:literal");
Element prop = query.createElementNS(DAV_NAMESPACE, "d:prop");
prop.appendChild(lastModified);
literal.setTextContent(String.valueOf(userId));

lessThan.appendChild(prop);
lessThan.appendChild(literal);

and.appendChild(lessThan);
and.appendChild(equalsElement);
whereElement.appendChild(and);
} else {
whereElement.appendChild(equalsElement);
}
}

if (searchType == SearchRemoteOperation.SearchType.GALLERY_SEARCH) {
Expand Down

0 comments on commit 2fee764

Please sign in to comment.