Permalink
Browse files

New file management

  • Loading branch information...
olvlvl committed Sep 3, 2015
1 parent 7638819 commit d0c4b83929c1f58e6fb9f699560f468875c75904
View
@@ -0,0 +1,23 @@
+<?php
+
+/*
+ * This file is part of the Icybee package.
+ *
+ * (c) Olivier Laviale <olivier.laviale@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Icybee\Modules\Files;
+
+use ICanBoogie;
+
+$hooks = Hooks::class . '::';
+
+return [
+
+ ICanBoogie\Core::class . '::lazy_get_file_storage_index' => $hooks . 'get_file_storage_index',
+ ICanBoogie\Core::class . '::lazy_get_file_storage' => $hooks . 'get_file_storage'
+
+];
View
@@ -23,15 +23,15 @@
'api:files:show' => [
- 'pattern' => '/api/files/<uuid:[0-9a-z\-]{36}>',
+ 'pattern' => '/api/files/<uuid:{:uuid:}>',
'controller' => ShowOperation::class,
'via' => Request::METHOD_GET
],
'api:files:download' => [
- 'pattern' => '/api/files/<uuid:[0-9a-z\-]{36}>/download',
+ 'pattern' => '/api/files/<uuid:{:uuid:}>/download',
'controller' => DownloadOperation::class,
'via' => Request::METHOD_GET
@@ -55,18 +55,32 @@
'files:show' => [
- 'pattern' => '/files/<uuid:[0-9a-z\-]{36}>',
+ 'pattern' => '/files/<uuid:{:uuid:}><extension:[\.a-z]*>',
'controller' => Routing\FilesController::class . '#show',
'via' => Request::METHOD_GET
],
'files:download' => [
- 'pattern' => '/files/<uuid:[0-9a-z\-]{36}>/download',
+ 'pattern' => '/files/download/<uuid:{:uuid:}><extension:[\.a-z]*>',
'controller' => Routing\FilesController::class . '#download',
'via' => Request::METHOD_GET
+ ],
+
+ 'files:protected:show' => [
+
+ 'pattern' => '/files/<nid:\d+><extension:[\.a-z]*>',
+ 'controller' => Routing\FilesAdminController::class . '#show'
+
+ ],
+
+ 'files:protected:download' => [
+
+ 'pattern' => '/files/download/<nid:\d+><extension:[\.a-z]*>',
+ 'controller' => Routing\FilesAdminController::class . '#download'
+
]
] + Make::admin('files', Routing\FilesAdminController::class, [
View
@@ -1,5 +1,14 @@
<?php
+/*
+ * This file is part of the Icybee package.
+ *
+ * (c) Olivier Laviale <olivier.laviale@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
namespace Icybee\Modules\Files;
use ICanBoogie\ActiveRecord\Model;
@@ -18,9 +27,9 @@
Model::EXTENDING => 'nodes',
Model::SCHEMA => [
- 'path' => 'varchar',
- 'mime' => 'varchar',
'size' => [ 'integer', 'unsigned' => true ],
+ 'mime' => [ 'varchar', 'charset' => 'ascii/general_ci' ],
+ 'extension' => [ 'varchar', 16, 'charset' => 'ascii/general_ci' ],
'description' => 'text'
]
@@ -0,0 +1,26 @@
+<?php
+
+/*
+ * This file is part of the Icybee package.
+ *
+ * (c) Olivier Laviale <olivier.laviale@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Icybee\Modules\Files\Binding;
+
+use Icybee\Modules\Files\Storage\FileStorage;
+use Icybee\Modules\Files\Storage\FileStorageIndex;
+
+/**
+ * {@link \ICanBoogie\Core} prototype bindings.
+ *
+ * @property FileStorageIndex $file_storage_index
+ * @property FileStorage $file_storage
+ */
+trait CoreBindings
+{
+
+}
View
@@ -21,6 +21,9 @@
use Icybee\Modules\Files as Root;
use Icybee\Modules\Files\File;
+/**
+ * @property File $record
+ */
class EditBlock extends \Icybee\Modules\Nodes\Block\EditBlock
{
const ACCEPT = '#files-accept';
@@ -67,6 +70,11 @@ protected function lazy_get_children()
$nid = $properties[File::NID];
$path = \ICanBoogie\strip_root($properties[File::PATH]);
+ if (!$path && $this->record)
+ {
+ $path = $this->record->path->relative;
+ }
+
$this->attributes = \ICanBoogie\array_merge_recursive($this->attributes, [
Form::HIDDENS => [
View
@@ -11,30 +11,41 @@
namespace Icybee\Modules\Files;
+use ICanBoogie\HTTP\File as HTTPFile;
+
+use Icybee\Binding\Core\PrototypedBindings;
+use Icybee\Modules\Files\Storage\Pathname;
use Icybee\Modules\Nodes\Node;
/**
* Representation of a managed file.
*
- * @property-read string $extension The file extension. If any, the extension includes the dot,
- * e.g. ".zip".
+ * @property-read \ICanBoogie\Core|\Icybee\Binding\Core\CoreBindings|Binding\CoreBindings $app
+ * @property-read Pathname $pathname Absolute path to the file.
*/
class File extends Node
{
+ use PrototypedBindings;
+
const MODEL_ID = 'files';
- const PATH = 'path';
const MIME = 'mime';
const SIZE = 'size';
+ const EXTENSION = 'extension';
const DESCRIPTION = 'description';
const HTTP_FILE = 'file';
/**
- * Path of the file, relative to the DOCUMENT_ROOT.
+ * @deprecated
+ */
+ const PATH = 'path';
+
+ /**
+ * Size of the file.
*
- * @var string
+ * @var int
*/
- public $path;
+ public $size;
/**
* MIME type of the file.
@@ -44,11 +55,11 @@ class File extends Node
public $mime;
/**
- * Size of the file.
+ * File extension, including the dot ".".
*
- * @var int
+ * @var string
*/
- public $size;
+ public $extension = '';
/**
* Description of the file.
@@ -58,59 +69,77 @@ class File extends Node
public $description = '';
/**
- * If {@link HTTP_FILE} is defined, the {@link \ICanBoogie\HTTP\File} instance is used to
- * set the {@link $mime} and {@link $size} properties, as well as the {@link $title} property
- * if it is empty.
+ * @return Pathname|null
+ */
+ protected function get_pathname()
+ {
+ return $this->app->file_storage->find($this->uuid);
+ }
+
+ /**
+ * If {@link HTTP_FILE} is defined, the {@link HTTPFile} instance is used to
+ * set the {@link $mime}, {@link $size} and {@link $extension} properties.
+ * The {@link $title} property is updated as well if it is empty.
*
- * After the record is saved, the {@link HTTP_FILE} property is removed. Also, the
- * {@link $path} property is updated.
+ * After the record is saved, the {@link HTTP_FILE} property is removed.
*/
public function save()
{
+ /* @var $file HTTPFile */
+
+ $file = null;
+
if (isset($this->{ self::HTTP_FILE }))
{
- /* @var $file \ICanBoogie\HTTP\File */
-
$file = $this->{ self::HTTP_FILE };
-
- $this->mime = $file->type;
- $this->size = $file->size;
-
- if (!$this->title)
- {
- $this->title = $file->unsuffixed_name;
- }
+ $this->save_file_begin($file);
}
$rc = parent::save();
- unset($this->{ self::HTTP_FILE });
-
- if ($rc)
+ if ($rc && $file)
{
- $this->path = $this->model->select(self::PATH)->filter_by_nid($rc)->rc;
+ unset($this->{ self::HTTP_FILE });
+
+ $this->save_file_end($file);
}
return $rc;
}
/**
- * Returns the extension of the file.
+ * Begins saving the HTTP file.
*
- * Note: The dot is included e.g. ".zip".
+ * @param HTTPFile $file
+ */
+ protected function save_file_begin(HTTPFile $file)
+ {
+ $this->mime = $file->type;
+ $this->size = $file->size;
+ $this->extension = $file->extension;
+
+ if (!$this->title)
+ {
+ $this->title = $file->unsuffixed_name;
+ }
+ }
+
+ /**
+ * Finishes saving the HTTP file.
*
- * @return string
+ * @param HTTPFile $file
*/
- protected function get_extension()
+ protected function save_file_end(HTTPFile $file)
{
- $extension = pathinfo($this->path, PATHINFO_EXTENSION);
+ $storage = $this->app->file_storage;
+ $pathname = $storage->create_pathname($file->pathname);
- if (!$extension)
+ if (!file_exists($pathname))
{
- return '';
+ $file->move($pathname);
}
- return '.' . $extension;
+ $storage->index($this->nid, $this->uuid, $pathname->hash);
}
public function url($type = 'view')
Oops, something went wrong.

0 comments on commit d0c4b83

Please sign in to comment.