2016-09-03 22:13:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Gallery;
|
|
|
|
|
|
|
|
use App\Album;
|
2016-09-05 14:27:20 +01:00
|
|
|
use App\Facade\Theme;
|
2016-09-11 07:19:11 +01:00
|
|
|
use App\Facade\UserConfig;
|
2016-09-09 17:08:35 +01:00
|
|
|
use App\Helpers\DbHelper;
|
2016-10-05 11:49:39 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
2016-09-03 22:13:05 +01:00
|
|
|
use app\Http\Controllers\Admin\AlbumController;
|
|
|
|
use App\Http\Controllers\Controller;
|
2016-09-11 07:19:11 +01:00
|
|
|
use App\Http\Middleware\VerifyCsrfToken;
|
2016-09-03 22:13:05 +01:00
|
|
|
use App\Photo;
|
2017-04-17 15:45:25 +01:00
|
|
|
use App\VisitorHit;
|
2016-10-28 12:59:36 +01:00
|
|
|
use Guzzle\Http\Mimetypes;
|
2016-09-03 22:13:05 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2017-04-16 10:04:47 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2016-09-11 07:19:11 +01:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2016-09-03 22:13:05 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
class PhotoController extends Controller
|
|
|
|
{
|
2016-09-04 21:59:32 +01:00
|
|
|
public function download(Request $request, $albumUrlAlias, $photoFilename)
|
2016-09-03 22:13:05 +01:00
|
|
|
{
|
2017-04-17 21:31:45 +01:00
|
|
|
$album = DbHelper::getAlbumByPath($albumUrlAlias);
|
2016-09-09 17:08:35 +01:00
|
|
|
if (is_null($album))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-15 08:36:17 +01:00
|
|
|
$this->authorizeForUser($this->getUser(), 'view', $album);
|
2016-09-11 07:19:11 +01:00
|
|
|
|
|
|
|
if (UserConfig::get('hotlink_protection'))
|
|
|
|
{
|
|
|
|
$referrer = $request->headers->get('Referer');
|
|
|
|
|
|
|
|
if (!is_null($referrer))
|
|
|
|
{
|
|
|
|
$hostname = parse_url($referrer, PHP_URL_HOST);
|
|
|
|
if (strtolower($hostname) != strtolower($request->getHttpHost()))
|
|
|
|
{
|
|
|
|
App::abort(403);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
App::abort(403);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2016-09-03 22:13:05 +01:00
|
|
|
|
|
|
|
$photo = PhotoController::loadPhotoByAlbumAndFilename($album, $photoFilename);
|
|
|
|
|
2016-09-11 07:19:11 +01:00
|
|
|
$thumbnail = $request->get('t');
|
|
|
|
if (is_null($thumbnail))
|
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
$this->authorizeForUser($this->getUser(), 'photo.download_original', $photo);
|
2016-09-11 07:19:11 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 15:45:25 +01:00
|
|
|
// Record the visit to the photo
|
|
|
|
if (UserConfig::get('enable_visitor_hits'))
|
|
|
|
{
|
|
|
|
DB::transaction(function () use ($album, $photo, $request, $thumbnail)
|
|
|
|
{
|
|
|
|
$photo->hits_download++;
|
|
|
|
$photo->save();
|
|
|
|
|
|
|
|
VisitorHit::fromRequest($request, $album->id, $photo->id, (is_null($thumbnail) ? 'original' : $thumbnail));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
$photoStream = $album->getAlbumSource()->fetchPhotoContent($photo, $thumbnail);
|
|
|
|
$mimeType = Mimetypes::getInstance()->fromFilename($photo->storage_file_name);
|
|
|
|
|
|
|
|
return response()->stream(
|
|
|
|
function() use ($photoStream)
|
|
|
|
{
|
|
|
|
echo $photoStream;
|
|
|
|
},
|
|
|
|
200,
|
|
|
|
[
|
|
|
|
'Content-Length' => $photoStream->getContentLength(),
|
|
|
|
'Content-Type' => $mimeType
|
|
|
|
]
|
|
|
|
);
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 11:49:39 +01:00
|
|
|
public function show(Request $request, $albumUrlAlias, $photoFilename)
|
2016-09-05 14:27:20 +01:00
|
|
|
{
|
2017-04-17 21:31:45 +01:00
|
|
|
$album = DbHelper::getAlbumByPath($albumUrlAlias);
|
2016-09-03 22:13:05 +01:00
|
|
|
if (is_null($album))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-15 08:36:17 +01:00
|
|
|
$this->authorizeForUser($this->getUser(), 'view', $album);
|
2016-09-09 17:08:35 +01:00
|
|
|
|
|
|
|
$photo = PhotoController::loadPhotoByAlbumAndFilename($album, $photoFilename);
|
|
|
|
|
2016-09-11 07:19:11 +01:00
|
|
|
$isOriginalAllowed = Gate::forUser($this->getUser())->allows('photo.download_original', $photo);
|
|
|
|
|
2017-09-12 20:54:29 +01:00
|
|
|
// Load the Next/Previous buttons
|
|
|
|
$thisPhotoDate = is_null($photo->taken_at) ? $photo->created_at : $photo->taken_at;
|
|
|
|
|
|
|
|
$previousPhoto = $album->photos()
|
|
|
|
->where(DB::raw('COALESCE(taken_at, created_at)'), '<', $thisPhotoDate)
|
|
|
|
->orderBy(DB::raw('COALESCE(taken_at, created_at)'), 'desc')
|
|
|
|
->first();
|
|
|
|
$nextPhoto = $album->photos()
|
|
|
|
->where(DB::raw('COALESCE(taken_at, created_at)'), '>', $thisPhotoDate)
|
|
|
|
->orderBy(DB::raw('COALESCE(taken_at, created_at)'))
|
|
|
|
->first();
|
2016-10-05 11:49:39 +01:00
|
|
|
|
2017-04-17 15:45:25 +01:00
|
|
|
// Record the visit to the photo
|
|
|
|
if (UserConfig::get('enable_visitor_hits'))
|
|
|
|
{
|
|
|
|
DB::transaction(function () use ($album, $photo, $request)
|
|
|
|
{
|
|
|
|
$photo->hits++;
|
|
|
|
$photo->save();
|
|
|
|
|
|
|
|
VisitorHit::fromRequest($request, $album->id, $photo->id);
|
|
|
|
});
|
|
|
|
}
|
2017-04-16 10:04:47 +01:00
|
|
|
|
2016-09-09 17:08:35 +01:00
|
|
|
return Theme::render('gallery.photo', [
|
|
|
|
'album' => $album,
|
2016-09-11 07:19:11 +01:00
|
|
|
'is_original_allowed' => $isOriginalAllowed,
|
2017-09-12 20:54:29 +01:00
|
|
|
'next_photo' => $nextPhoto,
|
2016-10-05 11:49:39 +01:00
|
|
|
'photo' => $photo,
|
2017-09-12 20:54:29 +01:00
|
|
|
'previous_photo' => $previousPhoto
|
2016-09-09 17:08:35 +01:00
|
|
|
]);
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return Photo
|
|
|
|
*/
|
|
|
|
public static function loadPhotoByAlbumAndFilename(Album $album, $filename)
|
|
|
|
{
|
|
|
|
$photo = Photo::where([
|
|
|
|
['album_id', $album->id],
|
2016-09-06 19:47:25 +01:00
|
|
|
['storage_file_name', $filename]
|
2016-09-03 22:13:05 +01:00
|
|
|
])->first();
|
|
|
|
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $photo;
|
|
|
|
}
|
|
|
|
}
|