Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/incapption-public/filesystem


https://github.com/incapption-public/filesystem

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# FileSystem

This package is an object wrapper for https://github.com/thephpleague/flysystem

## Installation
```bash
composer require incapption/file-system
```

## Requirements
```bash
PHP >= 7.2
```

## How to use
### 1. Create your own class
Create your own file class, which extends ```Incapption\FileSystem\File```.
>You always need an adapter, based on the official adapters https://flysystem.thephpleague.com/docs/
>
#### 1.1 Example for local files
```php
__delete();

$file = new MyFile();
$file->__write('user/1/101/images/avatar.jpg', file_get_contents('tmp/uploaded_avatar.jpg'));
```

---

#### 1.2 Example for S3 files
```php
'latest',
'region' => 'us-east-2',
'endpoint' => 'https://s3.us-east-2.amazonaws.com',
'credentials' => [
'key' => 'YOUR_API_KEY',
'secret' => 'YOUR_API_SECRET',
],
'http' => [
'timeout' => 10,
'connect_timeout' => 10,
],
]);

$adapter = new League\Flysystem\AwsS3V3\AwsS3V3Adapter(
$client,
'your-bucket-name'
);

parent::__construct($adapter, $filePath);
}

public function myCustomMethod()
{
// you can extend your own class with additional methods
}
}

// examples
$file = new MyFile('public/images/avatar03.jpg');
$file->__delete();

$file = new MyFile();
$file->__write('user/1/101/images/avatar.jpg', file_get_contents('tmp/uploaded_avatar.jpg'));
```
**Requirements for S3**

**Additional composer package**
```bash
# So you can use AwsS3V3Adapter
composer require league/flysystem-aws-s3-v3:2.4.3
```

**IAM Permissions**\
The required IAM permissions are as followed:
```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetObject",
"s3:DeleteObject",
"s3:GetObjectAcl",
"s3:PutObjectAcl",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::your-bucket-name",
"arn:aws:s3:::your-bucket-name/*"
]
}
]
}
```

### 2. Public methods
Methods you can use on your file object
```php
public function __construct(FilesystemAdapter $adapter, ?string $filePath = null);

public function __write(string $dest, $contents): FileInterface;

public function __writeStream(string $dest, $contents): FileInterface;

public function __move(string $dest): FileInterface;

public function __rename(string $new_name): FileInterface;

public function __copy(string $dest): FileInterface;

public function __delete(): bool;

public function getContent(): string;

public function getFullPath(): string;

public function getName(): string;

public function getSize(): int;

public function getExtension(): string;

public function getMimeType(): string;

public function getLastModified(): int;

public function getDirectoryName(): string;

public function toArray(): array;

public function toJson(): string;

/* IN ADDITION:
* all public function from \League\Flysystem\Filesystem
*/
```

### 3. Exceptions
>FilesystemException are thrown if something went wrong
```php

### 4. Things to know
- If you delete a file, the object gets resetted
- getExtension() returns the Extension without a leading dot
- Methods are throwing exception. If no exception is thrown, everything worked fine