Java Code Examples for com.google.api.services.drive.model.File#setTitle()

The following examples show how to use com.google.api.services.drive.model.File#setTitle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleDriveResource.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@Path("/create")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response createFile(String title) throws Exception {
    File fileMetadata = new File();
    fileMetadata.setTitle(title);
    HttpContent mediaContent = new ByteArrayContent("text/plain",
            "Hello Camel Quarkus Google Drive".getBytes(StandardCharsets.UTF_8));

    final Map<String, Object> headers = new HashMap<>();
    headers.put("CamelGoogleDrive.content", fileMetadata);
    headers.put("CamelGoogleDrive.mediaContent", mediaContent);
    File response = producerTemplate.requestBodyAndHeaders("google-drive://drive-files/insert", null, headers, File.class);
    return Response
            .created(new URI("https://camel.apache.org/"))
            .entity(response.getId())
            .build();
}
 
Example 2
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 6 votes vote down vote up
@Override
public String getExperimentPackageId(Context context, String directoryId) throws IOException {
  // If we don't know it locally, but have the experiment locally, it must not exist yet. So,
  // create it remotely.
  File folder = new File();
  folder.setTitle(context.getResources().getString(R.string.default_experiment_name));
  folder.setMimeType(FOLDER_MIME_TYPE);
  folder.setParents(Collections.singletonList(new ParentReference().setId(directoryId)));
  File packageId =
      driveApi
          .files()
          .insert(folder)
          .setFields("id")
          .execute();
  return packageId.getId();
}
 
Example 3
Source File: GoogleDriveIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
private static File uploadTestFile(ProducerTemplate template, String testName) {
    File fileMetadata = new File();
    fileMetadata.setTitle(GoogleDriveIntegrationTest.class.getName()+"."+testName+"-"+ UUID.randomUUID().toString());
    final String content = "Camel rocks!\n" //
            + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()) + "\n" //
            + "user: " + System.getProperty("user.name");
    HttpContent mediaContent = new ByteArrayContent("text/plain", content.getBytes(StandardCharsets.UTF_8));

    final Map<String, Object> headers = new HashMap<>();
    // parameter type is com.google.api.services.drive.model.File
    headers.put("CamelGoogleDrive.content", fileMetadata);
    // parameter type is com.google.api.client.http.AbstractInputStreamContent
    headers.put("CamelGoogleDrive.mediaContent", mediaContent);

    return template.requestBodyAndHeaders("google-drive://drive-files/insert", null, headers, File.class);
}
 
Example 4
Source File: BookmarkService.java    From drivemarks with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves or creates the folder with the given
 * name on the root level.
 * @param title
 * @return Retrieved or inserted folder.
 * @throws IOException
 */
public File createOrGetFolder(String title) throws IOException {
  Drive driveService = getDriveService();
  FileList list =
      driveService.files().list().setQ(QUERY_DRIVEMARKS_FOLDER).execute();
  File drivemarksDir = null;
  if (list == null || list.getItems().size() == 0) {
    // create directory
    File newDir = new File();
    newDir.setTitle(title);
    newDir.setMimeType(MIMETYPE_FOLDER);
    newDir.setParents(Arrays.asList(new ParentReference().setId("root")));
    drivemarksDir = driveService.files().insert(newDir).execute();
  } else {
    drivemarksDir = list.getItems().get(0);
  }
  return drivemarksDir;
}
 
Example 5
Source File: DriveSyncService.java    From narrate-android with Apache License 2.0 5 votes vote down vote up
private File getPhotosFolder() {
    try {

        File folder = getFile("photos");

        if (folder == null) {

            File photos = new File();
            photos.setTitle("photos");
            photos.setAppDataContents(true);
            photos.setMimeType(FOLDER_MIME);
            photos.setParents(Arrays.asList(new ParentReference().setId(getAppFolder().getId())));

            photos = service.files().insert(photos).execute();

            return photos;

        } else {
            return folder;
        }

    } catch (Exception e) {
        e.printStackTrace();
        if (!BuildConfig.DEBUG) Crashlytics.logException(e);
    }

    return null;
}
 
