MediaStorageService.php 5.7 KB

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