Search in sources :

Example 26 with ExportInformation

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);
}
Also used : FileList(com.google.api.services.drive.model.FileList) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ResultType(org.datatransferproject.spi.transfer.provider.ExportResult.ResultType) DigitalDocumentWrapper(org.datatransferproject.types.transfer.models.blob.DigitalDocumentWrapper) BlobbyStorageContainerResource(org.datatransferproject.types.transfer.models.blob.BlobbyStorageContainerResource) ExportInformation(org.datatransferproject.types.common.ExportInformation) Drive(com.google.api.services.drive.Drive) FileList(com.google.api.services.drive.model.FileList) ArrayList(java.util.ArrayList) List(com.google.api.services.drive.Drive.Files.List) DtpDigitalDocument(org.datatransferproject.types.transfer.models.blob.DtpDigitalDocument) File(com.google.api.services.drive.model.File) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Aggregations

ExportInformation (org.datatransferproject.types.common.ExportInformation)26 Test (org.junit.Test)20 ContainerResource (org.datatransferproject.types.common.models.ContainerResource)10 InOrder (org.mockito.InOrder)10 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)9 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)9 PaginationData (org.datatransferproject.types.common.PaginationData)8 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)8 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)7 MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)4 CalendarContainerResource (org.datatransferproject.types.common.models.calendar.CalendarContainerResource)3 TokensAndUrlAuthData (org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Stack (java.util.Stack)2 UUID (java.util.UUID)2 PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)2 IntPaginationToken (org.datatransferproject.types.common.IntPaginationToken)2 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1