use of com.google.api.services.drive.model.File in project components by Talend.
the class GoogleDriveUtils method createFolder.
/**
* Create a folder in the specified parent folder
*
* @param parentFolderId folder ID where to create folderName
* @param folderName new folder's name
* @return folder ID value
* @throws IOException when operation fails
*/
public String createFolder(String parentFolderId, String folderName) throws IOException {
File createdFolder = new File();
createdFolder.setName(folderName);
createdFolder.setMimeType(MIME_TYPE_FOLDER);
createdFolder.setParents(Collections.singletonList(parentFolderId));
return drive.files().create(createdFolder).setFields("id").execute().getId();
}
use of com.google.api.services.drive.model.File in project components by Talend.
the class GoogleDriveUtils method copyFile.
/**
* @param fileId ID of the fileName to copy
* @param destinationFolderId folder ID where to copy the fileId
* @param newName if not empty rename copy to this name
* @param deleteOriginal remove original fileName (aka mv)
* @return copied fileName ID
* @throws IOException when copy fails
*/
public String copyFile(String fileId, String destinationFolderId, String newName, boolean deleteOriginal) throws IOException {
LOG.debug("[copyFile] fileId: {}; destinationFolderId: {}, newName: {}; deleteOriginal: {}.", fileId, destinationFolderId, newName, deleteOriginal);
File copy = new File();
copy.setParents(Collections.singletonList(destinationFolderId));
if (!newName.isEmpty()) {
copy.setName(newName);
}
File resultFile = drive.files().copy(fileId, copy).setFields("id, parents").execute();
String copiedResourceId = resultFile.getId();
if (deleteOriginal) {
drive.files().delete(fileId).execute();
}
return copiedResourceId;
}
use of com.google.api.services.drive.model.File in project components by Talend.
the class GoogleDriveUtils method copyFolder.
/**
* @param sourceFolderId source folder ID
* @param destinationFolderId folder ID where to copy the sourceFolderId's content
* @param newName folder name to assign
* @return created folder ID
* @throws IOException when operation fails
*/
public String copyFolder(String sourceFolderId, String destinationFolderId, String newName) throws IOException {
LOG.debug("[copyFolder] sourceFolderId: {}; destinationFolderId: {}; newName: {}", sourceFolderId, destinationFolderId, newName);
// create a new folder
String newFolderId = createFolder(destinationFolderId, newName);
// Make a recursive copy of all files/folders inside the source folder
String query = format(Q_IN_PARENTS, sourceFolderId) + Q_AND + Q_NOT_TRASHED;
FileList originals = drive.files().list().setQ(query).execute();
LOG.debug("[copyFolder] Searching for copy {}", query);
for (File file : originals.getFiles()) {
if (file.getMimeType().equals(MIME_TYPE_FOLDER)) {
copyFolder(file.getId(), newFolderId, file.getName());
} else {
copyFile(file.getId(), newFolderId, file.getName(), false);
}
}
return newFolderId;
}
use of com.google.api.services.drive.model.File in project components by Talend.
the class GoogleDriveAbstractListReader method processFolder.
private boolean processFolder() throws IOException {
if (folderId == null && !subFolders.isEmpty()) {
folderId = subFolders.get(0);
subFolders.remove(0);
request.setQ(format(query, folderId));
LOG.debug("query = {} {}.", query, folderId);
}
searchResults.clear();
FileList files = request.execute();
for (File file : files.getFiles()) {
if (canAddSubFolder(file.getMimeType())) {
subFolders.add(file.getId());
}
if (canAddFile(file.getMimeType())) {
searchResults.add(file);
result.totalCount++;
}
}
request.setPageToken(files.getNextPageToken());
searchCount = searchResults.size();
// finished for folderId
if (StringUtils.isEmpty(request.getPageToken()) || searchCount == 0) {
folderId = null;
}
return searchCount > 0;
}
use of com.google.api.services.drive.model.File in project components by Talend.
the class GoogleDriveCreateRuntimeTest method setUp.
@Before
public void setUp() throws Exception {
super.setUp();
properties = new GoogleDriveCreateProperties("test");
properties.setupProperties();
properties = (GoogleDriveCreateProperties) setupConnectionWithInstalledApplicationWithIdAndSecret(properties);
//
properties.parentFolder.setValue(FOLDER_ROOT);
properties.newFolder.setValue(FOLDER_CREATE);
testRuntime = spy(GoogleDriveCreateRuntime.class);
doReturn(drive).when(testRuntime).getDriveService();
File fc = new File();
fc.setId(FOLDER_CREATE_ID);
when(drive.files().create(any(File.class)).setFields(eq("id")).execute()).thenReturn(fc);
//
FileList flA = new FileList();
List<File> fs = new ArrayList<>();
File fA = new File();
fA.setId("A");
fs.add(fA);
flA.setFiles(fs);
when(drive.files().list().setQ(eq(qA)).execute()).thenReturn(flA);
FileList flB = new FileList();
List<File> fsA = new ArrayList<>();
File fB = new File();
fB.setId("B");
fsA.add(fB);
flB.setFiles(fsA);
when(drive.files().list().setQ(eq(qB)).execute()).thenReturn(flB);
FileList flC = new FileList();
List<File> fsC = new ArrayList<>();
File fC = new File();
fC.setId("C");
fsC.add(fC);
flC.setFiles(fsC);
when(drive.files().list().setQ(eq(qC)).execute()).thenReturn(flC);
}
Aggregations