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:

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(
‘name’ => ‘name’,
‘contents’ => ‘api up’
‘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