123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace App\Http\Controllers\Settings;
- use App\Status;
- use App\Transformer\ActivityPub\ProfileTransformer;
- use App\Transformer\Api\StatusTransformer as StatusApiTransformer;
- use App\UserFilter;
- use Auth;
- use Cache;
- use Illuminate\Http\Request;
- use League\Fractal;
- use League\Fractal\Serializer\ArraySerializer;
- use Storage;
- trait ExportSettings
- {
- private const CHUNK_SIZE = 1000;
- private const STORAGE_BASE = 'user_exports';
- public function __construct()
- {
- $this->middleware('auth');
- }
- public function dataExport()
- {
- return view('settings.dataexport');
- }
- public function exportAccount()
- {
- $profile = Auth::user()->profile;
- $fractal = new Fractal\Manager;
- $fractal->setSerializer(new ArraySerializer);
- $resource = new Fractal\Resource\Item($profile, new ProfileTransformer);
- $data = $fractal->createData($resource)->toArray();
- return response()->streamDownload(function () use ($data) {
- echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
- }, 'account.json', [
- 'Content-Type' => 'application/json',
- ]);
- }
- public function exportFollowing()
- {
- $profile = Auth::user()->profile;
- $userId = Auth::id();
- $userExportPath = 'user_exports/'.$userId;
- $filename = 'pixelfed-following.json';
- $tempPath = $userExportPath.'/'.$filename;
- if (! Storage::exists($userExportPath)) {
- Storage::makeDirectory($userExportPath);
- }
- try {
- Storage::put($tempPath, '[');
- $profile->following()
- ->chunk(1000, function ($following) use ($tempPath) {
- $urls = $following->map(function ($follow) {
- return $follow->url();
- });
- $json = json_encode($urls,
- JSON_PRETTY_PRINT |
- JSON_UNESCAPED_SLASHES |
- JSON_UNESCAPED_UNICODE
- );
- $json = trim($json, '[]');
- if (Storage::size($tempPath) > 1) {
- $json = ','.$json;
- }
- Storage::append($tempPath, $json);
- });
- Storage::append($tempPath, ']');
- return response()->stream(
- function () use ($tempPath) {
- $handle = fopen(Storage::path($tempPath), 'rb');
- while (! feof($handle)) {
- echo fread($handle, 8192);
- flush();
- }
- fclose($handle);
- Storage::delete($tempPath);
- },
- 200,
- [
- 'Content-Type' => 'application/json',
- 'Content-Disposition' => 'attachment; filename="pixelfed-following.json"',
- ]
- );
- } catch (\Exception $e) {
- if (Storage::exists($tempPath)) {
- Storage::delete($tempPath);
- }
- throw $e;
- }
- }
- public function exportFollowers()
- {
- $profile = Auth::user()->profile;
- $userId = Auth::id();
- $userExportPath = 'user_exports/'.$userId;
- $filename = 'pixelfed-followers.json';
- $tempPath = $userExportPath.'/'.$filename;
- if (! Storage::exists($userExportPath)) {
- Storage::makeDirectory($userExportPath);
- }
- try {
- Storage::put($tempPath, '[');
- $profile->followers()
- ->chunk(1000, function ($followers) use ($tempPath) {
- $urls = $followers->map(function ($follower) {
- return $follower->url();
- });
- $json = json_encode($urls,
- JSON_PRETTY_PRINT |
- JSON_UNESCAPED_SLASHES |
- JSON_UNESCAPED_UNICODE
- );
- $json = trim($json, '[]');
- if (Storage::size($tempPath) > 1) {
- $json = ','.$json;
- }
- Storage::append($tempPath, $json);
- });
- Storage::append($tempPath, ']');
- return response()->stream(
- function () use ($tempPath) {
- $handle = fopen(Storage::path($tempPath), 'rb');
- while (! feof($handle)) {
- echo fread($handle, 8192);
- flush();
- }
- fclose($handle);
- Storage::delete($tempPath);
- },
- 200,
- [
- 'Content-Type' => 'application/json',
- 'Content-Disposition' => 'attachment; filename="pixelfed-followers.json"',
- ]
- );
- } catch (\Exception $e) {
- if (Storage::exists($tempPath)) {
- Storage::delete($tempPath);
- }
- throw $e;
- }
- }
- public function exportMuteBlockList()
- {
- $profile = Auth::user()->profile;
- $exists = UserFilter::select('id')
- ->whereUserId($profile->id)
- ->exists();
- if (! $exists) {
- return redirect()->back();
- }
- $data = Cache::remember('account:export:profile:muteblocklist:'.Auth::user()->profile->id, now()->addMinutes(60), function () use ($profile) {
- return json_encode([
- 'muted' => $profile->mutedProfileUrls(),
- 'blocked' => $profile->blockedProfileUrls(),
- ], JSON_PRETTY_PRINT);
- });
- return response()->streamDownload(function () use ($data) {
- echo $data;
- }, 'muted-and-blocked-accounts.json', [
- 'Content-Type' => 'application/json',
- ]);
- }
- public function exportStatuses(Request $request)
- {
- $profile = Auth::user()->profile;
- $userId = Auth::id();
- $userExportPath = self::STORAGE_BASE.'/'.$userId;
- $filename = 'pixelfed-statuses.json';
- $tempPath = $userExportPath.'/'.$filename;
- if (! Storage::exists($userExportPath)) {
- Storage::makeDirectory($userExportPath);
- }
- Storage::put($tempPath, '[');
- $fractal = new Fractal\Manager;
- $fractal->setSerializer(new ArraySerializer);
- try {
- Status::whereProfileId($profile->id)
- ->chunk(self::CHUNK_SIZE, function ($statuses) use ($fractal, $tempPath) {
- $resource = new Fractal\Resource\Collection($statuses, new StatusApiTransformer);
- $data = $fractal->createData($resource)->toArray();
- $json = json_encode($data,
- JSON_PRETTY_PRINT |
- JSON_UNESCAPED_SLASHES |
- JSON_UNESCAPED_UNICODE
- );
- $json = trim($json, '[]');
- if (Storage::size($tempPath) > 1) {
- $json = ','.$json;
- }
- Storage::append($tempPath, $json);
- });
- Storage::append($tempPath, ']');
- return response()->stream(
- function () use ($tempPath) {
- $handle = fopen(Storage::path($tempPath), 'rb');
- while (! feof($handle)) {
- echo fread($handle, 8192);
- flush();
- }
- fclose($handle);
- Storage::delete($tempPath);
- },
- 200,
- [
- 'Content-Type' => 'application/json',
- 'Content-Disposition' => 'attachment; filename="pixelfed-statuses.json"',
- ]
- );
- } catch (\Exception $e) {
- if (Storage::exists($tempPath)) {
- Storage::delete($tempPath);
- }
- throw $e;
- }
- }
- }
|