mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 06:21:27 +00:00
Update MediaPipeline, handle cloud object storage
This commit is contained in:
parent
2538673a7b
commit
be6d12fcb6
4 changed files with 98 additions and 8 deletions
|
@ -12,7 +12,7 @@ use Illuminate\Queue\SerializesModels;
|
|||
use ImageOptimizer;
|
||||
use Illuminate\Http\File;
|
||||
use App\Services\MediaPathService;
|
||||
use App\Services\MediaStorageService;
|
||||
use App\Jobs\MediaPipeline\MediaStoragePipeline;
|
||||
|
||||
class ImageUpdate implements ShouldQueue
|
||||
{
|
||||
|
@ -77,6 +77,7 @@ class ImageUpdate implements ShouldQueue
|
|||
$media->size = $total;
|
||||
$media->save();
|
||||
|
||||
MediaStorageService::store($media);
|
||||
MediaStoragePipeline::dispatch($media);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
31
app/Jobs/MediaPipeline/MediaStoragePipeline.php
Normal file
31
app/Jobs/MediaPipeline/MediaStoragePipeline.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs\MediaPipeline;
|
||||
|
||||
use App\Media;
|
||||
use Cache;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use App\Services\MediaStorageService;
|
||||
|
||||
class MediaStoragePipeline implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $media;
|
||||
|
||||
public function __construct(Media $media)
|
||||
{
|
||||
$this->media = $media;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
MediaStorageService::store($this->media);
|
||||
}
|
||||
|
||||
}
|
|
@ -33,7 +33,7 @@ class Media extends Model
|
|||
return $this->cdn_url;
|
||||
}
|
||||
|
||||
if(!empty($this->remote_media) && $this->remote_url) {
|
||||
if($this->remote_media && $this->remote_url) {
|
||||
return $this->remote_url;
|
||||
}
|
||||
|
||||
|
@ -46,10 +46,8 @@ class Media extends Model
|
|||
return $this->thumbnail_url;
|
||||
}
|
||||
|
||||
if($this->remote_url || $this->thumbnail_path) {
|
||||
return url(Storage::url(
|
||||
$this->remote_url ??
|
||||
$this->thumbnail_path));
|
||||
if(!$this->remote_media && $this->thumbnail_path) {
|
||||
return url(Storage::url($this->thumbnail_path));
|
||||
}
|
||||
|
||||
return url(Storage::url('public/no-preview.png'));
|
||||
|
|
|
@ -11,6 +11,7 @@ use Illuminate\Support\Str;
|
|||
use App\Media;
|
||||
use App\Profile;
|
||||
use App\User;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class MediaStorageService {
|
||||
|
||||
|
@ -23,6 +24,17 @@ class MediaStorageService {
|
|||
return;
|
||||
}
|
||||
|
||||
public static function head($url)
|
||||
{
|
||||
$c = new Client();
|
||||
$r = $c->request('HEAD', $url);
|
||||
$h = $r->getHeaders();
|
||||
return [
|
||||
'length' => $h['Content-Length'][0],
|
||||
'mime' => $h['Content-Type'][0]
|
||||
];
|
||||
}
|
||||
|
||||
protected function cloudStore($media)
|
||||
{
|
||||
if($media->remote_media == true) {
|
||||
|
@ -51,6 +63,7 @@ class MediaStorageService {
|
|||
$media->thumbnail_url = $thumbUrl;
|
||||
$media->cdn_url = $url;
|
||||
$media->optimized_url = $url;
|
||||
$media->replicated_at = now();
|
||||
$media->save();
|
||||
if($media->status_id) {
|
||||
Cache::forget('status:transformer:media:attachments:' . $media->status_id);
|
||||
|
@ -59,6 +72,53 @@ class MediaStorageService {
|
|||
|
||||
protected function remoteToCloud($media)
|
||||
{
|
||||
// todo
|
||||
$url = $media->remote_url;
|
||||
|
||||
if(!Helpers::validateUrl($url)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$head = $this->head($media->remote_url);
|
||||
$mimes = [
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
'video/mp4'
|
||||
];
|
||||
|
||||
$mime = $head['mime'];
|
||||
$max_size = (int) config('pixelfed.max_photo_size') * 1000;
|
||||
$media->size = $head['length'];
|
||||
$media->remote_media = true;
|
||||
$media->save();
|
||||
|
||||
if(!in_array($mime, $mimes)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if($head['length'] == $max_size) {
|
||||
return;
|
||||
}
|
||||
|
||||
$ext = $mime == 'image/jpeg' ? '.jpg' : ($mime == 'image/png' ? '.png' : 'mp4');
|
||||
|
||||
$base = MediaPathService::get($media->profile);
|
||||
$path = Str::random(40) . $ext;
|
||||
$tmpBase = storage_path('app/remcache/');
|
||||
$tmpPath = $media->profile_id . '-' . $path;
|
||||
$tmpName = $tmpBase . $tmpPath;
|
||||
$data = file_get_contents($url, false, null, 0, $head['length']);
|
||||
file_put_contents($tmpName, $data);
|
||||
$hash = hash_file('sha256', $tmpName);
|
||||
|
||||
$disk = Storage::disk(config('filesystems.cloud'));
|
||||
$file = $disk->putFileAs($base, new File($tmpName), $path, 'public');
|
||||
$permalink = $disk->url($file);
|
||||
$media->cdn_url = $permalink;
|
||||
$media->original_sha256 = $hash;
|
||||
$media->replicated_at = now();
|
||||
$media->save();
|
||||
|
||||
unlink($tmpName);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue