Skip to content

Commit 7a37f50

Browse files
committed
User export now includes profile picture
1 parent 02bc23c commit 7a37f50

File tree

1 file changed

+88
-60
lines changed

1 file changed

+88
-60
lines changed

app/Http/Controllers/UserController.php

Lines changed: 88 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -768,20 +768,40 @@ public function exportLinks(request $request)
768768
}
769769

770770
//Export all user data
771-
public function exportAll(request $request)
771+
public function exportAll(Request $request)
772772
{
773773
$userId = Auth::id();
774774
$user = User::find($userId);
775775
$links = Link::where('user_id', $userId)->get();
776-
776+
777777
if (!$user) {
778778
// handle the case where the user is null
779779
return response()->json(['message' => 'User not found'], 404);
780780
}
781-
781+
782782
$userData = $user->toArray();
783783
$userData['links'] = $links->toArray();
784+
785+
function findAvatar($name){
786+
$directory = base_path('/img');
787+
$files = scandir($directory);
788+
$pathinfo = "error.error";
789+
foreach($files as $file) {
790+
if (strpos($file, $name.'.') !== false) {
791+
$pathinfo = "/img/" . $name. "." . pathinfo($file, PATHINFO_EXTENSION);
792+
}}
793+
return $pathinfo;
794+
}
784795

796+
if (file_exists(base_path(findAvatar($userId)))){
797+
$imagePath = base_path(findAvatar($userId));
798+
$imageData = base64_encode(file_get_contents($imagePath));
799+
$userData['image_data'] = $imageData;
800+
801+
$imageExtension = pathinfo($imagePath, PATHINFO_EXTENSION);
802+
$userData['image_extension'] = $imageExtension;
803+
}
804+
785805
$domain = $_SERVER['HTTP_HOST'];
786806
$date = date('Y-m-d_H-i-s');
787807
$fileName = "user_data-$domain-$date.json";
@@ -790,67 +810,75 @@ public function exportAll(request $request)
790810
'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
791811
];
792812
return response()->json($userData, 200, $headers);
793-
813+
794814
return back();
795-
}
815+
}
796816

797-
//Import user data from file
798-
public function importData(Request $request)
799-
{
800-
try {
801-
// Get the JSON data from the uploaded file
802-
if (!$request->hasFile('import') || !$request->file('import')->isValid()) {
803-
throw new \Exception('File not uploaded or is faulty');
804-
}
805-
$file = $request->file('import');
806-
$jsonString = $file->get();
807-
$userData = json_decode($jsonString, true);
808-
809-
// Update the authenticated user's profile data if defined in the JSON file
810-
$user = auth()->user();
811-
if (isset($userData['name'])) {
812-
$user->name = $userData['name'];
813-
}
814-
if (isset($userData['littlelink_name'])) {
815-
$user->littlelink_name = $userData['littlelink_name'];
816-
}
817-
if (isset($userData['littlelink_description'])) {
818-
$user->littlelink_description = $userData['littlelink_description'];
819-
}
820-
if (isset($userData['image'])) {
821-
$user->image = $userData['image'];
822-
}
823-
$user->save();
824-
825-
// Delete all links for the authenticated user
826-
Link::where('user_id', $user->id)->delete();
827-
828-
// Loop through each link in $userData and create a new link for the user
829-
foreach ($userData['links'] as $linkData) {
830-
$newLink = new Link();
831-
832-
// Copy over the link data from $linkData to $newLink
833-
$newLink->button_id = $linkData['button_id'];
834-
$newLink->link = $linkData['link'];
835-
$newLink->title = $linkData['title'];
836-
$newLink->order = $linkData['order'];
837-
$newLink->click_number = $linkData['click_number'];
838-
$newLink->up_link = $linkData['up_link'];
839-
$newLink->custom_css = $linkData['custom_css'];
840-
$newLink->custom_icon = $linkData['custom_icon'];
841-
842-
// Set the user ID to the current user's ID
843-
$newLink->user_id = $user->id;
844-
845-
// Save the new link to the database
846-
$newLink->save();
817+
public function importData(Request $request)
818+
{
819+
try {
820+
// Get the JSON data from the uploaded file
821+
if (!$request->hasFile('import') || !$request->file('import')->isValid()) {
822+
throw new \Exception('File not uploaded or is faulty');
823+
}
824+
$file = $request->file('import');
825+
$jsonString = $file->get();
826+
$userData = json_decode($jsonString, true);
827+
828+
// Update the authenticated user's profile data if defined in the JSON file
829+
$user = auth()->user();
830+
if (isset($userData['name'])) {
831+
$user->name = $userData['name'];
832+
}
833+
if (isset($userData['littlelink_name'])) {
834+
$user->littlelink_name = $userData['littlelink_name'];
835+
}
836+
if (isset($userData['littlelink_description'])) {
837+
$user->littlelink_description = $userData['littlelink_description'];
838+
}
839+
if (isset($userData['image_data'])) {
840+
// Decode the image data from Base64
841+
$imageData = base64_decode($userData['image_data']);
842+
843+
// Save the image to the correct path with the correct file name and extension
844+
$filename = $user->id . '.' . $userData['image_extension'];
845+
file_put_contents(base_path('img/' . $filename), $imageData);
846+
847+
// Update the user's image field with the correct file name
848+
$user->image = $filename;
849+
}
850+
$user->save();
851+
852+
// Delete all links for the authenticated user
853+
Link::where('user_id', $user->id)->delete();
854+
855+
// Loop through each link in $userData and create a new link for the user
856+
foreach ($userData['links'] as $linkData) {
857+
$newLink = new Link();
858+
859+
// Copy over the link data from $linkData to $newLink
860+
$newLink->button_id = $linkData['button_id'];
861+
$newLink->link = $linkData['link'];
862+
$newLink->title = $linkData['title'];
863+
$newLink->order = $linkData['order'];
864+
$newLink->click_number = $linkData['click_number'];
865+
$newLink->up_link = $linkData['up_link'];
866+
$newLink->custom_css = $linkData['custom_css'];
867+
$newLink->custom_icon = $linkData['custom_icon'];
868+
869+
// Set the user ID to the current user's ID
870+
$newLink->user_id = $user->id;
871+
872+
// Save the new link to the database
873+
$newLink->save();
874+
}
875+
876+
return redirect('studio/profile')->with('success', 'Profile updated successfully!');
877+
} catch (\Exception $e) {
878+
return redirect('studio/profile')->with('error', 'An error occurred while updating your profile.');
847879
}
848-
849-
return redirect('studio/profile')->with('success', 'Profile updated successfully!');
850-
} catch (\Exception $e) {
851-
return redirect('studio/profile')->with('error', 'An error occurred while updating your profile.');
852880
}
853-
}
881+
854882

855883
//Edit/save page icons
856884
public function editIcons(request $request)

0 commit comments

Comments
 (0)