class Pcloud::File

Constants

FILE_CATAGORIES
SUPPORTED_FIND_BY_PARAMS
SUPPORTED_UPDATE_PARAMS

Attributes

category[R]
content_type[R]
created_at[R]
id[R]
is_deleted[R]
modified_at[R]
name[R]
parent_folder_id[R]
path[R]
size[R]

Public Class Methods

exists?(id) click to toggle source
# File lib/pcloud/file.rb, line 77
def exists?(id)
  find(id)
  true
rescue Pcloud::Client::ErrorResponse => e
  return false if e.message == "File not found."
  raise e
end
find(id) click to toggle source
# File lib/pcloud/file.rb, line 85
def find(id)
  parse_one(Client.execute("stat", query: { fileid: id }))
end
find_by(params) click to toggle source
# File lib/pcloud/file.rb, line 89
def find_by(params)
  unless (params.keys - SUPPORTED_FIND_BY_PARAMS).empty?
    raise InvalidParameters.new("Must be one of #{SUPPORTED_FIND_BY_PARAMS}")
  end
  raise InvalidParameters.new(":id takes precedent over :path, please only use one or the other") if params[:path] && params[:id]
  query = { path: params[:path], fileid: params[:id] }.compact
  parse_one(Client.execute("stat", query: query))
end
new(params) click to toggle source
# File lib/pcloud/file.rb, line 25
def initialize(params)
  @id = params.fetch(:id)
  @path = params.fetch(:path)
  @name = params.fetch(:name)
  @content_type = params.fetch(:content_type)
  @category = FILE_CATAGORIES.fetch((params.fetch(:category_id) || 0).to_s)
  @size = params.fetch(:size) # bytes
  @parent_folder_id = params.fetch(:parent_folder_id)
  @is_deleted = params.fetch(:is_deleted) || false
  @created_at = time_from(params.fetch(:created_at))
  @modified_at = time_from(params.fetch(:modified_at))
end
upload(params) click to toggle source
# File lib/pcloud/file.rb, line 98
def upload(params)
  process_upload(params)
end
upload!(params) click to toggle source
# File lib/pcloud/file.rb, line 102
def upload!(params)
  process_upload(params.merge({ overwrite: true }))
end

Private Class Methods

process_upload(params) click to toggle source
# File lib/pcloud/file.rb, line 108
def process_upload(params)
  file = params.fetch(:file)
  raise InvalidParameter.new("The :file parameter must be an instance of Ruby `File`") unless file.is_a?(::File)

  # === pCloud API behavior notes: ===
  # 1. If neither `path` nor `folder_id` is provided, the file will be
  #    uploaded into the users root directory by default.
  # 2. If the `filename` does not match the name of the `file` provided,
  #    pCloud will use the name of the `file` rather than renaming it.
  response = Client.execute(
    "uploadfile",
    body: {
      renameifexists: params[:overwrite] ? 0 : 1,
      path: params[:path],
      folderid: params[:folder_id],
      filename: params[:filename],
      file: file,
    }.compact,
  )
  # This method on the pCloud API can accept multiple uploads at once.
  # For now, this upload interface just takes a single file at a time
  # so we return just one file out of this method.
  uploaded_file = parse_many(response).first
  raise UploadFailed if uploaded_file.nil?
  uploaded_file
rescue KeyError => e
  missing_param = e.message.gsub("key not found: ", "")
  raise MissingParameter.new("#{missing_param} is required")
end

Public Instance Methods

delete() click to toggle source
# File lib/pcloud/file.rb, line 54
def delete
  parse_one(Client.execute("deletefile", query: { fileid: id }))
end
download_url() click to toggle source
# File lib/pcloud/file.rb, line 62
def download_url
  @download_url ||= begin
    file_url_parts = Client.execute(
      "getfilelink",
      query: { fileid: id, forcedownload: 1, skipfilename: 1 }
    )
    "https://#{file_url_parts["hosts"].first}#{file_url_parts["path"]}"
  end
  # This allows us to cache the expensive part of this method, requesting
  # a download URL from pcloud, while maintaining consistency if the file
  # name changes later.
  "#{@download_url}/#{URI.encode_www_form_component(name)}"
end
parent_folder() click to toggle source
# File lib/pcloud/file.rb, line 58
def parent_folder
  @parent_folder ||= Pcloud::Folder.find(parent_folder_id)
end
update(params) click to toggle source
# File lib/pcloud/file.rb, line 38
def update(params)
  unless (params.keys - SUPPORTED_UPDATE_PARAMS).empty?
    raise InvalidParameters.new("Must be one of #{SUPPORTED_UPDATE_PARAMS}")
  end
  if params[:path] && params[:path][0] != "/"
    raise InvalidParameter.new(":path param must start with `/`")
  end
  query = {
    fileid: id,
    tofolderid: params[:parent_folder_id] || nil,
    toname: params[:name] || nil,
    topath: params[:path] || nil
  }.compact
  parse_one(Client.execute("renamefile", query: query))
end