class GoogleDrive::File

A file in Google Drive, including Google Docs document/spreadsheet/presentation.

Use GoogleDrive::Session#files or GoogleDrive::Session#file_by_title to get this object.

In addition to the methods below, properties defined here are also available as attributes: developers.google.com/drive/v2/reference/files#resource

e.g.,

file.mime_type  # ==> "text/plain"

Attributes

api_file[R]

Wrapped Google::APIClient::Schema::Drive::V2::File object.

Public Instance Methods

acl(params = {}) click to toggle source

Returns GoogleDrive::Acl object for the file.

With the object, you can see and modify people who can access the file. Modifications take effect immediately.

e.g.

# Dumps people who have access:
for entry in file.acl
  p [entry.scope_type, entry.scope, entry.role]
  # => e.g. ["user", "example1@gmail.com", "owner"]
end

# Shares the file with new people:
# NOTE: This sends email to the new people.
file.acl.push(
    {:type => "user", :value => "example2@gmail.com", :role => "reader"})
file.acl.push(
    {:type => "user", :value => "example3@gmail.com", :role => "writer"})

# Changes the role of a person:
file.acl[1].role = "writer"

# Deletes an ACL entry:
file.acl.delete(file.acl[1])
# File lib/google_drive/file.rb, line 288
def acl(params = {})
  if !@acl || params[:reload]
    @acl = Acl.new(@session, self)
  end
  return @acl
end
acl_feed_url() click to toggle source

Deprecated ACL feed URL of the file.

# File lib/google_drive/file.rb, line 61
def acl_feed_url
  return self.document_feed_url + "/acl"
end
available_content_types() click to toggle source

Content types you can specify in methods #download_to_file, #download_to_string, #download_to_io .

This returns zero or one file type. You may be able to download the file in other formats using #export_as_file, #export_as_string, or export_to_io. Use export_links method to get available formats in these methods.

# File lib/google_drive/file.rb, line 89
def available_content_types
  if self.api_file.download_url
    return [self.api_file.mime_type]
  else
    return []
  end
end
copy(title) click to toggle source

Creates copy of this file with the given title.

# File lib/google_drive/file.rb, line 251
def copy(title)
  copied_file = @session.drive.files.copy.request_schema.new({
      "title" => title,
  })
  api_result = @session.execute!(
      :api_method => @session.drive.files.copy,
      :body_object => copied_file,
      :parameters => {"fileId" => self.id})
  return @session.wrap_api_file(api_result.data)
end
Also aliased as: duplicate
delete(permanent = false) click to toggle source

If permanent is false, moves the file to the trash. If permanent is true, deletes the file permanently.

# File lib/google_drive/file.rb, line 226
def delete(permanent = false)
  if permanent
    @session.execute!(
        :api_method => @session.drive.files.delete,
        :parameters => {"fileId" => self.id})
  else
    @session.execute!(
        :api_method => @session.drive.files.trash,
        :parameters => {"fileId" => self.id})
  end
  return nil
end
document_feed_url() click to toggle source

URL of feed used in the deprecated document list feed API.

# File lib/google_drive/file.rb, line 56
def document_feed_url
  return "https://docs.google.com/feeds/default/private/full/" + CGI.escape(self.resource_id)
end
download_to_file(path, params = {}) click to toggle source

Downloads the file to a local file. e.g.

file.download_to_file("/path/to/hoge.txt")

To export the file in other formats, use export_as_file.

# File lib/google_drive/file.rb, line 101
def download_to_file(path, params = {})
  open(path, "wb") do |f|
    download_to_io(f, params)
  end
end
download_to_io(io, params = {}) click to toggle source

Downloads the file and writes it to io.

To export the file in other formats, use export_to_io.

# File lib/google_drive/file.rb, line 119
def download_to_io(io, params = {})
  if !self.api_file.download_url
    raise(GoogleDrive::Error, "Downloading is not supported for this file.")
  end
  # TODO Use streaming if possible.
  api_result = @session.execute!(:uri => self.api_file.download_url)
  io.write(api_result.body)
end
download_to_string(params = {}) click to toggle source

Downloads the file and returns as a String.

To export the file in other formats, use export_as_string.

# File lib/google_drive/file.rb, line 110
def download_to_string(params = {})
  sio = StringIO.new()
  download_to_io(sio, params)
  return sio.string
end
duplicate(title)
Alias for: copy
export_as_file(path, format = nil) click to toggle source

Export the file to path in content type format. If format is nil, it is guessed from the file name.

e.g.,

spreadsheet.export_as_file("/path/to/hoge.csv")
spreadsheet.export_as_file("/path/to/hoge", "text/csv")

If you want to download the file in the original format, use #download_to_file instead.

