Class: Parse::File

Inherits:
Model
  • Object
show all
Defined in:
lib/parse/model/file.rb

Overview

Note:

The default MIME type for all files is image/jpeg. This can be default can be changed by setting a value to `Parse::File.default_mime_type`.

This class represents a Parse file pointer. `Parse::File` has helper methods to upload Parse files directly to Parse and manage file associations with your classes.

Examples:

file = File.open("file_path.jpg")
contents = file.read
file = Parse::File.new("myimage.jpg", contents , "image/jpeg")
file.saved? # => false
file.save

file.url # https://files.parsetfss.com/....

# or create and upload a remote file (auto-detected mime type)
file = Parse::File.create(some_url)

Constant Summary collapse

LEGACY_FILE_RX =

Regular expression that matches the old legacy Parse hosted file name

/^tfss-[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}-/
ATTRIBUTES =

The default attributes in a Parse File hash.

{  __type: :string, name: :string, url: :string }.freeze

Constants inherited from Model

Model::CLASS_INSTALLATION, Model::CLASS_PRODUCT, Model::CLASS_ROLE, Model::CLASS_SESSION, Model::CLASS_USER, Model::ID, Model::KEY_CLASS_NAME, Model::KEY_CREATED_AT, Model::KEY_OBJECT_ID, Model::KEY_UPDATED_AT, Model::OBJECT_ID, Model::TYPE_ACL, Model::TYPE_BYTES, Model::TYPE_DATE, Model::TYPE_FIELD, Model::TYPE_FILE, Model::TYPE_GEOPOINT, Model::TYPE_NUMBER, Model::TYPE_OBJECT, Model::TYPE_POINTER, Model::TYPE_RELATION

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

find_class

Methods included from Client::Connectable

#client

Constructor Details

#initialize(name, contents = nil, mime_type = nil) ⇒ File

The initializer to create a new file supports different inputs. If the first paramter is a string which starts with 'http', we then download the content of the file (and use the detected mime-type) to set the content and mime_type fields. If the first parameter is a hash, we assume it might be the Parse File hash format which contains url and name fields only. If the first paramter is a Parse::File, then we copy fields over Otherwise, creating a new file requires a name, the actual contents (usually from a File.open(“local.jpg”).read ) and the mime-type

Parameters:

  • name (String)
  • contents (Object) (defaults to: nil)
  • mime_type (String) (defaults to: nil)

    Default see default_mime_type


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/parse/model/file.rb', line 80

def initialize(name, contents = nil, mime_type = nil)
  mime_type ||= Parse::File.default_mime_type

  if name.is_a?(String) && name.start_with?('http') #could be url string
    file = open( name )
    @contents =  file.read
    @name = File.basename file.base_uri.to_s
    @mime_type = file.content_type
  elsif name.is_a?(Hash)
    self.attributes = name
  elsif name.is_a?(::File)
    @contents =  contents || name.read
    @name = File.basename name.to_path
  elsif name.is_a?(Parse::File)
    @name = name.name
    @url = name.url
  else
    @name = name
    @contents = contents
  end
  if @name.blank?
    raise ArgumentError, "Invalid Parse::File initialization with name '#{@name}'"
  end

  @mime_type ||= mime_type

end

Class Attribute Details

.default_mime_typeString

Returns The default mime type for created instances. Default: _'image/jpeg'_

Returns:

  • (String)

    The default mime type for created instances. Default: _'image/jpeg'_


55
56
57
# File 'lib/parse/model/file.rb', line 55

def default_mime_type
  @default_mime_type
end

.force_sslBoolean

Returns When set to true, it will make all calls to File#url

Returns:

  • (Boolean)

    When set to true, it will make all calls to File#url


58
59
60
# File 'lib/parse/model/file.rb', line 58

def force_ssl
  @force_ssl
end

Instance Attribute Details

#contentsObject

Returns the contents of the file.

Returns:

  • (Object)

    the contents of the file.


39
40
41
# File 'lib/parse/model/file.rb', line 39

def contents
  @contents
end

#mime_typeString

Returns the mime-type of the file whe

Returns:

  • (String)

    the mime-type of the file whe


42
43
44
# File 'lib/parse/model/file.rb', line 42

