49 lines
1.7 KiB
PHP
49 lines
1.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers\ThirdPartyServices;
|
||
|
|
||
|
use App\Http\Controllers\Controller;
|
||
|
use App\Mail\sendExportNotice;
|
||
|
use Illuminate\Support\Facades\Http;
|
||
|
use Illuminate\Support\Facades\Log;
|
||
|
use Illuminate\Support\Facades\Mail;
|
||
|
use RuntimeException;
|
||
|
|
||
|
class NextcloudService extends Controller
|
||
|
{
|
||
|
private $nextcloud_url;
|
||
|
private $nextcloud_user;
|
||
|
private $nextcloud_password;
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
$this->nextcloud_url = env('NEXTCLOUD_URL', false);
|
||
|
$this->nextcloud_user = env('NEXTCLOUD_USER', false);
|
||
|
$this->nextcloud_password = env('NEXTCLOUD_PASSWORD', false);
|
||
|
$this->nextcloud_upload_folder_path = env('NEXTCLOUD_UPLOAD_FOLDER_PATH', false);
|
||
|
$this->nextcloud_upload_folder_web_link = env('NEXTCLOUD_UPLOAD_FOLDER_WEB_LINK', false);
|
||
|
}
|
||
|
|
||
|
function nextcloud_upload_csv(string $file_name, string $file_path)
|
||
|
{
|
||
|
try {
|
||
|
$nextcloud_url = $this->nextcloud_url . '/remote.php/dav/files/' . $this->nextcloud_user . '/' . $this->nextcloud_upload_folder_path . '/' . $file_name;
|
||
|
$file_contents = fopen($file_path, 'r');
|
||
|
try {
|
||
|
$res = Http::withBasicAuth($this->nextcloud_user, $this->nextcloud_password)
|
||
|
->attach($file_name, $file_contents)
|
||
|
->put($nextcloud_url);
|
||
|
$res->throw();
|
||
|
} catch (\Exception $e) {
|
||
|
Log::error('nextcloud_upload_csv Upload error - ' . $e->getMessage());
|
||
|
throw new RuntimeException("E-NEXTCLOUD-UPLOAD");
|
||
|
}
|
||
|
// Close the file handle
|
||
|
fclose($file_contents);
|
||
|
return $res;
|
||
|
} catch (\Exception $e) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|