When creating nodes via the REST services I cannot get auto conversion to work. I'm sending my requests via JSON. The nodes are created and the videos are successfully created, but they won't auto encode. I upload the video to my server, move the video with the file_move function, then return the file object to my app, which then sends a request to create the node. Here's my PHP script that handles the uploading:

<?php

define('DRUPAL_ROOT', 'xxxxxxxxxxxxxxxxxxx');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
require_once DRUPAL_ROOT . '/includes/file.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

    $filename = $_FILES['video']["name"];
    $tmp = pathinfo($filename);
    $ext = $tmp['extension'];
    $userid = $_POST['userid'];
    global $user;

    function randomName()
    {
        $length = 10;
        $characters = 'qwertyuioplkjhgfdsazxcvbnm';
        $string = '';
        
        for($p = 0; $p < $length; $p++)
        {
            $string .= $characters[mt_rand(0,strlen($characters))];
        }
        return $string;
    }

    $randFileName = randomName();
    $randFileName = $randFileName.".".$ext;
    $target = "xxxxxxxxxxxxxx".$randFileName;

function saveVideo()
{
    global $userid;
    global $randFileName;
    global $target;
   
    $file = new stdClass;
    $file->uid = $userid;
    $file->filename = $randFileName;
    $file->uri = $target;
    $file->filemime = file_get_mimetype($target);
    $file->filesize = filesize($target);
    $file->status = FILE_STATUS_PERMANENT;

    $done= file_move($file, 'public://'.$randFileName, FILE_EXISTS_RENAME);
    file_usage_add($done, 'file', 'node', $userid); 
    
    echo json_encode( (array)$done );
}
    
    if (move_uploaded_file($_FILES['video']['tmp_name'], $target))
    {
            saveVideo();
    }
    else
    {
        echo 'Something went wrong!';
    }

?>

And here is my JSON structure to create the node:

var node = {
                              node:{
                                        title: title,
                                        type:'video', 
                                        body: {
                                              und: [
                                                            { 
                                                                  value: data, 
                                                                  format: 'full_html'
                                                            }
                                                      ]
                                        },
                                        field_video: {
       und: [
            {
                fid: file.fid,
                filename: file.filename,
                uri: file.uri,
                filemime: file.filemime,
                filesize: file.filesize,
                status: file.status,
                timestamp: file.timestamp,
                origname: file.filename
            }
        ]
    },
    field_species: {
                                            und: names
                                        },
            uid: userID,
                              }
                            };

I tried:

field_video: {
       und: [
            {
                fid: file.fid,
                filename: file.filename,
                uri: file.uri,
                filemime: file.filemime,
                filesize: file.filesize,
                status: file.status,
                timestamp: file.timestamp,
                origname: file.filename,
                autoconversion: true
            }

with autoconversion set to true, 'true', 1 and '1' and they all end up being FALSE regardless.

Can anyone point me in the right direction as how I can get the autoconversion to work when creating nodes through REST? They work fine from the site, but 99% of our users will use the app.

Comments

natefollmer’s picture

Fixed - Just changed the JSON structure of the field_video to this:

field_video: {
       und: [
            {
                fid: file.fid
            }

This triggers the auto conversion like normal, then the fields that were omitted are filled in after conversion.

heshanlk’s picture

Component: Video Transcoding » General
Status: Active » Closed (works as designed)