Package com.google.api.services.drive.model

Examples of com.google.api.services.drive.model.File


      try {
        refreshCredential();

        if (item.isType(ItemType.FILE)) {

          final File _parentDriveItem = _getHistoryFolder(item);
          if (_parentDriveItem != null) {

            final File copyOfdriveItem = new File();
            final ParentReference _parentReference = new ParentReference();
            _parentReference.setId(_parentDriveItem.getId());
            copyOfdriveItem.setParents(Arrays.asList(_parentReference));
            // copyOfdriveItem.setTitle(driveItem.getTitle());
            // copyOfdriveItem.setMimeType(driveItem.getMimeType());
            // copyOfdriveItem.setProperties(driveItem.getProperties());
            final File _copyOfDriveItem = service.files().copy(item.getRemoteIdentifier(), copyOfdriveItem).execute();
            if (_copyOfDriveItem == null) {
              throw new CloudsyncException("Could not make a history snapshot of item '" + item.getPath() + "'");
            }
          }
        }
        File driveItem = new File();
        final byte[] data = _prepareDriveItem(driveItem, item, handler, with_filedata);
        if (data == null) {
          driveItem = service.files().update(item.getRemoteIdentifier(), driveItem).execute();
        } else {
          final InputStreamContent params = new InputStreamContent(FILE, new ByteArrayInputStream(data));
          params.setLength(data.length);
          Update updater = service.files().update(item.getRemoteIdentifier(), driveItem, params);
          MediaHttpUploader uploader = updater.getMediaHttpUploader();
          prepareUploader(uploader, data);
          driveItem = updater.execute();
        }
        if (driveItem == null) {
          throw new CloudsyncException("Could not update item '" + item.getPath() + "'");
        } else if (driveItem.getLabels().getTrashed()) {
          throw new CloudsyncException("Remote item '" + item.getPath() + "' [" + driveItem.getId() + "] is trashed\ntry to run with --nocache");
        }
        _addToCache(driveItem, null);
        return;
      } catch (final NoSuchFileException e) {
        throw e;
View Full Code Here


    int retryCount = 0;
    do {
      try {
        refreshCredential();

        final File _parentDriveItem = _getHistoryFolder(item);
        if (_parentDriveItem != null) {

          final ParentReference parentReference = new ParentReference();
          parentReference.setId(_parentDriveItem.getId());
          File driveItem = new File();
          driveItem.setParents(Arrays.asList(parentReference));
          driveItem = service.files().patch(item.getRemoteIdentifier(), driveItem).execute();
          if (driveItem == null) {
            throw new CloudsyncException("Could not make a history snapshot of item '" + item.getPath() + "'");
          }
        } else {
View Full Code Here

    int retryCount = 0;
    do {
      try {
        refreshCredential();

        final File driveItem = _getDriveItem(item);
        final String downloadUrl = driveItem.getDownloadUrl();
        final HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
        return resp.getContent();
      } catch (final IOException e) {
        retryCount = validateException("remote get", item, e, retryCount);
      }
View Full Code Here

  @Override
  public void cleanHistory(final Handler handler) throws CloudsyncException {

    initService(handler);

    final File backupDriveFolder = _getBackupFolder();
    final File parentDriveItem = _getDriveFolder(basePath);

    try {
      refreshCredential();

      final List<File> child_items = new ArrayList<File>();
      for (File file : _readFolder(parentDriveItem.getId())) {

        if (backupDriveFolder.getId().equals(file.getId()) || !file.getTitle().startsWith(backupDriveFolder.getTitle())) {
          continue;
        }
        child_items.add(file);
View Full Code Here

    if (cacheFiles.containsKey(id)) {

      return cacheFiles.get(id);
    }

    File driveItem;

    try {
      driveItem = service.files().get(id).execute();

    } catch (HttpResponseException e) {

      if (e.getStatusCode() == 404) {
        throw new CloudsyncException("Couldn't find remote item '" + item.getPath() + "' [" + id + "]\ntry to run with --nocache");
      }

      throw e;
    }

    if (driveItem.getLabels().getTrashed()) {
      throw new CloudsyncException("Remote item '" + item.getPath() + "' [" + id + "] is trashed\ntry to run with --nocache");
    }

    _addToCache(driveItem, null);
    return driveItem;
View Full Code Here

    if (historyName == null) {
      return null;
    }

    final File driveRoot = _getBackupFolder();
    final List<String> parentDriveTitles = new ArrayList<String>();
    Item parentItem = item;
    do {
      parentItem = parentItem.getParent();
      if (parentItem.getRemoteIdentifier().equals(driveRoot.getId())) {
        break;
      }
      final File parentDriveItem = _getDriveItem(parentItem);
      parentDriveTitles.add(0, parentDriveItem.getTitle());
    } while (true);

    return _getDriveFolder(basePath + SEPARATOR + historyName + SEPARATOR + StringUtils.join(parentDriveTitles, SEPARATOR));
  }
View Full Code Here

  }

  private File _getDriveFolder(final String path) throws CloudsyncException {

    try {
      File parentItem = service.files().get("root").execute();

      final String[] folderNames = StringUtils.split(path, SEPARATOR);

      for (final String name : folderNames) {

        if (cacheParents.containsKey(parentItem.getId() + ':' + name)) {

          parentItem = cacheParents.get(parentItem.getId() + ':' + name);
        } else {

          final String q = "title='" + name + "' and '" + parentItem.getId() + "' in parents and trashed = false";

          final Drive.Files.List request = service.files().list();
          request.setQ(q);
          request.setMaxResults(MAX_RESULTS);

          do {

            FileList files = request.execute();

            final List<File> result = files.getItems();

            // array('q' => q))

            File _parentItem;

            if (result.size() == 0) {

              final File folder = new File();
              folder.setTitle(name);
              folder.setMimeType(FOLDER);
              final ParentReference parentReference = new ParentReference();
              parentReference.setId(parentItem.getId());
              folder.setParents(Arrays.asList(parentReference));
              _parentItem = service.files().insert(folder).execute();
              if (_parentItem == null) {
                throw new CloudsyncException("Could not create folder '" + name + "'");
              }
            } else if (result.size() == 1) {
View Full Code Here

        if (Logging.connectors.isDebugEnabled()) {
          Logging.connectors.debug("GOOGLEDRIVE: Processing document identifier '"
              + nodeId + "'");
        }

        File googleFile = getObject(nodeId);
        if (googleFile == null || (googleFile.containsKey("explicitlyTrashed") && googleFile.getExplicitlyTrashed())) {
          //its deleted, move on
          continue;
        }


        if (Logging.connectors.isDebugEnabled()) {
          Logging.connectors.debug("GOOGLEDRIVE: have this file:\t" + googleFile.getTitle());
        }

        if ("application/vnd.google-apps.folder".equals(googleFile.getMimeType())) {
          //if directory add its children

          if (Logging.connectors.isDebugEnabled()) {
            Logging.connectors.debug("GOOGLEDRIVE: its a directory");
          }

          // adding all the children + subdirs for a folder

          getSession();
          GetChildrenThread t = new GetChildrenThread(nodeId);
          try {
            t.start();
            boolean wasInterrupted = false;
            try {
              XThreadStringBuffer childBuffer = t.getBuffer();
              // Pick up the paths, and add them to the activities, before we join with the child thread.
              while (true) {
                // The only kind of exceptions this can throw are going to shut the process down.
                String child = childBuffer.fetch();
                if (child ==  null)
                  break;
                // Add the pageID to the queue
                activities.addDocumentReference(child, nodeId, RELATIONSHIP_CHILD);
              }
            } catch (InterruptedException e) {
              wasInterrupted = true;
              throw e;
            } catch (ManifoldCFException e) {
              if (e.getErrorCode() == ManifoldCFException.INTERRUPTED)
                wasInterrupted = true;
              throw e;
            } finally {
              if (!wasInterrupted)
                t.finishUp();
            }
          } catch (InterruptedException e) {
            t.interrupt();
            throw new ManifoldCFException("Interrupted: " + e.getMessage(), e,
              ManifoldCFException.INTERRUPTED);
          } catch (java.net.SocketTimeoutException e) {
            Logging.connectors.warn("GOOGLEDRIVE: Socket timeout adding child documents: " + e.getMessage(), e);
            handleIOException(e);
          } catch (InterruptedIOException e) {
            t.interrupt();
            throw new ManifoldCFException("Interrupted: " + e.getMessage(), e,
              ManifoldCFException.INTERRUPTED);
          } catch (IOException e) {
            Logging.connectors.warn("GOOGLEDRIVE: Error adding child documents: " + e.getMessage(), e);
            handleIOException(e);
          }

        } else {
          // its a file
          if (!scanOnly[i]) {
            doLog = true;

            if (Logging.connectors.isDebugEnabled()) {
              Logging.connectors.debug("GOOGLEDRIVE: its a file");
            }

            // We always direct to the PDF
            String documentURI = getUrl(googleFile, "application/pdf");

            // Get the file length
            Long fileLength = googleFile.getFileSize();
            if (fileLength != null) {

              // Unpack the version string
              ArrayList acls = new ArrayList();
              StringBuilder denyAclBuffer = new StringBuilder();
              int index = unpackList(acls,version,0,'+');
              if (index < version.length() && version.charAt(index++) == '+') {
                index = unpack(denyAclBuffer,version,index,'+');
              }

              //otherwise process
              RepositoryDocument rd = new RepositoryDocument();

              // Turn into acls and add into description
              String[] aclArray = new String[acls.size()];
              for (int j = 0; j < aclArray.length; j++) {
                aclArray[j] = (String)acls.get(j);
              }
              rd.setSecurityACL(RepositoryDocument.SECURITY_TYPE_DOCUMENT,aclArray);
              if (denyAclBuffer.length() > 0) {
                String[] denyAclArray = new String[]{denyAclBuffer.toString()};
                rd.setSecurityDenyACL(RepositoryDocument.SECURITY_TYPE_DOCUMENT,denyAclArray);
              }

              // Now do standard stuff
              String mimeType = googleFile.getMimeType();
              DateTime createdDate = googleFile.getCreatedDate();
              DateTime modifiedDate = googleFile.getModifiedDate();
              String extension = googleFile.getFileExtension();
              String title = googleFile.getTitle();
             
              if (mimeType != null)
                rd.setMimeType(mimeType);
              if (createdDate != null)
                rd.setCreatedDate(new Date(createdDate.getValue()));
              if (modifiedDate != null)
                rd.setModifiedDate(new Date(modifiedDate.getValue()));
              if (extension != null)
              {
                if (title == null)
                  title = "";
                rd.setFileName(title + "." + extension);
              }

              // Get general document metadata
              for (Entry<String, Object> entry : googleFile.entrySet()) {
                rd.addField(entry.getKey(), entry.getValue().toString());
              }

              // Fire up the document reading thread
              DocumentReadingThread t = new DocumentReadingThread(documentURI);
View Full Code Here

    // Sort it,
    java.util.Arrays.sort(acls);

    String[] rval = new String[documentIdentifiers.length];
    for (int i = 0; i < rval.length; i++) {
      File googleFile = getObject(documentIdentifiers[i]);
      if (!isDir(googleFile)) {
        String rev = googleFile.getModifiedDate().toStringRfc3339();
        if (StringUtils.isNotEmpty(rev)) {
          StringBuilder sb = new StringBuilder();

          // Acls
          packList(sb,acls,'+');
View Full Code Here

  }

  /** Get an individual document.
  */
  public File getObject(String id) throws IOException {
    File file = drive.files().get(id).execute();
    return file;
  }
View Full Code Here

TOP

Related Classes of com.google.api.services.drive.model.File

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.