ExportSettings.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace App\Http\Controllers\Settings;
  3. use App\Status;
  4. use App\Transformer\ActivityPub\ProfileTransformer;
  5. use App\Transformer\Api\StatusTransformer as StatusApiTransformer;
  6. use App\UserFilter;
  7. use Auth;
  8. use Cache;
  9. use Illuminate\Http\Request;
  10. use League\Fractal;
  11. use League\Fractal\Serializer\ArraySerializer;
  12. use Storage;
  13. trait ExportSettings
  14. {
  15. private const CHUNK_SIZE = 1000;
  16. private const STORAGE_BASE = 'user_exports';
  17. public function __construct()
  18. {
  19. $this->middleware('auth');
  20. }
  21. public function dataExport()
  22. {
  23. return view('settings.dataexport');
  24. }
  25. public function exportAccount()
  26. {
  27. $profile = Auth::user()->profile;
  28. $fractal = new Fractal\Manager;
  29. $fractal->setSerializer(new ArraySerializer);
  30. $resource = new Fractal\Resource\Item($profile, new ProfileTransformer);
  31. $data = $fractal->createData($resource)->toArray();
  32. return response()->streamDownload(function () use ($data) {
  33. echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  34. }, 'account.json', [
  35. 'Content-Type' => 'application/json',
  36. ]);
  37. }
  38. public function exportFollowing()
  39. {
  40. $profile = Auth::user()->profile;
  41. $userId = Auth::id();
  42. $userExportPath = 'user_exports/'.$userId;
  43. $filename = 'pixelfed-following.json';
  44. $tempPath = $userExportPath.'/'.$filename;
  45. if (! Storage::exists($userExportPath)) {
  46. Storage::makeDirectory($userExportPath);
  47. }
  48. try {
  49. Storage::put($tempPath, '[');
  50. $profile->following()
  51. ->chunk(1000, function ($following) use ($tempPath) {
  52. $urls = $following->map(function ($follow) {
  53. return $follow->url();
  54. });
  55. $json = json_encode($urls,
  56. JSON_PRETTY_PRINT |
  57. JSON_UNESCAPED_SLASHES |
  58. JSON_UNESCAPED_UNICODE
  59. );
  60. $json = trim($json, '[]');
  61. if (Storage::size($tempPath) > 1) {
  62. $json = ','.$json;
  63. }
  64. Storage::append($tempPath, $json);
  65. });
  66. Storage::append($tempPath, ']');
  67. return response()->stream(
  68. function () use ($tempPath) {
  69. $handle = fopen(Storage::path($tempPath), 'rb');
  70. while (! feof($handle)) {
  71. echo fread($handle, 8192);
  72. flush();
  73. }
  74. fclose($handle);
  75. Storage::delete($tempPath);
  76. },
  77. 200,
  78. [
  79. 'Content-Type' => 'application/json',
  80. 'Content-Disposition' => 'attachment; filename="pixelfed-following.json"',
  81. ]
  82. );
  83. } catch (\Exception $e) {
  84. if (Storage::exists($tempPath)) {
  85. Storage::delete($tempPath);
  86. }
  87. throw $e;
  88. }
  89. }
  90. public function exportFollowers()
  91. {
  92. $profile = Auth::user()->profile;
  93. $userId = Auth::id();
  94. $userExportPath = 'user_exports/'.$userId;
  95. $filename = 'pixelfed-followers.json';
  96. $tempPath = $userExportPath.'/'.$filename;
  97. if (! Storage::exists($userExportPath)) {
  98. Storage::makeDirectory($userExportPath);
  99. }
  100. try {
  101. Storage::put($tempPath, '[');
  102. $profile->followers()
  103. ->chunk(1000, function ($followers) use ($tempPath) {
  104. $urls = $followers->map(function ($follower) {
  105. return $follower->url();
  106. });
  107. $json = json_encode($urls,
  108. JSON_PRETTY_PRINT |
  109. JSON_UNESCAPED_SLASHES |
  110. JSON_UNESCAPED_UNICODE
  111. );
  112. $json = trim($json, '[]');
  113. if (Storage::size($tempPath) > 1) {
  114. $json = ','.$json;
  115. }
  116. Storage::append($tempPath, $json);
  117. });
  118. Storage::append($tempPath, ']');
  119. return response()->stream(
  120. function () use ($tempPath) {
  121. $handle = fopen(Storage::path($tempPath), 'rb');
  122. while (! feof($handle)) {
  123. echo fread($handle, 8192);
  124. flush();
  125. }
  126. fclose($handle);
  127. Storage::delete($tempPath);
  128. },
  129. 200,
  130. [
  131. 'Content-Type' => 'application/json',
  132. 'Content-Disposition' => 'attachment; filename="pixelfed-followers.json"',
  133. ]
  134. );
  135. } catch (\Exception $e) {
  136. if (Storage::exists($tempPath)) {
  137. Storage::delete($tempPath);
  138. }
  139. throw $e;
  140. }
  141. }
  142. public function exportMuteBlockList()
  143. {
  144. $profile = Auth::user()->profile;
  145. $exists = UserFilter::select('id')
  146. ->whereUserId($profile->id)
  147. ->exists();
  148. if (! $exists) {
  149. return redirect()->back();
  150. }
  151. $data = Cache::remember('account:export:profile:muteblocklist:'.Auth::user()->profile->id, now()->addMinutes(60), function () use ($profile) {
  152. return json_encode([
  153. 'muted' => $profile->mutedProfileUrls(),
  154. 'blocked' => $profile->blockedProfileUrls(),
  155. ], JSON_PRETTY_PRINT);
  156. });
  157. return response()->streamDownload(function () use ($data) {
  158. echo $data;
  159. }, 'muted-and-blocked-accounts.json', [
  160. 'Content-Type' => 'application/json',
  161. ]);
  162. }
  163. public function exportStatuses(Request $request)
  164. {
  165. $profile = Auth::user()->profile;
  166. $userId = Auth::id();
  167. $userExportPath = self::STORAGE_BASE.'/'.$userId;
  168. $filename = 'pixelfed-statuses.json';
  169. $tempPath = $userExportPath.'/'.$filename;
  170. if (! Storage::exists($userExportPath)) {
  171. Storage::makeDirectory($userExportPath);
  172. }
  173. Storage::put($tempPath, '[');
  174. $fractal = new Fractal\Manager;
  175. $fractal->setSerializer(new ArraySerializer);
  176. try {
  177. Status::whereProfileId($profile->id)
  178. ->chunk(self::CHUNK_SIZE, function ($statuses) use ($fractal, $tempPath) {
  179. $resource = new Fractal\Resource\Collection($statuses, new StatusApiTransformer);
  180. $data = $fractal->createData($resource)->toArray();
  181. $json = json_encode($data,
  182. JSON_PRETTY_PRINT |
  183. JSON_UNESCAPED_SLASHES |
  184. JSON_UNESCAPED_UNICODE
  185. );
  186. $json = trim($json, '[]');
  187. if (Storage::size($tempPath) > 1) {
  188. $json = ','.$json;
  189. }
  190. Storage::append($tempPath, $json);
  191. });
  192. Storage::append($tempPath, ']');
  193. return response()->stream(
  194. function () use ($tempPath) {
  195. $handle = fopen(Storage::path($tempPath), 'rb');
  196. while (! feof($handle)) {
  197. echo fread($handle, 8192);
  198. flush();
  199. }
  200. fclose($handle);
  201. Storage::delete($tempPath);
  202. },
  203. 200,
  204. [
  205. 'Content-Type' => 'application/json',
  206. 'Content-Disposition' => 'attachment; filename="pixelfed-statuses.json"',
  207. ]
  208. );
  209. } catch (\Exception $e) {
  210. if (Storage::exists($tempPath)) {
  211. Storage::delete($tempPath);
  212. }
  213. throw $e;
  214. }
  215. }
  216. }