def mime_type
  @mime_type
end

#nameString

Returns the name of the file including extension (if any)

Returns:

  • (String)

    the name of the file including extension (if any)


34
35
36
# File 'lib/parse/model/file.rb', line 34

def name
  @name
end

#urlString

Returns the url string for this Parse::File pointer. If the force_ssl option is set to true, it will make sure it returns a secure url.

Returns:

  • (String)

    the url string for the file.


36
37
38
# File 'lib/parse/model/file.rb', line 36

def url
  @url
end

Class Method Details

.basename(file_name, suffix = nil) ⇒ String

A proxy method for ::File.basename

Parameters:

Returns:

  • (String)

    File.basename(file_name)

See Also:

  • File.basename

161
162
163
164
165
166
167
# File 'lib/parse/model/file.rb', line 161

def self.basename(file_name, suffix = nil)
 if suffix.nil?
   ::File.basename(file_name)
 else
   ::File.basename(file_name, suffix)
 end
end

.create(url) ⇒ Parse::File

This creates a new Parse File Object with from a URL, saves it and returns it

Parameters:

  • url (String)

    A url which will be used to create the file and automatically save it.

Returns:

  • (Parse::File)

    A newly saved file based on contents of url


111
112
113
114
115
116
# File 'lib/parse/model/file.rb', line 111

def self.create(url)
  url = url.url if url.is_a?(Parse::File)
  file = self.new(url)
  file.save
  file
end

.parse_classModel::TYPE_FILE

Returns:


44
# File 'lib/parse/model/file.rb', line 44

def self.parse_class; TYPE_FILE; end

Instance Method Details

#==(u) ⇒ Boolean

Returns Two files are equal if they have the same url

Returns:

  • (Boolean)

    Two files are equal if they have the same url


140
141
142
143
# File 'lib/parse/model/file.rb', line 140

def ==(u)
  return false unless u.is_a?(self.class)
  @url == u.url
end

#attributesHash

Returns:


135
136
137
# File 'lib/parse/model/file.rb', line 135

def attributes
  ATTRIBUTES
end

#attributes=(h) ⇒ Object

Allows mass assignment from a Parse JSON hash.


146
147
148
149
150
151
152
153
154
# File 'lib/parse/model/file.rb', line 146

def attributes=(h)
  if h.is_a?(String)
    @url = h
    @name = File.basename(h)
  elsif h.is_a?(Hash)
    @url = h[FIELD_URL] || h[:url] || @url
    @name = h[FIELD_NAME] || h[:name] || @name
  end
end

#parse_classModel::TYPE_FILE Also known as: __type

Returns:


46
# File 'lib/parse/model/file.rb', line 46

def parse_class; self.class.parse_class; end

#parse_hosted_file?Boolean

Returns true if this file is hosted by Parse's servers.

Returns:

  • (Boolean)

    true if this file is hosted by Parse's servers.


184
185
186
187
# File 'lib/parse/model/file.rb', line 184

def parse_hosted_file?
  return false if @url.blank?
  ::File.basename(@url).starts_with?('tfss-') || @url.starts_with?('http://files.parsetfss.com')
end

#saveBoolean

Save the file by uploading it to Parse and creating a file pointer.

Returns:

  • (Boolean)

    true if successfully uploaded and saved.


171
172
173
174
175
176
177
178
179
180
181
# File 'lib/parse/model/file.rb', line 171

def save
  unless saved? || @contents.nil? || @name.nil?
    response = client.create_file(@name, @contents, @mime_type)
    unless response.error?
      result = response.result
      @name = result[FIELD_NAME] || File.basename(result[FIELD_URL])
      @url = result[FIELD_URL]
    end
  end
  saved?
end

#saved?Boolean

A File object is considered saved if the basename of the URL and the name parameters are equal

Returns:

  • (Boolean)

    true if this file has already been saved.


120
121
122
# File 'lib/parse/model/file.rb', line 120

def saved?
  @url.present? && @name.present? && @name == File.basename(@url)
end

#to_sString

Returns the url

Returns:

See Also:


196
197
198
# File 'lib/parse/model/file.rb', line 196

def to_s
  @url
end