1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-27 15:00:11 +02:00

feat(media): Media API implementation new structure #428

This commit is contained in:
Awilum
2020-06-02 23:53:33 +03:00
parent 6e60dd955d
commit 13439dc42a
4 changed files with 277 additions and 209 deletions

View File

@@ -15,7 +15,7 @@ use Intervention\Image\ImageManagerStatic as Image;
use Slim\Http\Environment;
use Slim\Http\Uri;
class Media
class MediaFiles
{
/**
* Flextype Dependency Container
@@ -42,7 +42,7 @@ class Media
*
* @access public
*/
public function createFile(array $file, string $folder)
public function create(array $file, string $folder)
{
$upload_folder = PATH['project'] . '/uploads/' . $folder . '/';
$upload_metadata_folder = PATH['project'] . '/uploads/.meta/' . $folder . '/';
@@ -90,7 +90,7 @@ class Media
//
if (($file['error'] != UPLOAD_ERR_INI_SIZE)
and ($file['error'] == UPLOAD_ERR_OK)
and ($file['size'] <= $max_size)) {
and ($file['size'] <= $max_file_size)) {
//
// Validation rule to test if an upload is an image and, optionally, is the correct size.
//
@@ -210,6 +210,33 @@ class Media
return false;
}
/**
* Fetch single file
*
* @param string $directory The directory to list.
*
* @return array A list of file metadata.
*/
public function fetchsingle(string $id) : array
{
$result = [];
if (Filesystem::has($this->flextype['media_files_meta']->getFileMetaLocation($id))) {
$result = $this->flextype['serializer']->decode(Filesystem::read($this->flextype['media_files_meta']->getFileMetaLocation($id)), 'yaml');
$result['url'] = 'project/uploads/' . $id;
if ($this->flextype['registry']->has('flextype.settings.url') && $this->flextype['registry']->get('flextype.settings.url') != '') {
$full_url = $this->flextype['registry']->get('flextype.settings.url');
} else {
$full_url = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
}
$result['full_url'] = $full_url . '/project/uploads/' . $id;
}
return $result;
}
/**
* Fetch files collection
*
@@ -217,11 +244,11 @@ class Media
*
* @return array A list of file metadata.
*/
public function fetchFilesCollection(string $folder) : array
public function fetchCollection(string $folder) : array
{
$result = [];
foreach (Filesystem::listContents($this->getDirMetaLocation($folder)) as $file) {
foreach (Filesystem::listContents($this->flextype['media_folders_meta']->getFolderMetaLocation($folder)) as $file) {
$result[$file['basename']] = $this->flextype['serializer']->decode(Filesystem::read($file['path']), 'yaml');
$result[$file['basename']]['url'] = 'project/uploads/' . $folder . '/' . $file['basename'];
@@ -237,55 +264,6 @@ class Media
return $result;
}
/**
* Fetch single file
*
* @param string $directory The directory to list.
*
* @return array A list of file metadata.
*/
public function fetchFilesSingle(string $id) : array
{
$result = [];
if (Filesystem::has($this->getFileMetaLocation($id))) {
$result = $this->flextype['serializer']->decode(Filesystem::read($this->getFileMetaLocation($id)), 'yaml');
$result['url'] = 'project/uploads/' . $id;
if ($this->flextype['registry']->has('flextype.settings.url') && $this->flextype['registry']->get('flextype.settings.url') != '') {
$full_url = $this->flextype['registry']->get('flextype.settings.url');
} else {
$full_url = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
}
$result['full_url'] = $full_url . '/project/uploads/' . $id;
}
return $result;
}
/**
* Create folder
*
* @param string $id Unique identifier of the folder.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function createFolder(string $id) : bool
{
if (!Filesystem::has($this->getDirLocation($id)) && !Filesystem::has($this->getDirMetaLocation($id))) {
if (Filesystem::createDir($this->getDirLocation($id))) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Rename file
*
@@ -296,15 +274,15 @@ class Media
*
* @access public
*/
public function renameFile(string $id, string $new_id) : bool
public function rename(string $id, string $new_id) : bool
{
if (!Filesystem::has($this->getFileLocation($new_id)) && !Filesystem::has($this->getFileMetaLocation($new_id))) {
if (rename($this->getFileLocation($id), $this->getFileLocation($new_id)) && rename($this->getFileMetaLocation($id), $this->getFileMetaLocation($new_id))) {
if (!Filesystem::has($this->getFileLocation($new_id)) && !Filesystem::has($this->flextype['media_files_meta']->getFileMetaLocation($new_id))) {
if (rename($this->getFileLocation($id), $this->getFileLocation($new_id)) && rename($this->flextype['media_files_meta']->getFileMetaLocation($id), $this->flextype['media_files_meta']->getFileMetaLocation($new_id))) {
// Update meta file
$file_data = $this->flextype['serializer']->decode(Filesystem::read($this->getFileMetaLocation($new_id)), 'yaml');
$file_data = $this->flextype['serializer']->decode(Filesystem::read($this->flextype['media_files_meta']->getFileMetaLocation($new_id)), 'yaml');
$file_data['filename'] = basename($new_id);
Filesystem::write($this->getFileMetaLocation($new_id), $this->flextype['serializer']->encode($file_data, 'yaml'));
Filesystem::write($this->flextype['media_files_meta']->getFileMetaLocation($new_id), $this->flextype['serializer']->encode($file_data, 'yaml'));
return true;
} else {
@@ -315,97 +293,6 @@ class Media
}
}
/**
* Update file meta information
*
* @param string $id Unique identifier of the file.
* @param string $field Field name
* @param string $value Field value
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function updateFileMeta(string $id, string $field, string $value) : bool
{
$file_data = $this->flextype['serializer']->decode(Filesystem::read($this->getFileMetaLocation($id)), 'yaml');
if (Arr::keyExists($file_data, $field)) {
Arr::set($file_data, $field, $value);
return Filesystem::write($this->getFileMetaLocation($id), $this->flextype['serializer']->encode($file_data, 'yaml'));
}
return false;
}
/**
* Add file meta information
*
* @param string $id Unique identifier of the file.
* @param string $field Field name
* @param string $value Field value
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function addFileMeta(string $id, string $field, string $value) : bool
{
$file_data = $this->flextype['serializer']->decode(Filesystem::read($this->getFileMetaLocation($id)), 'yaml');
if (!Arr::keyExists($file_data, $field)) {
Arr::set($file_data, $field, $value);
return Filesystem::write($this->getFileMetaLocation($id), $this->flextype['serializer']->encode($file_data, 'yaml'));
}
return false;
}
/**
* Delete file meta information
*
* @param string $id Unique identifier of the file.
* @param string $field Field name
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function deleteFileMeta(string $id, string $field) : bool
{
$file_data = $this->flextype['serializer']->decode(Filesystem::read($this->getFileMetaLocation($id)), 'yaml');
if (Arr::keyExists($file_data, $field)) {
Arr::delete($file_data, $field);
return Filesystem::write($this->getFileMetaLocation($id), $this->flextype['serializer']->encode($file_data, 'yaml'));
}
return false;
}
/**
* Rename folder
*
* @param string $id Unique identifier of the folder.
* @param string $new_id New Unique identifier of the folder.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function renameFolder(string $id, string $new_id) : bool
{
if (!Filesystem::has($this->getDirLocation($new_id)) && !Filesystem::has($this->getDirMetaLocation($new_id))) {
if (rename($this->getDirLocation($id), $this->getDirLocation($new_id)) && rename($this->getDirMetaLocation($id), $this->getDirMetaLocation($new_id))) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Delete file
*
@@ -415,25 +302,10 @@ class Media
*
* @access public
*/
public function deleteFile(string $id)
public function delete(string $id)
{
Filesystem::delete($this->getFileLocation($id));
Filesystem::delete($this->getFileMetaLocation($id));
}
/**
* Delete dir
*
* @param string $id Unique identifier of the file.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function deleteFolder(string $id)
{
Filesystem::deleteDir($this->getDirLocation($id));
Filesystem::deleteDir($this->getDirMetaLocation($id));
Filesystem::delete($this->flextype['media_files_meta']->getFileMetaLocation($id));
}
/**
@@ -449,46 +321,4 @@ class Media
{
return PATH['project'] . '/uploads/' . $id;
}
/**
* Get file meta location
*
* @param string $id Unique identifier of the file.
*
* @return string entry file location
*
* @access public
*/
public function getFileMetaLocation(string $id) : string
{
return PATH['project'] . '/uploads/.meta/' . $id . '.yaml';
}
/**
* Get files directory location
*
* @param string $id Unique identifier of the folder.
*
* @return string entry directory location
*
* @access public
*/
public function getDirLocation(string $id) : string
{
return PATH['project'] . '/uploads/' . $id;
}
/**
* Get files directory meta location
*
* @param string $id Unique identifier of the folder.
*
* @return string entry directory location
*
* @access public
*/
public function getDirMetaLocation(string $id) : string
{
return PATH['project'] . '/uploads/.meta/' . $id;
}
}

View File

@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Arr\Arr;
use Intervention\Image\ImageManagerStatic as Image;
use Slim\Http\Environment;
use Slim\Http\Uri;
class MediaFilesMeta
{
/**
* Update file meta information
*
* @param string $id Unique identifier of the file.
* @param string $field Field name
* @param string $value Field value
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function updateMeta(string $id, string $field, string $value) : bool
{
$file_data = $this->flextype['serializer']->decode(Filesystem::read($this->getFileMetaLocation($id)), 'yaml');
if (Arr::keyExists($file_data, $field)) {
Arr::set($file_data, $field, $value);
return Filesystem::write($this->getFileMetaLocation($id), $this->flextype['serializer']->encode($file_data, 'yaml'));
}
return false;
}
/**
* Add file meta information
*
* @param string $id Unique identifier of the file.
* @param string $field Field name
* @param string $value Field value
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function addMeta(string $id, string $field, string $value) : bool
{
$file_data = $this->flextype['serializer']->decode(Filesystem::read($this->getFileMetaLocation($id)), 'yaml');
if (!Arr::keyExists($file_data, $field)) {
Arr::set($file_data, $field, $value);
return Filesystem::write($this->getFileMetaLocation($id), $this->flextype['serializer']->encode($file_data, 'yaml'));
}
return false;
}
/**
* Delete file meta information
*
* @param string $id Unique identifier of the file.
* @param string $field Field name
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function deleteMeta(string $id, string $field) : bool
{
$file_data = $this->flextype['serializer']->decode(Filesystem::read($this->getFileMetaLocation($id)), 'yaml');
if (Arr::keyExists($file_data, $field)) {
Arr::delete($file_data, $field);
return Filesystem::write($this->getFileMetaLocation($id), $this->flextype['serializer']->encode($file_data, 'yaml'));
}
return false;
}
/**
* Get file meta location
*
* @param string $id Unique identifier of the file.
*
* @return string entry file location
*
* @access public
*/
public function getFileMetaLocation(string $id) : string
{
return PATH['project'] . '/uploads/.meta/' . $id . '.yaml';
}
}

View File

@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Arr\Arr;
use Intervention\Image\ImageManagerStatic as Image;
use Slim\Http\Environment;
use Slim\Http\Uri;
class MediaFoldes
{
/**
* Flextype Dependency Container
*
* @access private
*/
private $flextype;
/**
* Constructor
*
* @access public
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}
/**
* Create folder
*
* @param string $id Unique identifier of the folder.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function create(string $id) : bool
{
if (!Filesystem::has($this->getDirLocation($id)) && !Filesystem::has($this->flextype['media_folders_meta']->getDirMetaLocation($id))) {
if (Filesystem::createDir($this->getDirLocation($id))) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Rename folder
*
* @param string $id Unique identifier of the folder.
* @param string $new_id New Unique identifier of the folder.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function rename(string $id, string $new_id) : bool
{
if (!Filesystem::has($this->getDirLocation($new_id)) && !Filesystem::has($this->flextype['media_folders_meta']->getDirMetaLocation($new_id))) {
if (rename($this->getDirLocation($id), $this->getDirLocation($new_id)) && rename($this->flextype['media_folders_meta']->getDirMetaLocation($id), $this->flextype['media_folders_meta']->getDirMetaLocation($new_id))) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Delete dir
*
* @param string $id Unique identifier of the file.
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function delete(string $id)
{
Filesystem::deleteDir($this->getDirLocation($id));
Filesystem::deleteDir($this->flextype['media_folders_meta']->getDirMetaLocation($id));
}
/**
* Get files directory location
*
* @param string $id Unique identifier of the folder.
*
* @return string entry directory location
*
* @access public
*/
public function getDirLocation(string $id) : string
{
return PATH['project'] . '/uploads/' . $id;
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
class MediaFoldersMeta
{
/**
* Get files directory meta location
*
* @param string $id Unique identifier of the folder.
*
* @return string entry directory location
*
* @access public
*/
public function getDirMetaLocation(string $id) : string
{
return PATH['project'] . '/uploads/.meta/' . $id;
}
}