Skip to content

Commit b5b2de1

Browse files
authored
Upgrade FlySystem 2.0 (#21)
* Align with atk 2.4 * Add php-cs-fixer * Align to 2.4 and cleanup * Add helper class for View and Download of files * typo old namespace * Update readme with new namespace * Renamed basic.php to index.php * Add headers normalize * Typo classname * Upgrade FlySystem 2.0
1 parent 72ef0d4 commit b5b2de1

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ docs/build
33
/build
44
/vendor
55
.DS_Store
6-
demos/localfiles/file-*
6+
/demos/localfiles/file-*
7+
/demos/filestore.db

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"require": {
2828
"atk4/ui": "2.4.0",
2929
"php": ">=7.2",
30-
"league/flysystem": "^1.0"
30+
"league/flysystem": "^2.0"
3131
},
3232
"require-dev": {
3333
"phpunit/phpunit": "<6",

demos/index.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@
1313
use League\Flysystem\Adapter\Local;
1414
use League\Flysystem\Filesystem;
1515

16-
$app = new \Atk4\Ui\App('Filestore Demo');
17-
$app->initLayout([\Atk4\Ui\Layout\Centered::class]);
18-
19-
// change this as needed
20-
$app->db = Atk4\Data\Persistence::connect('mysql://root:root@localhost/atk4_filestore');
21-
22-
// specify folder where files will be actually stored
23-
$adapter = new Local(__DIR__ . '/localfiles');
24-
$filesystem = new Filesystem($adapter);
25-
2616
class Friend extends \Atk4\Data\Model
2717
{
2818
public $table = 'friend';
@@ -39,6 +29,30 @@ protected function init(): void
3929
}
4030
}
4131

32+
// specify folder where files will be actually stored
33+
$adapter = new \League\Flysystem\Local\LocalFilesystemAdapter(__DIR__ . '/localfiles');
34+
$filesystem = new Filesystem($adapter);
35+
36+
// init App
37+
$app = new \Atk4\Ui\App('Filestore Demo');
38+
$app->initLayout([\Atk4\Ui\Layout\Centered::class]);
39+
40+
// init db
41+
$db_file = __DIR__ . "/filestore.db";
42+
$db_file_exists = file_exists($db_file);
43+
// change this as needed
44+
$app->db = Atk4\Data\Persistence::connect('sqlite:' . $db_file);
45+
/*
46+
if (!$db_file_exists) {
47+
(new \Atk4\Schema\Migration(new \Friend($app->db, ['filesystem' => $filesystem,])))
48+
->dropIfExists()
49+
->create();
50+
(new \Atk4\Schema\Migration(new \Atk4\Filestore\Model\File($app->db)))
51+
->dropIfExists()
52+
->create();
53+
}
54+
*/
55+
4256
$col = Columns::addTo($app);
4357

4458
$form = Form::addTo($col->addColumn());

src/Form/Control/Upload.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Atk4\Filestore\Form\Control;
66

77
use Atk4\Filestore\Field\File;
8+
use League\MimeTypeDetection\FinfoMimeTypeDetector;
89

910
class Upload extends \Atk4\Ui\Form\Control\Upload
1011
{
@@ -39,17 +40,19 @@ protected function uploaded($file)
3940
fclose($stream);
4041
}
4142

43+
$detector = new \League\MimeTypeDetection\FinfoMimeTypeDetector();
44+
45+
$mimeType = $detector->detectMimeTypeFromFile($file['tmp_name']);
4246
// get meta from browser
43-
$f->set('meta_mime_type', $file['type']);
47+
$f->set('meta_mime_type', $mimeType);
4448

4549
// store meta-information
4650
$is = getimagesize($file['tmp_name']);
47-
if ($f->set('meta_is_image', (bool) $is)) {
48-
$f->set('meta_mime_type', $is['mime']);
51+
if ($f->set('meta_is_image', (bool) $is) === true) {
4952
$f->set('meta_image_width', $is[0]);
5053
$f->set('meta_image_height', $is[1]);
51-
//$m['extension'] = $is['mime'];
5254
}
55+
5356
$f->set('meta_md5', md5_file($file['tmp_name']));
5457
$f->set('meta_filename', $file['name']);
5558
$f->set('meta_size', $file['size']);

src/Helper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ private static function output(File $model, array $headers, App $app = null)
3232
$location = $model->get('location');
3333

3434
if ($app !== null) {
35-
$app->terminate($model->flysystem->get($location)->read(), $headers);
35+
$app->terminate($model->flysystem->read($location), $headers);
3636
}
3737

3838
$isCli = \PHP_SAPI === 'cli'; // for phpunit
3939

4040
foreach ($headers as $k => $v) {
4141
if (!$isCli) {
42-
4342
$kCamelCase = preg_replace_callback('~(?<![a-zA-Z])[a-z]~', function ($matches) {
4443
return strtoupper($matches[0]);
4544
}, $k);

0 commit comments

Comments
 (0)