Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function validateFileExtensions(EmbridgeAssetEntityInterface $asset, $extensions)
 {
     $errors = array();
     $regex = '/\\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i';
     if (!preg_match($regex, $asset->getFilename())) {
         $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions));
     }
     return $errors;
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function upload(EmbridgeAssetEntityInterface $asset, array $metadata = [])
 {
     $this->login();
     $file_path = $this->fileSystem->realpath($asset->getSourcePath());
     $filename = $asset->getFilename();
     // Sanitize metadata values.
     $metadata = array_filter($metadata, 'is_scalar');
     if (isset($metadata['libraries'])) {
         // The libraries value won't stick unless it is a string.
         $metadata['libraries'] = (string) $metadata['libraries'];
     }
     // Build the main request data.
     $json_values = ['description' => $filename];
     $json_values = array_merge($json_values, $metadata);
     $json_request = $this->jsonEncoder->encode($json_values);
     $body = ['multipart' => [['name' => 'jsonrequest', 'contents' => $json_request], ['name' => 'file', 'contents' => file_get_contents($file_path), 'filename' => $filename]]];
     $response_body = $this->doRequest(self::EMBRIDGE_UPLOAD_PATH_DEFAULT, $body);
     $asset->setAssetId($response_body['data']['id']);
     $asset->setSourcePath($response_body['data']['sourcepath']);
     return $asset;
 }