MediaStorageService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace App\Services;
  3. use App\Util\ActivityPub\Helpers;
  4. use Illuminate\Http\File;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Redis;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Str;
  9. use App\Media;
  10. use App\Profile;
  11. use App\User;
  12. use GuzzleHttp\Client;
  13. use App\Http\Controllers\AvatarController;
  14. use GuzzleHttp\Exception\RequestException;
  15. class MediaStorageService {
  16. public static function store(Media $media)
  17. {
  18. if(config('pixelfed.cloud_storage') == true) {
  19. (new self())->cloudStore($media);
  20. }
  21. return;
  22. }
  23. public static function avatar($avatar)
  24. {
  25. return (new self())->fetchAvatar($avatar);
  26. }
  27. public static function head($url)
  28. {
  29. $c = new Client();
  30. try {
  31. $r = $c->request('HEAD', $url);
  32. } catch (RequestException $e) {
  33. return false;
  34. }
  35. $h = $r->getHeaders();
  36. return [
  37. 'length' => $h['Content-Length'][0],
  38. 'mime' => $h['Content-Type'][0]
  39. ];
  40. }
  41. protected function cloudStore($media)
  42. {
  43. if($media->remote_media == true) {
  44. (new self())->remoteToCloud($media);
  45. } else {
  46. (new self())->localToCloud($media);
  47. }
  48. }
  49. protected function localToCloud($media)
  50. {
  51. $path = storage_path('app/'.$media->media_path);
  52. $thumb = storage_path('app/'.$media->thumbnail_path);
  53. $p = explode('/', $media->media_path);
  54. $name = array_pop($p);
  55. $pt = explode('/', $media->thumbnail_path);
  56. $thumbname = array_pop($pt);
  57. $storagePath = implode('/', $p);
  58. $disk = Storage::disk(config('filesystems.cloud'));
  59. $file = $disk->putFileAs($storagePath, new File($path), $name, 'public');
  60. $url = $disk->url($file);
  61. $thumbFile = $disk->putFileAs($storagePath, new File($thumb), $thumbname, 'public');
  62. $thumbUrl = $disk->url($thumbFile);
  63. $media->thumbnail_url = $thumbUrl;
  64. $media->cdn_url = $url;
  65. $media->optimized_url = $url;
  66. $media->replicated_at = now();
  67. $media->save();
  68. if($media->status_id) {
  69. Cache::forget('status:transformer:media:attachments:' . $media->status_id);
  70. }
  71. }
  72. protected function remoteToCloud($media)
  73. {
  74. $url = $media->remote_url;
  75. if(!Helpers::validateUrl($url)) {
  76. return;
  77. }
  78. $head = $this->head($media->remote_url);
  79. if(!$head) {
  80. return;
  81. }
  82. $mimes = [
  83. 'image/jpeg',
  84. 'image/png',
  85. 'video/mp4'
  86. ];
  87. $mime = $head['mime'];
  88. $max_size = (int) config('pixelfed.max_photo_size') * 1000;
  89. $media->size = $head['length'];
  90. $media->remote_media = true;
  91. $media->save();
  92. if(!in_array($mime, $mimes)) {
  93. return;
  94. }
  95. if($head['length'] >= $max_size) {
  96. return;
  97. }
  98. switch ($mime) {
  99. case 'image/png':
  100. $ext = '.png';
  101. break;
  102. case 'image/gif':
  103. $ext = '.gif';
  104. break;
  105. case 'image/jpeg':
  106. $ext = '.jpg';
  107. break;
  108. case 'video/mp4':
  109. $ext = '.mp4';
  110. break;
  111. }
  112. $base = MediaPathService::get($media->profile);
  113. $path = Str::random(40) . $ext;
  114. $tmpBase = storage_path('app/remcache/');
  115. $tmpPath = $media->profile_id . '-' . $path;
  116. $tmpName = $tmpBase . $tmpPath;
  117. $data = file_get_contents($url, false, null, 0, $head['length']);
  118. file_put_contents($tmpName, $data);
  119. $hash = hash_file('sha256', $tmpName);
  120. $disk = Storage::disk(config('filesystems.cloud'));
  121. $file = $disk->putFileAs($base, new File($tmpName), $path, 'public');
  122. $permalink = $disk->url($file);
  123. $media->media_path = $base . $path;
  124. $media->cdn_url = $permalink;
  125. $media->original_sha256 = $hash;
  126. $media->replicated_at = now();
  127. $media->save();
  128. if($media->status_id) {
  129. Cache::forget('status:transformer:media:attachments:' . $media->status_id);
  130. }
  131. unlink($tmpName);
  132. }
  133. protected function fetchAvatar($avatar)
  134. {
  135. $url = $avatar->remote_url;
  136. if($url == null || Helpers::validateUrl($url) == false) {
  137. return;
  138. }
  139. $head = $this->head($url);
  140. if($head == false) {
  141. return;
  142. }
  143. $mimes = [
  144. 'image/jpeg',
  145. 'image/png',
  146. ];
  147. $mime = $head['mime'];
  148. $max_size = (int) config('pixelfed.max_avatar_size') * 1000;
  149. if($avatar->last_fetched_at && $avatar->last_fetched_at->gt(now()->subDay())) {
  150. return;
  151. }
  152. // handle pleroma edge case
  153. if(Str::endsWith($mime, '; charset=utf-8')) {
  154. $mime = str_replace('; charset=utf-8', '', $mime);
  155. }
  156. if(!in_array($mime, $mimes)) {
  157. return;
  158. }
  159. if($head['length'] >= $max_size) {
  160. return;
  161. }
  162. if($avatar->size && $head['length'] == $avatar->size) {
  163. return;
  164. }
  165. $base = 'cache/avatars/' . $avatar->profile_id;
  166. $ext = $head['mime'] == 'image/jpeg' ? 'jpg' : 'png';
  167. $path = Str::random(20) . '_avatar.' . $ext;
  168. $tmpBase = storage_path('app/remcache/');
  169. $tmpPath = 'avatar_' . $avatar->profile_id . '-' . $path;
  170. $tmpName = $tmpBase . $tmpPath;
  171. $data = file_get_contents($url, false, null, 0, $head['length']);
  172. file_put_contents($tmpName, $data);
  173. $disk = Storage::disk(config('filesystems.cloud'));
  174. $file = $disk->putFileAs($base, new File($tmpName), $path, 'public');
  175. $permalink = $disk->url($file);
  176. $avatar->media_path = $base . $path;
  177. $avatar->is_remote = true;
  178. $avatar->cdn_url = $permalink;
  179. $avatar->size = $head['length'];
  180. $avatar->change_count = $avatar->change_count + 1;
  181. $avatar->last_fetched_at = now();
  182. $avatar->save();
  183. Cache::forget('avatar:' . $avatar->profile_id);
  184. unlink($tmpName);
  185. }
  186. }