added form entertaiment and fix file upload

This commit is contained in:
Jagad R R
2024-12-18 12:56:16 +07:00
parent 69ab2b056d
commit 24b9a82856
12 changed files with 836 additions and 62 deletions
+80 -7
View File
@@ -4,6 +4,7 @@ namespace App\Helpers;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Str;
class NextCloudHelper
{
@@ -19,17 +20,78 @@ class NextCloudHelper
public static function createFolder($baseUrl, $username, $password, $folderPath)
{
$client = new Client();
$url = rtrim($baseUrl, '/') . "/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/');
$folders = explode('/', ltrim($folderPath, '/')); // Split the folder path into an array
$currentPath = ''; // Initialize an empty string to build the path progressively
foreach ($folders as $folder) {
$currentPath .= '/' . $folder; // Add the current folder to the path
// Build the URL to create the folder
$url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}" . $currentPath;
try {
// Attempt to create the current folder
$response = $client->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 {
$response = $client->request('MKCOL', $url, [
'auth' => [$username, $password]
// 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' => 'Folder created successfully.'
'message' => 'File uploaded successfully.'
];
} catch (RequestException $e) {
return [
@@ -39,8 +101,19 @@ class NextCloudHelper
];
}
}
public static function getFileUrl($filePath)
{
$baseUrl = env('NEXT_CLOUD_URL');
$username = env('NEXT_CLOUD_USERNAME');
// Build the user-specific WebDAV file URL
$relativePath = "/mktia/remote.php/dav/files/{$username}/" . ltrim($filePath, '/');
// Return the full accessible URL
return rtrim($baseUrl, '/') . '/' . $relativePath;
}
/**
* Create a user in Nextcloud.