Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom poster images #103

Merged
merged 17 commits into from Nov 12, 2013
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions data/add_media_poster.sql
@@ -0,0 +1 @@
ALTER TABLE `media` ADD `poster` VARCHAR(255) NULL AFTER `url`;
46 changes: 46 additions & 0 deletions scripts/find404Posters.php
@@ -0,0 +1,46 @@
<?php
require_once dirname(__FILE__).'/../config.inc.php';

$mediahub = new UNL_MediaHub($dsn);

$list = new UNL_MediaHub_MediaList(array(
'filter' => new UNL_MediaHub_MediaList_Filter_WithPoster()
));

$list->options['limit'] = 3000;
$list->run();

if (count($list->items)) {
foreach ($list->items as $media) {
if (empty($media->poster)) {
continue;
}

// Try and get the poster
if (substr($media->poster, 0, 5) == 'http:'
|| substr($media->poster, 0, 6) == 'https:') {
$context = stream_context_create(array('http'=>array(
'method' => 'GET',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a HEAD request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'user_agent' => 'UNL MediaHub/mediahub.unl.edu'
)));

if ($result = @file_get_contents($media->poster, null, $context, -1, 8)) {
// Assume OK, $result would === false if there was a 404
continue;
}

if (false === $http_response_header) {
echo 'DNS failure, did the server move?'.PHP_EOL;
}

foreach ($http_response_header as $header) {
if (strpos($header, 'HTTP/1.1 404 Not Found') !== false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status header will always be the first in the array (no need to check the others). Also, not all servers will respond with HTTP/1.1. Also, servers will not always use the string Not Found. Only the response code is guaranteed by the protocol.

404 is also not the only problematic response.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very true. I was just copy/pasting the find404Media script. Both could use an update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be better to look for 200 instead of HTTP/1.1 404 Not Found. If its not 200 remove it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That would be better and can be easily parse by exploding the first header on whitespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// This file is GONE! Better remove it
echo 'REMOVING POSTER-'.PHP_EOL.'ID: '.$media->id.PHP_EOL.'Title: '.$media->title.PHP_EOL.'POSTER URL: '.$media->poster.PHP_EOL.PHP_EOL;
$media->poster = '';
$media->save();
}
}
}
}
}
4 changes: 4 additions & 0 deletions src/UNL/MediaHub/Media.php
Expand Up @@ -258,6 +258,10 @@ function addTag($newTag)
*/
function getThumbnailURL()
{
if (!empty($this->poster)) {
return $this->poster;
}

return UNL_MediaHub_Controller::$thumbnail_generator.urlencode($this->url);
}

Expand Down
38 changes: 38 additions & 0 deletions src/UNL/MediaHub/MediaList/Filter/WithPoster.php
@@ -0,0 +1,38 @@
<?php
class UNL_MediaHub_MediaList_Filter_WithPoster implements UNL_MediaHub_Filter
{
function __construct()
{

}

function apply(Doctrine_Query &$query)
{
$query->where('m.poster IS NOT NULL AND m.poster != ""');
}

function getLabel()
{
return 'Media with posters';
}

function getType()
{
return '';
}

function getValue()
{
return '';
}

function __toString()
{
return '';
}

public static function getDescription()
{
return 'Find media with custom poster images defined';
}
}
1 change: 1 addition & 0 deletions src/UNL/MediaHub/Models/BaseMedia.php
Expand Up @@ -9,6 +9,7 @@ public function setTableDefinition()
$this->hasColumn('url', 'string', null, array('fixed' => false, 'primary' => false, 'notnull' => true, 'autoincrement' => false,
'regexp' => '/^(https?):\/\/([^\/])+unl\.edu\/.*/',
'notblank' => true));
$this->hasColumn('poster', 'string', null, array('fixed' => false, 'primary' => false, 'notnull' => false, 'autoincrement' => false));
$this->hasColumn('length', 'integer', 4, array('unsigned' => 0, 'primary' => false, 'notnull' => false, 'autoincrement' => false));
$this->hasColumn('type', 'string', null, array('fixed' => false, 'primary' => false, 'notnull' => true, 'autoincrement' => false));
$this->hasColumn('title', 'string', null, array('fixed' => false, 'primary' => false, 'notnull' => true, 'autoincrement' => false));
Expand Down
2 changes: 2 additions & 0 deletions upgrade.php
Expand Up @@ -36,5 +36,7 @@ function exec_sql($db, $sql, $message, $fail_ok = false)
exec_sql($db, file_get_contents(dirname(__FILE__).'/data/add_featured_feed_fields.sql'), 'Adding featured feeds support', true);
exec_sql($db, file_get_contents(dirname(__FILE__).'/data/add_media_privacy.sql'), 'Adding media privacy settings', true);
exec_sql($db, file_get_contents(dirname(__FILE__).'/data/add_media_play_count.sql'), 'Adding media play count', true);
exec_sql($db, file_get_contents(dirname(__FILE__).'/data/add_media_poster.sql'), 'Adding media poster', true);


