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)

Defined Under Namespace

Classes: UntrustedHostError

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
DEFAULT_ALLOWED_REMOTE_PORTS =

Restrictive port allowlist for Parse::File URL fetches. By default only the standard HTTP/HTTPS ports are permitted. Operators may extend Parse::File.allowed_remote_ports for legitimate non-standard CDN ports.

[80, 443, 8080, 8443].freeze

Constants inherited from Model

Model::CLASS_AUDIENCE, Model::CLASS_INSTALLATION, Model::CLASS_JOB_SCHEDULE, Model::CLASS_JOB_STATUS, Model::CLASS_PRODUCT, Model::CLASS_PUSH_STATUS, Model::CLASS_ROLE, Model::CLASS_SCHEMA, 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_POLYGON, Model::TYPE_RELATION

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#dirty?, 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



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/parse/model/file.rb', line 318

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 = Parse::File.safe_open_url(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

.allowed_remote_hostsObject



132
133
134
# File 'lib/parse/model/file.rb', line 132

def allowed_remote_hosts
  @allowed_remote_hosts ||= []
end

.allowed_remote_portsObject



176
177
178
# File 'lib/parse/model/file.rb', line 176

def allowed_remote_ports
  @allowed_remote_ports ||= DEFAULT_ALLOWED_REMOTE_PORTS.dup
end

.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’_



104
105
106
# File 'lib/parse/model/file.rb', line 104

def default_mime_type
  @default_mime_type ||= "image/jpeg"
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



109
110
111
# File 'lib/parse/model/file.rb', line 109

def force_ssl
  @force_ssl ||= false
end

.max_remote_sizeObject



116
117
118
# File 'lib/parse/model/file.rb', line 116

def max_remote_size
  @max_remote_size ||= DEFAULT_MAX_REMOTE_SIZE
end

.remote_timeoutObject



122
123
124
# File 'lib/parse/model/file.rb', line 122

def remote_timeout
  @remote_timeout ||= DEFAULT_REMOTE_TIMEOUT
end

.trusted_url_hostsObject



152
153
154
# File 'lib/parse/model/file.rb', line 152

def trusted_url_hosts
  @trusted_url_hosts ||= ["files.parsetfss.com"]
end

.untrusted_url_policyObject



170
171
172
# File 'lib/parse/model/file.rb', line 170

def untrusted_url_policy
  @untrusted_url_policy ||= :warn
end

Instance Attribute Details

#contentsObject

Returns the contents of the file.

Returns:

  • (Object)

    the contents of the file.



81
82
83
# File 'lib/parse/model/file.rb', line 81

def contents
  @contents
end

#mime_typeString

Returns the mime-type of the file whe.

Returns:

  • (String)

    the mime-type of the file whe



84
85
86
# File 'lib/parse/model/file.rb', line 84

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)



70
71
72
# File 'lib/parse/model/file.rb', line 70

def name
  @name
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


473
474
475
476
477
478
479
# File 'lib/parse/model/file.rb', line 473

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



348
349
350
351
352
353
# File 'lib/parse/model/file.rb', line 348

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:



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

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



377
378
379
380
# File 'lib/parse/model/file.rb', line 377

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

#attributesHash

Returns:



372
373
374
# File 'lib/parse/model/file.rb', line 372

def attributes
  ATTRIBUTES
end

#attributes=(h) ⇒ Object

Allows mass assignment from a Parse JSON hash.



383
384
385
386
387
388
389
390
391
392
393
# File 'lib/parse/model/file.rb', line 383

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

#parse_classModel::TYPE_FILE Also known as: __type

Returns:



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

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.



496
497
498
499
# File 'lib/parse/model/file.rb', line 496

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.



483
484
485
486
487
488
489
490
491
492
493
# File 'lib/parse/model/file.rb', line 483

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.



357
358
359
# File 'lib/parse/model/file.rb', line 357

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

#to_sString

Returns the url.

Returns:

See Also:



508
509
510
# File 'lib/parse/model/file.rb', line 508

def to_s
  @url
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.



364
365
366
367
368
369
# File 'lib/parse/model/file.rb', line 364

def url
  if @url.present? && Parse::File.force_ssl && @url.starts_with?("http://")
    return @url.sub("http://", "https://")
  end
  @url
end

#url=(value) ⇒ Object

Assign the file’s URL. Routes through the same sanitize_hydrated_url validator that hydration uses, so caller-supplied URLs (e.g. ‘parse_file.url = params`) get the same trusted-host check as JSON-hydrated rows.

Parameters:

  • value (String, nil)

    the URL to assign.



76
77
78
# File 'lib/parse/model/file.rb', line 76

def url=(value)
  @url = Parse::File.sanitize_hydrated_url(value, fallback: @url, name: @name)
end