request('MKCOL', $url, [ 'auth' => [$username, $password], 'verify' => false // Disable SSL verification (only for testing purposes) ]); // If folder creation is successful, proceed to the next folder if ($response->getStatusCode() === 201) { echo "Folder '$currentPath' created successfully.\n"; } } catch (RequestException $e) { // Handle errors, such as folder already existing if ($e->getCode() === 409) { echo "Folder '$currentPath' already exists.\n"; } else { // For other errors echo "Failed to create folder '$currentPath': " . $e->getMessage() . "\n"; } } } return [ 'success' => true, 'status' => 200, 'message' => 'All folders processed.' ]; } public static function uploadFile($folderPath, $fileObject, $fileName) { $baseUrl = env('NEXT_CLOUD_URL'); $username = env('NEXT_CLOUD_USERNAME'); $password= env('NEXT_CLOUD_PASSWORD'); // Ensure the folder exists (or create it) $folderResponse = self::createFolder($baseUrl, $username, $password, $folderPath); // Check if folder creation was successful or if it already exists if (!$folderResponse['success']) { return [ 'success' => false, 'status' => $folderResponse['status'], 'message' => 'Failed to create folder: ' . $folderResponse['message'] ]; } $client = new Client(); $url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName; try { // If fileObject is a stream, we can use it directly // If it's raw content, ensure it's being properly passed $response = $client->request('PUT', $url, [ 'auth' => [$username, $password], 'body' => $fileObject, // File content or stream 'verify' => false // Disable SSL verification (only for testing purposes) ]); return [ 'success' => true, 'status' => $response->getStatusCode(), 'message' => 'File uploaded successfully.' ]; } catch (RequestException $e) { return [ 'success' => false, 'status' => $e->getCode(), 'message' => $e->getMessage(), ]; } } public static function getFileUrl($filePath) { $baseUrl = env('NEXT_CLOUD_URL'); $publicToken = '5ogLDbQEbEMsAbD'; // Replace with your actual token $relativePath = "/mktia/public.php/webdav/" . ltrim($filePath, '/'); // Build the full URL $url = rtrim($baseUrl, '/') . $relativePath . "?token={$publicToken}"; // Initialize cURL $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'OCS-APIRequest: true', // Ensure Nextcloud recognizes the API request ]); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Disable SSL host verification curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL peer verification $response = curl_exec($ch); // Handle cURL errors if ($response === false) { $error = curl_error($ch); curl_close($ch); throw new \Exception("cURL error: {$error}"); } curl_close($ch); return $url; } public static function getNextcloudFile($filePath) { // Check if the file exists if (Storage::disk('webdav')->exists($filePath)) { // Get the file contents $content = Storage::disk('webdav')->get($filePath); // Return the file as a response for download return response($content) ->header('Content-Type', 'application/pdf') ->header('Content-Disposition', 'inline; filename="' . basename($filePath) . '"'); } else { return response()->json(['error' => 'File not found'], 404); } } /** * Create a user in Nextcloud. * * @param string $baseUrl Nextcloud Base URL * @param string $adminUsername Admin Username * @param string $adminPassword Admin Password * @param string $userId New User ID * @param string $password New User Password * @param string $displayName New User Display Name * @param string $email New User Email * @return array Response Data */ public static function createUser($baseUrl, $adminUsername, $adminPassword, $userId, $password, $displayName, $email) { $client = new Client(); $url = rtrim($baseUrl, '/') . "/mktia/ocs/v1.php/cloud/users"; try { $response = $client->request('POST', $url, [ 'auth' => [$adminUsername, $adminPassword], 'headers' => [ 'OCS-APIRequest' => 'true', ], 'form_params' => [ 'userid' => $userId, 'password' => $password, 'displayName' => $displayName, 'email' => $email ] ]); return [ 'success' => true, 'status' => $response->getStatusCode(), 'message' => 'User created successfully.' ]; } catch (RequestException $e) { return [ 'success' => false, 'status' => $e->getCode(), 'message' => $e->getMessage(), ]; } } /** * Add a user to a group in Nextcloud. * * @param string $baseUrl Nextcloud Base URL * @param string $adminUsername Admin Username * @param string $adminPassword Admin Password * @param string $userId New User ID * @param string $groupName Group Name * @return array Response Data */ public static function addUserToGroup($baseUrl, $adminUsername, $adminPassword, $userId, $groupName) { $client = new Client(); $url = rtrim($baseUrl, '/') . "/mktia/ocs/v1.php/cloud/groups/{$groupName}"; try { $response = $client->request('POST', $url, [ 'auth' => [$adminUsername, $adminPassword], 'headers' => [ 'OCS-APIRequest' => 'true', ], 'form_params' => [ 'userid' => $userId ] ]); return [ 'success' => true, 'status' => $response->getStatusCode(), 'message' => 'User added to group successfully.' ]; } catch (RequestException $e) { return [ 'success' => false, 'status' => $e->getCode(), 'message' => $e->getMessage(), ]; } } }