Skip to content

Commit

Permalink
Upgrade Laravel 10 && Fixes Filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
spektra2147 committed Nov 17, 2023
1 parent e6aa89d commit 5d744d2
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 52 deletions.
82 changes: 60 additions & 22 deletions src/Disk/Adapter/AdapterFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ public function directoryExists(string $path): bool
*/
public function write(string $path, string $contents, $config = [])
{
$this->adapter->write($path, $contents, new Config($config));
try {
$this->adapter->write($path, $contents, new Config($config));

$entry = new FileAttributes($path, $this->fileSize($path), null, null, $this->mimeType($path));
$entry = new FileAttributes($path, $this->fileSize($path), null, null, $this->mimeType($path));

return dispatch_sync(new SyncFile($entry, $this->disk));

return dispatch_sync(new SyncFile($entry, $this->disk));
} catch (FilesystemException $exception) {
return false;
}
}

/**
Expand All @@ -108,11 +113,16 @@ public function write(string $path, string $contents, $config = [])
*/
public function writeStream(string $path, $contents, $config = [])
{
$this->adapter->writeStream($path, $contents, new Config($config));
try {
$this->adapter->writeStream($path, $contents, new Config($config));

$entry = new FileAttributes($path, $this->fileSize($path), null, null, $this->mimeType($path));

$entry = new FileAttributes($path, $this->fileSize($path), null, null, $this->mimeType($path));
return dispatch_sync(new SyncFile($entry, $this->disk));

return dispatch_sync(new SyncFile($entry, $this->disk));
} catch (FilesystemException $exception) {
return false;
}
}

/**
Expand Down Expand Up @@ -252,11 +262,18 @@ private function listDirectory(string $location)
*/
public function delete(string $path)
{
$entry = new FileAttributes($path, $this->fileSize($path), null, null, $this->mimeType($path));
try {
$entry = new FileAttributes($path, $this->fileSize($path), null, null, $this->mimeType($path));

$this->adapter->delete($path);
$this->adapter->delete($path);

dispatch_sync(new DeleteFile($entry));
dispatch_sync(new DeleteFile($entry));

return true;

} catch (FilesystemException $exception) {
return false;
}

}

Expand All @@ -267,9 +284,16 @@ public function delete(string $path)
*/
public function deleteDirectory(string $path)
{
$this->adapter->deleteDirectory($path);
try {
$this->adapter->deleteDirectory($path);

return dispatch_sync(new DeleteFolder($path));
dispatch_sync(new DeleteFolder($path));

return true;

} catch (FilesystemException $exception) {
return false;
}
}

/**
Expand All @@ -280,9 +304,14 @@ public function deleteDirectory(string $path)
*/
public function createDirectory(string $path, $config = [])
{
$this->adapter->createDirectory($path, new Config($config));
try {
$this->adapter->createDirectory($path, new Config($config));

return dispatch_sync(new SyncFolder($path, $this->disk));
return dispatch_sync(new SyncFolder($path, $this->disk));

} catch (FilesystemException $exception) {
return false;
}
}

/**
Expand All @@ -293,7 +322,12 @@ public function createDirectory(string $path, $config = [])
*/
public function move(string $source, string $destination, $config = [])
{
$this->adapter->move($source, $destination, new Config($config));
try {
$this->adapter->move($source, $destination, new Config($config));
return true;
} catch (FilesystemException $exception) {
return false;
}
}

/**
Expand All @@ -305,13 +339,17 @@ public function move(string $source, string $destination, $config = [])
*/
public function copy(string $source, string $destination, $config = [])
{
$this->adapter->copy($source, $destination, new Config($config));

if (is_dir($source)) {
return dispatch_sync(new SyncFolder($path, $this->disk));
} else {
$entry = new FileAttributes($path, $this->fileSize($path), null, null, $this->mimeType($path));
return dispatch_sync(new SyncFile($entry, $this->disk));
try {
$this->adapter->copy($source, $destination, new Config($config));

if (is_dir($source)) {
return dispatch_sync(new SyncFolder($path, $this->disk));
} else {
$entry = new FileAttributes($path, $this->fileSize($path), null, null, $this->mimeType($path));
return dispatch_sync(new SyncFile($entry, $this->disk));
}
} catch (FilesystemException $exception) {
return false;
}
}

Expand Down Expand Up @@ -358,4 +396,4 @@ public function getDisk()
{
return $this->disk;
}
}
}
2 changes: 1 addition & 1 deletion src/Disk/Adapter/Command/DeleteFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function handle(FileRepositoryInterface $files, FolderRepositoryInterface
{
$folder = $folders->findBySlug(dirname($this->file->path()));
if ($file = $files->findByNameAndFolder(basename($this->file->path()), $folder)) {
return $files->delete($file);
return $files->forceDelete($file);
}
}
}
4 changes: 2 additions & 2 deletions src/File/Command/SetDimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function handle()
if (!in_array($this->file->getExtension(), ['jpg', 'jpeg', 'png'])) {
return;
}

try {
list($width, $height) = getimagesize($this->file->path());
list($width, $height) = getimagesize(app('filesystem')->disk($this->file->getDiskSlug())->url($this->file->path()));
} catch (\Exception $e) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/File/FileDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function make(FileInterface $file)
$disk = $folder->getDisk();

return $response->setContent(
$this->manager->read("{$disk->getSlug()}://{$folder->getSlug()}/{$file->getName()}")
$this->manager->disk($file->getDiskSlug())->read("{$folder->getSlug()}/{$file->getName()}")
);
}
}
6 changes: 3 additions & 3 deletions src/File/FileImage.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php namespace Anomaly\FilesModule\File;

