Class: OmniSocials::Resources::Media

Inherits:
Object
  • Object
show all
Defined in:
lib/omnisocials/resources/media.rb

Overview

Media resource: upload, list, check, rename/move, and delete library files.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Media

Returns a new instance of Media.



7
8
9
# File 'lib/omnisocials/resources/media.rb', line 7

def initialize(client)
  @client = client
end

Instance Method Details

#check(url: nil, media_id: nil, size_bytes: nil, mime: nil) ⇒ Object

POST /media/check - preflight platform compatibility.

Provide one of: a public url, an existing media_id, or size_bytes + mime.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/omnisocials/resources/media.rb', line 97

def check(url: nil, media_id: nil, size_bytes: nil, mime: nil)
  body = Internal.drop_nil(
    {
      "url" => url,
      "media_id" => media_id,
      "size_bytes" => size_bytes,
      "mime" => mime
    }
  )
  @client.request("POST", "/media/check", json: body)
end

#create_upload_urlObject

POST /media/create-upload-url - mint a one-time presigned URL.

Returns "upload_token", "expires_in_seconds", .... POST the file to upload_url as multipart form data (field name "file") with NO auth headers; the token in the URL authenticates the single upload and expires after 10 minutes.



89
90
91
# File 'lib/omnisocials/resources/media.rb', line 89

def create_upload_url
  @client.request("POST", "/media/create-upload-url")
end

#delete(media_id) ⇒ Object

DELETE /media/id - delete a file. Returns nil (204).

Fails with 409 media_in_use if the file is attached to a scheduled or publishing post.



123
124
125
# File 'lib/omnisocials/resources/media.rb', line 123

def delete(media_id)
  @client.request("DELETE", "/media/#{media_id}")
end

#get(media_id) ⇒ Object

GET /media/id - fetch a single library file.



25
26
27
# File 'lib/omnisocials/resources/media.rb', line 25

def get(media_id)
  @client.request("GET", "/media/#{media_id}")
end

#list(limit: nil, offset: nil, search: nil, folder_id: nil) ⇒ Object

GET /media - list library files.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/omnisocials/resources/media.rb', line 12

def list(limit: nil, offset: nil, search: nil, folder_id: nil)
  @client.request(
    "GET", "/media",
    query: {
      "limit" => limit,
      "offset" => offset,
      "search" => search,
      "folder_id" => folder_id
    }
  )
end

#update(media_id, name: nil, folder_id: OmniSocials::NOT_GIVEN) ⇒ Object

PATCH /media/id - rename and/or move a file.

Pass folder_id: nil explicitly to move the file to the root ("All media"); omit it to leave the folder unchanged.



113
114
115
116
117
# File 'lib/omnisocials/resources/media.rb', line 113

def update(media_id, name: nil, folder_id: OmniSocials::NOT_GIVEN)
  body = Internal.drop_nil({ "name" => name })
  body["folder_id"] = folder_id unless folder_id.is_a?(NotGiven)
  @client.request("PATCH", "/media/#{media_id}", json: body)
end

#upload(file:, filename: nil, name: nil, folder: nil, folder_id: nil) ⇒ Object

POST /media/upload - multipart upload (max 100MB inline).

file is an IO, a file path (String or Pathname), or raw bytes (a binary-encoded String, e.g. from File.binread).

A PDF is split into image slides and the response carries slides plus a media_ids array instead of a single data item. For files over 100MB use upload_from_url (up to 1GB) or the create_upload_url presigned flow.



38
39
40
41
42
43
44
45
46
47
# File 'lib/omnisocials/resources/media.rb', line 38

def upload(file:, filename: nil, name: nil, folder: nil, folder_id: nil)
  upload_name, data = Internal.coerce_file(file, filename)
  fields = Internal.drop_nil(
    { "name" => name, "folder" => folder, "folder_id" => folder_id }
  )
  @client.request(
    "POST", "/media/upload",
    multipart: { fields: fields, filename: upload_name, data: data }
  )
end

#upload_from_base64(data:, mime_type:, filename: nil, name: nil, folder: nil, folder_id: nil) ⇒ Object

POST /media/upload-from-base64 - upload base64-encoded data (without a data URI prefix).



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/omnisocials/resources/media.rb', line 68

def upload_from_base64(data:, mime_type:, filename: nil, name: nil,
                       folder: nil, folder_id: nil)
  body = Internal.drop_nil(
    {
      "data" => data,
      "mime_type" => mime_type,
      "filename" => filename,
      "name" => name,
      "folder" => folder,
      "folder_id" => folder_id
    }
  )
  @client.request("POST", "/media/upload-from-base64", json: body)
end

#upload_from_url(url:, filename: nil, name: nil, folder: nil, folder_id: nil) ⇒ Object

POST /media/upload-from-url - server-side fetch, up to 1GB.

Files over 100MB are streamed in the background: the response has data == "processing"; poll get() until it is "ready".



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/omnisocials/resources/media.rb', line 53

def upload_from_url(url:, filename: nil, name: nil, folder: nil, folder_id: nil)
  body = Internal.drop_nil(
    {
      "url" => url,
      "filename" => filename,
      "name" => name,
      "folder" => folder,
      "folder_id" => folder_id
    }
  )
  @client.request("POST", "/media/upload-from-url", json: body)
end