User export now includes profile picture

This commit is contained in:
Julian Prieber 2023-03-09 00:59:52 +01:00
parent 02bc23c832
commit 7a37f50231
1 changed files with 90 additions and 62 deletions

View File

@ -768,7 +768,7 @@ class UserController extends Controller
}
//Export all user data
public function exportAll(request $request)
public function exportAll(Request $request)
{
$userId = Auth::id();
$user = User::find($userId);
@ -782,6 +782,26 @@ class UserController extends Controller
$userData = $user->toArray();
$userData['links'] = $links->toArray();
function findAvatar($name){
$directory = base_path('/img');
$files = scandir($directory);
$pathinfo = "error.error";
foreach($files as $file) {
if (strpos($file, $name.'.') !== false) {
$pathinfo = "/img/" . $name. "." . pathinfo($file, PATHINFO_EXTENSION);
}}
return $pathinfo;
}
if (file_exists(base_path(findAvatar($userId)))){
$imagePath = base_path(findAvatar($userId));
$imageData = base64_encode(file_get_contents($imagePath));
$userData['image_data'] = $imageData;
$imageExtension = pathinfo($imagePath, PATHINFO_EXTENSION);
$userData['image_extension'] = $imageExtension;
}
$domain = $_SERVER['HTTP_HOST'];
$date = date('Y-m-d_H-i-s');
$fileName = "user_data-$domain-$date.json";
@ -794,9 +814,8 @@ class UserController extends Controller
return back();
}
//Import user data from file
public function importData(Request $request)
{
public function importData(Request $request)
{
try {
// Get the JSON data from the uploaded file
if (!$request->hasFile('import') || !$request->file('import')->isValid()) {
@ -817,8 +836,16 @@ public function importData(Request $request)
if (isset($userData['littlelink_description'])) {
$user->littlelink_description = $userData['littlelink_description'];
}
if (isset($userData['image'])) {
$user->image = $userData['image'];
if (isset($userData['image_data'])) {
// Decode the image data from Base64
$imageData = base64_decode($userData['image_data']);
// Save the image to the correct path with the correct file name and extension
$filename = $user->id . '.' . $userData['image_extension'];
file_put_contents(base_path('img/' . $filename), $imageData);
// Update the user's image field with the correct file name
$user->image = $filename;
}
$user->save();
@ -850,7 +877,8 @@ public function importData(Request $request)
} catch (\Exception $e) {
return redirect('studio/profile')->with('error', 'An error occurred while updating your profile.');
}
}
}
//Edit/save page icons
public function editIcons(request $request)