1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 14:16:46 +02:00

feat(media): we should have media instead of uploads folder Media API #515

This commit is contained in:
Awilum
2020-12-17 20:27:49 +03:00
parent 01a9872884
commit d5c3fc75a1
11 changed files with 55 additions and 55 deletions

View File

@@ -70,7 +70,7 @@ flextype()->get('/api/images/{path:.+}', function (Request $request, Response $r
// Update calls counter
filesystem()->file($delivery_images_token_file_path)->put(flextype('yaml')->encode(array_replace_recursive($delivery_images_token_file_data, ['calls' => $delivery_images_token_file_data['calls'] + 1])));
if (filesystem()->file(PATH['project'] . '/uploads/entries/' . $args['path'])->exists()) {
if (filesystem()->file(PATH['project'] . '/media/entries/' . $args['path'])->exists()) {
return flextype('images')->getImageResponse($args['path'], $_GET);
}

View File

@@ -62,8 +62,8 @@ class MediaFiles
*/
public function upload(array $file, string $folder)
{
$uploadFolder = PATH['project'] . '/uploads/' . $folder . '/';
$uploadMetadataFolder = PATH['project'] . '/uploads/.meta/' . $folder . '/';
$uploadFolder = PATH['project'] . '/media/' . $folder . '/';
$uploadMetadataFolder = PATH['project'] . '/media/.meta/' . $folder . '/';
if (! filesystem()->directory($uploadFolder)->exists()) {
filesystem()->directory($uploadFolder)->create(0755, true);
@@ -232,7 +232,7 @@ class MediaFiles
$result[$basename]['basename'] = explode('.', basename(flextype('media_files_meta')->getFileMetaLocation($basename)))[0];
$result[$basename]['extension'] = ltrim(strstr($basename, '.'), '.');
$result[$basename]['dirname'] = pathinfo(str_replace('/.meta', '', $file->getPathname()))['dirname'];
$result[$basename]['url'] = 'project/uploads/' . $id . '/' . $basename;
$result[$basename]['url'] = 'project/media/' . $id . '/' . $basename;
if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') {
$fullUrl = flextype('registry')->get('flextype.settings.url');
@@ -240,7 +240,7 @@ class MediaFiles
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
}
$result[$basename]['full_url'] = $fullUrl . '/project/uploads/' . $id . '/' . $basename;
$result[$basename]['full_url'] = $fullUrl . '/project/media/' . $id . '/' . $basename;
}
$result = filter($result, $options);
@@ -257,7 +257,7 @@ class MediaFiles
$result['extension'] = ltrim(strstr($id, '.'), '.');
$result['dirname'] = pathinfo(str_replace('/.meta', '', flextype('media_files_meta')->getFileMetaLocation($id)))['dirname'];
$result['url'] = 'project/uploads/' . $id;
$result['url'] = 'project/media/' . $id;
if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') {
$fullUrl = flextype('registry')->get('flextype.settings.url');
@@ -265,7 +265,7 @@ class MediaFiles
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
}
$result['full_url'] = $fullUrl . '/project/uploads/' . $id;
$result['full_url'] = $fullUrl . '/project/media/' . $id;
}
$result = filter($result, $options);
@@ -358,7 +358,7 @@ class MediaFiles
*/
public function getFileLocation(string $id): string
{
return PATH['project'] . '/uploads/' . $id;
return PATH['project'] . '/media/' . $id;
}
/**

View File

@@ -101,6 +101,6 @@ class MediaFilesMeta
*/
public function getFileMetaLocation(string $id): string
{
return PATH['project'] . '/uploads/.meta/' . $id . '.yaml';
return PATH['project'] . '/media/.meta/' . $id . '.yaml';
}
}

View File

@@ -46,7 +46,7 @@ class MediaFolders
if (filesystem()->directory(flextype('media_folders_meta')->getDirectoryMetaLocation($id))->exists()) {
$result['path'] = $id;
$result['full_path'] = str_replace('/.meta', '', flextype('media_folders_meta')->getDirectoryMetaLocation($id));
$result['url'] = 'project/uploads/' . $id;
$result['url'] = 'project/media/' . $id;
if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') {
$fullUrl = flextype('registry')->get('flextype.settings.url');
@@ -54,7 +54,7 @@ class MediaFolders
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
}
$result['full_url'] = $fullUrl . '/project/uploads/' . $id;
$result['full_url'] = $fullUrl . '/project/media/' . $id;
}
$result = filter($result, $options);
@@ -179,6 +179,6 @@ class MediaFolders
*/
public function getDirectoryLocation(string $id): string
{
return PATH['project'] . '/uploads/' . $id;
return PATH['project'] . '/media/' . $id;
}
}

View File

@@ -26,6 +26,6 @@ class MediaFoldersMeta
*/
public function getDirectoryMetaLocation(string $id): string
{
return PATH['project'] . '/uploads/.meta/' . $id;
return PATH['project'] . '/media/.meta/' . $id;
}
}

View File

