v0.9.3
This commit is contained in:
commit
22f164046e
286 files changed
+19176
No files matched your search
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support\IriksStorage;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use IriksIT\Manager\Exceptions\MediaStorageException;
|
||||
use IriksIT\Manager\Support\Names;
|
||||
use League\Flysystem\Config;
|
||||
use League\Flysystem\DirectoryAttributes;
|
||||
use League\Flysystem\FileAttributes;
|
||||
use League\Flysystem\FilesystemAdapter;
|
||||
use League\Flysystem\UnableToCopyFile;
|
||||
use League\Flysystem\UnableToCreateDirectory;
|
||||
use League\Flysystem\UnableToDeleteDirectory;
|
||||
use League\Flysystem\UnableToDeleteFile;
|
||||
use League\Flysystem\UnableToMoveFile;
|
||||
use League\Flysystem\UnableToReadFile;
|
||||
use League\Flysystem\UnableToRetrieveMetadata;
|
||||
use League\Flysystem\UnableToSetVisibility;
|
||||
use League\Flysystem\UnableToWriteFile;
|
||||
|
||||
/**
|
||||
* A Laravel disk (`driver` => `iriksit`) whose files live at the IriksIT API. The API knows files by an id of its own, not
|
||||
* by a path, so every path that is written is remembered next to that id (table `storage_files`). A path that has no entry
|
||||
* and no slash is taken for an id itself: that is how media rows that were made by a site's own code (the id as location)
|
||||
* keep working. There are no folders, no visibility and no listing.
|
||||
*/
|
||||
class Adapter implements FilesystemAdapter
|
||||
{
|
||||
public function __construct(private readonly Client $client) {}
|
||||
|
||||
public function fileExists(string $path): bool
|
||||
{
|
||||
$id = $this->id($path);
|
||||
|
||||
if ($id === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->client->details($id) !== null;
|
||||
} catch (MediaStorageException $e) {
|
||||
throw UnableToRetrieveMetadata::create($path, 'exists', $e->getMessage(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function directoryExists(string $path): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function write(string $path, string $contents, Config $config): void
|
||||
{
|
||||
$stream = fopen('php://temp', 'r+b');
|
||||
fwrite($stream, $contents);
|
||||
rewind($stream);
|
||||
|
||||
try {
|
||||
$this->writeStream($path, $stream, $config);
|
||||
} finally {
|
||||
// The HTTP client closes what it sent itself.
|
||||
if (is_resource($stream)) {
|
||||
fclose($stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function writeStream(string $path, $contents, Config $config): void
|
||||
{
|
||||
try {
|
||||
$stored = $this->client->store($contents, basename($path));
|
||||
} catch (MediaStorageException $e) {
|
||||
throw UnableToWriteFile::atLocation($path, $e->getMessage(), $e);
|
||||
}
|
||||
|
||||
DB::table($this->table())->updateOrInsert(['path' => $path], ['remote_id' => $stored['id']]);
|
||||
}
|
||||
|
||||
public function read(string $path): string
|
||||
{
|
||||
$stream = $this->readStream($path);
|
||||
$contents = stream_get_contents($stream);
|
||||
is_resource($stream) && fclose($stream);
|
||||
|
||||
return $contents === false ? throw UnableToReadFile::fromLocation($path) : $contents;
|
||||
}
|
||||
|
||||
public function readStream(string $path)
|
||||
{
|
||||
$id = $this->id($path) ?? throw UnableToReadFile::fromLocation($path, 'There is no such file.');
|
||||
|
||||
try {
|
||||
$body = $this->client->open($id);
|
||||
} 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;
|
||||
}
|
||||
|
||||
public function delete(string $path): void
|
||||
{
|
||||
$id = $this->id($path);
|
||||
|
||||
if ($id === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->client->delete($id);
|
||||
} catch (MediaStorageException $e) {
|
||||
throw UnableToDeleteFile::atLocation($path, $e->getMessage(), $e);
|
||||
}
|
||||
|
||||
DB::table($this->table())->where('path', $path)->delete();
|
||||
}
|
||||
|
||||
public function deleteDirectory(string $path): void
|
||||
{
|
||||
throw UnableToDeleteDirectory::atLocation($path, 'The IriksIT storage has no folders.');
|
||||
}
|
||||
|
||||
public function createDirectory(string $path, Config $config): void
|
||||
{
|
||||
throw UnableToCreateDirectory::atLocation($path, 'The IriksIT storage has no folders.');
|
||||
}
|
||||
|
||||
public function setVisibility(string $path, string $visibility): void
|
||||
{
|
||||
throw UnableToSetVisibility::atLocation($path, 'The IriksIT storage has no visibility.');
|
||||
}
|
||||
|
||||
public function visibility(string $path): FileAttributes
|
||||
{
|
||||
throw UnableToRetrieveMetadata::visibility($path, 'The IriksIT storage has no visibility.');
|
||||
}
|
||||
|
||||
public function mimeType(string $path): FileAttributes
|
||||
{
|
||||
$details = $this->details($path, 'mime_type');
|
||||
|
||||
return new FileAttributes($path, null, null, null, $details['mime_type'] ?? throw UnableToRetrieveMetadata::mimeType($path));
|
||||
}
|
||||
|
||||
public function lastModified(string $path): FileAttributes
|
||||
{
|
||||
$created = $this->details($path, 'last_modified')['created_at'] ?? null;
|
||||
|
||||
return new FileAttributes($path, null, null, $created !== null ? strtotime($created) ?: null : throw UnableToRetrieveMetadata::lastModified($path));
|
||||
}
|
||||
|
||||
public function fileSize(string $path): FileAttributes
|
||||
{
|
||||
return new FileAttributes($path, $this->details($path, 'file_size')['size']);
|
||||
}
|
||||
|
||||
/** @return iterable<FileAttributes|DirectoryAttributes> */
|
||||
public function listContents(string $path, bool $deep): iterable
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function move(string $source, string $destination, Config $config): void
|
||||
{
|
||||
throw UnableToMoveFile::fromLocationTo($source, $destination);
|
||||
}
|
||||
|
||||
public function copy(string $source, string $destination, Config $config): void
|
||||
{
|
||||
throw UnableToCopyFile::fromLocationTo($source, $destination);
|
||||
}
|
||||
|
||||
/** @return array{size: int, mime_type: string|null, created_at: string|null} */
|
||||
private function details(string $path, string $what): array
|
||||
{
|
||||
$id = $this->id($path) ?? throw UnableToRetrieveMetadata::create($path, $what, 'There is no such file.');
|
||||
|
||||
try {
|
||||
return $this->client->details($id) ?? throw UnableToRetrieveMetadata::create($path, $what, 'There is no such file.');
|
||||
} catch (MediaStorageException $e) {
|
||||
throw UnableToRetrieveMetadata::create($path, $what, $e->getMessage(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/** The id the API gave to what was written at this path, or the path itself when it looks like an id. */
|
||||
private function id(string $path): ?string
|
||||
{
|
||||
$id = DB::table($this->table())->where('path', $path)->value('remote_id');
|
||||
|
||||
if (is_string($id)) {
|
||||
return $id;
|
||||
}
|
||||
|
||||
return $path !== '' && ! str_contains($path, '/') ? $path : null;
|
||||
}
|
||||
|
||||
private function table(): string
|
||||
{
|
||||
return Names::table('storage_files');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user