Example 6
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
@Override
public void insertExperimentLibraryFile(java.io.File libraryFile) throws IOException {
  File file = new File();
  file.setTitle(EXPERIMENT_LIBRARY_PROTO);
  file.setParents(Collections.singletonList(new ParentReference().setId(APP_DATA_FOLDER)));
  FileContent content = new FileContent(MIME_TYPE, libraryFile);
  driveApi
      .files()
      .insert(file, content)
      .execute();
}
 
Example 7
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
private void updateFile(File serverFile, java.io.File localFile) throws IOException {
  FileContent content = new FileContent(MIME_TYPE, localFile);
  File file = new File();
  file.setTitle(serverFile.getTitle());
  file.setId(serverFile.getId());
  driveApi
      .files()
      .update(serverFile.getId(), file, content)
      .execute();
}
 
Example 8
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
private void insertFile(java.io.File localFile, String packageId) throws IOException {
  FileContent content = new FileContent(MIME_TYPE, localFile);
  File file = new File();
  file.setTitle(localFile.getName());
  file.setParents(Collections.singletonList(new ParentReference().setId(packageId)));
  driveApi
      .files()
      .insert(file, content)
      .execute();
}
 
Example 9
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
@Override
public long insertExperimentProto(
    java.io.File localFile, String packageId, String experimentTitle) throws IOException {
  FileContent content = new FileContent(MIME_TYPE, localFile);

  File file = new File();
  file.setTitle(EXPERIMENT_PROTO_FILE);
  file.setParents(Collections.singletonList(new ParentReference().setId(packageId)));
  driveApi
      .files()
      .insert(file, content)
      .execute();

  File drivePackage = new File();
  drivePackage.setId(packageId);
  drivePackage.setTitle(experimentTitle);
  driveApi
      .files()
      .patch(packageId, drivePackage)
      .execute();

  FileVersion versionProto =
      FileVersion.newBuilder().setMinorVersion(MINOR_VERSION).setVersion(VERSION).build();

  ByteArrayContent versionBytes = new ByteArrayContent(MIME_TYPE, versionProto.toByteArray());

  File version = new File();
  version.setParents(Collections.singletonList(new ParentReference().setId(packageId)));
  version.setTitle(VERSION_PROTO_FILE);
  driveApi.files().insert(version, versionBytes).execute();

  return getExperimentProtoMetadata(packageId).getVersion();
}
 
Example 10
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
@Override
public long updateExperimentProto(
    java.io.File localFile,
    DriveFile serverExperimentProtoMetadata,
    String packageId,
    String experimentTitle)
    throws IOException {
  FileContent content = new FileContent(MIME_TYPE, localFile);

  // We can't re-use the file metadata that we already have, for some opaque reason about fields
  // that are already defined in the metadata we have, and can't be defined for an update.
  File file = new File();
  file.setTitle(serverExperimentProtoMetadata.getTitle());
  file.setId(serverExperimentProtoMetadata.getId());
  driveApi
      .files()
      .update(serverExperimentProtoMetadata.getId(), file, content)
      .execute();

  File drivePackage = new File();
  drivePackage.setId(packageId);
  drivePackage.setTitle(experimentTitle);
  driveApi
      .files()
      .patch(packageId, drivePackage)
      .execute();

  return getExperimentProtoMetadata(packageId).getVersion();
}
 
Example 11
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
@Override
public String createNewSJFolder() throws IOException {
  File folder = new File();
  folder.setTitle(FOLDER_NAME);
  folder.setMimeType(FOLDER_MIME_TYPE);
  folder.setParents(Collections.singletonList(new ParentReference().setId("root")));
  File withId =
      driveApi
          .files()
          .insert(folder)
          .setFields("id")
          .execute();
  return withId.getId();
}
 
Example 12
Source File: TGDriveBrowser.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void createElement(final TGBrowserCallBack<TGBrowserElement> cb, final String name) {
	try {
		File file = new File();
		file.setTitle(name);
		file.setParents(Arrays.asList(new ParentReference().setId(this.folder.getFile().getId())));
		
		cb.onSuccess(new TGDriveBrowserFile(file, this.folder));
	} catch (RuntimeException e) {
		cb.handleError(e);
	}
}
 
