use of org.datatransferproject.types.common.ExportInformation in project data-transfer-project by google.
the class DriveExporter method export.
@Override
public ExportResult<BlobbyStorageContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> optionalExportInformation) throws Exception {
Drive driveInterface = getDriveInterface((authData));
List driveListOperation = driveInterface.files().list();
// If the folder Id isn't specified then use root
String parentId = "root";
if (optionalExportInformation.isPresent()) {
ExportInformation exportInformation = optionalExportInformation.get();
if (exportInformation.getPaginationData() != null) {
StringPaginationToken paginationToken = (StringPaginationToken) exportInformation.getPaginationData();
driveListOperation.setPageToken(paginationToken.getToken());
}
if (exportInformation.getContainerResource() != null) {
BlobbyStorageContainerResource parent = (BlobbyStorageContainerResource) exportInformation.getContainerResource();
parentId = parent.getId();
}
}
driveListOperation.setFields("files(id, name, modifiedTime, mimeType)").setQ(String.format(DRIVE_QUERY_FORMAT, parentId));
ArrayList<DigitalDocumentWrapper> files = new ArrayList<>();
ArrayList<BlobbyStorageContainerResource> folders = new ArrayList<>();
FileList fileList = driveListOperation.execute();
for (File file : fileList.getFiles()) {
if (FOLDER_MIME_TYPE.equals(file.getMimeType())) {
folders.add(new BlobbyStorageContainerResource(file.getName(), file.getId(), null, null));
} else if (FUSION_TABLE_MIME_TYPE.equals(file.getMimeType())) {
monitor.info(() -> "Exporting of fusion tables is not yet supported: " + file);
} else if (MAP_MIME_TYPE.equals(file.getMimeType())) {
monitor.info(() -> "Exporting of maps is not yet supported: " + file);
} else {
try {
InputStream inputStream;
String newMimeType = file.getMimeType();
if (EXPORT_FORMATS.containsKey(file.getMimeType())) {
newMimeType = EXPORT_FORMATS.get(file.getMimeType());
inputStream = driveInterface.files().export(file.getId(), newMimeType).executeMedia().getContent();
} else {
inputStream = driveInterface.files().get(file.getId()).setAlt("media").executeMedia().getContent();
}
jobStore.create(jobId, file.getId(), inputStream);
files.add(new DigitalDocumentWrapper(new DtpDigitalDocument(file.getName(), file.getModifiedTime().toStringRfc3339(), newMimeType), file.getMimeType(), file.getId()));
} catch (Exception e) {
monitor.severe(() -> "Error exporting " + file, e);
}
}
monitor.info(() -> "Exported " + file);
}
ResultType resultType = isDone(fileList) ? ResultType.END : ResultType.CONTINUE;
BlobbyStorageContainerResource result = new BlobbyStorageContainerResource(null, parentId, files, folders);
StringPaginationToken paginationToken = null;
if (!Strings.isNullOrEmpty(fileList.getNextPageToken())) {
paginationToken = new StringPaginationToken(fileList.getNextPageToken());
}
ContinuationData continuationData = new ContinuationData(paginationToken);
folders.forEach(continuationData::addContainerResource);
return new ExportResult<>(resultType, result, continuationData);
}
Aggregations