Skip to content

Commit

Permalink
[TM-886] implement sys_get_temp_dir for upload files
Browse files Browse the repository at this point in the history
  • Loading branch information
egrojMonroy committed Jun 11, 2024
1 parent 118db12 commit 3c7831e
Showing 1 changed file with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -62,7 +61,9 @@ public function storeGeometry(Request $request)
*/
public function insertGeojsonToDB(string $geojsonFilename, ?string $site_id = null)
{
$geojsonData = Storage::get("public/geojson_files/{$geojsonFilename}");
$tempDir = sys_get_temp_dir();
$geojsonPath = $tempDir . DIRECTORY_SEPARATOR . $geojsonFilename;
$geojsonData = file_get_contents($geojsonPath);
$geojson = json_decode($geojsonData, true);

SitePolygonValidator::validate('FEATURE_BOUNDS', $geojson, false);
Expand Down Expand Up @@ -108,7 +109,9 @@ public function validateDataInDB(Request $request)

public function getGeometryProperties(string $geojsonFilename)
{
$geojsonData = Storage::get("public/geojson_files/{$geojsonFilename}");
$tempDir = sys_get_temp_dir();
$geojsonPath = $tempDir . DIRECTORY_SEPARATOR . $geojsonFilename;
$geojsonData = file_get_contents($geojsonPath);
$geojson = json_decode($geojsonData, true);
if (! isset($geojson['features'])) {
return ['error' => 'GeoJSON file does not contain features'];
Expand All @@ -132,15 +135,12 @@ public function uploadKMLFile(Request $request)
if ($request->hasFile('file')) {
$site_id = $request->input('uuid');
$kmlfile = $request->file('file');
$directory = storage_path('app/public/kml_files');
if (! file_exists($directory)) {
mkdir($directory, 0755, true);
}
$tempDir = sys_get_temp_dir();
$filename = uniqid('kml_file_') . '.' . $kmlfile->getClientOriginalExtension();
$kmlfile->move($directory, $filename);
$kmlPath = $tempDir . DIRECTORY_SEPARATOR . $filename;
$kmlfile->move($tempDir, $filename);
$geojsonFilename = Str::replaceLast('.kml', '.geojson', $filename);
$geojsonPath = storage_path("app/public/geojson_files/{$geojsonFilename}");
$kmlPath = storage_path("app/public/kml_files/{$filename}");
$geojsonPath = $tempDir . DIRECTORY_SEPARATOR . $geojsonFilename;
$process = new Process(['ogr2ogr', '-f', 'GeoJSON', $geojsonPath, $kmlPath]);
$process->run();
if (! $process->isSuccessful()) {
Expand Down Expand Up @@ -185,7 +185,8 @@ public function uploadShapefile(Request $request)
if ($file->getClientOriginalExtension() !== 'zip') {
return response()->json(['error' => 'Only ZIP files are allowed'], 400);
}
$directory = storage_path('app/public/shapefiles/' . uniqid('shapefile_'));
$tempDir = sys_get_temp_dir();
$directory = $tempDir . DIRECTORY_SEPARATOR . uniqid('shapefile_');
mkdir($directory, 0755, true);

// Extract the contents of the ZIP file
Expand All @@ -198,7 +199,7 @@ public function uploadShapefile(Request $request)
return response()->json(['error' => 'Shapefile (.shp) not found in the ZIP file'], 400);
}
$geojsonFilename = Str::replaceLast('.shp', '.geojson', basename($shpFile));
$geojsonPath = storage_path("app/public/geojson_files/{$geojsonFilename}");
$geojsonPath = $tempDir . DIRECTORY_SEPARATOR . $geojsonFilename;
$process = new Process(['ogr2ogr', '-f', 'GeoJSON', $geojsonPath, $shpFile]);
$process->run();
if (! $process->isSuccessful()) {
Expand All @@ -216,7 +217,6 @@ public function uploadShapefile(Request $request)
return response()->json(['error' => 'Failed to open the ZIP file'], 400);
}
} else {

return response()->json(['error' => 'No file uploaded'], 400);
}
}
Expand Down Expand Up @@ -345,12 +345,10 @@ public function uploadGeoJSONFile(Request $request)
if ($request->hasFile('file')) {
$site_id = $request->input('uuid');
$file = $request->file('file');
$directory = storage_path('app/public/geojson_files');
if (! file_exists($directory)) {
mkdir($directory, 0755, true);
}
$tempDir = sys_get_temp_dir();
$filename = uniqid('geojson_file_') . '.' . $file->getClientOriginalExtension();
$file->move($directory, $filename);
$filePath = $tempDir . DIRECTORY_SEPARATOR . $filename;
$file->move($tempDir, $filename);
$uuid = $this->insertGeojsonToDB($filename, $site_id);
if (is_array($uuid) && isset($uuid['error'])) {
return response()->json(['error' => 'Failed to insert GeoJSON data into the database', 'message' => $uuid['error']], 500);
Expand Down Expand Up @@ -390,8 +388,8 @@ public function getPolygonAsGeoJSONDownload(Request $request)
$uuid = $request->query('uuid');

$polygonGeometry = PolygonGeometry::where('uuid', $uuid)
->select(DB::raw('ST_AsGeoJSON(geom) AS geojsonGeom'))
->first();
->select(DB::raw('ST_AsGeoJSON(geom) AS geojsonGeom'))
->first();

Log::info('Polygon Geometry', ['polygonGeometry' => $polygonGeometry]);
if (! $polygonGeometry) {
Expand Down Expand Up @@ -447,8 +445,8 @@ public function getAllPolygonsAsGeoJSONDownload(Request $request)
foreach ($polygonsUuids as $polygonUuid) {
$feature = [];
$polygonGeometry = PolygonGeometry::where('uuid', $polygonUuid)
->select(DB::raw('ST_AsGeoJSON(geom) AS geojsonGeom'))
->first();
->select(DB::raw('ST_AsGeoJSON(geom) AS geojsonGeom'))
->first();
if (! $polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}
Expand Down

0 comments on commit 3c7831e

Please sign in to comment.