/**
  * Handle the command.
  *
  * @param DiskRepositoryInterface $disks
  * @param FileRepositoryInterface $files
  * @param FileFieldTypeParser     $parser
  * @param Request                 $request
  * @param MountManager            $manager
  *
  * @return null|bool|FileInterface
  */
 public function handle(DiskRepositoryInterface $disks, FileRepositoryInterface $files, FileFieldTypeParser $parser, Request $request, MountManager $manager)
 {
     $path = trim(array_get($this->fieldType->getConfig(), 'path'), './');
     $file = $request->file($this->fieldType->getInputName());
     $value = $request->get($this->fieldType->getInputName() . '_id');
     /**
      * Make sure we have at least
      * some kind of input.
      */
     if ($file === null) {
         if (!$value) {
             return null;
         }
         return $files->find($value);
     }
     // Make sure we have a valid upload disk. First by slug.
     if (!($disk = $disks->findBySlug($slug = array_get($this->fieldType->getConfig(), 'disk')))) {
         // If that fails look up by id.
         if (!($disk = $disks->find($id = array_get($this->fieldType->getConfig(), 'disk')))) {
             return null;
         }
     }
     // Make the path.
     $path = $parser->parse($path, $this->fieldType);
     $path = (!empty($path) ? $path . '/' : null) . $file->getClientOriginalName();
     return $manager->putStream($disk->path($path), fopen($file->getRealPath(), 'r+'));
 }
 /**
  * Handle the command.
  *
  * @param DiskRepositoryInterface $disks
  * @param FileRepositoryInterface $files
  * @param FileFieldTypeParser     $parser
  * @param Request                 $request
  * @param MountManager            $manager
  * @return null|bool|FileInterface
  */
 public function handle(DiskRepositoryInterface $disks, FileRepositoryInterface $files, FileFieldTypeParser $parser, Request $request, MountManager $manager)
 {
     $path = trim(array_get($this->fieldType->getConfig(), 'path'), './');
     $entry = $this->fieldType->getEntry();
     $file = $request->file($this->fieldType->getInputName());
     $value = $request->get($this->fieldType->getInputName() . '_id');
     /**
      * Make sure we have at least
      * some kind of input.
      */
     if ($file === null) {
         if (!$value) {
             return null;
         }
         return $files->find($value);
     }
     // Make sure we have a valid upload disk.
     if (!($disk = $disks->find($id = array_get($this->fieldType->getConfig(), 'disk')))) {
         throw new \Exception("The configured disk [{$id}] for [{$this->fieldType->getInputName()}] could not be found.");
     }
     // Make the path.
     $path = $parser->parse($path, $this->fieldType);
     $path = (!empty($path) ? $path . '/' : null) . $file->getClientOriginalName();
     return $manager->putStream($disk->path($path), fopen($file->getRealPath(), 'r+'));
 }
 /**
  * Sync a file.
  *
  * @param File          $resource
  * @param DiskInterface $disk
  * @return null|FileInterface
  */
 public function sync(File $resource, DiskInterface $disk)
 {
     $folder = $this->syncFolder($resource, $disk);
     if (!($file = $this->files->findByName(basename($resource->getPath()), $disk, $folder))) {
         $file = $this->files->create(['name' => basename($resource->getPath()), 'folder_id' => $folder ? $folder->getId() : null, 'disk_id' => $disk->getId(), 'size' => $resource->getSize(), 'mime_type' => $resource->getMimetype(), 'extension' => pathinfo($resource->getPath(), PATHINFO_EXTENSION)]);
     }
     return $file;
 }
Beispiel #4
0
 /**
  * Handle the command.
  *
  * @param FileRepositoryInterface   $files
  * @param FolderRepositoryInterface $folders
  */
 public function handle(FileRepositoryInterface $files, FolderRepositoryInterface $folders)
 {
     $folder = $folders->findBySlug(dirname($this->file->getPath()));
     $file = $files->findByNameAndFolder(basename($this->file->getPath()), $folder);
     if ($file) {
         $files->delete($file);
     }
 }
