SharedMapController.php
This handles the storage and retrieval of shared map data in JSON format
storeSharedMap
storeSharedMappublic function storeSharedMap(Request $request)
{
// Get JSON data from the request input.
$jsonData = $request->input('jsonData');
// Decode JSON data into an associative array.
$jsonData = json_decode($jsonData, true);
// Generate a unique hash for the filename using md5 and uniqid.
$uniqueHash = md5(uniqid(rand(), true));
// Define the directory where shared maps will be stored.
$directory = 'SharedMap';
// Create the full path for the new JSON file.
$fileSharedMap = $directory . '/' . $uniqueHash . '.json';
// Create the directory if it does not exist.
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
// Convert the JSON data back to pretty-printed JSON.
$prettyPrintedJson = json_encode($jsonData, JSON_PRETTY_PRINT);
// Open the file for writing.
$file = fopen($fileSharedMap, 'w');
// If the file is successfully opened, write the JSON data and close the file.
if ($file) {
fwrite($file, $prettyPrintedJson);
fclose($file);
}
// Prepare and return a success response with the generated filename.
$response = [
'SUCCESS' => 1,
'FILENAME' => $uniqueHash,
];
return $response;
}getSharedMap
getSharedMapLast updated