2024-11-06 15:35:54 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-11-14 17:04:30 +01:00
|
|
|
namespace OCA\WebTransfer\Controller;
|
2024-11-06 15:35:54 +01:00
|
|
|
|
2024-11-14 17:04:30 +01:00
|
|
|
use OCA\WebTransfer\AppInfo\Application;
|
2024-11-06 15:35:54 +01:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
|
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
|
|
|
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
|
|
|
|
use OCP\AppFramework\Http\Attribute\OpenAPI;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2024-11-19 14:15:09 +01:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IResponse;
|
2024-11-19 15:29:48 +01:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2024-11-06 15:35:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress UnusedClass
|
|
|
|
*/
|
|
|
|
class PageController extends Controller {
|
|
|
|
#[NoCSRFRequired]
|
|
|
|
#[NoAdminRequired]
|
|
|
|
#[OpenAPI(OpenAPI::SCOPE_IGNORE)]
|
|
|
|
#[FrontpageRoute(verb: 'GET', url: '/')]
|
|
|
|
public function index(): TemplateResponse {
|
|
|
|
return new TemplateResponse(
|
|
|
|
Application::APP_ID,
|
|
|
|
'index',
|
|
|
|
);
|
|
|
|
}
|
2024-11-19 14:15:09 +01:00
|
|
|
|
|
|
|
#[NoCSRFRequired]
|
2024-11-19 15:29:48 +01:00
|
|
|
#[NoAdminRequired]
|
2024-12-04 14:05:11 +01:00
|
|
|
#[OpenAPI(OpenAPI::SCOPE_IGNORE)]
|
2024-12-11 14:14:36 +01:00
|
|
|
#[FrontpageRoute(verb: 'GET', url: '/zipDrop')]
|
2024-12-04 14:05:11 +01:00
|
|
|
public function zipDrop() {
|
2024-12-11 14:14:36 +01:00
|
|
|
// Récupérer le paramètre subUrl (compatible GET et POST)
|
2024-12-04 14:05:11 +01:00
|
|
|
$subUrl = $this->request->getParam('subUrl');
|
2024-12-11 14:14:36 +01:00
|
|
|
|
2024-12-04 14:05:11 +01:00
|
|
|
if (!$subUrl) {
|
2024-12-11 14:14:36 +01:00
|
|
|
return new \OCP\AppFramework\Http\DataResponse([
|
|
|
|
'error' => 'Le paramètre subUrl est manquant'
|
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionnel : Validation de l'URL
|
|
|
|
if (filter_var($subUrl, FILTER_VALIDATE_URL) === false) {
|
|
|
|
return new \OCP\AppFramework\Http\DataResponse([
|
|
|
|
'error' => 'subUrl n\'est pas une URL valide'
|
|
|
|
], 400);
|
2024-12-04 14:05:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$parameters = array('archiveUrl' => $subUrl);
|
|
|
|
|
2024-12-11 14:14:36 +01:00
|
|
|
// Réponse de succès
|
|
|
|
return new TemplateResponse(
|
2024-11-19 15:42:44 +01:00
|
|
|
Application::APP_ID,
|
|
|
|
'index',
|
|
|
|
$parameters
|
|
|
|
);
|
2024-12-04 14:05:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[NoCSRFRequired]
|
|
|
|
#[NoAdminRequired]
|
|
|
|
#[OpenAPI(OpenAPI::SCOPE_IGNORE)]
|
2024-12-11 14:14:36 +01:00
|
|
|
#[FrontpageRoute(verb: 'GET', url: '/getZipFile')]
|
2024-12-04 14:05:11 +01:00
|
|
|
public function getZipFile() {
|
2024-12-11 14:14:36 +01:00
|
|
|
// Récupérer les données envoyées dans la requête
|
|
|
|
$zipUrl = $this->request->getParam('subUrl');
|
|
|
|
|
|
|
|
// Initialiser les paramètres de réponse
|
|
|
|
$parameters = [
|
|
|
|
'status' => 'error', // Par défaut, la réponse indique une erreur
|
|
|
|
'message' => '',
|
|
|
|
'data' => null
|
|
|
|
];
|
2024-12-04 14:05:11 +01:00
|
|
|
|
|
|
|
// Valider l'URL
|
|
|
|
if (!$zipUrl || filter_var($zipUrl, FILTER_VALIDATE_URL) === false) {
|
2024-12-11 14:14:36 +01:00
|
|
|
$parameters['message'] = 'Invalid URL';
|
|
|
|
return new JsonResponse($parameters, 400); // 400 Bad Request
|
2024-12-04 14:05:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-12-11 14:14:36 +01:00
|
|
|
// Initialiser cURL
|
2024-12-04 14:05:11 +01:00
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $zipUrl);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
2024-12-11 14:14:36 +01:00
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Timeout de 10 secondes
|
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Suivre les redirections
|
2024-12-04 14:05:11 +01:00
|
|
|
|
2024-12-11 14:14:36 +01:00
|
|
|
// Récupérer le contenu
|
2024-12-04 14:05:11 +01:00
|
|
|
$response = curl_exec($ch);
|
|
|
|
|
2024-12-11 14:14:36 +01:00
|
|
|
// Gérer les erreurs cURL
|
2024-12-04 14:05:11 +01:00
|
|
|
if (curl_errno($ch)) {
|
2024-12-11 14:14:36 +01:00
|
|
|
$parameters['message'] = 'cURL error: ' . curl_error($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
return new JsonResponse(['parameters' => $parameters, 'status' => 500]); // 500 Internal Server Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vérifier le code HTTP
|
|
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
if ($httpCode !== 200) {
|
|
|
|
$parameters['message'] = "HTTP error: $httpCode";
|
|
|
|
return new JsonResponse(['parameters' => $parameters, 'status' => $httpCode]);
|
2024-12-04 14:05:11 +01:00
|
|
|
}
|
2024-12-11 14:14:36 +01:00
|
|
|
|
|
|
|
// Encodage explicite en UTF-8 si nécessaire
|
|
|
|
if (!mb_detect_encoding($response, 'UTF-8', true)) {
|
|
|
|
$response = utf8_encode($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Si tout est OK, construire la réponse
|
|
|
|
$parameters['status'] = 'success';
|
|
|
|
$parameters['message'] = 'File retrieved successfully';
|
|
|
|
$parameters['data'] = $response; // Encodage Base64 pour éviter les problèmes d'encodage JSON
|
|
|
|
|
|
|
|
return new JsonResponse(['parameters' => $parameters, 'status' => 200]); // 200 OK
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$parameters['message'] = 'Exception: ' . $e->getMessage();
|
|
|
|
return new JsonResponse(['parameters' => $parameters, 'status' => 500]); // 500 Internal Server Error
|
2024-12-04 14:05:11 +01:00
|
|
|
}
|
2024-11-19 14:15:09 +01:00
|
|
|
}
|
2024-12-11 14:14:36 +01:00
|
|
|
}
|