save base64 images alogside entity creation I need to save and edit entities via services like {user, nodes, products ...etc}

the problem comes with images posted form mobile apps encoded base64.

I saved the file and stored the object in session to be used in hook_services_request_postprocess_alter to be added to the entity saved by loading it again and  assign the value to the field

// in preprocess
$_SESSION['file'] = file_save_data(base64_decode($args[0]['image']),'public://test.png');

// in postprocess
$product = entity_load('commerce_product', array($result-&gt;product_id));
$wrapper = entity_metadata_wrapper('commerce_product', $product);

//$wrapper-&gt;field_field_user_avatar-&gt;file-&gt;set($_SESSION['file']);
//$wrapper-&gt;field_field_user_avatar['und'][0]['value']=(array)$_SESSION['file']; 
//$wrapper-&gt;save();   
entity_save('commerce_product', $product);


what missing here I have tried many answers either adding the file object to the field when creating the field nor entity saving
It works after getting file mime type here is the code sample

$data = base64_decode($args[1]['avatar']);    
$finfo = finfo_open();
        $mime_type = finfo_buffer($finfo, $data, FILEINFO_MIME_TYPE);
        finfo_close($finfo);
        // get file type png. jpg
        $ext = $mime_type ? str_replace('image/', '', $mime_type) : 'png';
        $file_name = str_replace(' ', '-', $args[1]['name']);


then you we save the file using 

$file = file_save_data($data, 'public://users/'.$file_name.'.'.$ext, FILE_EXISTS_REPLACE);

    $args[1]['field_field_user_picture'] = array(
        'fid' =&gt; $file-&gt;fid,
        'width' =&gt; 100,
        'height' =&gt; 100,
        'alt' =&gt; $args[1]['name'],
        'title' =&gt; $args[1]['name']
    );