Search in sources :

Example 1 with BlobbyStorageContainerResource

use of org.datatransferproject.types.transfer.models.blob.BlobbyStorageContainerResource 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)

Example 2 with BlobbyStorageContainerResource

use of org.datatransferproject.types.transfer.models.blob.BlobbyStorageContainerResource in project data-transfer-project by google.

the class DriveImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, IdempotentImportExecutor idempotentExecutor, TokensAndUrlAuthData authData, BlobbyStorageContainerResource data) throws Exception {
    String parentId;
    Drive driveInterface = getDriveInterface(authData);
    // Let the parent ID be empty for the root level
    if (Strings.isNullOrEmpty(data.getId()) || "root".equals(data.getId())) {
        parentId = idempotentExecutor.executeOrThrowException(ROOT_FOLDER_ID, data.getName(), () -> importSingleFolder(driveInterface, "MigratedContent", null));
    } else {
        parentId = idempotentExecutor.getCachedValue(ROOT_FOLDER_ID);
    }
    // Uploads album metadata
    if (data.getFolders() != null && data.getFolders().size() > 0) {
        for (BlobbyStorageContainerResource folder : data.getFolders()) {
            idempotentExecutor.executeAndSwallowIOExceptions(folder.getId(), folder.getName(), () -> importSingleFolder(driveInterface, folder.getName(), parentId));
        }
    }
    // Uploads photos
    if (data.getFiles() != null && data.getFiles().size() > 0) {
        for (DigitalDocumentWrapper file : data.getFiles()) {
            idempotentExecutor.executeAndSwallowIOExceptions(Integer.toString(file.hashCode()), file.getDtpDigitalDocument().getName(), () -> importSingleFile(jobId, driveInterface, file, parentId));
        }
    }
    return ImportResult.OK;
}
Also used : Drive(com.google.api.services.drive.Drive) DigitalDocumentWrapper(org.datatransferproject.types.transfer.models.blob.DigitalDocumentWrapper) BlobbyStorageContainerResource(org.datatransferproject.types.transfer.models.blob.BlobbyStorageContainerResource)

Aggregations

Drive (com.google.api.services.drive.Drive)2 BlobbyStorageContainerResource (org.datatransferproject.types.transfer.models.blob.BlobbyStorageContainerResource)2 DigitalDocumentWrapper (org.datatransferproject.types.transfer.models.blob.DigitalDocumentWrapper)2 List (com.google.api.services.drive.Drive.Files.List)1 File (com.google.api.services.drive.model.File)1 FileList (com.google.api.services.drive.model.FileList)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)1 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)1 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)1 ExportInformation (org.datatransferproject.types.common.ExportInformation)1 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)1 DtpDigitalDocument (org.datatransferproject.types.transfer.models.blob.DtpDigitalDocument)1