diff --git a/src/TusServer.php b/src/TusServer.php index 870d39e..989a158 100644 --- a/src/TusServer.php +++ b/src/TusServer.php @@ -2,22 +2,25 @@ namespace Drupal\tus; +use Drupal\Core\Config\ConfigFactoryInterface; use TusPhp\Tus\Server as TusPhp; use Drupal\file\Entity\File; use Symfony\Component\HttpKernel\Exception\HttpException; +use Drupal\Core\Controller\ControllerBase; /** * Class TusServer. */ -class TusServer implements TusServerInterface { - +class TusServer extends ControllerBase implements TusServerInterface { /** - * Constructs a new TusServer object. + * TusServer constructor. + * @param ConfigFactoryInterface $configFactory */ public function __construct() { } + /** * Determine the Drupal URI for a file based on TUS upload key and meta params * from the upload client. @@ -67,16 +70,18 @@ class TusServer implements TusServerInterface { */ public function getServer($uploadKey = '', $postData = []) { // Ensure TUS cache directory exists. - $tusCacheDir = 'private://tus'; + $config = $this->config('tus.settings'); + $scheme = $config->get('scheme') ? $config->get('scheme') : "public://"; + $tusCacheDir = $scheme . 'tus'; if (!file_prepare_directory($tusCacheDir, FILE_CREATE_DIRECTORY)) { - throw new HttpException(500, 'TUS cache folder "private://tus" is not writable.'); + throw new HttpException(500, "TUS cache folder '$tusCacheDir'' is not writable."); } // Set TUS config cache directory. \TusPhp\Config::set([ 'file' => [ - 'dir' => drupal_realpath('private://tus') . '/', + 'dir' => \Drupal::service('file_system')->realpath($tusCacheDir) . '/', 'name' => 'tus_php.cache', - ] + ], ]); // Initialize TUS server. @@ -111,7 +116,9 @@ class TusServer implements TusServerInterface { // Get the file destination. $destination = $this->determineDestination($uploadKey, $postData); // Set the upload directory for TUS. - $server->setUploadDir(drupal_realpath($destination)); + $server->setUploadDir(\Drupal::service('file_system')->realpath($destination)); + $server->setUploadDir('/var/www/html'); // Debug + $server->setUploadDir($destination); return $server; } @@ -119,13 +126,13 @@ class TusServer implements TusServerInterface { /** * Create the file in Drupal and send response. * - * @param array $postData + * @param array $postData * Array of file details from TUS client. * * @return array * The created file details. */ - public function uploadComplete($postData = []) { + public function uploadComplete(array $postData = []) { // If no post data, we can't proceed. if (empty($postData['file'])) { throw new HttpException(500, 'TUS uploadComplete did not receive file info.'); @@ -140,15 +147,14 @@ class TusServer implements TusServerInterface { // Get file destination. $destination = $this->determineDestination($uploadKey, $postData['file']['meta']); $fileName = $postData['file']['name']; - $fileUri = $destination . '/' . $fileName; + $fileUri = $destination . '/' . $fileName; // Check if the file already exists. Re-use the existing entity if so. // We can do this because even if the filenames are the same on 2 different // files, the checksum performed by TUS will cause a new uploadKey, and // therefor a new folder and file entity entry. - if (file_exists(drupal_realpath($fileUri))) { - $fileExists = TRUE; - } + $handle = @fopen($fileUri, 'r'); + $fileExists = $handle ? TRUE : FALSE; // Check if we have a file_managed record for the file anywhere. $fileStorage = \Drupal::entityTypeManager()->getStorage('file'); @@ -175,15 +181,17 @@ class TusServer implements TusServerInterface { if (file_exists(drupal_realpath($file->getFileUri()))) { $fileExists = TRUE; } + $handle = @fopen($file->getFileUri(), 'r'); + $fileExists = $handle ? TRUE : FALSE; } // If the file didn't already exist, create the record now. if ($fileExists && !$fileEntityExists) { // Create the file entity. $file = File::create([ - 'uid' => \Drupal::currentUser()->id(), + 'uid' => \Drupal::currentUser()->id(), 'filename' => $fileName, - 'uri' => $fileUri, + 'uri' => $fileUri, 'filemime' => $postData['file']['type'], 'filesize' => $postData['file']['size'], ]); diff --git a/src/TusServerInterface.php b/src/TusServerInterface.php index 958a210..33d1bd0 100644 --- a/src/TusServerInterface.php +++ b/src/TusServerInterface.php @@ -7,7 +7,9 @@ namespace Drupal\tus; */ interface TusServerInterface { - /** + /** + * Determine Drupal URI. + * * Determine the Drupal URI for a file based on TUS upload key and meta params * from the upload client. * @@ -19,23 +21,25 @@ interface TusServerInterface { * @return string * The intended destination uri for the file. */ - public function determineDestination($uploadKey, $fieldInfo = []); + public function determineDestination($uploadKey, array $fieldInfo = []); /** * Configure and return TusServer instance. * * @return TusServer + * The TusServer */ public function getServer($uploadKey = '', $postData = []); /** * Create the file in Drupal and send response. * - * @param array $postData + * @param array $postData * Array of file details from TUS client. * * @return array * The created file details. */ - public function uploadComplete($postData = []); + public function uploadComplete(array $postData = []); + } diff --git a/tus.routing.yml b/tus.routing.yml index 26c5620..98faed9 100644 --- a/tus.routing.yml +++ b/tus.routing.yml @@ -21,3 +21,13 @@ tus.upload.complete: _format: 'json' options: no_cache: TRUE + +tus.tus_settings_form: + path: '/admin/config/tus/settings' + defaults: + _form: '\Drupal\tus\Form\TusSettingsForm' + _title: 'TusSettingsForm' + requirements: + _permission: 'access administration pages' + options: + _admin_route: TRUE