diff --git a/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php b/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php index b550bb0df..f9efb777f 100644 --- a/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php +++ b/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php @@ -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; @@ -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); @@ -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']; @@ -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()) { @@ -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 @@ -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()) { @@ -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); } } @@ -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); @@ -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) { @@ -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); }