MediaStorageService.php 6.2 KB

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