/** * Add media type. * * @param MediaType $mediaType * * @return $this */ public function add(MediaType $mediaType) { $this->mediaTypes[(string) $mediaType] = $mediaType; foreach ($mediaType->getMimetypes() as $mimetype) { $this->mimetypeMap[$mimetype] = (string) $mediaType; } return $this; }
public function execute(RequestInterface $request) : HttpResource { $response = $this->transport->fulfill($request); $attributes = $this->parser->parse($request, $response, new Map('string', AttributeInterface::class)); if ($attributes->contains('content_type')) { $mediaType = MediaType::fromString((string) $attributes->get('content_type')->content()); } else { $mediaType = new NullMediaType(); } return new HttpResource($request->url(), $mediaType, $attributes, $response->body()); }
/** * Allow selection and customisation of CMS media types/tags. */ public function getCMSFields() { $fields = parent::getCMSFields(); // Display the media type as read only if media page children exist. $this->AllChildren()->where("ClassName != 'MediaHolder'")->exists() && $this->MediaType()->exists() ? $fields->addFieldToTab('Root.Main', ReadonlyField::create('Media', 'Media Type', $this->MediaType()->Title), 'Title') : $fields->addFieldToTab('Root.Main', DropdownField::create('MediaTypeID', 'Media Type', array_merge(array(0 => ''), MediaType::get()->map()->toArray())), 'Title'); // Allow customisation of the media URL format. $formats = array('Y/m/d/' => 'year/month/day/media', 'Y/m/' => 'year/month/media', 'Y/' => 'year/media', '-' => 'media'); $fields->insertBefore(DropdownField::create('URLFormatting', 'URL Formatting', $formats)->setRightTitle('The <strong>media</strong> URL format'), 'Content'); // Allow customisation of media types, depending on the current CMS user permissions. $fields->findOrMakeTab('Root.ManageMedia.TypesAttributes', 'Types and Attributes'); $fields->findOrMakeTab('Root.ManageMedia')->setTitle('Manage ALL Media'); $fields->addFieldToTab('Root.ManageMedia.TypesAttributes', GridField::create('TypesAttributes', 'Types and Attributes', MediaType::get(), GridFieldConfig_RecordEditor::create()->removeComponentsByType('GridFieldDeleteAction'))->setModelClass('MediaType')); // Allow customisation of media categories and tags. $fields->findOrMakeTab('Root.ManageMedia.CategoriesTags', 'Categories and Tags'); $fields->addFieldToTab('Root.ManageMedia.CategoriesTags', GridField::create('CategoriesTags', 'Categories and Tags', MediaTag::get(), GridFieldConfig_RecordEditor::create()->removeComponentsByType('GridFieldDeleteAction'))->setModelClass('MediaTag')); // Allow extension customisation. $this->extend('updateMediaHolderCMSFields', $fields); return $fields; }
/** * Apply the parent holder media type and update any respective media type attributes. */ public function onBeforeWrite() { parent::onBeforeWrite(); // Set the default media page date. if (is_null($this->Date)) { $this->Date = date('Y-m-d'); } // Confirm that the external link exists. if ($this->ExternalLink) { if (stripos($this->ExternalLink, 'http') === false) { $this->ExternalLink = 'http://' . $this->ExternalLink; } $file_headers = @get_headers($this->ExternalLink); if (!$file_headers || strripos($file_headers[0], '404 Not Found')) { $this->ExternalLink = null; } } // Apply the changes from each media type attribute. foreach ($this->record as $name => $value) { if (strrpos($name, 'MediaAttribute')) { $ID = substr($name, 0, strpos($name, '_')); $attribute = MediaAttribute::get_by_id('MediaAttribute', $ID); $attribute->Content = $value; $attribute->write(); } } // Apply the parent holder media type. $parent = $this->getParent(); if ($parent) { $type = $parent->MediaType(); if ($type->exists()) { $this->MediaTypeID = $type->ID; $type = $type->Title; } else { $existing = MediaType::get_one('MediaType'); $parent->MediaTypeID = $existing->ID; $parent->write(); $this->MediaTypeID = $existing->ID; $type = $existing->Title; } // Merge the default and custom default media types and their respective attributes. $temporary = array(); foreach (self::$custom_defaults as $default => $attributes) { if (isset(self::$page_defaults[$default])) { self::$page_defaults[$default] = array_unique(array_merge(self::$page_defaults[$default], $attributes)); } else { $temporary[$default] = $attributes; } } $defaults = array_merge(self::$page_defaults, $temporary); // Retrieve existing attributes for the respective media type. $attributes = MediaAttribute::get()->innerJoin('MediaPage', 'MediaAttribute.MediaPageID = MediaPage.ID')->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where(array('MediaType.Title = ?' => $type, 'MediaAttribute.LinkID = ?' => -1)); // Apply existing attributes to a new media page. if (!$this->MediaAttributes()->exists()) { if ($attributes->exists()) { foreach ($attributes as $attribute) { // Create a new attribute for each one found. $new = MediaAttribute::create(); $new->OriginalTitle = $attribute->OriginalTitle; $new->Title = $attribute->Title; $new->LinkID = $attribute->ID; $new->MediaPageID = $this->ID; $this->MediaAttributes()->add($new); $new->write(); } } else { if (isset($defaults[$type])) { foreach ($defaults[$type] as $attribute) { $new = MediaAttribute::create(); $new->Title = $attribute; $new->LinkID = -1; $new->MediaPageID = $this->ID; $this->MediaAttributes()->add($new); $new->write(); } } } } else { // Determine whether there are new attributes for this media page. if ($attributes->exists() && isset($defaults[$type])) { $defaults = $defaults[$type]; foreach ($attributes as $attribute) { $title = $attribute->OriginalTitle; foreach ($defaults as $index => $default) { if ($title === $default) { // This attribute already exists. unset($defaults[$index]); // Determine whether this media page requires the attribute. if (!$this->MediaAttributes()->filter('OriginalTitle', $title)->exists()) { // Create a new attribute. $new = MediaAttribute::create(); $new->OriginalTitle = $title; $new->Title = $attribute->Title; $new->LinkID = $attribute->ID; $new->MediaPageID = $this->ID; $this->MediaAttributes()->add($new); $new->write(); } break; } } } if (count($defaults)) { // Create a new attribute for the remaining defaults. foreach ($defaults as $attribute) { $new = MediaAttribute::create(); $new->Title = $attribute; $new->LinkID = -1; $new->MediaPageID = $this->ID; $this->MediaAttributes()->add($new); $new->write(); } } } } } }
public function __construct(string $name, StreamInterface $content) { parent::__construct(basename($name, '.csv') . '.csv', $content, MediaType::fromString('text/csv')); }
public function actionGetImage($id) { if (!($file = \MediaData::model()->findByPk($id))) { throw new CHttpException(404, 'File not found'); } $filepath = $file->getPath(); if (!file_exists($file->getPath())) { throw new CException('File not found on filesystem: ' . $file->getPath()); } //var_dump($file); //die; header('Content-Type: ' . \MediaType::model()->findByPk($file->media_type_id)->type_mime); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Content-Length: ' . $file->original_file_size); ob_clean(); flush(); readfile($filepath); }
/** * Confirm that the current type is valid. */ public function validate() { $result = parent::validate(); // Confirm that the current type has been given a title and doesn't already exist. if ($result->valid() && !$this->Title) { $result->error('"Title" required!'); } else { if ($result->valid() && MediaType::get_one('MediaType', array('ID != ?' => $this->ID, 'Title = ?' => $this->Title))) { $result->error('Type already exists!'); } } // Allow extension customisation. $this->extend('validateMediaType', $result); return $result; }
/** * Apply the parent holder media type and update any respective media type attributes. */ public function onBeforeWrite() { parent::onBeforeWrite(); // Set the default media page date to the current time. if (is_null($this->Date)) { $this->Date = SS_Datetime::now()->Format('Y-m-d H:i:s'); } // Confirm that the external link exists. if ($this->ExternalLink) { if (stripos($this->ExternalLink, 'http') === false) { $this->ExternalLink = 'http://' . $this->ExternalLink; } $file_headers = @get_headers($this->ExternalLink); if (!$file_headers || strripos($file_headers[0], '404 Not Found')) { $this->ExternalLink = null; } } // Apply the changes from each media type attribute. foreach ($this->record as $name => $value) { if (strrpos($name, 'MediaAttribute')) { $ID = substr($name, 0, strpos($name, '_')); $attribute = MediaAttribute::get_by_id('MediaAttribute', $ID); $attribute->Content = $value; $attribute->write(); } } // Apply the parent holder media type. $parent = $this->getParent(); if ($parent) { $type = $parent->MediaType(); if ($type->exists()) { $this->MediaTypeID = $type->ID; $type = $type->Title; } else { $existing = MediaType::get_one('MediaType'); $parent->MediaTypeID = $existing->ID; $parent->write(); $this->MediaTypeID = $existing->ID; $type = $existing->Title; } // Merge the default and custom default media types and their respective attributes. $temporary = array(); foreach (self::$custom_defaults as $default => $attributes) { if (isset(self::$page_defaults[$default])) { self::$page_defaults[$default] = array_unique(array_merge(self::$page_defaults[$default], $attributes)); } else { $temporary[$default] = $attributes; } } $defaults = array_merge(self::$page_defaults, $temporary); // Apply existing attributes to a new media page. if (!$this->MediaAttributes()->exists()) { // Retrieve existing attributes for the respective media type. $attributes = MediaAttribute::get()->innerJoin('MediaPage', 'MediaAttribute.MediaPageID = MediaPage.ID')->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where("MediaType.Title = '" . Convert::raw2sql($type) . "' AND MediaAttribute.LinkID = -1"); if ($attributes->exists()) { foreach ($attributes as $attribute) { // Create a new attribute for each one found. $new = MediaAttribute::create(); $new->Title = $attribute->Title; $new->LinkID = $attribute->ID; $new->MediaPageID = $this->ID; $this->MediaAttributes()->add($new); $new->write(); } } else { if (isset($defaults[$type])) { foreach ($defaults[$type] as $attribute) { $new = MediaAttribute::create(); $new->Title = $attribute; $new->LinkID = -1; $new->MediaPageID = $this->ID; $this->MediaAttributes()->add($new); $new->write(); } } } } } }
/** * Confirm that the current type is valid. */ public function validate() { $result = parent::validate(); // Confirm that the current type has been given a title and doesn't already exist. if ($result->valid() && !$this->Title) { $result->error('"Title" required!'); } else { if ($result->valid() && MediaType::get_one('MediaType', "ID != " . (int) $this->ID . " AND Title = '" . Convert::raw2sql($this->Title) . "'")) { $result->error('Type already exists!'); } } // Allow extension customisation. $this->extend('validateMediaType', $result); return $result; }
/** * Open the file in the given folder * * @param string $folder * @param string $file * * @return FileInterface */ private function open(string $folder, string $file) : FileInterface { $path = $folder . '/' . $file; if ($this->files->contains($path)) { return $this->files->get($path); } if (is_dir($path)) { $object = new Directory($file, (function ($folder) { $handle = opendir($folder); while (($name = readdir($handle)) !== false) { (yield $this->open($folder, $name)); } closedir($handle); })($path)); } else { $object = new File($file, new LazyStream($path), MediaType::fromString(mime_content_type($path))); } $this->files = $this->files->put($path, $object); return $object; }
/** * @expectedException Innmind\Filesystem\Exception\InvalidMediaTypeStringException */ public function testThrowWhenInvalidMediaTypeString() { MediaType::fromString('foo'); }
/** * We check whether we can open all registered IANA content types */ public function testRegisteredIANAMediaTypes() { $dom = new \DOMDocument(); $dom->load(__DIR__ . '/media-types.xml'); $elements = $dom->getElementsByTagName('file'); foreach ($elements as $element) { if ($element->getAttribute('type') == 'template') { $mediaType = new MediaType($element->textContent); $this->assertNotEmpty($mediaType->getType()); $this->assertNotEmpty($mediaType->getSubType()); } } }
public function testMediaType() { $f = new File('foo', new StringStream('bar'), $mt = MediaType::fromString('application/json')); $this->assertSame($mt, $f->mediaType()); }
public static function work(&$controllerContext, &$viewContext) { $viewContext->viewName = 'user-favorites'; $viewContext->meta->title = $viewContext->user->name . ' — Favorites (' . Media::toString($viewContext->media) . ') — ' . Config::$title; $viewContext->meta->description = $viewContext->user->name . '\'s ' . Media::toString($viewContext->media) . ' favorites.'; WebMediaHelper::addHighcharts($viewContext); WebMediaHelper::addTablesorter($viewContext); WebMediaHelper::addInfobox($viewContext); WebMediaHelper::addEntries($viewContext); WebMediaHelper::addCustom($viewContext); $list = $viewContext->user->getMixedUserMedia($viewContext->media); $listNonPlanned = UserMediaFilter::doFilter($list, UserMediaFilter::nonPlanned()); $favCreators = MediaCreatorDistribution::fromEntries($listNonPlanned); $favGenres = MediaGenreDistribution::fromEntries($listNonPlanned); $favYears = MediaYearDistribution::fromEntries($listNonPlanned); $favDecades = MediaDecadeDistribution::fromEntries($listNonPlanned); $favTypes = MediaTypeDistribution::fromEntries($listNonPlanned); $viewContext->yearName = ["", MediaType::toString(1, $viewContext->media), '', 'Dropped/On-Hold']; $favWatchedYears = []; foreach ($favTypes->getGroupsKeys(AbstractDistribution::IGNORE_NULL_KEY | AbstractDistribution::IGNORE_EMPTY_GROUPS) as $type) { if ($type != 1) { $viewContext->yearName[2] .= ucfirst(MediaType::toString($type, $viewContext->media) . '/'); } foreach ($favTypes->getGroupEntries($type) as $entry) { $category = 2; if ($entry->sub_type == 1) { $category = 1; } if ($entry->status == UserListStatus::Dropped || $entry->status == UserListStatus::OnHold) { $category = 3; } $favWatchedYears[$category][MediaYearDistribution::getPublishedYear($entry)][] = $entry; } } $viewContext->yearName[2] = trim($viewContext->yearName[2], '/'); $favWatchedDecades = []; foreach ($favTypes->getGroupsKeys(AbstractDistribution::IGNORE_NULL_KEY | AbstractDistribution::IGNORE_EMPTY_GROUPS) as $type) { foreach ($favTypes->getGroupEntries($type) as $entry) { $category = 2; if ($entry->sub_type == 1) { $category = 1; } if ($entry->status == UserListStatus::Dropped || $entry->status == UserListStatus::OnHold) { $category = 3; } $favWatchedDecades[$category][MediaDecadeDistribution::getPublishedDecade($entry)][] = $entry; } } $maxYear = max(array_keys($favYears->getGroupsKeys(AbstractDistribution::IGNORE_NULL_KEY))); $maxDecade = max(array_keys($favDecades->getGroupsKeys(AbstractDistribution::IGNORE_NULL_KEY))); for ($i = 1; $i < 4; $i++) { if (!isset($favWatchedYears[$i])) { $favWatchedYears[$i][$maxYear] = []; } if (!isset($favWatchedDecades[$i])) { $favWatchedDecades[$i][$maxDecade] = []; } } $viewContext->favCreators = $favCreators; $viewContext->favGenres = $favGenres; $viewContext->favYears = $favYears; $viewContext->favDecades = $favDecades; $viewContext->favTypes = $favTypes; $viewContext->favWatchedYears = $favWatchedYears; $viewContext->favWatchedDecades = $favWatchedDecades; $distMeanScore = []; $distTimeSpent = []; foreach ([$favCreators, $favGenres, $favDecades, $favYears] as $dist) { $meanScore = []; $timeSpent = []; foreach ($dist->getGroupsKeys(AbstractDistribution::IGNORE_NULL_KEY) as $safeKey => $key) { $meanScore[$safeKey] = 0; $timeSpent[$safeKey] = 0; $subEntries = $dist->getGroupEntries($key); $scoreCount = 0; foreach ($subEntries as $entry) { $timeSpent[$safeKey] += $entry->finished_duration; $meanScore[$safeKey] += $entry->score; $scoreCount += $entry->score > 0; } $meanScore[$safeKey] /= max(1, $scoreCount); } $distMeanScore[get_class($dist)] = $meanScore; $distTimeSpent[get_class($dist)] = $timeSpent; } $viewContext->creatorScores = $distMeanScore[get_class($favCreators)]; $viewContext->genreScores = $distMeanScore[get_class($favGenres)]; $viewContext->yearScores = $distMeanScore[get_class($favYears)]; $viewContext->decadeScores = $distMeanScore[get_class($favDecades)]; $viewContext->creatorTimeSpent = $distTimeSpent[get_class($favCreators)]; $viewContext->genreTimeSpent = $distTimeSpent[get_class($favGenres)]; $viewContext->typePercentages = TextHelper::roundPercentages($favTypes->getGroupsSizes()); $viewContext->genreValues = DistributionEvaluator::evaluate($favGenres); $viewContext->creatorValues = DistributionEvaluator::evaluate($favCreators); }