Class: Parse::File
Overview
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.
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_portsfor 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
- .allowed_remote_hosts ⇒ Object
- .allowed_remote_ports ⇒ Object
-
.default_mime_type ⇒ String
The default mime type for created instances.
-
.force_ssl ⇒ Boolean
When set to true, it will make all calls to File#url.
- .max_remote_size ⇒ Object
- .remote_timeout ⇒ Object
- .trusted_url_hosts ⇒ Object
- .untrusted_url_policy ⇒ Object
Instance Attribute Summary collapse
-
#contents ⇒ Object
The contents of the file.
-
#mime_type ⇒ String
The mime-type of the file whe.
-
#name ⇒ String
The name of the file including extension (if any).
Class Method Summary collapse
-
.basename(file_name, suffix = nil) ⇒ String
A proxy method for ::File.basename.
-
.create(url) ⇒ Parse::File
This creates a new Parse File Object with from a URL, saves it and returns it.
- .parse_class ⇒ Model::TYPE_FILE
Instance Method Summary collapse
-
#==(u) ⇒ Boolean
Two files are equal if they have the same url.
- #attributes ⇒ Hash
-
#attributes=(h) ⇒ Object
Allows mass assignment from a Parse JSON hash.
-
#initialize(name, contents = nil, mime_type = nil) ⇒ File
constructor
The initializer to create a new file supports different inputs.
- #parse_class ⇒ Model::TYPE_FILE (also: #__type)
-
#parse_hosted_file? ⇒ Boolean
True if this file is hosted by Parse’s servers.
-
#save ⇒ Boolean
Save the file by uploading it to Parse and creating a file pointer.
-
#saved? ⇒ Boolean
A File object is considered saved if the basename of the URL and the name parameters are equal.
-
#to_s ⇒ String
The url.
-
#url ⇒ String
Returns the url string for this Parse::File pointer.
-
#url=(value) ⇒ Object
Assign the file’s URL.
Methods inherited from Model
Methods included from Client::Connectable
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
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_hosts ⇒ Object
132 133 134 |
# File 'lib/parse/model/file.rb', line 132 def allowed_remote_hosts @allowed_remote_hosts ||= [] end |
.allowed_remote_ports ⇒ Object
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_type ⇒ String
Returns 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_ssl ⇒ Boolean
Returns 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_size ⇒ Object
116 117 118 |
# File 'lib/parse/model/file.rb', line 116 def max_remote_size @max_remote_size ||= DEFAULT_MAX_REMOTE_SIZE end |
.remote_timeout ⇒ Object
122 123 124 |
# File 'lib/parse/model/file.rb', line 122 def remote_timeout @remote_timeout ||= DEFAULT_REMOTE_TIMEOUT end |
.trusted_url_hosts ⇒ Object
152 153 154 |
# File 'lib/parse/model/file.rb', line 152 def trusted_url_hosts @trusted_url_hosts ||= ["files.parsetfss.com"] end |
.untrusted_url_policy ⇒ Object
170 171 172 |
# File 'lib/parse/model/file.rb', line 170 def untrusted_url_policy @untrusted_url_policy ||= :warn end |
Instance Attribute Details
#contents ⇒ Object
Returns the contents of the file.
81 82 83 |
# File 'lib/parse/model/file.rb', line 81 def contents @contents end |
#mime_type ⇒ String
Returns the mime-type of the file whe.
84 85 86 |
# File 'lib/parse/model/file.rb', line 84 def mime_type @mime_type end |
#name ⇒ String
Returns 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
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
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_class ⇒ Model::TYPE_FILE
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.
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 |
#attributes ⇒ Hash
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_class ⇒ Model::TYPE_FILE Also known as: __type
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.
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 |
#save ⇒ Boolean
Save the file by uploading it to Parse and creating a file pointer.
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
357 358 359 |
# File 'lib/parse/model/file.rb', line 357 def saved? @url.present? && @name.present? && @name == File.basename(@url) end |
#to_s ⇒ String
Returns the url.
508 509 510 |
# File 'lib/parse/model/file.rb', line 508 def to_s @url end |
#url ⇒ String
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.
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.
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 |