You are here

public function BlogapiCommunicator::newMedia in Blog API 8

Callback for uploading a new image.

Parameters

$ct: Content type machine name.

$username: Drupal username.

$pass: Drupal password.

$data: The image contents.

Return value

array|object Either an error or the image URL.

File

src/BlogapiCommunicator.php, line 643

Class

BlogapiCommunicator
Class BlogapiCommunicator.

Namespace

Drupal\blogapi

Code

public function newMedia($ct, $username, $pass, $data) {

  // Check user authentication.
  $user = $this
    ->authenticate($username, $pass, TRUE);
  if (!$user) {
    return $this
      ->returnXmlError(self::BLOGAPI_XML_ERROR_AUTH);
  }

  // Check if content type is manageable with blogapi.
  if (!$this
    ->validateBlogId($ct)) {
    return $this
      ->returnXmlError(self::BLOGAPI_XML_ERROR_CT);
  }
  $uri = 'public://' . $data['name'];
  $bits = $data['bits'];
  $entity = file_save_data($bits, $uri);
  if ($entity) {

    // Check the upload filesize.
    $max_filesize = file_upload_max_size();
    if ($max_filesize && $entity
      ->getSize() > $max_filesize) {
      return $this
        ->returnXmlError(self::BLOGAPI_XML_ERROR_IMG_SIZE, $max_filesize);
    }
    $new_uri = $entity
      ->getFileUri();
    $url = Url::fromUri(file_create_url($new_uri))
      ->toString();
    return [
      'url' => $url,
      'struct',
    ];
  }
  return $this
    ->returnXmlError(self::BLOGAPI_XML_ERROR_IMG_SAVE);
}