MediaStorageService.php 6.8 KB

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