/**
* Uploads a snap.
*
* @todo
* Fix media ID generation; it looks like they're GUIDs now.
*
* @param int $type
* The media type, i.e. MEDIA_IMAGE or MEDIA_VIDEO.
* @param data $data
* The file data to upload.
*
* @return mixed
* The ID of the uploaded media or FALSE on failure.
*/
public function upload($type, $data)
{
// Make sure we're logged in and have a valid access token.
if (!$this->auth_token || !$this->username) {
return FALSE;
}
// To make cURL happy, we write the data to a file first.
$temp = tempnam(sys_get_temp_dir(), 'Snap');
file_put_contents($temp, parent::encryptECB($data));
if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
$cfile = curl_file_create($temp, $type == self::MEDIA_IMAGE ? 'image/jpeg' : 'video/quicktime', 'snap');
}
$media_id = strtoupper($this->username) . '~' . time();
$timestamp = parent::timestamp();
$result = parent::post('/upload', array('media_id' => $media_id, 'type' => $type, 'data' => version_compare(PHP_VERSION, '5.5.0', '>=') ? $cfile : '@' . $temp . ';filename=data', 'timestamp' => $timestamp, 'username' => $this->username), array($this->auth_token, $timestamp), TRUE);
unlink($temp);
return is_null($result) ? $media_id : FALSE;
}