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;
}To explain what this storeSharedMap method does:
Request Handling:
It takes a
Requestobject as a parameter, presumably containing JSON data for a shared map.
JSON Data Handling:
It retrieves JSON data from the request input.
It decodes the JSON data into an associative array.
File and Directory Handling:
It generates a unique hash for the filename using
md5(uniqid(rand(), true)).It defines the directory where shared maps will be stored as 'SharedMap'.
It creates the full path for the new JSON file.
Directory Creation:
It creates the 'SharedMap' directory if it does not exist.
JSON File Creation:
It converts the JSON data back to pretty-printed JSON.
It opens the file for writing and writes the JSON data.
If successful, it closes the file.
Response Preparation:
It prepares a success response with the generated filename.
The response includes
'SUCCESS'set to1and'FILENAME'set to the unique hash.
Response Return:
It returns the prepared success response.
getSharedMap
getSharedMappublic function getSharedMap(Request $request)
{
// Get the filename from the request input.
$fileName = $request->input('fileName');
// Define the directory where shared maps are stored.
$directory = 'SharedMap';
// Create the full path for the JSON file.
$fileSharedMap = $directory . '/' . $fileName . '.json';
// Check if the file exists.
if (file_exists($fileSharedMap)) {
// Read the content of the file.
$fileContent = file_get_contents($fileSharedMap);
// If file content is successfully retrieved, decode it into an associative array.
if ($fileContent !== false) {
// Prepare and return a success response with the retrieved data.
$response = [
'SUCCESS' => 1,
'DATA' => json_decode($fileContent, true),
];
return $response;
}
} else {
// If the file does not exist, prepare and return a failure response.
$response = [
'SUCCESS' => 0,
];
return $response;
}
}To explain what this getSharedMap method does:
Request Handling:
It takes a
Requestobject as a parameter, presumably containing the filename of the shared map to retrieve.
Filename Extraction:
It extracts the filename from the request input.
File and Directory Handling:
It defines the directory where shared maps are stored as 'SharedMap'.
It creates the full path for the JSON file.
File Existence Check:
It checks if the file with the specified filename exists.
File Reading and Decoding:
If the file exists, it reads the content of the file.
It decodes the file content into an associative array.
Response Preparation (Success):
If successful, it prepares a success response with the retrieved data.
The response includes
'SUCCESS'set to1and'DATA'containing the decoded file content.
Response Preparation (Failure):
If the file does not exist, it prepares a failure response with
'SUCCESS'set to0.
Response Return:
It returns the prepared success or failure response.
Last updated