use Anomaly\Streams\Platform\Image\Image;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Routing\ResponseFactory;
use League\Flysystem\MountManager;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -26,10 +26,10 @@ class FileImage extends FileResponse
* Create a new FileImage instance.
*
* @param ResponseFactory $response
* @param MountManager $manager
* @param FilesystemManager $manager
* @param Image $image
*/
public function __construct(ResponseFactory $response, MountManager $manager, Image $image)
public function __construct(ResponseFactory $response, FilesystemManager $manager, Image $image)
{
$this->image = $image;

Expand Down
18 changes: 11 additions & 7 deletions src/File/FileManager.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace Anomaly\FilesModule\File;

use Anomaly\FilesModule\Disk\Adapter\Command\MoveFile;
use League\Flysystem\MountManager;
use Illuminate\Filesystem\FilesystemManager;

/**
* Class FileManager
Expand All @@ -16,16 +16,16 @@ class FileManager
/**
* The mount manager.
*
* @var MountManager
* @var FilesystemManager
*/
protected $manager;

/**
* Create a new FileManager instance.
*
* @param MountManager $manager
* @param FilesystemManager $manager
*/
public function __construct(MountManager $manager)
public function __construct(FilesystemManager $manager)
{
$this->manager = $manager;
}
Expand All @@ -37,13 +37,17 @@ public function __construct(MountManager $manager)
*/
public function move($from, $to)
{
$result = $this->manager->put($to, $contents = $this->manager->read($from), ['sync' => false]);
list($from_disk, $from_path) = explode('://', $from, 2);
list($to_disk, $to_path) = explode('://', $to, 2);

if ($result && $this->manager->has($to)) {
$stream = $this->manager->disk($from_disk)->readStream($from_path);
$result = $this->manager->disk($to_disk)->writeStream($to_path, $stream);

if ($result && $this->manager->disk($to_disk)->fileExists($to_path)) {
$result = dispatch_sync(new MoveFile($from, $to));
}

$this->manager->delete($from);
$this->manager->disk($from_disk)->delete($from_path);

return $result;
}
Expand Down
9 changes: 3 additions & 6 deletions src/File/FileModel.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?php namespace Anomaly\FilesModule\File;

use League\Flysystem\File;
use Illuminate\Support\Str;
use League\Flysystem\MountManager;
use Anomaly\Streams\Platform\Image\Image;
use League\Flysystem\FilesystemInterface;
use Anomaly\FilesModule\File\Command\GetType;
use Anomaly\FilesModule\File\Command\GetImage;
use Anomaly\FilesModule\File\Command\GetResource;
Expand Down Expand Up @@ -76,18 +73,18 @@ public function url()
/**
* Return the resource filesystem.
*
* @return null|AdapterFilesystem|FilesystemInterface
* @return null|AdapterFilesystem
*/
public function filesystem()
{
return app(MountManager::class)->getFilesystem($this->getDiskSlug());
return app('filesystem')->disk($this->getDiskSlug());
}

/**
* Return the file resource.
* Return the file resource.
*
* @return null|File
* @return null
*/
public function resource()
{
Expand Down
2 changes: 1 addition & 1 deletion src/File/FileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public function make(FileInterface $file)

$response->headers->set('Content-Disposition', 'inline');

return $response->setContent($this->manager->read($file->location()));
return $response->setContent($this->manager->disk($file->getDiskSlug())->read($file->path()));
}
}
8 changes: 4 additions & 4 deletions src/File/FileResponse.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace Anomaly\FilesModule\File;

use Anomaly\FilesModule\File\Contract\FileInterface;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Http\Response;
use Illuminate\Routing\ResponseFactory;
use League\Flysystem\MountManager;

/**
* Class FileResponse
Expand All @@ -18,7 +18,7 @@ class FileResponse
/**
* The mount manager.
*
* @var MountManager
* @var FilesystemManager
*/
protected $manager;

Expand All @@ -33,9 +33,9 @@ class FileResponse
* Create a new FileResponse
*
* @param ResponseFactory $response
* @param MountManager $manager
* @param FilesystemManager $manager
*/
public function __construct(ResponseFactory $response, MountManager $manager)
public function __construct(ResponseFactory $response, FilesystemManager $manager)
{
$this->manager = $manager;
$this->response = $response;
Expand Down
10 changes: 5 additions & 5 deletions src/File/FileStreamer.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace Anomaly\FilesModule\File;

use Anomaly\FilesModule\File\Contract\FileInterface;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Http\Request;
use Illuminate\Routing\ResponseFactory;
use League\Flysystem\MountManager;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -27,12 +27,12 @@ class FileStreamer extends FileResponse
* Create a new FileStreamer instance.
*
* @param Request $request
* @param MountManager $manager
* @param FilesystemManager $manager
* @param ResponseFactory $response
*/
public function __construct(
Request $request,
MountManager $manager,
FilesystemManager $manager,
ResponseFactory $response
) {
$this->request = $request;
Expand All @@ -49,8 +49,8 @@ public function __construct(
public function stream(FileInterface $file)
{
$response = $this->make($file);

$stream = $this->manager->readStream($file->location());
$stream = $this->manager->disk($file->getDiskSlug())->readStream($file->path());

return $this->response->stream(
function () use ($stream) {
Expand Down

0 comments on commit 5d744d2

Please sign in to comment.