stage file upload an

This commit is contained in:
Fiqh Pratama
2025-10-13 14:55:46 +07:00
parent dd5dbcab8f
commit 76d4aa9b90
9 changed files with 1081 additions and 226 deletions
@@ -68,4 +68,60 @@ class NextCloudController extends Controller
->header('Pragma', 'public')
->header('Content-Length', strlen($response));
}
public function preview($file)
{
$filePath = base64_decode($file);
$baseUrl = rtrim(env('NEXT_CLOUD_URL'), '/');
$username = env('NEXT_CLOUD_USERNAME');
$password = env('NEXT_CLOUD_PASSWORD');
$filePath = trim((string) $filePath);
$segments = explode('/', ltrim($filePath, '/'));
$encodedPath = implode('/', array_map('rawurlencode', $segments));
$relativePath = "remote.php/dav/files/{$username}/" . $encodedPath;
$url = $baseUrl . '/' . $relativePath;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
if ($response === false) {
curl_close($ch);
abort(404);
}
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if ($statusCode >= 400 || !$response) {
abort(404);
}
if (!$contentType) {
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
$contentType = match ($extension) {
'jpg', 'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'pdf' => 'application/pdf',
default => 'application/octet-stream',
};
}
$filename = basename($filePath);
return response($response, 200)
->header('Content-Type', $contentType)
->header('Content-Disposition', "inline; filename=\"{$filename}\"")
->header('Content-Length', strlen($response));
}
}