1.8 API Replace media item

Just wondering if there is a way to replace an existing media item using the API and keep the same media ID? The same way you can do it through the GUI and update all layouts at the same time?

After reading through the code, I think I have figured this out.

You must post to /api/library/mcaas/{mediaId}

Just wondering if there is a reason for calling $accessToken->expire(); in the macaas function?

Secondly, every time I upload via the api my files get saved into ā€œapi/libraryā€ rather then my default file store. Am I doing something wrong? How can I save to ā€œfiles/ā€ ?

I think youā€™ve misunderstood how the GUI works.

When you replace a file in the GUI, it always gets a new media ID.

If you choose to update all instances, then the CMS simply visits all the layouts that media is used in and updates the link to the newly uploaded media ID.

The mcaas API call is not what you want at all. I think what youā€™d do is use the media edit route in the API, ensuring you pass the flag to update the media in all layouts:
http://xibo.org.uk/manual-tempel/api/#!/library/libraryEdit

Iā€™m not certain of the exact mechanics of that. Iā€™m sure @dan can add any further pointers if needed.

With regard to the file upload location, at a guess, you perhaps havenā€™t set a fully qualified path to the CMS library in the CMS settings. Ensure the path you entered is complete there.

This is not very well documented iā€™m afraid - when you edit a media item and want to replace the associated file, you need to call the post routine: http://xibo.org.uk/manual-tempel/api/#!/library/libraryAdd

You need to make sure you provide the oldMediaId as a POST parameter, which will actually trigger the replace.

If you are comfortable looking in code this is the end point: https://github.com/xibosignage/xibo-cms/blob/develop/lib/Controller/Library.php#L500

There is an example for adding a new file here: https://github.com/xibosignage/oauth2-xibo-cms/blob/master/example/file.php#L30, in the case of an edit youā€™d just add the oldMediaId to the multi-part post data.

The mcaas route expires tokens because they are ā€œone-shotā€ tokens created for use in media conversion routines (not replacement/edits)

Thanks guys, with both of your suggestions I have it working in poster. Really appreciate your help!

I have Authentication, GET and POST working in PHP, with the exception of the multipart image post. I keep getting ā€œFiletype not allowedā€. I am not using guzzle, just a drupal httpclient. Would it be possible to get any pointers on where I might be going wrong with the below code (similar code works for the Auth request):

$file = fopen($imageURL,ā€˜rā€™);

$post_data = array(
array(
ā€˜nameā€™ => ā€˜nameā€™,
ā€˜contentsā€™ => ā€˜api upā€™
),
array(
ā€˜nameā€™ => ā€˜filesā€™,
ā€˜contentsā€™ => $file
)
);

  $url = $endpoint . 'library';
  $postRequest = new HttpClientRequest($url, array(
    'method' => HttpClientRequest::METHOD_POST,
    'headers' => array('Content-Type' => array('multipart/formdata; boundary=----WebKitFormBoundaryzUd5n1AiZ0wdjlAL')),
    'data' =>  $post_data
  ));
   $postRequest->addHeader('Authorization', 'Bearer ' .  $access_key);

try {
$resp = $client->execute($postRequest);
}
catch (Exception $e) {
watchdog(ā€˜Xiboā€™, ā€˜Failed to POSTā€™);
}

This usually means that the file cannot be retrieved from the HTTP request - iā€™m guessing because itā€™s not formatted as a multipart message.

Iā€™ve not used Drupal before, but from what iā€™ve read Drupal 8 uses Guzzle under the hood. That being said I canā€™t find documentation for HttpClientRequest - it looks like it might be this library?

Do you have any documentation on that library?

I decided just to install guzzle and I now have the Xibo api working with my Drupal site. Itā€™s absolutely brilliant - keep up the good work guys!

1 Like