Skip to content

Commit

Permalink
update sync function
Browse files Browse the repository at this point in the history
  • Loading branch information
spektra2147 committed Nov 11, 2023
1 parent 2f3a585 commit e6aa89d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
26 changes: 11 additions & 15 deletions src/Console/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Anomaly\FilesModule\Folder\Contract\FolderInterface;
use Anomaly\FilesModule\Folder\Contract\FolderRepositoryInterface;
use Illuminate\Console\Command;
use Illuminate\Filesystem\FilesystemManager;
use League\Flysystem\File;
use League\Flysystem\MountManager;

/**
* Class Sync
Expand Down Expand Up @@ -35,38 +35,34 @@ class Sync extends Command
/**
* Handle the command.
*
* @param MountManager $manager
* @param FilesystemManager $manager
* @param FileSynchronizer $synchronizer
* @param FolderRepositoryInterface $folders
* @param FileRepositoryInterface $files
*/
public function handle(
MountManager $manager,
FilesystemManager $manager,
FileSynchronizer $synchronizer,
FolderRepositoryInterface $folders,
FileRepositoryInterface $files
) {
/* @var FolderInterface $folder */
foreach ($folders->all() as $folder) {

$contents = array_filter(
$manager->listContents($folder->path()),
function (array $file) {
return $file['type'] == 'file';
}
);
$contents = $manager->disk($folder->getDisk()->getSlug())->listContents($folder->getSlug(), false);

$contents = $contents->filter(function ($file) {
return $file->type() == 'file';
});

$this->info('Checking:' . $folder->path());

foreach ($contents as $file) {
if (!$files->findByNameAndFolder($file['basename'], $folder)) {

/* @var File $resource */
$resource = $manager->get($path = $folder->path($file['basename']));
if (!$files->findByNameAndFolder(basename($file->path()), $folder)) {

$synchronizer->sync($resource, $folder->getDisk());
$synchronizer->sync($file, $folder->getDisk());

$this->info('Synced: ' . $path);
$this->info('Synced: ' . $file->path());
}
}
}
Expand Down
60 changes: 58 additions & 2 deletions src/Disk/Adapter/AdapterFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
use Anomaly\FilesModule\Disk\Adapter\Command\SyncFile;
use Anomaly\FilesModule\Disk\Adapter\Command\SyncFolder;
use Anomaly\FilesModule\Disk\Contract\DiskInterface;
use Illuminate\Support\Collection;
use League\Flysystem\Config;
use League\Flysystem\Directory;
use League\Flysystem\DirectoryAttributes;
use League\Flysystem\DirectoryListing;
use League\Flysystem\File;
use League\Flysystem\FileAttributes;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\Handler;
use League\Flysystem\PathPrefixer;
use League\Flysystem\SymbolicLinkEncountered;
use League\Flysystem\Util;
use function League\Flysystem\path;

/**
* Class AdapterFilesystem
Expand Down Expand Up @@ -185,9 +191,59 @@ public function fileSize(string $path): int
* @return iterable
* @throws \League\Flysystem\FilesystemException
*/
public function listContents(string $path, bool $deep): iterable
public function listContents(string $path, bool $deep)
{
return $this->adapter->listContents($path, $deep);
$path = $this->url($path);
if (!is_dir($path)) {
return;
}

$iterator = $this->listDirectory($path);

$contents = new Collection();

foreach ($iterator as $fileInfo) {
$pathName = $fileInfo->getPathname();

if ($fileInfo->isLink()) {
if ($this->linkHandling & self::SKIP_LINKS) {
continue;
}
throw SymbolicLinkEncountered::atLocation($pathName);
}

$prefixer = new PathPrefixer($this->baseUrl, DIRECTORY_SEPARATOR);
$path = $prefixer->stripPrefix($pathName);

$lastModified = $fileInfo->getMTime();
$isDirectory = $fileInfo->isDir();
$permissions = octdec(substr(sprintf('%o', $fileInfo->getPerms()), -4));

$item = $isDirectory ? new DirectoryAttributes(str_replace('\\', '/', $path), null, $lastModified) : new FileAttributes(
str_replace('\\', '/', $path),
$fileInfo->getSize(),
null,
$lastModified,
$this->mimeType($path)
);

$contents->add($item);
}

return $contents;
}

private function listDirectory(string $location)
{
$iterator = new \DirectoryIterator($location);

foreach ($iterator as $item) {
if ($item->isDot()) {
continue;
}

yield $item;
}
}

/**
Expand Down

0 comments on commit e6aa89d

Please sign in to comment.