public function createAttachmentEntity(EntityInterface $entity, $filePath, array $tags = [])
    {
        if (!file_exists($filePath)) {
            throw new \Exception("File {$filePath} does not exist.");
        }
        if (!is_readable($filePath)) {
            throw new \Exception("File {$filePath} cannot be read.");
        }
        $file = new File($filePath);
        $info = $file->info();

        // in filepath, we store the path relative to the Attachment.path configuration
        // to make it easy to switch storage
        $info = $this->__getFileName($info, $entity);
        $targetPath = $entity->source() . '/' . $entity->id . '/' . $info['basename'];

        $attachment = $this->newEntity([
            'model' => $entity->source(),
            'foreign_key' => $entity->id,
            'filename' => $info['basename'],
            'filesize' => $info['filesize'],
            'filetype' => $info['mime'],
            'filepath' => $targetPath,
            'tmpPath' => $filePath,
            'tags' => $tags
        ]);
        return $attachment;
    }
public function createAttachmentEntity(EntityInterface $entity, $filePath, array $tags = [])
    {
        if (!file_exists($filePath)) {
            throw new \Exception("File {$filePath} does not exist.");
        }
        if (!is_readable($filePath)) {
            throw new \Exception("File {$filePath} cannot be read.");
        }
        $file = new File($filePath);
        $info = $file->info();

        // in filepath, we store the path relative to the Attachment.path configuration
        // to make it easy to switch storage
        $info = $this->__getFileName($info, $entity);
        $targetPath = $entity->source() . '/' . $entity->id . '/' . $info['basename'];

        $attachment = $this->newEntity([
            'model' => $entity->source(),
            'foreign_key' => $entity->id,
            'filename' => $info['basename'],
            'filesize' => $info['filesize'],
            'filetype' => $info['mime'],
            'filepath' => $targetPath,
            'tmpPath' => $filePath,
            'tags' => $tags
        ]);
        return $attachment;
    }
public function createAttachmentEntity ( EntityInterface $ entity , $ filePath , array $ tags = [ ] ) { if ( ! file_exists ( $ filePath ) ) { throw new \ Exception ( "File {$filePath} does not exist." ) ; } if ( ! is_readable ( $ filePath ) ) { throw new \ Exception ( "File {$filePath} cannot be read." ) ; } $ file = new File ( $ filePath ) ; $ info = $ file -> info ( ) ; // in filepath, we store the path relative to the Attachment.path configuration // to make it easy to switch storage $ info = $ this -> __getFileName ( $ info , $ entity ) ; $ targetPath = $ entity -> source ( ) . '/' . $ entity -> id . '/' . $ info [ 'basename' ] ; $ attachment = $ this -> newEntity ( [ 'model' => $ entity -> source ( ) , 'foreign_key' => $ entity -> id , 'filename' => $ info [ 'basename' ] , 'filesize' => $ info [ 'filesize' ] , 'filetype' => $ info [ 'mime' ] , 'filepath' => $ targetPath , 'tmpPath' => $ filePath , 'tags' => $ tags ] ) ; return $ attachment ; }
Creates an Attachment entity based on the given file

@param EntityInterface $entity Entity the file will be attached to
@param string $filePath Absolute path to the file
@param array $tags Indexed array of tags to be assigned
@return Attachment
@throws \Exception If the given file doesn't exist or isn't readable
Creates an Attachment entity based on the given file