echo 'Upgrade complete!';
9 changes: 9 additions & 0 deletions www/manager/templates/Feed/Media/Form.tpl.php
Expand Up @@ -69,6 +69,15 @@
?>
</select>
</li>
<?php
$text = '';
if (isset($context->media) && $context->media->isVideo()) {
$text = 'This image will override the one chosen above.';
}
?>
<li><label>URL of custom poster image<span class="helper">If filled in, this image will be displayed as the thumbnail for the media. <?php echo $text; ?></span></label>
<input id="media_poster" name="poster" type="text" class="validate-url" value="<?php echo htmlentities(@$context->media->poster, ENT_QUOTES); ?>" />
</li>
<li style="display:none;"><label for="submit_existing" class="element">&nbsp;</label><div class="element"><input id="submit_existing" name="submit_existing" value="Save" type="submit" /></div></li>
</ol>
</fieldset>
Expand Down
11 changes: 10 additions & 1 deletion www/manager/templates/Media/Preview/Video.tpl.php
Expand Up @@ -5,13 +5,22 @@
<li>Click the "Set Image" button to save this as your image representation.</li>
<li>Continue with the form below.</li>
</ol>

<h5 class="sec_header">Your Image</h5>
<div id="imageOverlay">
<p>We're updating your image, this may take a few minutes depending on video length. <strong>Now is a good time to make sure the information below is up to snuff!</strong></p>
</div>
<img src="<?php echo $context->getThumbnailURL(); ?>" id="thumbnail" alt="Thumbnail preview" />
<a class="action" id="setImage" href="#">Set Image</a>
<div id="poster_picker">
<a class="action" id="setImage" href="#">Set Image</a>
</div>
<div id="poster_picker_disabled">
<p>
The poster picker has been disabled. Enable it by <a id="enable_poster_picker" href="#">removing the custom post image url</a>.
</p>
</div>
</div>

<div id="videoDisplay" class="two_col right">
<?php echo $savvy->render($context, 'MediaPlayer.tpl.php'); ?>
</div>
5 changes: 5 additions & 0 deletions www/templates/html/MediaPlayer/Video.tpl.php
Expand Up @@ -14,6 +14,11 @@
$autoplay = '';
}

//Don't auto play on the addmedia view
if (isset($controller->options['view']) && $controller->options['view'] == 'addmedia') {
$autoplay = '';
}

if (isset($controller->options['autoplay']) && !$controller->options['autoplay']) {
$autoplay = '';
}
Expand Down
33 changes: 32 additions & 1 deletion www/templates/html/scripts/mediaDetails.js
Expand Up @@ -82,7 +82,18 @@ var mediaDetails = function() {
WDN.jQuery('#thumbnail').attr('src', thumbnail.src);
};
thumbnail.onerror = '';
}
},

hidePosterPicker: function() {
WDN.jQuery('#poster_picker').hide();
WDN.jQuery('#poster_picker_disabled').show();
},

showPosterPicker: function() {
WDN.jQuery('#poster_picker').show();
WDN.jQuery('#poster_picker_disabled').hide();
mediaDetails.updateThumbnail();
}
};
}();

Expand All @@ -99,6 +110,26 @@ WDN.jQuery(document).ready(function() {
WDN.jQuery("#fileUpload").hide();
}

if (WDN.jQuery('#media_poster').val() !== '') {
WDN.jQuery('#poster_picker').hide();
} else {
WDN.jQuery('#poster_picker_disabled').hide();
}

WDN.jQuery('#media_poster').on('keyup', function() {
if (this.value == '') {
mediaDetails.showPosterPicker();
} else {
WDN.jQuery('#thumbnail').attr('src', this.value);
mediaDetails.hidePosterPicker();
}
});

WDN.jQuery('#enable_poster_picker').click(function() {
WDN.jQuery('#media_poster').val('');
mediaDetails.showPosterPicker();
});

WDN.jQuery("#mediaSubmit").click(function(event) { //called when a user adds video

if (document.getElementById("file_upload").value == '') {
Expand Down