Class: Pinnacle::Wrapper::Tools::FileUploader

Inherits:
Tools::File::Client show all
Defined in:
lib/pinnacle/wrapper/tools/file_uploader.rb

Constant Summary collapse

SUPPORTED_MIME_TYPES =
Set.new(
  [
    "audio/mpeg", "audio/mp4", "audio/ogg", "audio/aac",
    "audio/webm", "audio/wav", "audio/3gpp", "audio/amr",
    "video/mp4", "video/mpeg", "video/quicktime", "video/webm",
    "video/3gpp", "video/H264", "video/x-m4v",
    "image/jpeg", "image/png", "image/gif", "image/bmp",
    "image/tiff", "image/webp",
    "application/pdf", "text/csv", "application/rtf",
    "text/vcard", "text/calendar"
  ]
).freeze
MIME_TYPES =
{
  ".mp3" => "audio/mpeg",
  ".mp4" => "video/mp4",
  ".ogg" => "audio/ogg",
  ".aac" => "audio/aac",
  ".webm" => "video/webm",
  ".wav" => "audio/wav",
  ".3gp" => "video/3gpp",
  ".3gpp" => "video/3gpp",
  ".amr" => "audio/amr",
  ".mpeg" => "video/mpeg",
  ".mpg" => "video/mpeg",
  ".mov" => "video/quicktime",
  ".m4v" => "video/x-m4v",
  ".jpg" => "image/jpeg",
  ".jpeg" => "image/jpeg",
  ".png" => "image/png",
  ".gif" => "image/gif",
  ".bmp" => "image/bmp",
  ".tiff" => "image/tiff",
  ".tif" => "image/tiff",
  ".webp" => "image/webp",
  ".pdf" => "application/pdf",
  ".csv" => "text/csv",
  ".rtf" => "application/rtf",
  ".vcf" => "text/vcard",
  ".vcard" => "text/vcard",
  ".ics" => "text/calendar"
}.freeze

Instance Method Summary collapse

Methods inherited from Tools::File::Client

#initialize, #refresh, #upload

Constructor Details

This class inherits a constructor from Pinnacle::Tools::File::Client

Instance Method Details

#upload_from_path(file_path, name: nil, options: nil, request_options: {}) ⇒ String

Upload a file from local filesystem.

Examples:

Upload a file

client = Pinnacle::Client.new(api_key: "your-api-key")
url = client.tools.file.upload_from_path("/path/to/image.png")
puts url # => "https://..."

Upload with custom name

url = client.tools.file.upload_from_path(
  "/path/to/image.png",
  name: "my-custom-name.png"
)

Parameters:

  • file_path (String)

    Path to the file

  • name (String, nil) (defaults to: nil)

    Override filename

  • options (Hash, nil) (defaults to: nil)

    Upload options

  • request_options (Hash) (defaults to: {})

    Additional request options

Returns:

  • (String)

    Download URL

Raises:



75
76
77
78
79
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pinnacle/wrapper/tools/file_uploader.rb', line 75

def upload_from_path(file_path, name: nil, options: nil, request_options: {})
  unless ::File.exist?(file_path)
    raise Pinnacle::Errors::NotFoundError.new(
      "File not found: #{file_path}",
      code: 404
    )
  end

  if ::File.directory?(file_path)
    raise Pinnacle::Errors::ClientError.new(
      "Path is a directory, not a file: #{file_path}",
      code: 400
    )
  end

  size = ::File.size(file_path)
  file_name = name || ::File.basename(file_path)
  content_type = get_mime_type(file_path)

  upload_result = upload(
    content_type: content_type,
    size: size,
    name: file_name,
    options: options,
    request_options: request_options
  )

  if upload_result.upload_url
    uri = URI.parse(upload_result.upload_url)
    file_content = ::File.binread(file_path)

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = uri.scheme == "https"

    request = Net::HTTP::Put.new(uri.request_uri)
    request["Content-Type"] = content_type
    request.body = file_content

    response = http.request(request)

    unless response.is_a?(Net::HTTPSuccess)
      raise Pinnacle::Errors::ServerError.new(
        "Failed to upload file: #{response.code}",
        code: response.code.to_i
      )
    end
  end

  upload_result.download_url
end