Example 13
Source File: BookmarkService.java    From drivemarks with Apache License 2.0 5 votes vote down vote up
/**
 * Inserts a shortcut file into drivemarks folder with the
 * given title and link.
 * @param title
 * @param link
 * @return Inserted {@code File} object.
 * @throws IOException
 */
public File insert(String title, String link) throws IOException {
  Drive driveService = getDriveService();
  File folder = createOrGetFolder("drivemarks");
  // insert bookmark file
  File file = new File();
  file.setTitle(title);
  file.setDescription(link);
  file.setMimeType(MIMETYPE_DRIVEMARK);
  file.setParents(
      Arrays.asList(new ParentReference().setId(folder.getId())));
  return driveService.files().insert(file).execute();
}
 
Example 14
Source File: MediaUploadWorkitemHandler.java    From jbpm-work-items with Apache License 2.0 4 votes vote down vote up
public void executeWorkItem(WorkItem workItem,
                            WorkItemManager workItemManager) {

    Document docToUpload = (Document) workItem.getParameter("DocToUpload");
    String docMimeType = (String) workItem.getParameter("DocMimeType");
    String uploadPath = (String) workItem.getParameter("UploadPath");

    try {

        RequiredParameterValidator.validate(this.getClass(),
                                            workItem);

        Drive drive = auth.getDriveService(appName,
                                           clientSecret);
        File fileMetadata = new File();
        fileMetadata.setTitle(docToUpload.getName());
        fileMetadata.setAlternateLink(docToUpload.getLink());
        if (docToUpload.getLastModified() != null) {
            fileMetadata.setModifiedDate(new DateTime(docToUpload.getLastModified()));
        }

        java.io.File tempDocFile = java.io.File.createTempFile(FilenameUtils.getBaseName(docToUpload.getName()),
                                                               "." + FilenameUtils.getExtension(docToUpload.getName()));
        FileOutputStream fos = new FileOutputStream(tempDocFile);
        fos.write(docToUpload.getContent());
        fos.close();

        FileContent mediaContent = new FileContent(docMimeType,
                                                   tempDocFile);

        Drive.Files.Insert insert = drive.files().insert(fileMetadata,
                                                         mediaContent);
        MediaHttpUploader uploader = insert.getMediaHttpUploader();
        uploader.setDirectUploadEnabled(true);
        uploader.setProgressListener(new MediaUploadProgressListener());
        insert.execute();

        workItemManager.completeWorkItem(workItem.getId(),
                                         null);
    } catch (Exception e) {
        handleException(e);
    }
}
 
Example 15
Source File: validation.java    From oncokb with GNU Affero General Public License v3.0 4 votes vote down vote up
private static void getWorksheets() throws IOException, ServiceException {
    String propFileName = "properties/config.properties";
    Properties prop = new Properties();
    ValidationConfig config = new ValidationConfig();
    InputStream inputStream = config.getStram(propFileName);

    if (inputStream != null) {
        try {
            prop.load(inputStream);
        } catch (IOException ex) {
            Logger.getLogger(validation.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
    }

    String REPORT_PARENT_FOLDER = prop.getProperty("google.report_parent_folder");
    String REPORT_DATA_TEMPLATE = prop.getProperty("google.report_data_template");

    System.out.println("Got drive service");

    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    Date date = new Date();

    String fileName = "Data run " + dateFormat.format(date);
    File file = new File();
    file.setTitle(fileName);
    file.setParents(Arrays.asList(new ParentReference().setId(REPORT_PARENT_FOLDER)));
    file.setDescription("New File created from server");

    System.out.println("Copying file");

    file = driveService.files().copy(REPORT_DATA_TEMPLATE, file).execute();

    System.out.println("Successfully copied file. Start to change file content");

    String fileId = file.getId();
    URL SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full/" + fileId);

    SpreadsheetEntry spreadSheetEntry = spreadsheetService.getEntry(SPREADSHEET_FEED_URL, SpreadsheetEntry.class);

    WorksheetFeed worksheetFeed = spreadsheetService.getFeed(
        spreadSheetEntry.getWorksheetFeedUrl(), WorksheetFeed.class);
    worksheets = worksheetFeed.getEntries();
}