Beispiel #5
0
 /**
  * Locate a file by disk and path.
  *
  * @param $folder
  * @param $path
  * @return FileInterface|null
  */
 public function locate($folder, $name)
 {
     $folder = $this->folders->findBySlug($folder);
     if (!($file = $this->files->findByNameAndFolder($name, $folder))) {
         return null;
     }
     return $file;
 }
 /**
  * Modify the value for database storage.
  *
  * @param  $value
  * @return int
  */
 public function modify($value)
 {
     if ($value instanceof FileInterface) {
         return $value->getId();
     }
     if ($value && ($file = $this->files->find($value))) {
         return $file->getId();
     }
     return null;
 }
 /**
  * Restore the value from storage format.
  *
  * @param  $value
  * @return null|FileInterface
  */
 public function restore($value)
 {
     if (!$this->needsRestoring($value)) {
         return $value;
     }
     if ($value && ($file = $this->files->find($value))) {
         return $file;
     }
     return null;
 }
Beispiel #8
0
 /**
  * Handle the command.
  *
  * @param FileRepositoryInterface $files
  */
 public function handle(FileRepositoryInterface $files)
 {
     foreach ($this->folder->getFiles() as $file) {
         if ($this->folder->isForceDeleting()) {
             $files->forceDelete($file);
         } else {
             $files->delete($file);
         }
     }
 }
 /**
  * Restore the value from storage format.
  *
  * @param  $value
  * @return null|FileInterface
  */
 public function restore($value)
 {
     if ($value instanceof FileInterface) {
         return $value;
     }
     if ($value && ($file = $this->files->find($value))) {
         return $file;
     }
     return null;
 }
Beispiel #10
0
 /**
  * Locate a file by disk and path.
  *
  * @param $disk
  * @param $path
  * @return FileInterface|null
  */
 public function locate($disk, $path)
 {
     if (!($disk = $this->disks->findBySlug($disk))) {
         return null;
     }
     $folder = dirname($path) !== '.' ? $this->folders->findByPath(dirname($path), $disk) : null;
     if (!($file = $this->files->findByName(basename($path), $disk, $folder))) {
         return null;
     }
     return $file;
 }
 /**
  * Sync a file.
  *
  * @param File          $resource
  * @param DiskInterface $disk
  * @return null|FileInterface
  */
 public function sync(File $resource, DiskInterface $disk)
 {
     $folder = $this->syncFolder($resource, $disk);
     if (!($file = $this->files->findByNameAndFolder(basename($resource->getPath()), $folder))) {
         $file = $this->files->create(['name' => basename($resource->getPath()), 'folder_id' => $folder ? $folder->getId() : null, 'disk_id' => $disk->getId(), 'size' => $resource->getSize(), 'mime_type' => $resource->getMimetype(), 'extension' => pathinfo($resource->getPath(), PATHINFO_EXTENSION), 'entry_type' => $folder->getEntryModelName(), 'deleted_at' => null]);
     } else {
         if ($file->trashed()) {
             $this->files->restore($file);
         }
         $this->files->save($file);
     }
     return $file;
 }
Beispiel #12
0
 /**
  * Handle the command.
  *
  * @param FileRepositoryInterface   $files
  * @param FolderRepositoryInterface $folders
  * @param Decorator                 $decorator
  * @return \Anomaly\FilesModule\File\Contract\FileInterface|\Anomaly\Streams\Platform\Model\EloquentModel|null
  */
 public function handle(FileRepositoryInterface $files, FolderRepositoryInterface $folders)
 {
     if (is_numeric($this->identifier)) {
         return $files->find($this->identifier);
     }
     if (!is_numeric($this->identifier)) {
         list($folder, $name) = explode('/', $this->identifier);
         if (!($folder = $folders->findBySlug($folder))) {
             return null;
         }
         return $files->findByNameAndFolder($name, $folder);
     }
     return null;
 }
