Merge branch 'master' into bug-api-method

This commit is contained in:
Jeremy Kauffman 2017-03-29 20:43:26 -04:00 committed by GitHub
commit c2bcd04eea
4 changed files with 78 additions and 21 deletions

View file

@ -19,17 +19,39 @@ class LBRY
if ($serverOutput) if ($serverOutput)
{ {
$responseData = json_decode($serverOutput, true); $responseData = json_decode($serverOutput, true);
if (isset($responseData['error']))
if (isset($responseData['error'])) { {
throw new Exception($responseData['error']); throw new Exception($responseData['error']['message'] ?? 'Something unknown went wrong');
} }
if (isset($responseData['result']))
if (isset($responseData['result'])) { {
return $responseData['result']; return $responseData['result'];
} }
throw new Exception('Received unknown response format.');
} }
} }
public static function publishPublicClaim($name, $tmpFileName)
{
$filePath = '/home/lbry/spee.ch/publishes/newupload-' . random_int(1, PHP_INT_MAX);
move_uploaded_file($tmpFileName, $filePath);
$apiResult = LBRY::api('publish', [
'name' => $name,
'bid' => 1,
'file_path' => $filePath,
'description' => 'An image published from spee.ch',
'author' => 'https://spee.ch',
'language' => 'en',
'license' => 'Public Domain',
'nsfw' => 0,
'title' => 'Image published from spee.ch'
]);
return isset($apiResult['claim_id']);
}
public static function findTopPublicFreeClaim($name) public static function findTopPublicFreeClaim($name)
{ {
$claims = LBRY::api('claim_list', ['name' => $name]); $claims = LBRY::api('claim_list', ['name' => $name]);
@ -42,8 +64,9 @@ class LBRY
$freePublicClaims = array_filter($claims['claims'], function($claim) { $freePublicClaims = array_filter($claims['claims'], function($claim) {
$metadata = json_decode($claim['value'], true); $metadata = json_decode($claim['value'], true);
return return
//TODO: Expand these checks //TODO: Expand these checks AND verify it is an image claim!
($metadata['license'] == "Public Domain" || stripos($metadata['license'], 'Creative Commons') !== false) && ($metadata['license'] == "Public Domain" || stripos($metadata['license'], 'Creative Commons') !== false) &&
in_array($metadata['content_type'], ['image/jpeg', 'image/png']) &&
!isset($metadata['fee']); !isset($metadata['fee']);
}); });

View file

@ -4,10 +4,6 @@ if (!defined('ROOT_PAGE')) { die('not allowed'); }
$claim = LBRY::findTopPublicFreeClaim($name); $claim = LBRY::findTopPublicFreeClaim($name);
//echo '<pre>';
//print_r($claim);
//die('over');
if ($claim) if ($claim)
{ {
$getResult = LBRY::api('get', ['name' => $name, 'claim_id' => $claim['claim_id']]); $getResult = LBRY::api('get', ['name' => $name, 'claim_id' => $claim['claim_id']]);
@ -15,13 +11,13 @@ if ($claim)
if (isset($getResult['completed']) && $getResult['completed'] && isset($getResult['download_path'])) if (isset($getResult['completed']) && $getResult['completed'] && isset($getResult['download_path']))
{ {
$path = $getResult['download_path']; $path = $getResult['download_path'];
header('Content-type: image/jpeg'); header('Content-type: ' . $getResult['metadata']['content_type']);
header('Content-length: ' . filesize($path)); header('Content-length: ' . filesize($path));
readfile($getResult['download_path']); readfile($getResult['download_path']);
} }
elseif (isset($getResult['written_bytes'])) elseif (isset($getResult['written_bytes']))
{ {
echo 'This image is on it\'s way... maybe.<br/>'; echo 'This image is on it\'s way...<br/>';
echo 'Received: ' . $getResult['written_bytes'] . " / " . $getResult['total_bytes'] . ' bytes'; echo 'Received: ' . $getResult['written_bytes'] . " / " . $getResult['total_bytes'] . ' bytes';
} }
else else
@ -29,9 +25,14 @@ if ($claim)
echo 'There seems to be a valid claim, but are having trouble retrieving the content.'; echo 'There seems to be a valid claim, but are having trouble retrieving the content.';
} }
} }
elseif (isset($_GET['new']) && $_GET['new'])
{
echo 'Your image is on the way. It can take a few minutes to reach the blockchain and be public. You can refresh this page to check the progress.';
}
else else
{ {
echo 'No valid claim for this name. Make one! https://lbry.io/quickstart'; echo 'No valid claim for this name. Make one!';
include './publish.php';
} }
exit(0); exit(0);

View file

@ -1,24 +1,43 @@
<?php <?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('ROOT_PAGE', 1); define('ROOT_PAGE', 1);
require_once './LBRY.class.php'; require_once './LBRY.class.php';
$name = ltrim(urldecode($_SERVER['REQUEST_URI']), '/'); $name = ltrim(urldecode($_SERVER['REQUEST_URI']), '/');
if ($name) if ($name)
{ {
error_reporting(E_ALL);
ini_set('display_errors', 1);
include './image.php'; include './image.php';
exit(0); exit(0);
} }
if (isset($_POST['publish']) && isset($_POST['name']) && isset($_FILES['file']))
{
$success = LBRY::publishPublicClaim($_POST['name'], $_FILES['file']['tmp_name']);
if ($success)
{
header('Location: /' . $_POST['name'] . '?new=1');
}
else
{
echo '<p>Something went wrong publishing your content. We are only somewhat sorry.</p>';
}
exit(0);
}
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<h1>spee.ch</h1> <h1>spee.ch</h1>
<p>OMG we are live, it is actually happening. It is still happening.</p> <p>spee.ch is a single-serving site that read and publish images to and from the LBRY blockchain.</p>
<p>In just a few hours, this site will morph from this utterly bare 6 lines of HTML into a decentralized, censorship-resistant, truly free image sharing site powered by <a href="https://lbry.io">LBRY</a>. <p>Examples:</p>
<p>You can watch it happen in real time, starting at 6pm EST tonight (March 29th):</p> <ul>
<?php foreach(['thailand', 'doitlive', 'coconuts', 'cow-air-balloon'] as $sampleName): ?>
<li><a href="/<?php echo $sampleName ?>">spee.ch/<?php echo $sampleName ?></a></li>
<?php endforeach ?>
</ul>
<h3>Publish Your Own</h3>
<?php include './publish.php' ?>
<h3>About This Site</h3>
<p>It was built live in a little over 2 hours on March 29th, 2017. You can watch the video here:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/C9LCapt_OYw" frameborder="0" allowfullscreen></iframe> <iframe width="560" height="315" src="https://www.youtube.com/embed/C9LCapt_OYw" frameborder="0" allowfullscreen></iframe>
<p><a href="https://github.com/lbryio/spee.ch">GitHub repo</a></p>

14
publish.php Normal file
View file

@ -0,0 +1,14 @@
<form method="POST" action="/" enctype="multipart/form-data">
<div>
<input type="file" name="file" />
</div>
<?php if (isset($name) && $name != ''): ?>
<input type="hidden" name="name" value="<?php echo $name ?>" />
<?php else: ?>
<div>
lbry://<input type="text" name="name" />
</div>
<?php endif ?>
<input type="submit" name="publish" value="Go" />
<p>Publishing can take a few moments. Please be patient.</p>
</form>