Skip to content

Commit

Permalink
Merge pull request #26 from dmstr/feature/file-events
Browse files Browse the repository at this point in the history
Added file events
schmunk42 authored Apr 14, 2022
2 parents 77096d7 + 74b74a2 commit 8799508
Showing 2 changed files with 140 additions and 14 deletions.
98 changes: 84 additions & 14 deletions controllers/ApiController.php
Original file line number Diff line number Diff line change
@@ -9,12 +9,15 @@

namespace hrzg\filefly\controllers;

use Exception;
use hrzg\filefly\components\FileManager;
use hrzg\filefly\events\FileEvent;
use hrzg\filefly\models\FileflyHashmap;
use hrzg\filefly\Module;
use hrzg\filefly\Module as Filefly;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
use Yii;
use yii\filters\AccessControl;
use yii\filters\Cors;
use yii\filters\VerbFilter;
@@ -23,8 +26,6 @@
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use \Exception;
use Yii;

/**
* --- PUBLIC PROPERTIES ---
@@ -158,7 +159,7 @@ public function actionMove($newPath, $items)
} else {

foreach ($paths as $oldPath) {

$this->trigger(FileEvent::EVENT_BEFORE_MOVE, new FileEvent(['filename' => $oldPath]));
$fileSystem->check($oldPath, $this->module->repair);

if (!$fileSystem->get($oldPath)->isFile() && !$fileSystem->get($oldPath)->isDir()) {
@@ -173,13 +174,23 @@ public function actionMove($newPath, $items)
$errorMessage = 'movefailed';
break;
}
$success = true;

// Update permissions
$fileSystem->setAccess($oldPath, $destPath);
$this->trigger(FileEvent::EVENT_AFTER_MOVE, new FileEvent(['filename' => $destPath]));
}

}

if ($success) {
$this->trigger(FileEvent::EVENT_MOVE_SUCCESS, new FileEvent());
} else {
$this->trigger(FileEvent::EVENT_MOVE_ERROR, new FileEvent([
'errorMessage' => $errorMessage
]));
}

return $this->asJson([
'result' => [
'success' => $success,
@@ -208,7 +219,7 @@ public function actionCopy($items, $newPath, $newFilename = null)
} else {

foreach ($paths as $oldPath) {

$this->trigger(FileEvent::EVENT_BEFORE_COPY, new FileEvent(['filename' => $oldPath]));
// ensure hashmap entry
$fileSystem->check($oldPath, $this->module->repair);
if (!$fileSystem->get($oldPath)->isFile()) {
@@ -226,14 +237,25 @@ public function actionCopy($items, $newPath, $newFilename = null)
$copied = $fileSystem->get($oldPath)->copy($filename);
if ($copied === false) {
$errorMessage = 'copyfailed';
} else {
$success = true;
}

// Set new permission
$fileSystem->setAccess($filename);
$this->trigger(FileEvent::EVENT_AFTER_COPY, new FileEvent(['filename' => $oldPath]));
}

}

if ($success) {
$this->trigger(FileEvent::EVENT_COPY_SUCCESS, new FileEvent());
} else {
$this->trigger(FileEvent::EVENT_COPY_ERROR, new FileEvent([
'errorMessage' => $errorMessage
]));
}

return $this->asJson([
'result' => [
'success' => $success,
@@ -255,15 +277,24 @@ public function actionCreateFolder($newPath)
if (!$fileSystem->grantAccess($newPath, FileManager::ACCESS_UPDATE)) {
$errorMessage = 'nopermission';
} else {

$this->trigger(FileEvent::EVENT_BEFORE_CREATE_FOLDER, new FileEvent(['filename' => $newPath]));
if ($fileSystem->has($newPath)) {
$errorMessage = 'exists';
} else if ($fileSystem->createDir($newPath)) {
$success = true;
} else {
if ($fileSystem->createDir($newPath)) {
$success = true;
}
}

$fileSystem->setAccess($newPath);
$this->trigger(FileEvent::EVENT_AFTER_CREATE_FOLDER, new FileEvent(['filename' => $newPath]));
}

if ($success) {
$this->trigger(FileEvent::EVENT_CREATE_FOLDER_SUCCESS, new FileEvent());
} else {
$this->trigger(FileEvent::EVENT_CREATE_FOLDER_ERROR, new FileEvent([
'errorMessage' => $errorMessage
]));
}

return $this->asJson([
@@ -294,16 +325,23 @@ public function actionRename($item, $newItemPath)
if (!$fileSystem->get($item)->isFile() && !$fileSystem->get($item)->isDir()) {
$errorMessage = 'file_not_found';
}

$this->trigger(FileEvent::EVENT_BEFORE_RENAME, new FileEvent(['filename' => $item]));
if ($fileSystem->get($item)->rename($newItemPath)) {
$success = true;
$fileSystem->setAccess($item, $newItemPath);
} else {
$errorMessage = 'renaming_failed';
}

$this->trigger(FileEvent::EVENT_AFTER_RENAME, new FileEvent(['filename' => $newItemPath]));
}

if ($success) {
$this->trigger(FileEvent::EVENT_RENAME_SUCCESS, new FileEvent());
} else {
$this->trigger(FileEvent::EVENT_RENAME_ERROR, new FileEvent([
'errorMessage' => $errorMessage
]));
}

return $this->asJson([
'result' => [
@@ -334,6 +372,7 @@ public function actionUpload($path)

if ($fileSystem->grantAccess($path, FileManager::ACCESS_UPDATE)) {
foreach ($this->_uploadedFiles as $file) {
$this->trigger(FileEvent::EVENT_BEFORE_UPLOAD, new FileEvent(['filename' => $file['name'] ?? 'name']));
$stream = fopen($file['tmp_name'], 'rb+');

if ($this->module->slugNames) {
@@ -353,7 +392,8 @@ public function actionUpload($path)
$mimeTypes = \Yii::$app->settings->get('mime-whitelist', 'filefly');
empty($mimeTypes) ? $acceptedMimeTypes = [] : $acceptedMimeTypes = explode(",", $mimeTypes);

if( in_array(mime_content_type($file['tmp_name']), $acceptedMimeTypes, false) || empty($acceptedMimeTypes)){
if (in_array(mime_content_type($file['tmp_name']), $acceptedMimeTypes,
false) || empty($acceptedMimeTypes)) {
$uploaded = $fileSystem->writeStream(
$fullPath,
$stream,
@@ -376,11 +416,20 @@ public function actionUpload($path)

$success = true;
$fileSystem->setAccess($fullPath);
$this->trigger(FileEvent::EVENT_AFTER_UPLOAD, new FileEvent(['filename' => $file['name'] ?? '']));
}
} else {
$errorMessage = 'permission_upload_denied';
}

if ($success) {
$this->trigger(FileEvent::EVENT_UPLOAD_SUCCESS, new FileEvent());
} else {
$this->trigger(FileEvent::EVENT_UPLOAD_ERROR, new FileEvent([
'errorMessage' => $errorMessage
]));
}

return $this->asJson([
'result' => [
'success' => $success,
@@ -458,7 +507,7 @@ public function actionRemove($items)
$success = false;
$errorMessage = '';
foreach ($paths as $path) {

$this->trigger(FileEvent::EVENT_BEFORE_REMOVE, new FileEvent(['filename' => $path]));
$fileSystem->check($path, $this->module->repair);

if (!$fileSystem->grantAccess($path, FileManager::ACCESS_DELETE)) {
@@ -488,11 +537,20 @@ public function actionRemove($items)
break;
}
$success = true;
$this->trigger(FileEvent::EVENT_AFTER_REMOVE, new FileEvent(['filename' => $path]));
}
if ($anyDeniedPerm) {
$errorMessage = 'permission_delete_denied';
}

if ($success) {
$this->trigger(FileEvent::EVENT_REMOVE_SUCCESS, new FileEvent(['filename' => $paths]));
} else {
$this->trigger(FileEvent::EVENT_REMOVE_ERROR, new FileEvent([
'errorMessage' => $errorMessage
]));
}

return $this->asJson([
'result' => [
'success' => $success,
@@ -530,6 +588,7 @@ public function actionDownload($path)
$headers->add('Connection', 'Keep-Alive');

$stream = $fileSystem->readStream($path);
$this->trigger(FileEvent::EVENT_BEFORE_DOWNLOAD, new FileEvent(['filename' => $path]));
$response->sendStreamAsFile(
$stream,
basename($path),
@@ -538,6 +597,7 @@ public function actionDownload($path)
} catch (Exception $e) {
throw new NotFoundHttpException();
}
$this->trigger(FileEvent::EVENT_AFTER_DOWNLOAD, new FileEvent(['filename' => $path]));
Yii::$app->end();
}

@@ -608,13 +668,22 @@ public function actionChangePermissions($path = null, $item = null)
// ensure hashmap entry
$fileSystem->check($path, $this->module->repair);

$this->trigger(FileEvent::EVENT_BEFORE_CHANGE_PERMISSION, new FileEvent(['filename' => $path]));
$updateItemAuth = $fileSystem->updatePermission($item, $path);

$this->trigger(FileEvent::EVENT_AFTER_CHANGE_PERMISSION, new FileEvent(['filename' => $path]));
$errorMessage = "";
if ($updateItemAuth === false) {
$errorMessage = "error updating permissions";
}

if ($updateItemAuth) {
$this->trigger(FileEvent::EVENT_CHANGE_PERMISSION_SUCCESS, new FileEvent(['filename' => $path]));
} else {
$this->trigger(FileEvent::EVENT_CHANGE_PERMISSION_ERROR, new FileEvent([
'errorMessage' => $errorMessage
]));
}

return $this->asJson([
'result' => [
'success' => $updateItemAuth,
@@ -640,7 +709,8 @@ public function actionSearch($q, $limit = '')
foreach ($query->all() as $item) {

// check read permissions or is folder
if (!$fileSystem->grantAccess($item['path'], Filefly::ACCESS_READ) || $fileSystem->get($item['path'])->isDir()) {
if (!$fileSystem->grantAccess($item['path'],
Filefly::ACCESS_READ) || $fileSystem->get($item['path'])->isDir()) {
continue;
}

56 changes: 56 additions & 0 deletions events/FileEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace hrzg\filefly\events;

use yii\base\Event;

/**
* --- PROPERTIES ---
*
* @author Elias Luhr
*/
class FileEvent extends Event
{
public const EVENT_BEFORE_UPLOAD = 'beforeUpload';
public const EVENT_AFTER_UPLOAD = 'afterUpload';
public const EVENT_UPLOAD_SUCCESS = 'uploadSuccess';
public const EVENT_UPLOAD_ERROR = 'uploadError';
public const EVENT_BEFORE_REMOVE = 'beforeRemove';
public const EVENT_AFTER_REMOVE = 'afterRemove';
public const EVENT_REMOVE_SUCCESS = 'removeSuccess';
public const EVENT_REMOVE_ERROR = 'removeError';
public const EVENT_BEFORE_DOWNLOAD = 'beforeDownload';
public const EVENT_AFTER_DOWNLOAD = 'afterDownload';
public const EVENT_BEFORE_CHANGE_PERMISSION = 'beforeChangePermission';
public const EVENT_AFTER_CHANGE_PERMISSION = 'afterChangePermission';
public const EVENT_CHANGE_PERMISSION_SUCCESS = 'changePermissionSuccess';
public const EVENT_CHANGE_PERMISSION_ERROR = 'changePermissionError';
public const EVENT_BEFORE_RENAME = 'beforeRename';
public const EVENT_AFTER_RENAME = 'afterRename';
public const EVENT_RENAME_SUCCESS = 'renameSuccess';
public const EVENT_RENAME_ERROR = 'renameError';
public const EVENT_BEFORE_CREATE_FOLDER = 'beforeCreateFolder';
public const EVENT_AFTER_CREATE_FOLDER = 'afterCreateFolder';
public const EVENT_CREATE_FOLDER_SUCCESS = 'createFolderSuccess';
public const EVENT_CREATE_FOLDER_ERROR = 'createFolderError';
public const EVENT_BEFORE_COPY = 'beforeCopy';
public const EVENT_AFTER_COPY = 'afterCopy';
public const EVENT_COPY_SUCCESS = 'copySuccess';
public const EVENT_COPY_ERROR = 'copyError';
public const EVENT_BEFORE_MOVE = 'beforeMove';
public const EVENT_AFTER_MOVE = 'afterMove';
public const EVENT_MOVE_SUCCESS = 'moveSuccess';
public const EVENT_MOVE_ERROR = 'moveError';

/**
* @var string|array filename: name of the uploaded file or created directory
*
* @var array
*/
public $filename;

/**
* @var string
*/
public $errorMessage;
}

0 comments on commit 8799508

Please sign in to comment.