Add service API for Ladill Mail large attachment uploads.
Deploy Ladill Transfer / deploy (push) Successful in 39s

Webmail can POST /api/v1/transfers with a mailbox address to create share links for files above 25 MB.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-06-08 11:00:11 +00:00
co-authored by Cursor
parent e76955eaa4
commit fb131cd7fa
9 changed files with 259 additions and 0 deletions
@@ -0,0 +1,76 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Services\Identity\MailboxUserResolver;
use App\Services\Transfer\TransferService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use RuntimeException;
class TransferController extends Controller
{
public function __construct(
private TransferService $transfers,
private MailboxUserResolver $users,
) {}
/** Service-to-service upload (e.g. Ladill Mail for attachments above 25 MB). */
public function store(Request $request): JsonResponse
{
if ($request->attributes->get('service_caller') !== 'webmail') {
return response()->json(['message' => 'Forbidden.'], 403);
}
$maxKb = (int) ceil(((int) config('transfer.max_file_bytes', 524288000)) / 1024);
$maxFiles = (int) config('transfer.max_files_per_transfer', 20);
$data = $request->validate([
'mailbox' => ['required', 'email', 'max:255'],
'title' => ['nullable', 'string', 'max:120'],
'message' => ['nullable', 'string', 'max:2000'],
'retention_days' => ['nullable', 'integer', 'min:1', 'max:365'],
'files' => ['required', 'array', 'min:1', 'max:'.$maxFiles],
'files.*' => ['required', 'file', 'max:'.$maxKb],
]);
try {
$user = $this->users->resolve((string) $data['mailbox']);
} catch (RuntimeException $e) {
return response()->json(['message' => $e->getMessage()], 422);
}
$title = trim((string) ($data['title'] ?? ''));
if ($title === '') {
$title = 'Email attachment';
}
$retentionDays = (int) ($data['retention_days'] ?? config('transfer.mail_retention_days', config('transfer.default_retention_days', 30)));
try {
$transfer = $this->transfers->create($user, [
'title' => $title,
'message' => $data['message'] ?? null,
'retention_days' => $retentionDays,
], $request->file('files', []));
} catch (RuntimeException $e) {
return response()->json(['message' => $e->getMessage()], 422);
}
$transfer->load(['files', 'qrCode']);
return response()->json([
'data' => [
'id' => $transfer->id,
'title' => $transfer->title,
'public_url' => $transfer->qrCode?->publicUrl(),
'expires_at' => $transfer->expires_at?->toIso8601String(),
'files' => $transfer->files->map(fn ($file) => [
'name' => $file->original_name,
'size_bytes' => $file->size_bytes,
])->values(),
],
], 201);
}
}
@@ -0,0 +1,37 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Service-to-service auth for internal APIs. Validates the bearer token against
* per-consumer keys in config("{namespace}.service_api_keys").
*/
class AuthenticateService
{
public function handle(Request $request, Closure $next, string $namespace = 'transfer'): Response
{
$token = (string) $request->bearerToken();
$caller = null;
if ($token !== '') {
foreach ((array) config("{$namespace}.service_api_keys", []) as $name => $key) {
if (is_string($key) && $key !== '' && hash_equals($key, $token)) {
$caller = $name;
break;
}
}
}
if ($caller === null) {
return response()->json(['error' => 'Unauthorized.'], 401);
}
$request->attributes->set('service_caller', $caller);
return $next($request);
}
}
@@ -0,0 +1,58 @@
<?php
namespace App\Services\Identity;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use RuntimeException;
/** Resolves a Ladill Transfer user mirror for a mailbox address (webmail integration). */
class MailboxUserResolver
{
public function resolve(string $mailbox): User
{
$mailbox = strtolower(trim($mailbox));
if ($mailbox === '' || ! str_contains($mailbox, '@')) {
throw new RuntimeException('Invalid mailbox address.');
}
$existing = User::query()->where('email', $mailbox)->first();
if ($existing) {
return $existing;
}
$base = rtrim((string) config('identity.api_url'), '/');
$key = (string) config('identity.api_key');
if ($base === '' || $key === '') {
throw new RuntimeException('Identity API is not configured.');
}
$res = Http::withToken($key)
->acceptJson()
->timeout(10)
->get("{$base}/identity/profile", ['mailbox' => $mailbox]);
if ($res->failed()) {
throw new RuntimeException('Could not resolve the Ladill account for this mailbox.');
}
$data = $res->json('data');
if (! is_array($data)) {
throw new RuntimeException('Could not resolve the Ladill account for this mailbox.');
}
$publicId = trim((string) ($data['public_id'] ?? ''));
if ($publicId === '') {
throw new RuntimeException('No Ladill account is linked to this mailbox.');
}
return User::updateOrCreate(
['public_id' => $publicId],
[
'name' => trim((string) ($data['name'] ?? '')) ?: $mailbox,
'email' => $mailbox,
'avatar_url' => ($picture = trim((string) ($data['picture'] ?? ''))) !== '' ? $picture : null,
],
);
}
}