@@ -269,7 +269,7 @@ flextype()->container()['images'] = static function () {
// Set source filesystem
$source = new Flysystem(
new Local(PATH['project'] . '/uploads/entries/')
new Local(PATH['project'] . '/media/entries/')
);
// Set cache filesystem

View File

@@ -5,23 +5,23 @@ use Flextype\Component\Filesystem\Filesystem;
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/entries')->create();
filesystem()->directory(PATH['project'] . '/uploads')->create(0755, true);
filesystem()->directory(PATH['project'] . '/uploads/.meta')->create(0755, true);
filesystem()->directory(PATH['project'] . '/uploads/foo')->create(0755, true);
filesystem()->directory(PATH['project'] . '/uploads/.meta/foo')->create(0755, true);
filesystem()->directory(PATH['project'] . '/media/.meta')->create(0755, true);
filesystem()->directory(PATH['project'] . '/media/foo')->create(0755, true);
filesystem()->directory(PATH['project'] . '/media/.meta/foo')->create(0755, true);
});
afterEach(function (): void {
filesystem()->directory(PATH['project'] . '/uploads/.meta')->delete();
filesystem()->directory(PATH['project'] . '/media/.meta')->delete();
filesystem()->directory(PATH['project'] . '/uploads')->delete();
filesystem()->directory(PATH['project'] . '/entries')->delete();
});
test('test media_files field', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/uploads/bar.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/bar.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Bar', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/bar.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/bar.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Bar', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
flextype('entries')->create('media', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/media/entry.md')->get()));

View File

@@ -4,41 +4,41 @@ declare(strict_types=1);
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/uploads')->create(0755, true);
filesystem()->directory(PATH['project'] . '/uploads/.meta')->create(0755, true);
filesystem()->directory(PATH['project'] . '/media/.meta')->create(0755, true);
});
afterEach(function (): void {
filesystem()->directory(PATH['project'] . '/uploads/.meta')->delete();
filesystem()->directory(PATH['project'] . '/media/.meta')->delete();
filesystem()->directory(PATH['project'] . '/uploads')->delete();
});
test('test update() method', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertTrue(flextype('media_files_meta')->update('foo.txt', 'description', 'Foo description'));
$this->assertEquals('Foo description', flextype('yaml')->decode(filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->get())['description']);
$this->assertEquals('Foo description', flextype('yaml')->decode(filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->get())['description']);
});
test('test add() method', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertTrue(flextype('media_files_meta')->add('foo.txt', 'bar', 'Bar'));
$this->assertEquals('Bar', flextype('yaml')->decode(filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->get())['bar']);
$this->assertEquals('Bar', flextype('yaml')->decode(filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->get())['bar']);
});
test('test delete() method', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertTrue(flextype('media_files_meta')->delete('foo.txt', 'title'));
$this->assertTrue(empty(flextype('yaml')->decode(filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->get())['bar']));
$this->assertTrue(empty(flextype('yaml')->decode(filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->get())['bar']));
});
test('test getFileMetaLocation() method', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertStringContainsString('foo.txt.yaml',
flextype('media_files_meta')->getFileMetaLocation('foo.txt'));
});

View File

@@ -4,19 +4,19 @@ declare(strict_types=1);
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/uploads')->create(0755, true);
filesystem()->directory(PATH['project'] . '/uploads/.meta')->create(0755, true);
filesystem()->directory(PATH['project'] . '/media/.meta')->create(0755, true);
});
afterEach(function (): void {
filesystem()->directory(PATH['project'] . '/uploads/.meta')->delete();
filesystem()->directory(PATH['project'] . '/media/.meta')->delete();
filesystem()->directory(PATH['project'] . '/uploads')->delete();
});
test('test fetch() method', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/uploads/bar.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/bar.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Bar', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/bar.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/bar.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Bar', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertTrue(count(flextype('media_files')->fetch('foo.txt')) > 0);
$this->assertEquals('Foo', flextype('media_files')->fetch('foo.txt')['title']);
@@ -26,8 +26,8 @@ test('test fetch() method', function () {
});
test('test move() method', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertTrue(flextype('media_files')->move('foo.txt', 'bar.txt'));
$this->assertTrue(flextype('media_files')->move('bar.txt', 'foo.txt'));
@@ -38,8 +38,8 @@ test('test copy() method', function () {
$this->assertTrue(flextype('media_folders')->create('foo'));
$this->assertTrue(flextype('media_folders')->create('bar'));
filesystem()->file(PATH['project'] . '/uploads/foo/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertTrue(flextype('media_files')->copy('foo/foo.txt', 'bar/foo.txt'));
$this->assertTrue(flextype('media_files')->copy('foo/foo.txt', 'bar/bar.txt'));
@@ -47,22 +47,22 @@ test('test copy() method', function () {
});
test('test has() method', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertTrue(flextype('media_files')->has('foo.txt'));
$this->assertFalse(flextype('media_files')->has('bar.txt'));
});
test('test getFileLocation() method', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertStringContainsString('foo.txt', flextype('media_files')->getFileLocation('foo.txt'));
});
test('test delete() method', function () {
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
filesystem()->file(PATH['project'] . '/media/foo.txt')->put('foo');
filesystem()->file(PATH['project'] . '/media/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
$this->assertTrue(flextype('media_files')->delete('foo.txt'));
$this->assertFalse(flextype('media_files')->delete('foo.txt'));
});

View File

@@ -4,11 +4,11 @@ declare(strict_types=1);
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/uploads')->create(0755, true);
filesystem()->directory(PATH['project'] . '/uploads/.meta')->create(0755, true);
filesystem()->directory(PATH['project'] . '/media/.meta')->create(0755, true);
});
afterEach(function (): void {
filesystem()->directory(PATH['project'] . '/uploads/.meta')->delete();
filesystem()->directory(PATH['project'] . '/media/.meta')->delete();
filesystem()->directory(PATH['project'] . '/uploads')->delete();
});

View File

@@ -4,11 +4,11 @@ declare(strict_types=1);
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/uploads')->create(0755, true);
filesystem()->directory(PATH['project'] . '/uploads/.meta')->create(0755, true);
filesystem()->directory(PATH['project'] . '/media/.meta')->create(0755, true);
});
afterEach(function (): void {
filesystem()->directory(PATH['project'] . '/uploads/.meta')->delete();
filesystem()->directory(PATH['project'] . '/media/.meta')->delete();
filesystem()->directory(PATH['project'] . '/uploads')->delete();
});