Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1396974b03 |
No files matched your search
@@ -4,6 +4,12 @@ Every release has a section here, named after its version (for example `v0.1.0 -
|
||||
heading). The release pull request to `production` is stopped by PR CI when its section is missing, and the text of
|
||||
the section is the text of the release in Gitea.
|
||||
|
||||
## v0.9.6 - 2026-12-04
|
||||
|
||||
- **Media at the IriksIT API, as a site that already used it expects.** The file route tells the API what kind of file it wants (`X-Media-Kind`) and passes on the placeholder the API sends when it cannot serve it (a picture for a picture; a clip with status 200 for a video, because a player refuses anything else), never cached. A busy API is told apart from a file that is gone: 503 with `Retry-After` instead of a 404 that a cache may keep. A download is no longer collected on the server first, so a video starts playing at once. New: `Media::kind()`, `Adapter::openFile()`, and `MediaStorageException` carries the placeholder.
|
||||
- **Date fields of a list.** An empty date is saved as no date (before: an empty text, which a strict database refuses in a date column and which wiped a date that was there), and a date column that is cast to a date and time is shown as the day, which is what the field takes. Deleting a file while the API cannot be reached keeps the row and answers 502, instead of a 500.
|
||||
- **A tap on the phone number or WhatsApp button** counts only for a page of the site (where the statistics are on) and never for a program, as the pulse does.
|
||||
|
||||
## v0.9.5 - 2026-12-04
|
||||
|
||||
- **Lists for blocks.** A `cards` block can show the latest published posts (`source` = `posts`, each with a `url` from the new `manager.blocks.post_url` / `MANAGER_BLOCKS_POST_URL`), and can be limited to the records whose yes/no field is on (`only_if`, applied before the count). Every item whose list has a select that points at another list gets `<field>_label`, the name behind the id.
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace IriksIT\Manager\Exceptions;
|
||||
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
@@ -10,8 +11,19 @@ use RuntimeException;
|
||||
*/
|
||||
class MediaStorageException extends RuntimeException
|
||||
{
|
||||
public function __construct(string $message, public readonly int $status = 0, public readonly ?int $retryAfter = null)
|
||||
{
|
||||
/**
|
||||
* @param StreamInterface|null $placeholder what the API sent instead of the file: a picture, clip, sound or page of the same kind that says what is wrong
|
||||
* @param string|null $placeholderType its content type
|
||||
* @param string|null $reason the API's name for what is wrong (not-found, unavailable, ...)
|
||||
*/
|
||||
public function __construct(
|
||||
string $message,
|
||||
public readonly int $status = 0,
|
||||
public readonly ?int $retryAfter = null,
|
||||
public readonly ?StreamInterface $placeholder = null,
|
||||
public readonly ?string $placeholderType = null,
|
||||
public readonly ?string $reason = null,
|
||||
) {
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,21 @@ class Media extends Model
|
||||
return $this->belongsTo(config('manager.user_model'), 'uploaded_by');
|
||||
}
|
||||
|
||||
/**
|
||||
* What kind of media this is, in the words of the IriksIT API: it sends a placeholder of this kind when the file itself cannot be served.
|
||||
* Null for a file there is nothing to show for.
|
||||
*/
|
||||
public function kind(): ?string
|
||||
{
|
||||
return match (true) {
|
||||
str_starts_with($this->mime_type, 'image/') => 'image',
|
||||
str_starts_with($this->mime_type, 'video/') => 'video',
|
||||
str_starts_with($this->mime_type, 'audio/') => 'audio',
|
||||
$this->mime_type === 'application/pdf' => 'pdf',
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
public function isImage(): bool
|
||||
{
|
||||
return str_starts_with($this->mime_type, 'image/');
|
||||
|
||||
@@ -6,10 +6,13 @@ use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use IriksIT\Manager\Modules\Statistics\VisitRecorder;
|
||||
use IriksIT\Manager\Support\Leads\Intake;
|
||||
use IriksIT\Manager\Support\Leads\Phone;
|
||||
use IriksIT\Manager\Support\Leads\Report;
|
||||
use IriksIT\Manager\Support\Login\Turnstile;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
use IriksIT\Manager\Support\UserAgent;
|
||||
|
||||
/**
|
||||
* The one place the forms of the public site are sent to (`manager.leads.form`). A form is a name (the last part of the address) with a
|
||||
@@ -52,7 +55,18 @@ class LeadFormController
|
||||
public function tap(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate(['kind' => ['required', 'in:call,whatsapp'], 'path' => ['required', 'string', 'max:255']]);
|
||||
Report::tap($data['kind'], parse_url($data['path'], PHP_URL_PATH) ?: '/');
|
||||
$path = parse_url($data['path'], PHP_URL_PATH) ?: '/';
|
||||
|
||||
// A program is no caller, and where the statistics are on, only a page that exists counts (nobody can fill the list with made-up addresses).
|
||||
if ((new UserAgent((string) $request->userAgent()))->isBot()) {
|
||||
return response()->json(['ok' => true], 201);
|
||||
}
|
||||
|
||||
if (app(ModuleRegistry::class)->enabled('statistics') && ($path = app(VisitRecorder::class)->pathOf($path)) === null) {
|
||||
return response()->json(['ok' => true], 201);
|
||||
}
|
||||
|
||||
Report::tap($data['kind'], $path);
|
||||
|
||||
return response()->json(['ok' => true], 201);
|
||||
}
|
||||
|
||||
@@ -53,6 +53,11 @@ class MediaFileController
|
||||
try {
|
||||
$stream = $library->open($media);
|
||||
} catch (MediaStorageException $e) {
|
||||
// The API sent a picture, clip, sound or page that says what is wrong: show that instead of an error the browser cannot display.
|
||||
if ($e->placeholder !== null && $e->placeholderType !== null) {
|
||||
return $this->placeholder($e);
|
||||
}
|
||||
|
||||
if ($e->status === 404) {
|
||||
abort(404);
|
||||
}
|
||||
@@ -63,6 +68,48 @@ class MediaFileController
|
||||
return response()->stream(fn () => $this->send($stream, $start, $length), $partial ? 206 : 200, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Passes the API's placeholder on. With the status the API gave, except for video and audio: a player refuses anything that is not a 200.
|
||||
* Never cached: it would outlive the problem.
|
||||
*/
|
||||
private function placeholder(MediaStorageException $e): Response
|
||||
{
|
||||
$body = $e->placeholder;
|
||||
$playable = str_starts_with((string) $e->placeholderType, 'video/') || str_starts_with((string) $e->placeholderType, 'audio/');
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => (string) $e->placeholderType,
|
||||
'Content-Disposition' => 'inline',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
'Cache-Control' => 'no-store',
|
||||
'X-Media-Fallback' => (string) $e->reason,
|
||||
];
|
||||
|
||||
if ($e->retryAfter !== null) {
|
||||
$headers['Retry-After'] = (string) $e->retryAfter;
|
||||
}
|
||||
|
||||
return response()->stream(function () use ($body): void {
|
||||
if ($body === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
while (! $body->eof()) {
|
||||
$chunk = $body->read(65536);
|
||||
|
||||
if ($chunk === '') {
|
||||
break;
|
||||
}
|
||||
|
||||
echo $chunk;
|
||||
}
|
||||
} finally {
|
||||
$body->close();
|
||||
}
|
||||
}, $playable ? 200 : ($e->status ?: 503), $headers);
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
private function headers(Media $media): array
|
||||
{
|
||||
|
||||
@@ -2,13 +2,17 @@
|
||||
|
||||
namespace IriksIT\Manager\Modules\Media;
|
||||
|
||||
use GuzzleHttp\Psr7\StreamWrapper;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Filesystem\FilesystemAdapter;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use IriksIT\Manager\Exceptions\MediaStorageException;
|
||||
use IriksIT\Manager\Models\Media;
|
||||
use IriksIT\Manager\Support\IriksStorage\Adapter as IriksAdapter;
|
||||
use IriksIT\Manager\Support\MediaUsage;
|
||||
use League\Flysystem\FilesystemException;
|
||||
|
||||
/**
|
||||
* The one place that knows where media files physically are. Everything else works with Media
|
||||
@@ -129,7 +133,14 @@ class MediaLibrary
|
||||
*/
|
||||
public function open(Media $media)
|
||||
{
|
||||
$stream = Storage::disk($media->disk)->readStream($media->location);
|
||||
$disk = Storage::disk($media->disk);
|
||||
|
||||
// At the IriksIT API: through its own way in, so a busy API is told apart from a file that is gone, and a placeholder can be passed on.
|
||||
if ($disk instanceof FilesystemAdapter && ($adapter = $disk->getAdapter()) instanceof IriksAdapter) {
|
||||
return StreamWrapper::getResource($adapter->openFile($media->location, $media->kind()));
|
||||
}
|
||||
|
||||
$stream = $disk->readStream($media->location);
|
||||
|
||||
if (! is_resource($stream)) {
|
||||
throw new MediaStorageException('The file is no longer there.', 404);
|
||||
@@ -143,9 +154,14 @@ class MediaLibrary
|
||||
{
|
||||
$disk = Storage::disk($media->disk);
|
||||
|
||||
// A storage that cannot be reached throws its own exceptions: the row stays, and the caller hears the one it knows.
|
||||
try {
|
||||
if ($disk->exists($media->location) && ! $disk->delete($media->location)) {
|
||||
throw new MediaStorageException('The file could not be deleted.');
|
||||
}
|
||||
} catch (FilesystemException $e) {
|
||||
throw new MediaStorageException('The file could not be deleted: '.$e->getMessage(), 0);
|
||||
}
|
||||
|
||||
$media->delete();
|
||||
}
|
||||
|
||||
@@ -153,6 +153,8 @@ final class Fields
|
||||
$out[$key] = match ($field['type']) {
|
||||
'toggle' => filter_var($value, FILTER_VALIDATE_BOOLEAN),
|
||||
'number' => $value === null || $value === '' ? null : $value + 0,
|
||||
// No date is no date: an empty text in a date column is an error on a strict database, and it is not what was meant.
|
||||
'date' => $value === null || $value === '' ? null : (is_scalar($value) ? trim((string) $value) : null),
|
||||
'select' => $field['source'] !== null && ($value === null || $value === '') ? null : (is_scalar($value) ? trim((string) $value) : ''),
|
||||
'list', 'media_list' => self::strings($value),
|
||||
'repeater' => self::cleanItems($field['items'], $value),
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace IriksIT\Manager\Support\IriksStorage;
|
||||
|
||||
use GuzzleHttp\Psr7\StreamWrapper;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use IriksIT\Manager\Exceptions\MediaStorageException;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
@@ -18,6 +19,7 @@ use League\Flysystem\UnableToReadFile;
|
||||
use League\Flysystem\UnableToRetrieveMetadata;
|
||||
use League\Flysystem\UnableToSetVisibility;
|
||||
use League\Flysystem\UnableToWriteFile;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
/**
|
||||
* A Laravel disk (`driver` => `iriksit`) whose files live at the IriksIT API. The API knows files by an id of its own, not
|
||||
@@ -87,22 +89,24 @@ class Adapter implements FilesystemAdapter
|
||||
|
||||
public function readStream(string $path)
|
||||
{
|
||||
$id = $this->id($path) ?? throw UnableToReadFile::fromLocation($path, 'There is no such file.');
|
||||
|
||||
try {
|
||||
$body = $this->client->open($id);
|
||||
return StreamWrapper::getResource($this->openFile($path));
|
||||
} catch (MediaStorageException $e) {
|
||||
throw UnableToReadFile::fromLocation($path, $e->getMessage(), $e);
|
||||
}
|
||||
|
||||
$stream = fopen('php://temp', 'r+b');
|
||||
while (! $body->eof()) {
|
||||
fwrite($stream, $body->read(1024 * 1024));
|
||||
}
|
||||
$body->close();
|
||||
rewind($stream);
|
||||
|
||||
return $stream;
|
||||
/**
|
||||
* The content of a file as it comes in (nothing is kept here first, so a video starts playing at once). `$kind` (image, video, audio
|
||||
* or pdf) lets the API answer with a placeholder when it cannot serve the file; that travels in the exception.
|
||||
*
|
||||
* @throws MediaStorageException with status 404 when there is no such file
|
||||
*/
|
||||
public function openFile(string $path, ?string $kind = null): StreamInterface
|
||||
{
|
||||
$id = $this->id($path) ?? throw new MediaStorageException('There is no such file.', 404);
|
||||
|
||||
return $this->client->open($id, $kind);
|
||||
}
|
||||
|
||||
public function delete(string $path): void
|
||||
|
||||
@@ -61,14 +61,17 @@ class Client
|
||||
/**
|
||||
* The content of a file as a stream; read it and close it.
|
||||
*
|
||||
* `$kind` (image, video, audio or pdf) tells the API what is expected: when the file cannot be served it then sends a placeholder of that
|
||||
* kind instead of an error, and that travels along in the exception.
|
||||
*
|
||||
* @throws MediaStorageException with status 404 when the API does not know the file
|
||||
*/
|
||||
public function open(string $id): StreamInterface
|
||||
public function open(string $id, ?string $kind = null): StreamInterface
|
||||
{
|
||||
$response = $this->call(fn (PendingRequest $request) => $request->withOptions(['stream' => true])->get($this->url('/storage/'.rawurlencode($id).'/download')));
|
||||
$response = $this->call(fn (PendingRequest $request) => $request->withHeaders($kind !== null ? ['X-Media-Kind' => $kind] : [])->withOptions(['stream' => true])->get($this->url('/storage/'.rawurlencode($id).'/download')));
|
||||
|
||||
if ($response->status() !== 200) {
|
||||
throw $this->failure('download a file', $response);
|
||||
throw $this->failure('download a file', $response, withPlaceholder: true);
|
||||
}
|
||||
|
||||
return $response->toPsrResponse()->getBody();
|
||||
@@ -266,11 +269,26 @@ class Client
|
||||
}
|
||||
}
|
||||
|
||||
private function failure(string $action, Response $response): MediaStorageException
|
||||
private function failure(string $action, Response $response, bool $withPlaceholder = false): MediaStorageException
|
||||
{
|
||||
$retry = $response->header('Retry-After');
|
||||
$fallback = $response->header('X-Media-Fallback');
|
||||
$type = $response->header('Content-Type');
|
||||
|
||||
// The API sent a placeholder instead of the file: the header says so, and the type is one a browser may show. Its body is that picture, not a message.
|
||||
if ($withPlaceholder && preg_match('/^[a-z-]{1,30}$/', $fallback) === 1 && preg_match('#^(image/png|video/mp4|audio/mpeg|application/pdf)$#', $type) === 1) {
|
||||
return new MediaStorageException(
|
||||
"The IriksIT storage could not {$action} (HTTP {$response->status()}: {$fallback}).",
|
||||
$response->status(),
|
||||
is_numeric($retry) ? (int) $retry : null,
|
||||
$response->toPsrResponse()->getBody(),
|
||||
$type,
|
||||
$fallback,
|
||||
);
|
||||
}
|
||||
|
||||
$reason = $response->json('message');
|
||||
$detail = is_string($reason) && $reason !== '' ? ': '.mb_substr($reason, 0, 200) : '';
|
||||
$retry = $response->header('Retry-After');
|
||||
|
||||
return new MediaStorageException(
|
||||
"The IriksIT storage could not {$action} (HTTP {$response->status()}{$detail}).",
|
||||
|
||||
@@ -96,7 +96,10 @@ final class Resources
|
||||
$values = [];
|
||||
|
||||
foreach ($resource['fields'] as $key => $field) {
|
||||
$values[$key] = $stored !== null ? ($stored[$key] ?? $field['default']) : ($model->getAttribute($key) ?? $field['default']);
|
||||
$value = $stored !== null ? ($stored[$key] ?? $field['default']) : ($model->getAttribute($key) ?? $field['default']);
|
||||
|
||||
// A date column that is cast to a date and time is shown as the day it is, which is what a date field takes.
|
||||
$values[$key] = $field['type'] === 'date' && $value instanceof \DateTimeInterface ? $value->format('Y-m-d') : ($field['type'] === 'date' && $value === '' ? null : $value);
|
||||
}
|
||||
|
||||
return ['id' => $model->getKey(), 'values' => $values];
|
||||
|
||||
Reference in new issue
Block a user