Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #14475. #14613

Merged
merged 4 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

import ch.cyberduck.core.ListProgressListener;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathAttributes;
import ch.cyberduck.core.SimplePathPredicate;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.features.Home;
import ch.cyberduck.core.preferences.HostPreferences;

import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -37,12 +37,14 @@ public class DriveSearchListService extends AbstractDriveListService {
private final DriveSession session;
private final DriveFileIdProvider fileid;
private final String query;
private final DriveAttributesFinderFeature attributes;

public DriveSearchListService(final DriveSession session, final DriveFileIdProvider fileid, final String query) {
super(session, fileid, new HostPreferences(session.getHost()).getInteger("googledrive.list.limit"), DEFAULT_FIELDS);
this.session = session;
this.fileid = fileid;
this.query = query;
this.attributes = new DriveAttributesFinderFeature(session, fileid);
}

@Override
Expand All @@ -56,10 +58,19 @@ protected Set<Path> parents(final Path directory, final File f) throws Backgroun
try {
// Parent may not be current working directory when searching recursively
final Set<Path> tree = new HashSet<>();
final String workdirId = session.getClient().files().get(fileid.getFileId(directory))
.setSupportsAllDrives(new HostPreferences(session.getHost()).getBoolean("googledrive.teamdrive.enable")).execute().getId();
for(String parentid : f.getParents()) {
tree.addAll(this.parents(directory, workdirId, parentid, new ArrayDeque<>()));
if(null == f.getParents()) {
// Shared files do not have parent set
tree.add(DriveHomeFinderService.SHARED_FOLDER_NAME);
}
else {
for(String parentId : f.getParents()) {
final Set<Path> parents = this.parents(parentId, new ArrayDeque<>());
for(Path parent : parents) {
if(parent.isChild(directory) || new SimplePathPredicate(parent).test(directory)) {
tree.add(parent);
}
}
}
}
return tree;
}
Expand All @@ -68,23 +79,26 @@ protected Set<Path> parents(final Path directory, final File f) throws Backgroun
}
}

private Set<Path> parents(final Path directory, final String workdirId, String id, final Deque<File> dequeue) throws IOException {
/**
* @return The IDs of the parent folders which contain the file.
*/
private Set<Path> parents(String fileId, final Deque<File> dequeue) throws IOException {
final Set<Path> tree = new HashSet<>();
while(!workdirId.equals(id)) {
final File f = session.getClient().files().get(id).setFields(String.format("parents,%s", DriveAttributesFinderFeature.DEFAULT_FIELDS)).execute();
while(true) {
final File f = session.getClient().files().get(fileId).setFields(String.format("parents,%s", DriveAttributesFinderFeature.DEFAULT_FIELDS)).execute();
dequeue.push(f);
if(null == f.getParents()) {
break;
}
for(String parentid : f.getParents()) {
tree.addAll(this.parents(directory, workdirId, parentid, new ArrayDeque<>(dequeue)));
id = parentid;
tree.addAll(this.parents(parentid, new ArrayDeque<>(dequeue)));
fileId = parentid;
}
}
Path parent = directory;
Path parent = Home.ROOT;
while(dequeue.size() > 0) {
final File p = dequeue.pop();
parent = new Path(parent, p.getName(), EnumSet.of(Path.Type.directory), new PathAttributes().withFileId(p.getId()));
final File f = dequeue.pop();
parent = new Path(parent, f.getName(), this.toType(f), attributes.toAttributes(f));
}
tree.add(parent);
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.api.services.drive.model.File;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@Category(IntegrationTest.class)
public class DriveSearchListServiceTest extends AbstractDriveTest {
Expand All @@ -45,14 +46,29 @@ public void testQuery() throws Exception {
final Path directory = new DriveDirectoryFeature(session, fileid).mkdir(new Path(DriveHomeFinderService.MYDRIVE_FOLDER, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
final String name = new AlphanumericRandomStringService().random();
final Drive.Files.Create insert = session.getClient().files().create(new File()
.setName(name)
.setParents(Collections.singletonList(fileid.getFileId(directory))));
.setName(name)
.setParents(Collections.singletonList(fileid.getFileId(directory))));
final File execute = insert.execute();
execute.setVersion(1L);
final Path f1 = new Path(directory, name, EnumSet.of(Path.Type.file), new DriveAttributesFinderFeature(session, fileid).toAttributes(execute));
final AttributedList<Path> list = new DriveSearchListService(session, fileid, name).list(DriveHomeFinderService.MYDRIVE_FOLDER, new DisabledListProgressListener());
assertEquals(1, list.size());
assertEquals(f1, list.get(0));
new DriveDeleteFeature(session, fileid).delete(Arrays.asList(f1, directory), new DisabledPasswordCallback(), new Delete.DisabledCallback());
final Path file = new Path(directory, name, EnumSet.of(Path.Type.file), new DriveAttributesFinderFeature(session, fileid).toAttributes(execute));
{
final Path subdirectory = new DriveDirectoryFeature(session, fileid).mkdir(new Path(directory, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
assertTrue(new DriveSearchListService(session, fileid, name).list(subdirectory, new DisabledListProgressListener()).isEmpty());
new DriveDeleteFeature(session, fileid).delete(Collections.singletonList(subdirectory), new DisabledPasswordCallback(), new Delete.DisabledCallback());
}
{
final AttributedList<Path> list = new DriveSearchListService(session, fileid, name).list(DriveHomeFinderService.MYDRIVE_FOLDER, new DisabledListProgressListener());
assertEquals(1, list.size());
assertEquals(file, list.get(0));
}
{
assertTrue(new DriveSearchListService(session, fileid, name).list(DriveHomeFinderService.SHARED_FOLDER_NAME, new DisabledListProgressListener()).isEmpty());
}
{
final AttributedList<Path> list = new DriveSearchListService(session, fileid, name).list(directory, new DisabledListProgressListener());
assertEquals(1, list.size());
assertEquals(file, list.get(0));
}
new DriveDeleteFeature(session, fileid).delete(Arrays.asList(file, directory), new DisabledPasswordCallback(), new Delete.DisabledCallback());
}
}