# File lib/google_drive/file.rb, line 136
def export_as_file(path, format = nil)
  if !format
    format = EXT_TO_CONTENT_TYPE[::File.extname(path).downcase]
    if !format
      raise(ArgumentError,
          ("Cannot guess format from the file name: %s\n" +
           "Specify format argument explicitly.") %
          path)
    end
  end
  open(path, "wb") do |f|
    export_to_io(f, format)
  end
end
export_as_string(format) click to toggle source

Export the file as String in content type format.

e.g.,

spreadsheet.export_as_string("text/csv")

If you want to download the file in the original format, use #download_to_string instead.

# File lib/google_drive/file.rb, line 157
def export_as_string(format)
  sio = StringIO.new()
  export_to_io(sio, format)
  return sio.string
end
export_to_io(io, format) click to toggle source

Export the file to io in content type format.

If you want to download the file in the original format, use #download_to_io instead.

# File lib/google_drive/file.rb, line 166
def export_to_io(io, format)
  mime_type = EXT_TO_CONTENT_TYPE["." + format] || format
  if !self.export_links
    raise(
        GoogleDrive::Error,
        "This file doesn't support exporting. You may still download the file in the " +
        "original format using download_to_file, download_to_string or download_to_io.")
  end
  export_url = self.export_links[mime_type]
  if !export_url
    raise(
        GoogleDrive::Error,
        "This file doesn't support export with mime type %p. Supported mime types: %p" %
            [mime_type, self.export_links.to_hash().keys])
  end
  # TODO Use streaming if possible.
  api_result = @session.execute!(:uri => export_url)
  io.write(api_result.body)
end
human_url() click to toggle source

URL to view/edit the file in a Web browser.

e.g. “docs.google.com/file/d/xxxx/edit

# File lib/google_drive/file.rb, line 79
def human_url
  return self.alternate_link
end
inspect() click to toggle source
# File lib/google_drive/file.rb, line 295
def inspect
  return "\#<%p id=%p title=%p>" % [self.class, self.id, self.title]
end
reload_metadata() click to toggle source

Reloads file metadata such as title and acl.

# File lib/google_drive/file.rb, line 40
def reload_metadata()
  api_result = @session.execute!(
    :api_method => @session.drive.files.get,
    :parameters => { "fileId" => self.id })
  @api_file = api_result.data
  if @acl
    @acl = Acl.new(@session, self)
  end
end
rename(title) click to toggle source

Renames title of the file.

# File lib/google_drive/file.rb, line 240
def rename(title)
  api_result = @session.execute!(
      :api_method => @session.drive.files.patch,
      :body_object => {"title" => title},
      :parameters => {"fileId" => self.id})
  @api_file = api_result.data
end
Also aliased as: title=
resource_id() click to toggle source

Returns #resource_type + “:” + id.

# File lib/google_drive/file.rb, line 51
def resource_id
  return "%s:%s" % [self.resource_type, self.id]
end
resource_type() click to toggle source

The type of resourse. e.g. “document”, “spreadsheet”, “folder”

# File lib/google_drive/file.rb, line 66
def resource_type
  return self.mime_type.slice(/^application\/vnd.google-apps.(.+)$/, 1) || "file"
end
title(params = {}) click to toggle source

Title of the file.

# File lib/google_drive/file.rb, line 71
def title(params = {})
  reload_metadata() if params[:reload]
  return self.api_file.title
end
title=(title)
Alias for: rename
update_from_file(path, params = {}) click to toggle source

Updates the file with the content of the local file.

e.g.

file.update_from_file("/path/to/hoge.txt")
# File lib/google_drive/file.rb, line 199
def update_from_file(path, params = {})
  file_name = ::File.basename(path)
  params = {:file_name => file_name}.merge(params)
  media = new_upload_io(path, params)
  return update_from_media(media, params)
end
update_from_io(io, params = {}) click to toggle source

Reads content from io and updates the file with the content.

# File lib/google_drive/file.rb, line 207
def update_from_io(io, params = {})
  media = new_upload_io(io, params)
  return update_from_media(media, params)
end
update_from_media(media, params = {}) click to toggle source

Reads content from media and updates the file with the content.

# File lib/google_drive/file.rb, line 213
def update_from_media(media, params = {})
  api_result = @session.execute!(
      :api_method => @session.drive.files.update,
      :media => media,
      :parameters => {
        "fileId" => self.id,
        "uploadType" => "media",
      })
  return @session.wrap_api_file(api_result.data)
end
update_from_string(content, params = {}) click to toggle source

Updates the file with content.

e.g.

file.update_from_string("Good bye, world.")
# File lib/google_drive/file.rb, line 190
def update_from_string(content, params = {})
  media = new_upload_io(StringIO.new(content), params)
  return update_from_media(media, params)
end