Beispiel #13
0
 /**
  * Fire the command.
  *
  * @param FileRepositoryInterface $files
  */
 public function fire(FileRepositoryInterface $files)
 {
     $missing = false;
     /* @var FileInterface|EloquentModel $file */
     foreach ($files->all() as $file) {
         if (!$file->resource()) {
             $missing = true;
             if (!$this->option('pretend')) {
                 $files->delete($file);
             }
             $this->info($file->path() . ' ' . (!$this->option('pretend') ? 'deleted' : 'missing') . '.');
         }
     }
     if (!$missing) {
         $this->info('Files database is clean.');
     }
 }
 /**
  * Upload a file.
  *
  * @param UploadedFile    $file
  * @param FolderInterface $folder
  * @return bool|FileInterface
  */
 public function upload(UploadedFile $file, FolderInterface $folder)
 {
     $rules = 'required';
     if ($allowed = $folder->getAllowedTypes()) {
         $rules .= '|mimes:' . implode(',', $allowed);
     }
     $validation = $this->validator->make(['file' => $file], ['file' => $rules]);
     if (!$validation->passes()) {
         return false;
     }
     $disk = $folder->getDisk();
     /* @var FileInterface $entry */
     $entry = $this->manager->put($disk->getSlug() . '://' . $folder->getSlug() . '/' . $file->getClientOriginalName(), file_get_contents($file->getRealPath()));
     if (in_array($entry->getExtension(), $this->config->get('anomaly.module.files::mimes.types.image'))) {
         $size = getimagesize($file->getRealPath());
         $this->files->save($entry->setAttribute('width', $size[0])->setAttribute('height', $size[1]));
     }
     return $entry;
 }
 /**
  * Fire just before saving a folder.
  *
  * @param EntryInterface|FolderInterface $entry
  */
 public function deleting(EntryInterface $entry)
 {
     $this->manager->deleteDir($entry->diskPath());
     // Delete contained files.
     foreach ($entry->getFiles() as $file) {
         $this->files->delete($file);
     }
     // Delete contained folders.
     foreach ($entry->getChildren() as $folder) {
         $this->folders->delete($folder);
     }
 }
 /**
  * Return the details of a file or folder.
  *
  * @param $disk
  * @param $path
  * @return string
  */
 public function view($disk, $path)
 {
     $disk = $this->disks->findBySlug($disk);
     $folder = $this->folders->findByPath(dirname($path), $disk);
     $file = $this->files->findByName(basename($path), $disk, $folder);
     if (!$disk || !$folder && !$file) {
         abort(404);
     }
     if ($file) {
         return json_encode($file);
     }
     if ($folder) {
         return json_encode($folder);
     }
 }
Beispiel #17
0
 /**
  * Redirect to a file's URL.
  *
  * @param FileRepositoryInterface $files
  * @param                         $id
  * @return \Illuminate\Http\RedirectResponse
  */
 public function view(FileRepositoryInterface $files, $id)
 {
     /* @var FileInterface $file */
     $file = $files->find($id);
     return $this->redirect->to('files/' . $file->getFolder()->getSlug() . '/' . $file->getName());
 }
Beispiel #18
0
 /**
  * Handle the command.
  *
  * @param FileRepositoryInterface $files
  */
 public function handle(FileRepositoryInterface $files)
 {
     foreach ($this->folder->getFiles() as $file) {
         $files->delete($file);
     }
 }
 /**
  * Redirect to a file's public URL.s
  *
  * @param FileRepositoryInterface $files
  * @param Redirector              $redirector
  * @param                         $id
  * @return \Illuminate\Http\RedirectResponse
  */
 public function view(FileRepositoryInterface $files, Redirector $redirector, $id)
 {
     $file = $files->find($id);
     return $redirector->to($file->publicPath());
 }